use of com.couchbase.client.core.msg.kv.GetRequest 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.kv.GetRequest 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.kv.GetRequest in project couchbase-jvm-clients by couchbase.
the class PartitionSelectionStrategyTest method selectNullIfPinedIsNotConnected.
@Test
void selectNullIfPinedIsNotConnected() {
EndpointSelectionStrategy strategy = new PartitionSelectionStrategy();
Endpoint endpoint1 = mock(Endpoint.class);
Endpoint endpoint2 = mock(Endpoint.class);
Endpoint endpoint3 = mock(Endpoint.class);
when(endpoint1.state()).thenReturn(EndpointState.DISCONNECTED);
when(endpoint2.state()).thenReturn(EndpointState.CONNECTED);
when(endpoint3.state()).thenReturn(EndpointState.CONNECTED);
when(endpoint1.freeToWrite()).thenReturn(true);
when(endpoint2.freeToWrite()).thenReturn(true);
when(endpoint3.freeToWrite()).thenReturn(true);
List<Endpoint> endpoints = Arrays.asList(endpoint1, endpoint2, endpoint3);
GetRequest request = mock(GetRequest.class);
when(request.partition()).thenReturn((short) 12);
Endpoint selected = strategy.select(request, endpoints);
for (int i = 0; i < 1000; i++) {
assertNull(selected);
}
}
use of com.couchbase.client.core.msg.kv.GetRequest in project couchbase-jvm-clients by couchbase.
the class KeyValueIntegrationTest method timesOutVeryLowTimeoutDurations.
/**
* The timer wheel has a resolution if 100ms by default, so very low timeouts might go through and never have
* a chance of getting into the next tick.
*
* <p>The code has additional checks in place to proactively check for such a timeout. This test makes sure that
* super low timeouts always hit.</p>
*/
@Test
void timesOutVeryLowTimeoutDurations() {
GetRequest getRequest = new GetRequest("foo", Duration.ofNanos(1), core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), null);
core.send(getRequest);
ExecutionException exception = assertThrows(ExecutionException.class, () -> getRequest.response().get());
assertTrue(exception.getCause() instanceof TimeoutException);
}
use of com.couchbase.client.core.msg.kv.GetRequest in project couchbase-jvm-clients by couchbase.
the class KeyValueIntegrationTest method insertAndGet.
/**
* Validate that an inserted document can be read subsequently.
*/
@Test
void insertAndGet() throws Exception {
String id = UUID.randomUUID().toString();
byte[] content = "hello, world".getBytes(UTF_8);
InsertRequest insertRequest = new InsertRequest(id, content, 0, 0, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), Optional.empty(), null);
core.send(insertRequest);
InsertResponse insertResponse = insertRequest.response().get();
assertTrue(insertResponse.status().success());
GetRequest getRequest = new GetRequest(id, kvTimeout, core.context(), CollectionIdentifier.fromDefault(config().bucketname()), env.retryStrategy(), null);
core.send(getRequest);
GetResponse getResponse = getRequest.response().get();
assertTrue(getResponse.status().success());
assertArrayEquals(content, getResponse.content());
assertTrue(getResponse.cas() != 0);
}
Aggregations