use of com.couchbase.client.core.msg.RequestContext in project couchbase-jvm-clients by couchbase.
the class LoggingEventConsumerTest method attachesClientContextIfEnabled.
@Test
void attachesClientContextIfEnabled() {
LoggingEventConsumer loggingEventConsumer = LoggingEventConsumer.create(LoggerConfig.enableDiagnosticContext(true).customLogger(logger).build());
RequestContext context = mock(RequestContext.class);
Map<String, Object> userContext = new HashMap<>();
userContext.put("hello", "world");
when(context.clientContext()).thenReturn(userContext);
RequestRetryScheduledEvent retryEvent = new RequestRetryScheduledEvent(Duration.ofSeconds(1), context, GetRequest.class, RetryReason.UNKNOWN);
loggingEventConsumer.accept(retryEvent);
verify(logger, times(1)).attachContext(userContext);
}
use of com.couchbase.client.core.msg.RequestContext in project couchbase-jvm-clients by couchbase.
the class KeyValueLocatorTest method locateGetRequestForCouchbaseBucket.
@Test
@SuppressWarnings("unchecked")
void locateGetRequestForCouchbaseBucket() {
Locator locator = new KeyValueLocator();
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);
GetRequest getRequestMock = mock(GetRequest.class);
ClusterConfig configMock = mock(ClusterConfig.class);
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));
CouchbaseBucketConfig bucketMock = mock(CouchbaseBucketConfig.class);
when(getRequestMock.bucket()).thenReturn("bucket");
when(getRequestMock.key()).thenReturn("key".getBytes(UTF_8));
CoreContext coreContext = new CoreContext(mock(Core.class), 1, mock(CoreEnvironment.class), mock(Authenticator.class));
when(getRequestMock.context()).thenReturn(new RequestContext(coreContext, getRequestMock));
when(configMock.bucketConfig("bucket")).thenReturn(bucketMock);
when(bucketMock.nodes()).thenReturn(Arrays.asList(nodeInfo1, nodeInfo2));
when(bucketMock.numberOfPartitions()).thenReturn(1024);
when(bucketMock.nodeIndexForActive(656, false)).thenReturn((short) 0);
when(bucketMock.nodeAtIndex(0)).thenReturn(nodeInfo1);
locator.dispatch(getRequestMock, nodes, configMock, null);
verify(node1Mock, times(1)).send(getRequestMock);
verify(node2Mock, never()).send(getRequestMock);
}
use of com.couchbase.client.core.msg.RequestContext in project couchbase-jvm-clients by couchbase.
the class KeyValueLocatorTest method pickFastForwardIfAvailableAndNmvbSeen.
@Test
@SuppressWarnings("unchecked")
void pickFastForwardIfAvailableAndNmvbSeen() {
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(true);
// 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);
// Dispatch with retry 0
when(requestCtx.retryAttempts()).thenReturn(0);
locator.dispatch(getRequest, nodes, configMock, null);
verify(node1Mock, times(1)).send(getRequest);
verify(node2Mock, never()).send(getRequest);
// Dispatch with retry 1 but no nmvb seen, still go to the active
when(requestCtx.retryAttempts()).thenReturn(1);
locator.dispatch(getRequest, nodes, configMock, null);
verify(node1Mock, times(2)).send(getRequest);
verify(node2Mock, never()).send(getRequest);
// Dispatch with retry 2, now we see a NMVB
when(requestCtx.retryAttempts()).thenReturn(2);
when(getRequest.rejectedWithNotMyVbucket()).thenReturn(1);
locator.dispatch(getRequest, nodes, configMock, null);
verify(node1Mock, times(2)).send(getRequest);
verify(node2Mock, times(1)).send(getRequest);
}
use of com.couchbase.client.core.msg.RequestContext in project couchbase-jvm-clients by couchbase.
the class KeyValueLocatorTest method pickCurrentIfNoFFMapAndRetry.
@Test
@SuppressWarnings("unchecked")
void pickCurrentIfNoFFMapAndRetry() {
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);
// Dispatch with retry 0
when(requestCtx.retryAttempts()).thenReturn(0);
locator.dispatch(getRequest, nodes, configMock, null);
verify(node1Mock, times(1)).send(getRequest);
verify(node2Mock, never()).send(getRequest);
// Dispatch with retry 1
when(requestCtx.retryAttempts()).thenReturn(1);
locator.dispatch(getRequest, nodes, configMock, null);
verify(node1Mock, times(2)).send(getRequest);
verify(node2Mock, never()).send(getRequest);
// Dispatch with retry 5
when(requestCtx.retryAttempts()).thenReturn(5);
locator.dispatch(getRequest, nodes, configMock, null);
verify(node1Mock, times(3)).send(getRequest);
verify(node2Mock, never()).send(getRequest);
}
use of com.couchbase.client.core.msg.RequestContext in project couchbase-jvm-clients by couchbase.
the class NodeTest method sendsToFoundGlobalService.
@Test
void sendsToFoundGlobalService() {
final Service s = mock(Service.class);
Node node = new Node(CTX, mock(NodeIdentifier.class), NO_ALTERNATE) {
@Override
protected Service createService(ServiceType serviceType, int port, Optional<String> bucket) {
when(s.state()).thenReturn(ServiceState.CONNECTED);
when(s.type()).thenReturn(serviceType);
when(s.states()).thenReturn(DirectProcessor.create());
return s;
}
};
node.addService(ServiceType.QUERY, 8091, Optional.empty()).block();
QueryRequest r = mock(QueryRequest.class);
when(r.serviceType()).thenReturn(ServiceType.QUERY);
when(r.context()).thenReturn(new RequestContext(CTX, r));
node.send(r);
verify(s, times(1)).send(eq(r));
}
Aggregations