Search in sources :

Example 1 with FutureListener

use of com.ibm.etcd.client.FutureListener in project etcd-java by IBM.

the class PersistentLeaseKey method putKey.

// called only from our serialized executor context
protected void putKey(long leaseId) {
    if (leaseId == 0L || closeFuture != null)
        return;
    if (updateFuture != null && !updateFuture.isDone()) {
        // if the cancellation wins then putKey will be immediately retried
        updateFuture.cancel(false);
        return;
    }
    // execute a transaction which either sets the lease on an existing key
    // or creates the key with the lease if it doesn't exist
    PutRequest.Builder putBld = PutRequest.newBuilder().setKey(key).setLease(leaseId);
    KvClient.FluentTxnRequest req = client.getKvClient().txnIf().exists(key).backoffRetry(() -> closeFuture == null && isActive());
    ListenableFuture<? extends Object> fut;
    ListenableFuture<TxnResponse> txnFut;
    if (rangeCache == null) {
        fut = txnFut = req.then().put(putBld.setIgnoreValue(true)).elseDo().put(putBld.setIgnoreValue(false).setValue(defaultValue)).async();
    } else {
        RangeRequest getOp = RangeRequest.newBuilder().setKey(key).build();
        txnFut = req.then().put(putBld.setIgnoreValue(true)).get(getOp).elseDo().put(putBld.setIgnoreValue(false).setValue(defaultValue)).get(getOp).async();
        fut = Futures.transform(txnFut, (Function<TxnResponse, Object>) tr -> rangeCache.offerUpdate(tr.getResponses(1).getResponseRange().getKvs(0), false));
    }
    if (!isDone())
        fut = Futures.transform(fut, (Function<Object, Object>) r -> set(key));
    // this callback is to trigger an immediate retry in case the attempt was cancelled by a more
    // recent lease state change to active
    Futures.addCallback(fut, (FutureListener<Object>) (v, t) -> {
        if (t instanceof CancellationException && isActive())
            putKey(leaseId);
    }, executor);
    updateFuture = fut;
}
Also used : LeaseState(com.ibm.etcd.client.lease.PersistentLease.LeaseState) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CancellationException(java.util.concurrent.CancellationException) Executor(java.util.concurrent.Executor) EtcdClient(com.ibm.etcd.client.EtcdClient) FutureListener(com.ibm.etcd.client.FutureListener) KvClient(com.ibm.etcd.client.kv.KvClient) SettableFuture(com.google.common.util.concurrent.SettableFuture) TxnResponse(com.ibm.etcd.api.TxnResponse) SerializingExecutor(com.ibm.etcd.client.SerializingExecutor) ByteString(com.google.protobuf.ByteString) ListenerObserver(com.ibm.etcd.client.ListenerObserver) Futures(com.google.common.util.concurrent.Futures) PersistentLease(com.ibm.etcd.client.lease.PersistentLease) RangeRequest(com.ibm.etcd.api.RangeRequest) PutRequest(com.ibm.etcd.api.PutRequest) AbstractFuture(com.google.common.util.concurrent.AbstractFuture) Function(com.google.common.base.Function) CancellationException(java.util.concurrent.CancellationException) RangeRequest(com.ibm.etcd.api.RangeRequest) PutRequest(com.ibm.etcd.api.PutRequest) KvClient(com.ibm.etcd.client.kv.KvClient) TxnResponse(com.ibm.etcd.api.TxnResponse)

Aggregations

Function (com.google.common.base.Function)1 AbstractFuture (com.google.common.util.concurrent.AbstractFuture)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 ByteString (com.google.protobuf.ByteString)1 PutRequest (com.ibm.etcd.api.PutRequest)1 RangeRequest (com.ibm.etcd.api.RangeRequest)1 TxnResponse (com.ibm.etcd.api.TxnResponse)1 EtcdClient (com.ibm.etcd.client.EtcdClient)1 FutureListener (com.ibm.etcd.client.FutureListener)1 ListenerObserver (com.ibm.etcd.client.ListenerObserver)1 SerializingExecutor (com.ibm.etcd.client.SerializingExecutor)1 KvClient (com.ibm.etcd.client.kv.KvClient)1 PersistentLease (com.ibm.etcd.client.lease.PersistentLease)1 LeaseState (com.ibm.etcd.client.lease.PersistentLease.LeaseState)1 CancellationException (java.util.concurrent.CancellationException)1 Executor (java.util.concurrent.Executor)1