Search in sources :

Example 6 with CouchbaseBucketConfig

use of com.couchbase.client.core.config.CouchbaseBucketConfig in project couchbase-jvm-clients by couchbase.

the class KeyValueLocatorTest method pickCurrentIfNoFFMapAndNmvbSeen.

@Test
@SuppressWarnings("unchecked")
void pickCurrentIfNoFFMapAndNmvbSeen() {
    Locator locator = new KeyValueLocator();
    // Setup 2 nodes
    NodeInfo nodeInfo1 = new NodeInfo("http://foo:1234", "192.168.56.101:8091", Collections.EMPTY_MAP, null);
    NodeInfo nodeInfo2 = new NodeInfo("http://foo:1234", "192.168.56.102:8091", Collections.EMPTY_MAP, null);
    Node node1Mock = mock(Node.class);
    when(node1Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.101", 8091));
    Node node2Mock = mock(Node.class);
    when(node2Mock.identifier()).thenReturn(new NodeIdentifier("192.168.56.102", 8091));
    List<Node> nodes = new ArrayList<>(Arrays.asList(node1Mock, node2Mock));
    // Configure Cluster and Bucket config
    ClusterConfig configMock = mock(ClusterConfig.class);
    CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
    when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
    when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
    when(bucketMock.numberOfPartitions()).thenReturn(1024);
    when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);
    when(bucketMock.nodeAtIndex(1)).thenReturn(nodeInfo2);
    when(bucketMock.hasFastForwardMap()).thenReturn(false);
    // Fake a vbucket move in ffwd map from node 0 to node 1
    when(bucketMock.nodeIndexForActive(656, false)).thenReturn((short) 0);
    when(bucketMock.nodeIndexForActive(656, true)).thenReturn((short) 1);
    // Create Request
    GetRequest getRequest = mock(GetRequest.class);
    when(getRequest.bucket()).thenReturn("bucket");
    when(getRequest.key()).thenReturn("key".getBytes(UTF_8));
    RequestContext requestCtx = mock(RequestContext.class);
    when(getRequest.context()).thenReturn(requestCtx);
    when(getRequest.rejectedWithNotMyVbucket()).thenReturn(9);
    locator.dispatch(getRequest, nodes, configMock, null);
    verify(node1Mock, times(1)).send(getRequest);
    verify(node2Mock, never()).send(getRequest);
    when(getRequest.rejectedWithNotMyVbucket()).thenReturn(1);
    locator.dispatch(getRequest, nodes, configMock, null);
    verify(node1Mock, times(2)).send(getRequest);
    verify(node2Mock, never()).send(getRequest);
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) NodeInfo(com.couchbase.client.core.config.NodeInfo) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ArrayList(java.util.ArrayList) RequestContext(com.couchbase.client.core.msg.RequestContext) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 7 with CouchbaseBucketConfig

use of com.couchbase.client.core.config.CouchbaseBucketConfig in project couchbase-jvm-clients by couchbase.

the class ReplicaHelper method getAllReplicasRequests.

/**
 * Helper method to assemble a stream of requests to the active and all replicas
 *
 * @param core the core to execute the request
 * @param collectionIdentifier the collection containing the document
 * @param documentId the ID of the document
 * @param clientContext (nullable) client context info
 * @param retryStrategy the retry strategy to use
 * @param timeout the timeout until we need to stop the get all replicas
 * @param parent the "get all/any replicas" request span
 * @return a stream of requests.
 */
