io.etcd.jetcd.lease.LeaseKeepAliveResponse Java Examples

The following examples show how to use io.etcd.jetcd.lease.LeaseKeepAliveResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EtcdServiceImpl.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
@Override
public long getLease(String leaseName, long leaseTTL) throws Exception{

    Lease lease = etcdClient.getLeaseClient();
    long leaseId = lease.grant(leaseTTL).get().getID();

    lease.keepAlive(leaseId, new StreamObserver<LeaseKeepAliveResponse>() {
        @Override
        public void onNext(LeaseKeepAliveResponse value) {
            logger.debug("{} lease keeps alive for [{}]s:", leaseName, value.getTTL());
        }

        @Override
        public void onError(Throwable t) {
            logger.debug("{} lease renewal Exception!", leaseName, t.fillInStackTrace());
        }

        @Override
        public void onCompleted() {
            logger.info("{} lease completed!", leaseName);
        }
    });

    return leaseId;
}
 
Example #2
Source File: LeaderSelector.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
/**
 * acquire etcd lease and keep lease avtive
 *
 * @return
 * @throws InterruptedException
 * @throws ExecutionException
 */
private long acquireActiveLease() throws InterruptedException, ExecutionException {
    long leaseId = leaseClient.grant(leaseTTL).get().getID();
    logger.debug("LeaderSelector get leaseId:[{}] and ttl:[{}]", leaseId, leaseTTL);
    this.leaseCloser = leaseClient.keepAlive(leaseId, new StreamObserver<LeaseKeepAliveResponse>() {
        @Override
        public void onNext(LeaseKeepAliveResponse value) {
            logger.debug("LeaderSelector lease keeps alive for [{}]s:", value.getTTL());
        }

        @Override
        public void onError(Throwable t) {
            logger.debug("LeaderSelector lease renewal Exception!", t.fillInStackTrace());
            cancelTask();
        }

        @Override
        public void onCompleted() {
            logger.info("LeaderSelector lease renewal completed! start canceling task.");
            cancelTask();
        }
    });
    return leaseId;
}
 
Example #3
Source File: LeaseImpl.java    From jetcd with Apache License 2.0 6 votes vote down vote up
private synchronized void processKeepAliveResponse(io.etcd.jetcd.api.LeaseKeepAliveResponse leaseKeepAliveResponse) {
    if (this.closed) {
        return;
    }

    final long leaseID = leaseKeepAliveResponse.getID();
    final long ttl = leaseKeepAliveResponse.getTTL();
    final KeepAlive ka = this.keepAlives.get(leaseID);

    if (ka == null) {
        // return if the corresponding keep alive has closed.
        return;
    }

    if (ttl > 0) {
        long nextKeepAlive = System.currentTimeMillis() + ttl * 1000 / 3;
        ka.setNextKeepAlive(nextKeepAlive);
        ka.setDeadLine(System.currentTimeMillis() + ttl * 1000);
        ka.onNext(leaseKeepAliveResponse);
    } else {
        // lease expired; close all keep alive
        this.removeKeepAlive(leaseID);
        ka.onError(newEtcdException(ErrorCode.NOT_FOUND, "etcdserver: requested lease not found"));
    }
}
 
Example #4
Source File: LeaseImpl.java    From jetcd with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<LeaseKeepAliveResponse> keepAliveOnce(long leaseId) {
    CompletableFuture<LeaseKeepAliveResponse> future = new CompletableFuture<>();

    StreamObserver<LeaseKeepAliveRequest> requestObserver = Observers.observe(
        this.leaseStub::leaseKeepAlive,
        response -> future.complete(new LeaseKeepAliveResponse(response)),
        throwable -> future.completeExceptionally(toEtcdException(throwable)));

    // cancel grpc stream when leaseKeepAliveResponseCompletableFuture completes.
    CompletableFuture<LeaseKeepAliveResponse> answer = future
        .whenCompleteAsync((val, throwable) -> requestObserver.onCompleted(), connectionManager.getExecutorService());

    requestObserver.onNext(LeaseKeepAliveRequest.newBuilder().setID(leaseId).build());

    return answer;
}
 
Example #5
Source File: EtcdRegistry.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
/**
 * 发送心跳到 etcd, 表明该host是活着的
 */
private void keepAlive() {
    Executors.newSingleThreadExecutor().submit(
            () -> {
                try {
                    lease.keepAlive(leaseId, new StreamObserver<LeaseKeepAliveResponse>() {
                        @Override
                        public void onNext(LeaseKeepAliveResponse value) {

                        }

                        @Override
                        public void onError(Throwable t) {

                        }

                        @Override
                        public void onCompleted() {

                        }
                    });
                    log.info("KeepAlive lease:" + leaseId + "; Hex format:" + Long.toHexString(leaseId));
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
    );
}
 
Example #6
Source File: LeaseTest.java    From jetcd with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: LeaseImpl.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public void addObserver(StreamObserver<LeaseKeepAliveResponse> observer) {
    this.observers.add(observer);
}
 
Example #8
Source File: LeaseImpl.java    From jetcd with Apache License 2.0 4 votes vote down vote up
public void removeObserver(StreamObserver<LeaseKeepAliveResponse> listener) {
    this.observers.remove(listener);
    if (this.observers.isEmpty()) {
        removeKeepAlive(leaseId);
    }
}
 
Example #9
Source File: LeaseImpl.java    From jetcd with Apache License 2.0 4 votes vote down vote up
@Override
public void onNext(io.etcd.jetcd.api.LeaseKeepAliveResponse response) {
    for (StreamObserver<LeaseKeepAliveResponse> observer : observers) {
        observer.onNext(new LeaseKeepAliveResponse(response));
    }
}
 
Example #10
Source File: LeaseImpl.java    From jetcd with Apache License 2.0 4 votes vote down vote up
@Override
public void onError(Throwable throwable) {
    for (StreamObserver<LeaseKeepAliveResponse> observer : observers) {
        observer.onError(toEtcdException(throwable));
    }
}
 
Example #11
Source File: Lease.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * keep alive one lease only once.
 *
 * @param  leaseId id of lease to keep alive once
 * @return         The keep alive response
 */
CompletableFuture<LeaseKeepAliveResponse> keepAliveOnce(long leaseId);
 
Example #12
Source File: Lease.java    From jetcd with Apache License 2.0 2 votes vote down vote up
/**
 * keep the given lease alive forever.
 *
 * @param  leaseId  lease to be keep alive forever.
 * @param  observer the observer
 * @return          a KeepAliveListener that listens for KeepAlive responses.
 */
CloseableClient keepAlive(long leaseId, StreamObserver<LeaseKeepAliveResponse> observer);