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);
}
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;
}
}
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));
}
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;
}
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());
}
}
Aggregations