public static CompletableFuture<Stream<GetRequest>> getAllReplicasRequests(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Map<String, Object> clientContext, final RetryStrategy retryStrategy, final Duration timeout, final RequestSpan parent) {
    notNullOrEmpty(documentId, "Id");
    final CoreContext coreContext = core.context();
    final CoreEnvironment environment = coreContext.environment();
    final BucketConfig config = core.clusterConfig().bucketConfig(collectionIdentifier.bucket());
    if (config instanceof CouchbaseBucketConfig) {
        int numReplicas = ((CouchbaseBucketConfig) config).numberOfReplicas();
        List<GetRequest> requests = new ArrayList<>(numReplicas + 1);
        RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, parent);
        GetRequest activeRequest = new GetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, span);
        activeRequest.context().clientContext(clientContext);
        requests.add(activeRequest);
        for (short replica = 1; replica <= numReplicas; replica++) {
            RequestSpan replicaSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET_REPLICA, parent);
            ReplicaGetRequest replicaRequest = new ReplicaGetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, replica, replicaSpan);
            replicaRequest.context().clientContext(clientContext);
            requests.add(replicaRequest);
        }
        return CompletableFuture.completedFuture(requests.stream());
    } else if (config == null) {
        // no bucket config found, it might be in-flight being opened so we need to reschedule the operation until
        // the timeout fires!
        final Duration retryDelay = Duration.ofMillis(100);
        final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
        coreContext.environment().timer().schedule(() -> {
            getAllReplicasRequests(core, collectionIdentifier, documentId, clientContext, retryStrategy, timeout.minus(retryDelay), parent).whenComplete((getRequestStream, throwable) -> {
                if (throwable != null) {
                    future.completeExceptionally(throwable);
                } else {
                    future.complete(getRequestStream);
                }
            });
        }, retryDelay);
        return future;
    } else {
        final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
        future.completeExceptionally(CommonExceptions.getFromReplicaNotCouchbaseBucket());
        return future;
    }
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) DefaultErrorUtil.keyValueStatusToException(com.couchbase.client.core.error.DefaultErrorUtil.keyValueStatusToException) TracingIdentifiers(com.couchbase.client.core.cnc.TracingIdentifiers) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DocumentUnretrievableException(com.couchbase.client.core.error.DocumentUnretrievableException) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Stability(com.couchbase.client.core.annotation.Stability) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) IndividualReplicaGetFailedEvent(com.couchbase.client.core.cnc.events.request.IndividualReplicaGetFailedEvent) BucketConfig(com.couchbase.client.core.config.BucketConfig) Reactor(com.couchbase.client.core.Reactor) CommonExceptions(com.couchbase.client.core.error.CommonExceptions) Mono(reactor.core.publisher.Mono) CompletionException(java.util.concurrent.CompletionException) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) Collectors(java.util.stream.Collectors) Validators.notNullOrEmpty(com.couchbase.client.core.util.Validators.notNullOrEmpty) Flux(reactor.core.publisher.Flux) List(java.util.List) Stream(java.util.stream.Stream) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ErrorContext(com.couchbase.client.core.error.context.ErrorContext) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) Core(com.couchbase.client.core.Core) ReducedKeyValueErrorContext(com.couchbase.client.core.error.context.ReducedKeyValueErrorContext) Collections(java.util.Collections) AggregateErrorContext(com.couchbase.client.core.error.context.AggregateErrorContext) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) Duration(java.time.Duration) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig) RequestSpan(com.couchbase.client.core.cnc.RequestSpan) CompletableFuture(java.util.concurrent.CompletableFuture) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest) ReplicaGetRequest(com.couchbase.client.core.msg.kv.ReplicaGetRequest)

Example 8 with CouchbaseBucketConfig

use of com.couchbase.client.core.config.CouchbaseBucketConfig in project couchbase-jvm-clients by couchbase.

the class ViewLocatorTest method dispatchesOnlyToHostsWithPrimaryPartitionsEnabled.

