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