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);
}
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);
}
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);
}
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());
}
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);
}
Aggregations