@Test
void dispatchesOnlyToHostsWithPrimaryPartitionsEnabled() {
    ViewLocator locator = new ViewLocator();
    ViewRequest request = mock(ViewRequest.class);
    when(request.bucket()).thenReturn("bucket");
    CouchbaseBucketConfig bucketConfig = mock(CouchbaseBucketConfig.class);
    ClusterConfig config = mock(ClusterConfig.class);
    when(config.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasPrimaryPartitionsOnNode("1.2.3.4")).thenReturn(true);
    when(bucketConfig.hasPrimaryPartitionsOnNode("1.2.3.5")).thenReturn(false);
    Node node1 = mock(Node.class);
    when(node1.identifier()).thenReturn(new NodeIdentifier("1.2.3.4", 1234));
    assertTrue(locator.nodeCanBeUsed(node1, request, config));
    Node node2 = mock(Node.class);
    when(node2.identifier()).thenReturn(new NodeIdentifier("1.2.3.5", 1234));
    assertFalse(locator.nodeCanBeUsed(node2, request, config));
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ViewRequest(com.couchbase.client.core.msg.view.ViewRequest) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Test(org.junit.jupiter.api.Test)

Example 9 with CouchbaseBucketConfig

use of com.couchbase.client.core.config.CouchbaseBucketConfig in project couchbase-jvm-clients by couchbase.

the class Observe method validateReplicas.

private static int validateReplicas(final BucketConfig bucketConfig, final ObservePersistTo persistTo, final ObserveReplicateTo replicateTo) {
    if (!(bucketConfig instanceof CouchbaseBucketConfig)) {
        throw new FeatureNotAvailableException("Only couchbase buckets support PersistTo and/or ReplicateTo");
    }
    CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) bucketConfig;
    int numReplicas = cbc.numberOfReplicas();
    if (cbc.ephemeral() && persistTo.value() != 0) {
        throw new FeatureNotAvailableException("Ephemeral Buckets do not support " + "PersistTo");
    }
    if (replicateTo.touchesReplica() && replicateTo.value() > numReplicas) {
        throw new ReplicaNotConfiguredException("Not enough replicas configured on " + "the bucket");
    }
    if (persistTo.touchesReplica() && persistTo.value() - 1 > numReplicas) {
        throw new ReplicaNotConfiguredException("Not enough replicas configured on " + "the bucket");
    }
    return numReplicas;
}
Also used : FeatureNotAvailableException(com.couchbase.client.core.error.FeatureNotAvailableException) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ReplicaNotConfiguredException(com.couchbase.client.core.error.ReplicaNotConfiguredException)

Example 10 with CouchbaseBucketConfig

use of com.couchbase.client.core.config.CouchbaseBucketConfig in project couchbase-jvm-clients by couchbase.

the class NodeLocatorHelper method replicaNodesForId.

/**
 * Returns all target replica nodes addresses for a given document ID on the bucket.
 *
 * @param id the document id to convert.
 * @return the node for the given document id.
 */
public List<String> replicaNodesForId(final String id) {
    BucketConfig config = bucketConfig.get();
    if (config instanceof CouchbaseBucketConfig) {
        CouchbaseBucketConfig cbc = (CouchbaseBucketConfig) config;
        List<String> replicas = new ArrayList<>();
        for (int i = 1; i <= cbc.numberOfReplicas(); i++) {
            replicas.add(replicaNodeForId(id, i));
        }
        return replicas;
    } else {
        throw new UnsupportedOperationException("Bucket type not supported: " + config.getClass().getName());
    }
}
Also used : CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) ArrayList(java.util.ArrayList) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) MemcachedBucketConfig(com.couchbase.client.core.config.MemcachedBucketConfig) BucketConfig(com.couchbase.client.core.config.BucketConfig)

Aggregations

CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)11 ArrayList (java.util.ArrayList)7 BucketConfig (com.couchbase.client.core.config.BucketConfig)5 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)5 NodeInfo (com.couchbase.client.core.config.NodeInfo)5 GetRequest (com.couchbase.client.core.msg.kv.GetRequest)5 Test (org.junit.jupiter.api.Test)5 RequestContext (com.couchbase.client.core.msg.RequestContext)4 Core (com.couchbase.client.core.Core)3 MemcachedBucketConfig (com.couchbase.client.core.config.MemcachedBucketConfig)3 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)3 CoreContext (com.couchbase.client.core.CoreContext)2 Reactor (com.couchbase.client.core.Reactor)2 Stability (com.couchbase.client.core.annotation.Stability)2 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)2 Duration (java.time.Duration)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Flux (reactor.core.publisher.Flux)2