Search in sources :

Example 1 with ResponseOp

use of com.ibm.etcd.api.ResponseOp in project etcd-java by IBM.

the class RangeCache method fullRefreshCache.

// internal method - should not be called while watch is active
protected ListenableFuture<Boolean> fullRefreshCache() {
    ListenableFuture<List<RangeResponse>> rrfut;
    long seenUpTo = seenUpToRev.get();
    boolean firstTime = (seenUpTo == 0L);
    if (firstTime || entries.size() <= 20) {
        // TODO *maybe* chunking (for large caches)
        ListenableFuture<RangeResponse> rrf = kvClient.get(fromKey).rangeEnd(toKey).backoffRetry(() -> !closed).timeout(300_000L).async();
        rrfut = Futures.transform(rrf, Collections::singletonList, directExecutor());
    } else {
        // in case the local cache is large, reduce data transfer by requesting
        // just keys, and full key+value only for those modified since seenUpToRev
        RangeRequest.Builder rangeReqBld = RangeRequest.newBuilder().setKey(fromKey).setRangeEnd(toKey);
        RangeRequest newModsReq = rangeReqBld.setMinModRevision(seenUpTo + 1).build();
        RangeRequest otherKeysReq = rangeReqBld.clearMinModRevision().setMaxModRevision(seenUpTo).setKeysOnly(true).build();
        ListenableFuture<TxnResponse> trf = kvClient.batch().get(newModsReq).get(otherKeysReq).backoffRetry(() -> !closed).timeout(300_000L).async();
        rrfut = Futures.transform(trf, tr -> tr.getResponsesList().stream().map(ResponseOp::getResponseRange).collect(Collectors.toList()), directExecutor());
    }
    final SettableFuture<Boolean> promise = new SettableFuture<Boolean>() {

        @Override
        protected void interruptTask() {
            if (rrfut.cancel(true)) {
                return;
            }
            Watch theWatch;
            synchronized (RangeCache.this) {
                theWatch = watch;
            }
            if (theWatch != null) {
                theWatch.close();
            }
        }
    };
    Futures.addCallback(rrfut, (FutureListener<List<RangeResponse>>) (rrs, err) -> {
        if (rrs != null)
            try {
                setupWatch(rrs, firstTime, promise);
                return;
            } catch (Throwable t) {
                err = t;
            }
        promise.setException(err);
    }, listenerExecutor);
    return promise;
}
Also used : FluentWatchRequest(com.ibm.etcd.client.kv.KvClient.FluentWatchRequest) EtcdClient(com.ibm.etcd.client.EtcdClient) CompareResult(com.ibm.etcd.api.Compare.CompareResult) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) RevisionCompactedException(com.ibm.etcd.client.watch.RevisionCompactedException) ResponseOp(com.ibm.etcd.api.ResponseOp) StreamObserver(io.grpc.stub.StreamObserver) Compare(com.ibm.etcd.api.Compare) Map(java.util.Map) Status(io.grpc.Status) RangeResponse(com.ibm.etcd.api.RangeResponse) CancellationException(java.util.concurrent.CancellationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) EventType(com.ibm.etcd.client.utils.RangeCache.Listener.EventType) Set(java.util.Set) NavigableSet(java.util.NavigableSet) NavigableMap(java.util.NavigableMap) GuardedBy(javax.annotation.concurrent.GuardedBy) Collectors(java.util.stream.Collectors) RequestOp(com.ibm.etcd.api.RequestOp) MoreExecutors.directExecutor(com.google.common.util.concurrent.MoreExecutors.directExecutor) ByteString(com.google.protobuf.ByteString) CompareOrBuilder(com.ibm.etcd.api.CompareOrBuilder) List(java.util.List) Stream(java.util.stream.Stream) GrpcClient.waitFor(com.ibm.etcd.client.GrpcClient.waitFor) DeleteRangeResponse(com.ibm.etcd.api.DeleteRangeResponse) RangeRequest(com.ibm.etcd.api.RangeRequest) Event(com.ibm.etcd.api.Event) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GrpcClient(com.ibm.etcd.client.GrpcClient) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) WatchUpdate(com.ibm.etcd.client.kv.WatchUpdate) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Watch(com.ibm.etcd.client.kv.KvClient.Watch) TxnResponse(com.ibm.etcd.api.TxnResponse) Iterators(com.google.common.collect.Iterators) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) DeleteRangeRequest(com.ibm.etcd.api.DeleteRangeRequest) Code(io.grpc.Status.Code) KeyUtils(com.ibm.etcd.client.KeyUtils) PutRequest(com.ibm.etcd.api.PutRequest) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Executor(java.util.concurrent.Executor) FutureListener(com.ibm.etcd.client.FutureListener) KvClient(com.ibm.etcd.client.kv.KvClient) KeyValue(com.ibm.etcd.api.KeyValue) TxnRequest(com.ibm.etcd.api.TxnRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Futures(com.google.common.util.concurrent.Futures) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Collections(java.util.Collections) AbstractFuture(com.google.common.util.concurrent.AbstractFuture) CompareTarget(com.ibm.etcd.api.Compare.CompareTarget) RangeResponse(com.ibm.etcd.api.RangeResponse) DeleteRangeResponse(com.ibm.etcd.api.DeleteRangeResponse) RangeRequest(com.ibm.etcd.api.RangeRequest) DeleteRangeRequest(com.ibm.etcd.api.DeleteRangeRequest) Watch(com.ibm.etcd.client.kv.KvClient.Watch) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TxnResponse(com.ibm.etcd.api.TxnResponse)

Aggregations

Iterators (com.google.common.collect.Iterators)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 MoreExecutors.directExecutor (com.google.common.util.concurrent.MoreExecutors.directExecutor)1 ByteString (com.google.protobuf.ByteString)1 Compare (com.ibm.etcd.api.Compare)1 CompareResult (com.ibm.etcd.api.Compare.CompareResult)1 CompareTarget (com.ibm.etcd.api.Compare.CompareTarget)1 CompareOrBuilder (com.ibm.etcd.api.CompareOrBuilder)1 DeleteRangeRequest (com.ibm.etcd.api.DeleteRangeRequest)1 DeleteRangeResponse (com.ibm.etcd.api.DeleteRangeResponse)1 Event (com.ibm.etcd.api.Event)1 KeyValue (com.ibm.etcd.api.KeyValue)1 PutRequest (com.ibm.etcd.api.PutRequest)1 RangeRequest (com.ibm.etcd.api.RangeRequest)1 RangeResponse (com.ibm.etcd.api.RangeResponse)1 RequestOp (com.ibm.etcd.api.RequestOp)1 ResponseOp (com.ibm.etcd.api.ResponseOp)1