Search in sources :

Example 1 with GetRequest

use of com.couchbase.client.core.msg.kv.GetRequest in project couchbase-jvm-clients by couchbase.

the class TransportEncryptionIntegrationTest method runKeyValueOperation.

private void runKeyValueOperation(Core core, CoreEnvironment env) 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)

Example 2 with GetRequest

use of com.couchbase.client.core.msg.kv.GetRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandlerTest method closesChannelOnCertainStatusCodes.

/**
 * As part of the KV error map, certain status codes have been identified as "must close" on the channel
 * to avoid further problems.
 *
 * <p>This test makes sure that on all of those codes, the channel gets closed accordingly.</p>
 */
@Test
void closesChannelOnCertainStatusCodes() {
    Set<MemcacheProtocol.Status> closeOnThese = EnumSet.of(MemcacheProtocol.Status.INTERNAL_SERVER_ERROR, MemcacheProtocol.Status.NO_BUCKET, MemcacheProtocol.Status.NOT_INITIALIZED);
    for (MemcacheProtocol.Status status : closeOnThese) {
        EmbeddedChannel channel = new EmbeddedChannel(new KeyValueMessageHandler(null, CTX, Optional.of(BUCKET)));
        try {
            GetRequest request1 = new GetRequest("key", Duration.ofSeconds(1), CTX, CID, FailFastRetryStrategy.INSTANCE, null);
            channel.writeOutbound(request1);
            ByteBuf getResponse = MemcacheProtocol.response(channel.alloc(), MemcacheProtocol.Opcode.GET, (byte) 0, status.status(), request1.opaque(), 0, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
            channel.writeInbound(getResponse);
            assertFalse(channel.isOpen());
            assertEquals(0, getResponse.refCnt());
        } finally {
            channel.finishAndReleaseAll();
        }
    }
}
Also used : GetRequest(com.couchbase.client.core.msg.kv.GetRequest) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 3 with GetRequest

use of com.couchbase.client.core.msg.kv.GetRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueMessageHandlerTest method attemptsRetryIfInstructedByErrorMap.

/**
 * If an unknown response code is returned and the consulted error map indicates a retry, it should be passed to
 * the retry orchestrator for correct handling.
 */
@Test
void attemptsRetryIfInstructedByErrorMap() {
    EmbeddedChannel channel = new EmbeddedChannel(new KeyValueMessageHandler(null, CTX, Optional.of(BUCKET)));
    ErrorMap errorMap = mock(ErrorMap.class);
    Map<Short, ErrorMap.ErrorCode> errors = new HashMap<>();
    ErrorMap.ErrorCode code = mock(ErrorMap.ErrorCode.class);
    errors.put((short) 0xFF, code);
    Set<ErrorMap.ErrorAttribute> attributes = new HashSet<>();
    attributes.add(ErrorMap.ErrorAttribute.RETRY_NOW);
    when(code.attributes()).thenReturn(attributes);
    when(errorMap.errors()).thenReturn(errors);
    channel.attr(ChannelAttributes.ERROR_MAP_KEY).set(errorMap);
    channel.pipeline().fireChannelActive();
    try {
        GetRequest request = new GetRequest("key", Duration.ofSeconds(1), CTX, CID, FailFastRetryStrategy.INSTANCE, null);
        channel.writeOutbound(request);
        ByteBuf getResponse = MemcacheProtocol.response(channel.alloc(), MemcacheProtocol.Opcode.GET, (byte) 0, (short) 0xFF, request.opaque(), 0, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER, Unpooled.EMPTY_BUFFER);
        channel.writeInbound(getResponse);
        ExecutionException exception = assertThrows(ExecutionException.class, () -> request.response().get());
        assertTrue(exception.getCause() instanceof RequestCanceledException);
        assertEquals("NO_MORE_RETRIES", request.cancellationReason().identifier());
        assertEquals(RetryReason.KV_ERROR_MAP_INDICATED, request.cancellationReason().innerReason());
        assertEquals(0, getResponse.refCnt());
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : HashMap(java.util.HashMap) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with GetRequest

use of com.couchbase.client.core.msg.kv.GetRequest in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method fullGetRequest.

/**
 * Helper method to create a get request for a full doc fetch.
 *
 * @param id the document id which is used to uniquely identify it.
 * @param opts custom options to change the default behavior.
 * @return the get request.
 */
@Stability.Internal
GetRequest fullGetRequest(final String id, final GetOptions.Built opts) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, opts.parentSpan().orElse(null));
    GetRequest request = new GetRequest(id, timeout, coreContext, collectionIdentifier, retryStrategy, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : SubdocGetRequest(com.couchbase.client.core.msg.kv.SubdocGetRequest) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 5 with GetRequest

use of com.couchbase.client.core.msg.kv.GetRequest 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);
}
Also used : CoreContext(com.couchbase.client.core.CoreContext) CouchbaseBucketConfig(com.couchbase.client.core.config.CouchbaseBucketConfig) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) ArrayList(java.util.ArrayList) NodeInfo(com.couchbase.client.core.config.NodeInfo) GetRequest(com.couchbase.client.core.msg.kv.GetRequest) RequestContext(com.couchbase.client.core.msg.RequestContext) Authenticator(com.couchbase.client.core.env.Authenticator) ClusterConfig(com.couchbase.client.core.config.ClusterConfig) Core(com.couchbase.client.core.Core) 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