use of com.couchbase.client.core.msg.kv.KeyValueRequest in project couchbase-jvm-clients by couchbase.
the class KeyValueLocator method dispatch.
@Override
public void dispatch(final Request<? extends Response> request, final List<Node> nodes, final ClusterConfig config, final CoreContext ctx) {
if (request.target() != null) {
dispatchTargeted(request, nodes, ctx);
} else {
KeyValueRequest r = (KeyValueRequest) request;
String bucket = r.bucket();
BucketConfig bucketConfig = config.bucketConfig(bucket);
if (bucketConfig == null) {
// Since a bucket is opened lazily, it might not be available yet (or for some
// other reason the config is gone) - send it into retry!
RetryOrchestrator.maybeRetry(ctx, request, ctx.core().configurationProvider().bucketConfigLoadInProgress() ? RetryReason.BUCKET_OPEN_IN_PROGRESS : RetryReason.BUCKET_NOT_AVAILABLE);
return;
}
if (bucketConfig instanceof CouchbaseBucketConfig) {
couchbaseBucket(r, nodes, (CouchbaseBucketConfig) bucketConfig, ctx);
} else if (bucketConfig instanceof MemcachedBucketConfig) {
memcacheBucket(r, nodes, (MemcachedBucketConfig) bucketConfig, ctx);
} else {
throw new IllegalStateException("Unsupported Bucket Type: " + bucketConfig + " for request " + request);
}
}
}
use of com.couchbase.client.core.msg.kv.KeyValueRequest in project couchbase-jvm-clients by couchbase.
the class KeyValueMessageHandler method write.
@Override
@SuppressWarnings({ "unchecked" })
public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
if (msg instanceof KeyValueRequest) {
KeyValueRequest<Response> request = (KeyValueRequest<Response>) msg;
int opaque = request.opaque();
writtenRequests.put(opaque, request);
try {
ctx.write(request.encode(ctx.alloc(), opaque, channelContext), promise);
writtenRequestDispatchTimings.put(opaque, (Long) System.nanoTime());
if (request.requestSpan() != null) {
RequestTracer tracer = endpointContext.environment().requestTracer();
RequestSpan dispatchSpan = tracer.requestSpan(TracingIdentifiers.SPAN_DISPATCH, request.requestSpan());
if (!isInternalTracer) {
setCommonDispatchSpanAttributes(dispatchSpan, ctx.channel().attr(ChannelAttributes.CHANNEL_ID_KEY).get(), ioContext.localHostname(), ioContext.localPort(), endpoint.remoteHostname(), endpoint.remotePort(), null);
setNumericOperationId(dispatchSpan, request.opaque());
setCommonKVSpanAttributes(dispatchSpan, request);
}
writtenRequestDispatchSpans.put(opaque, dispatchSpan);
}
} catch (Throwable err) {
writtenRequests.remove(opaque);
if (err instanceof CollectionNotFoundException) {
if (channelContext.collectionsEnabled()) {
ConfigurationProvider cp = ioContext.core().configurationProvider();
if (cp.collectionRefreshInProgress(request.collectionIdentifier())) {
RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.COLLECTION_MAP_REFRESH_IN_PROGRESS);
} else if (cp.config().bucketConfig(request.bucket()) instanceof MemcachedBucketConfig) {
request.fail(FeatureNotAvailableException.collectionsForMemcached());
} else {
handleOutdatedCollection(request, RetryReason.COLLECTION_NOT_FOUND);
}
return;
}
}
request.fail(err);
}
} else {
eventBus.publish(new InvalidRequestDetectedEvent(ioContext, ServiceType.KV, msg));
ctx.channel().close().addListener(f -> eventBus.publish(new ChannelClosedProactivelyEvent(ioContext, ChannelClosedProactivelyEvent.Reason.INVALID_REQUEST_DETECTED)));
}
}
use of com.couchbase.client.core.msg.kv.KeyValueRequest in project couchbase-jvm-clients by couchbase.
the class KeyValueMessageHandler method handleNotMyVbucket.
/**
* Helper method to handle a "not my vbucket" response.
*
* @param request the request to retry.
* @param response the response to extract the config from, potentially.
*/
private void handleNotMyVbucket(final KeyValueRequest<Response> request, final ByteBuf response) {
request.indicateRejectedWithNotMyVbucket();
eventBus.publish(new NotMyVbucketReceivedEvent(ioContext, request.partition()));
final String origin = request.context().lastDispatchedTo() != null ? request.context().lastDispatchedTo().hostname() : null;
RetryOrchestrator.maybeRetry(ioContext, request, RetryReason.KV_NOT_MY_VBUCKET);
body(response).map(b -> b.toString(UTF_8).trim()).filter(c -> c.startsWith("{")).ifPresent(c -> ioContext.core().configurationProvider().proposeBucketConfig(new ProposedBucketConfigContext(request.bucket(), c, origin)));
}
use of com.couchbase.client.core.msg.kv.KeyValueRequest in project couchbase-jvm-clients by couchbase.
the class NodeTest method retriesIfLocalServiceNotFound.
@Test
@SuppressWarnings({ "unchecked" })
void retriesIfLocalServiceNotFound() {
final Service s = mock(Service.class);
final AtomicReference<Request<?>> retried = new AtomicReference<>();
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.states()).thenReturn(DirectProcessor.create());
when(s.type()).thenReturn(serviceType);
return s;
}
@Override
protected <R extends Request<? extends Response>> void sendIntoRetry(R request) {
retried.set(request);
}
};
node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
KeyValueRequest r = mock(KeyValueRequest.class);
when(r.serviceType()).thenReturn(ServiceType.KV);
when(r.bucket()).thenReturn("other_bucket");
node.send(r);
verify(s, never()).send(eq(r));
assertEquals(r, retried.get());
}
use of com.couchbase.client.core.msg.kv.KeyValueRequest in project couchbase-jvm-clients by couchbase.
the class NodeTest method sendsToFoundLocalService.
@Test
@SuppressWarnings({ "unchecked" })
void sendsToFoundLocalService() {
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.states()).thenReturn(DirectProcessor.create());
when(s.type()).thenReturn(serviceType);
return s;
}
};
node.addService(ServiceType.KV, 11210, Optional.of("bucket")).block();
KeyValueRequest r = mock(KeyValueRequest.class);
when(r.serviceType()).thenReturn(ServiceType.KV);
when(r.bucket()).thenReturn("bucket");
when(r.context()).thenReturn(new RequestContext(CTX, r));
node.send(r);
verify(s, times(1)).send(eq(r));
}
Aggregations