Search in sources :

Example 1 with LeaseKeepAliveResponse

use of io.etcd.jetcd.lease.LeaseKeepAliveResponse in project dubbo by alibaba.

the class LeaseTest method testKeepAlive.

@Test
public void testKeepAlive() throws ExecutionException, InterruptedException {
    long leaseID = leaseClient.grant(2).get().getID();
    kvClient.put(KEY, VALUE, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
    assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(1);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<LeaseKeepAliveResponse> responseRef = new AtomicReference<>();
    StreamObserver<LeaseKeepAliveResponse> observer = Observers.observer(response -> {
        responseRef.set(response);
        latch.countDown();
    });
    try (CloseableClient c = leaseClient.keepAlive(leaseID, observer)) {
        latch.await(5, TimeUnit.SECONDS);
        LeaseKeepAliveResponse response = responseRef.get();
        assertThat(response.getTTL()).isGreaterThan(0);
    }
    Thread.sleep(3000);
    assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(0);
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) CloseableClient(io.etcd.jetcd.support.CloseableClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 2 with LeaseKeepAliveResponse

use of io.etcd.jetcd.lease.LeaseKeepAliveResponse in project jetcd by coreos.

the class LeaseTest method testKeepAliveOnce.

@Test
public void testKeepAliveOnce() throws ExecutionException, InterruptedException {
    long leaseID = leaseClient.grant(2).get().getID();
    kvClient.put(KEY, VALUE, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
    assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(1);
    LeaseKeepAliveResponse rp = leaseClient.keepAliveOnce(leaseID).get();
    assertThat(rp.getTTL()).isGreaterThan(0);
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) Test(org.junit.jupiter.api.Test)

Example 3 with LeaseKeepAliveResponse

use of io.etcd.jetcd.lease.LeaseKeepAliveResponse in project jetcd by coreos.

the class LeaseTest method testKeepAlive.

@Test
public void testKeepAlive() throws ExecutionException, InterruptedException {
    long leaseID = leaseClient.grant(2).get().getID();
    kvClient.put(KEY, VALUE, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
    assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(1);
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<LeaseKeepAliveResponse> responseRef = new AtomicReference<>();
    StreamObserver<LeaseKeepAliveResponse> observer = Observers.observer(response -> {
        responseRef.set(response);
        latch.countDown();
    });
    try (CloseableClient c = leaseClient.keepAlive(leaseID, observer)) {
        latch.await(5, TimeUnit.SECONDS);
        LeaseKeepAliveResponse response = responseRef.get();
        assertThat(response.getTTL()).isGreaterThan(0);
    }
    Thread.sleep(3000);
    assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(0);
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) CloseableClient(io.etcd.jetcd.support.CloseableClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with LeaseKeepAliveResponse

use of io.etcd.jetcd.lease.LeaseKeepAliveResponse in project jetcd by coreos.

the class LeaseImpl method keepAliveOnce.

@Override
public CompletableFuture<LeaseKeepAliveResponse> keepAliveOnce(long leaseId) {
    final CompletableFuture<LeaseKeepAliveResponse> future = new CompletableFuture<>();
    final CloseableClient ka = keepAlive(leaseId, new StreamObserver<LeaseKeepAliveResponse>() {

        @Override
        public void onNext(LeaseKeepAliveResponse value) {
            future.complete(value);
        }

        @Override
        public void onError(Throwable t) {
            future.completeExceptionally(toEtcdException(t));
        }

        @Override
        public void onCompleted() {
        }
    });
    return future.whenCompleteAsync((val, throwable) -> ka.close(), connectionManager().getExecutorService());
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) CompletableFuture(java.util.concurrent.CompletableFuture) CloseableClient(io.etcd.jetcd.support.CloseableClient)

Example 5 with LeaseKeepAliveResponse

use of io.etcd.jetcd.lease.LeaseKeepAliveResponse in project dubbo by alibaba.

the class JEtcdClientWrapper method keepAlive.

@SuppressWarnings("unchecked")
private <T> void keepAlive(long lease, Consumer<T> onFailed) {
    final StreamObserver<LeaseKeepAliveResponse> observer = new Observers.Builder().onError((e) -> {
        if (e instanceof EtcdException) {
            EtcdException error = (EtcdException) e;
            /**
             * ttl has expired
             */
            if (error.getErrorCode() == ErrorCode.NOT_FOUND) {
                keepAlive0(onFailed);
            }
        }
    }).onCompleted(() -> {
        /**
         * deadline reached.
         */
        keepAlive0(onFailed);
    }).build();
    /**
     * If there is already a keepalive, cancel first
     */
    cancelKeepAlive();
    /**
     * create and set new keepAlive to globalKeepAliveRef
     */
    this.keepAlive = client.getLeaseClient().keepAlive(lease, observer);
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) Observers(io.etcd.jetcd.support.Observers) EtcdException(io.etcd.jetcd.common.exception.EtcdException)

Aggregations

LeaseKeepAliveResponse (io.etcd.jetcd.lease.LeaseKeepAliveResponse)6 Test (org.junit.jupiter.api.Test)4 CloseableClient (io.etcd.jetcd.support.CloseableClient)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 EtcdException (io.etcd.jetcd.common.exception.EtcdException)1 Observers (io.etcd.jetcd.support.Observers)1 CompletableFuture (java.util.concurrent.CompletableFuture)1