use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class ReplicaHelper method getAllReplicasRequests.
/**
* Helper method to assemble a stream of requests to the active and all replicas
*
* @param core the core to execute the request
* @param collectionIdentifier the collection containing the document
* @param documentId the ID of the document
* @param clientContext (nullable) client context info
* @param retryStrategy the retry strategy to use
* @param timeout the timeout until we need to stop the get all replicas
* @param parent the "get all/any replicas" request span
* @return a stream of requests.
*/
public static CompletableFuture<Stream<GetRequest>> getAllReplicasRequests(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Map<String, Object> clientContext, final RetryStrategy retryStrategy, final Duration timeout, final RequestSpan parent) {
notNullOrEmpty(documentId, "Id");
final CoreContext coreContext = core.context();
final CoreEnvironment environment = coreContext.environment();
final BucketConfig config = core.clusterConfig().bucketConfig(collectionIdentifier.bucket());
if (config instanceof CouchbaseBucketConfig) {
int numReplicas = ((CouchbaseBucketConfig) config).numberOfReplicas();
List<GetRequest> requests = new ArrayList<>(numReplicas + 1);
RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET, parent);
GetRequest activeRequest = new GetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, span);
activeRequest.context().clientContext(clientContext);
requests.add(activeRequest);
for (short replica = 1; replica <= numReplicas; replica++) {
RequestSpan replicaSpan = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_GET_REPLICA, parent);
ReplicaGetRequest replicaRequest = new ReplicaGetRequest(documentId, timeout, coreContext, collectionIdentifier, retryStrategy, replica, replicaSpan);
replicaRequest.context().clientContext(clientContext);
requests.add(replicaRequest);
}
return CompletableFuture.completedFuture(requests.stream());
} else if (config == null) {
// no bucket config found, it might be in-flight being opened so we need to reschedule the operation until
// the timeout fires!
final Duration retryDelay = Duration.ofMillis(100);
final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
coreContext.environment().timer().schedule(() -> {
getAllReplicasRequests(core, collectionIdentifier, documentId, clientContext, retryStrategy, timeout.minus(retryDelay), parent).whenComplete((getRequestStream, throwable) -> {
if (throwable != null) {
future.completeExceptionally(throwable);
} else {
future.complete(getRequestStream);
}
});
}, retryDelay);
return future;
} else {
final CompletableFuture<Stream<GetRequest>> future = new CompletableFuture<>();
future.completeExceptionally(CommonExceptions.getFromReplicaNotCouchbaseBucket());
return future;
}
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method beforeEach.
@BeforeEach
void beforeEach() {
eventLoopGroup = new NioEventLoopGroup(1);
eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
environment = CoreEnvironment.builder().eventBus(eventBus).build();
CoreContext coreContext = new CoreContext(mock(Core.class), 1, environment, authenticator);
ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class BaseEndpointTest method disconnectDuringRetry.
/**
* Make sure that while we are retrying and a disconnect event comes along, we stop
* retrying and end up in the disconnected state right away.
*/
@Test
void disconnectDuringRetry() {
SimpleEventBus eventBus = new SimpleEventBus(true, Collections.singletonList(EndpointStateChangedEvent.class));
CoreEnvironment env = CoreEnvironment.builder().eventBus(eventBus).timeoutConfig(TimeoutConfig.connectTimeout(Duration.ofMillis(10))).build();
CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, authenticator);
ServiceContext ctx = new ServiceContext(coreContext, LOCALHOST, 1234, ServiceType.KV, Optional.empty());
try {
final CompletableFuture<Channel> cf = new CompletableFuture<>();
InstrumentedEndpoint endpoint = InstrumentedEndpoint.create(eventLoopGroup, ctx, () -> Mono.fromFuture(cf));
endpoint.connect();
waitUntilCondition(() -> eventBus.publishedEvents().size() >= 3);
endpoint.disconnect();
waitUntilCondition(() -> endpoint.state() == EndpointState.DISCONNECTED);
waitUntilCondition(() -> eventBus.publishedEvents().size() >= 4);
int warn = 0;
int debug = 0;
for (Event event : eventBus.publishedEvents()) {
if (event.severity() == Event.Severity.WARN) {
warn++;
assertTrue(event instanceof EndpointConnectionFailedEvent);
} else if (event.severity() == Event.Severity.DEBUG) {
debug++;
assertTrue(event instanceof EndpointConnectionAbortedEvent);
} else {
throw new RuntimeException("Unexpected Event: " + event);
}
}
assertEquals(3, warn);
assertEquals(1, debug);
} finally {
environment.shutdown();
}
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class RequestContextTest method customPayloadCanBeAttached.
@Test
void customPayloadCanBeAttached() {
Request<?> request = mock(Request.class);
Core core = mock(Core.class);
RequestContext ctx = new RequestContext(new CoreContext(core, 1, null, mock(Authenticator.class)), request);
assertNull(ctx.clientContext());
Map<String, Object> payload = new HashMap<>();
payload.put("foo", true);
ctx.clientContext(payload);
assertEquals(payload, ctx.clientContext());
}
use of com.couchbase.client.core.CoreContext in project couchbase-jvm-clients by couchbase.
the class RetryOrchestratorTest method cancelIfNoMoreRetriesAllowed.
@Test
@SuppressWarnings({ "unchecked" })
void cancelIfNoMoreRetriesAllowed() {
RetryStrategy retryStrategy = mock(RetryStrategy.class);
when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.noRetry()));
Request<?> request = mock(Request.class);
when(request.completed()).thenReturn(false);
when(request.retryStrategy()).thenReturn(retryStrategy);
RequestContext requestContext = mock(RequestContext.class);
when(request.context()).thenReturn(requestContext);
CoreEnvironment env = mock(CoreEnvironment.class);
SimpleEventBus eventBus = new SimpleEventBus(true);
when(env.eventBus()).thenReturn(eventBus);
CoreContext context = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
RetryOrchestrator.maybeRetry(context, request, RetryReason.UNKNOWN);
verify(request, times(1)).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN), Function.identity());
assertEquals(1, eventBus.publishedEvents().size());
RequestNotRetriedEvent retryEvent = (RequestNotRetriedEvent) eventBus.publishedEvents().get(0);
assertEquals(Event.Severity.INFO, retryEvent.severity());
assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
assertEquals(requestContext, retryEvent.context());
}
Aggregations