Search in sources :

Example 1 with MultiObserveViaCasRequest

use of com.couchbase.client.core.msg.kv.MultiObserveViaCasRequest in project couchbase-jvm-clients by couchbase.

the class ReactiveBatchHelper method existsBytes.

/**
 * Performs the bulk logic of fetching a config and splitting up the observe requests.
 *
 * @param collection the collection on which the query should be performed.
 * @param ids the list of ids which should be checked.
 * @return a flux of all
 */
private static Flux<byte[]> existsBytes(final Collection collection, final java.util.Collection<String> ids) {
    final Core core = collection.core();
    final CoreEnvironment env = core.context().environment();
    BucketConfig config = core.clusterConfig().bucketConfig(collection.bucketName());
    if (core.configurationProvider().bucketConfigLoadInProgress() || config == null) {
        // and then try again. In a steady state this should not happen.
        return Mono.delay(Duration.ofMillis(100), env.scheduler()).flatMapMany(ign -> existsBytes(collection, ids));
    }
    long start = System.nanoTime();
    if (!(config instanceof CouchbaseBucketConfig)) {
        throw new IllegalStateException("Only couchbase (and ephemeral) buckets are supported at this point!");
    }
    Map<NodeIdentifier, Map<byte[], Short>> nodeEntries = new HashMap<>(config.nodes().size());
    for (NodeInfo node : config.nodes()) {
        nodeEntries.put(node.identifier(), new HashMap<>(ids.size() / config.nodes().size()));
    }
    CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
    CollectionIdentifier ci = new CollectionIdentifier(collection.bucketName(), Optional.of(collection.scopeName()), Optional.of(collection.name()));
    for (String id : ids) {
        byte[] encodedId = id.getBytes(StandardCharsets.UTF_8);
        int partitionId = KeyValueLocator.partitionForKey(encodedId, cbc.numberOfPartitions());
        int nodeId = cbc.nodeIndexForActive(partitionId, false);
        NodeInfo nodeInfo = cbc.nodeAtIndex(nodeId);
        nodeEntries.get(nodeInfo.identifier()).put(encodedId, (short) partitionId);
    }
    List<Mono<MultiObserveViaCasResponse>> responses = new ArrayList<>(nodeEntries.size());
    List<MultiObserveViaCasRequest> requests = new ArrayList<>(nodeEntries.size());
    for (Map.Entry<NodeIdentifier, Map<byte[], Short>> node : nodeEntries.entrySet()) {
        if (node.getValue().isEmpty()) {
            // service enabled and 2) have keys that we need to fetch
            continue;
        }
        MultiObserveViaCasRequest request = new MultiObserveViaCasRequest(env.timeoutConfig().kvTimeout(), core.context(), env.retryStrategy(), ci, node.getKey(), node.getValue(), PMGET_PREDICATE);
        core.send(request);
        requests.add(request);
        responses.add(Reactor.wrap(request, request.response(), true));
    }
    return Flux.merge(responses).flatMap(response -> Flux.fromIterable(response.observed().keySet())).onErrorMap(throwable -> {
        BatchErrorContext ctx = new BatchErrorContext(Collections.unmodifiableList(requests));
        return new BatchHelperFailureException("Failed to perform BatchHelper bulk operation", throwable, ctx);
    }).doOnComplete(() -> core.context().environment().eventBus().publish(new BatchHelperExistsCompletedEvent(Duration.ofNanos(System.nanoTime() - start), new BatchErrorContext(Collections.unmodifiableList(requests)))));
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) HashMap(java.util.HashMap) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) Collection(com.couchbase.client.java.Collection) KeyValueLocator(com.couchbase.client.core.node.KeyValueLocator) Duration(java.time.Duration) Map(java.util.Map) Stability(com.couchbase.client.core.annotation.Stability) BucketConfig(com.couchbase.client.core.config.BucketConfig) Reactor(com.couchbase.client.core.Reactor) Predicate(java.util.function.Predicate) NodeInfo(com.couchbase.client.core.config.NodeInfo) ObserveViaCasResponse(com.couchbase.client.core.msg.kv.ObserveViaCasResponse) MultiObserveViaCasRequest(com.couchbase.client.core.msg.kv.MultiObserveViaCasRequest) Mono(reactor.core.publisher.Mono) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) StandardCharsets(java.nio.charset.StandardCharsets) MultiObserveViaCasResponse(com.couchbase.client.core.msg.kv.MultiObserveViaCasResponse) Flux(reactor.core.publisher.Flux) List(java.util.List) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) BatchHelperExistsCompletedEvent(com.couchbase.client.java.cnc.evnts.BatchHelperExistsCompletedEvent) Core(com.couchbase.client.core.Core) Collections(java.util.Collections) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MultiObserveViaCasRequest(com.couchbase.client.core.msg.kv.MultiObserveViaCasRequest) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig) Core(com.couchbase.client.core.Core) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) Mono(reactor.core.publisher.Mono) NodeInfo(com.couchbase.client.core.config.NodeInfo) NodeIdentifier(com.couchbase.client.core.node.NodeIdentifier) BatchHelperExistsCompletedEvent(com.couchbase.client.java.cnc.evnts.BatchHelperExistsCompletedEvent) HashMap(java.util.HashMap) Map(java.util.Map) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier)

Example 2 with MultiObserveViaCasRequest

use of com.couchbase.client.core.msg.kv.MultiObserveViaCasRequest in project couchbase-jvm-clients by couchbase.

the class BatchErrorContext method injectExportableParams.

@Override
public void injectExportableParams(final Map<String, Object> input) {
    super.injectExportableParams(input);
    List<Object> requestContexts = new ArrayList<>();
    for (MultiObserveViaCasRequest request : requests) {
        Map<String, Object> rcx = new HashMap<>();
        request.context().injectExportableParams(rcx);
        requestContexts.add(rcx);
    }
    input.put("requests", requestContexts);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MultiObserveViaCasRequest(com.couchbase.client.core.msg.kv.MultiObserveViaCasRequest)

Aggregations

MultiObserveViaCasRequest (com.couchbase.client.core.msg.kv.MultiObserveViaCasRequest)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Core (com.couchbase.client.core.Core)1 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 BucketConfig (com.couchbase.client.core.config.BucketConfig)1 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)1 NodeInfo (com.couchbase.client.core.config.NodeInfo)1 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 MultiObserveViaCasResponse (com.couchbase.client.core.msg.kv.MultiObserveViaCasResponse)1 ObserveViaCasResponse (com.couchbase.client.core.msg.kv.ObserveViaCasResponse)1 KeyValueLocator (com.couchbase.client.core.node.KeyValueLocator)1 NodeIdentifier (com.couchbase.client.core.node.NodeIdentifier)1 Collection (com.couchbase.client.java.Collection)1 BatchHelperExistsCompletedEvent (com.couchbase.client.java.cnc.evnts.BatchHelperExistsCompletedEvent)1 GetResult (com.couchbase.client.java.kv.GetResult)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Duration (java.time.Duration)1