Search in sources :

Example 6 with GetRequest

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);
}
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 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);
}
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 8 with 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);
    }
}
Also used : Endpoint(com.couchbase.client.core.endpoint.Endpoint) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) EndpointSelectionStrategy(com.couchbase.client.core.service.EndpointSelectionStrategy) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Example 9 with GetRequest

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);
}
Also used : GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(com.couchbase.client.core.error.TimeoutException) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 10 with GetRequest

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);
}
Also used : InsertResponse(com.couchbase.client.core.msg.kv.InsertResponse) InsertRequest(com.couchbase.client.core.msg.kv.InsertRequest) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) GetResponse(com.couchbase.client.core.msg.kv.GetResponse) CoreIntegrationTest(com.couchbase.client.core.util.CoreIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

GetRequest (com.couchbase.client.core.msg.kv.GetRequest)18 Test (org.junit.jupiter.api.Test)14 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)6 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)6 CouchbaseBucketConfig (com.couchbase.client.core.config.CouchbaseBucketConfig)5 ArrayList (java.util.ArrayList)5 ClusterConfig (com.couchbase.client.core.config.ClusterConfig)4 NodeInfo (com.couchbase.client.core.config.NodeInfo)4 RequestContext (com.couchbase.client.core.msg.RequestContext)4 GetResponse (com.couchbase.client.core.msg.kv.GetResponse)3 Core (com.couchbase.client.core.Core)2 CoreContext (com.couchbase.client.core.CoreContext)2 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 Endpoint (com.couchbase.client.core.endpoint.Endpoint)2 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)2 InsertRequest (com.couchbase.client.core.msg.kv.InsertRequest)2 InsertResponse (com.couchbase.client.core.msg.kv.InsertResponse)2 SubdocGetRequest (com.couchbase.client.core.msg.kv.SubdocGetRequest)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 EndpointSelectionStrategy (com.couchbase.client.core.service.EndpointSelectionStrategy)2