use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class ReplicaHelper method getAnyReplicaAsync.
/**
* @param clientContext (nullable)
* @param parentSpan (nullable)
* @param responseMapper converts the GetReplicaResponse to the client's native result type
*/
public static <R> CompletableFuture<R> getAnyReplicaAsync(final Core core, final CollectionIdentifier collectionIdentifier, final String documentId, final Duration timeout, final RetryStrategy retryStrategy, final Map<String, Object> clientContext, final RequestSpan parentSpan, final Function<GetReplicaResponse, R> responseMapper) {
RequestSpan getAnySpan = core.context().environment().requestTracer().requestSpan(TracingIdentifiers.SPAN_GET_ANY_REPLICA, parentSpan);
CompletableFuture<List<CompletableFuture<R>>> listOfFutures = getAllReplicasAsync(core, collectionIdentifier, documentId, timeout, retryStrategy, clientContext, getAnySpan, responseMapper);
// Aggregating the futures here will discard the individual errors, which we don't need
CompletableFuture<R> anyReplicaFuture = new CompletableFuture<>();
listOfFutures.whenComplete((futures, throwable) -> {
if (throwable != null) {
anyReplicaFuture.completeExceptionally(throwable);
}
final AtomicBoolean successCompleted = new AtomicBoolean(false);
final AtomicInteger totalCompleted = new AtomicInteger(0);
final List<ErrorContext> nestedContexts = Collections.synchronizedList(new ArrayList<>());
futures.forEach(individual -> individual.whenComplete((result, error) -> {
int completed = totalCompleted.incrementAndGet();
if (error != null) {
if (error instanceof CompletionException && error.getCause() instanceof CouchbaseException) {
nestedContexts.add(((CouchbaseException) error.getCause()).context());
}
}
if (result != null && successCompleted.compareAndSet(false, true)) {
anyReplicaFuture.complete(result);
}
if (!successCompleted.get() && completed == futures.size()) {
anyReplicaFuture.completeExceptionally(new DocumentUnretrievableException(new AggregateErrorContext(nestedContexts)));
}
}));
});
return anyReplicaFuture.whenComplete((getReplicaResult, throwable) -> getAnySpan.end());
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class ClusterManagerBucketRefresherTest method beforeEach.
@BeforeEach
void beforeEach() {
SimpleEventBus eventBus = new SimpleEventBus(true);
env = CoreEnvironment.builder().eventBus(eventBus).build();
CoreContext coreContext = mock(CoreContext.class);
core = mock(Core.class);
when(core.context()).thenReturn(coreContext);
when(coreContext.environment()).thenReturn(env);
ConfigurationProvider provider = mock(ConfigurationProvider.class);
refresher = new ClusterManagerBucketRefresher(provider, core);
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class NodeTest method beforeAll.
@BeforeAll
static void beforeAll() {
Core core = mock(Core.class);
ENV = CoreEnvironment.builder().build();
CTX = new CoreContext(core, 1, ENV, mock(Authenticator.class));
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class RetryOrchestratorTest method retryWithDelay.
@Test
@SuppressWarnings({ "unchecked" })
void retryWithDelay() {
Timer timer = Timer.createAndStart(CoreEnvironment.DEFAULT_MAX_NUM_REQUESTS_IN_RETRY);
RetryStrategy retryStrategy = mock(RetryStrategy.class);
when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.withDuration(Duration.ofMillis(200))));
Request<?> request = mock(Request.class);
RequestContext requestContext = mock(RequestContext.class);
when(request.completed()).thenReturn(false);
when(request.context()).thenReturn(requestContext);
when(request.retryStrategy()).thenReturn(retryStrategy);
when(request.absoluteTimeout()).thenReturn(System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(2500));
Core core = mock(Core.class);
CoreEnvironment env = mock(CoreEnvironment.class);
SimpleEventBus eventBus = new SimpleEventBus(true);
when(env.timer()).thenReturn(timer);
when(env.eventBus()).thenReturn(eventBus);
CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
long start = System.nanoTime();
RetryOrchestrator.maybeRetry(ctx, request, RetryReason.UNKNOWN);
verify(requestContext, times(1)).incrementRetryAttempts(Duration.ofMillis(200), RetryReason.UNKNOWN);
verify(request, never()).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN));
waitUntilCondition(() -> !Mockito.mockingDetails(core).getInvocations().isEmpty());
long end = System.nanoTime();
verify(core, times(1)).send(request, false);
verify(core, never()).send(request);
assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 200);
timer.stop();
assertEquals(1, eventBus.publishedEvents().size());
RequestRetryScheduledEvent retryEvent = (RequestRetryScheduledEvent) eventBus.publishedEvents().get(0);
assertEquals(Event.Severity.DEBUG, retryEvent.severity());
assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
assertEquals(requestContext, retryEvent.context());
assertEquals(RetryReason.UNKNOWN, retryEvent.retryReason());
}
use of com.couchbase.client.core.Core in project couchbase-jvm-clients by couchbase.
the class RetryOrchestratorTest method capsRetryDelay.
@Test
@SuppressWarnings({ "unchecked" })
void capsRetryDelay() {
Timer timer = Timer.createAndStart(CoreEnvironment.DEFAULT_MAX_NUM_REQUESTS_IN_RETRY);
RetryStrategy retryStrategy = mock(RetryStrategy.class);
when(retryStrategy.shouldRetry(any(Request.class), any(RetryReason.class))).thenReturn(CompletableFuture.completedFuture(RetryAction.withDuration(Duration.ofMillis(200))));
Request<?> request = mock(Request.class);
RequestContext requestContext = mock(RequestContext.class);
when(request.completed()).thenReturn(false);
when(request.context()).thenReturn(requestContext);
when(request.retryStrategy()).thenReturn(retryStrategy);
when(request.absoluteTimeout()).thenAnswer(invocationOnMock -> System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(10));
Core core = mock(Core.class);
CoreEnvironment env = mock(CoreEnvironment.class);
SimpleEventBus eventBus = new SimpleEventBus(true);
when(env.timer()).thenReturn(timer);
when(env.eventBus()).thenReturn(eventBus);
CoreContext ctx = new CoreContext(core, 1, env, mock(Authenticator.class));
long start = System.nanoTime();
RetryOrchestrator.maybeRetry(ctx, request, RetryReason.UNKNOWN);
verify(requestContext, never()).incrementRetryAttempts((Duration.ofMillis(200)), RetryReason.UNKNOWN);
verify(request, never()).cancel(CancellationReason.noMoreRetries(RetryReason.UNKNOWN));
waitUntilCondition(() -> !Mockito.mockingDetails(core).getInvocations().isEmpty());
long end = System.nanoTime();
verify(core, times(1)).send(request, false);
verify(core, never()).send(request);
assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) < 200);
timer.stop();
assertEquals(1, eventBus.publishedEvents().size());
RequestRetryScheduledEvent retryEvent = (RequestRetryScheduledEvent) eventBus.publishedEvents().get(0);
assertEquals(Event.Severity.DEBUG, retryEvent.severity());
assertEquals(Event.Category.REQUEST.path(), retryEvent.category());
assertEquals(requestContext, retryEvent.context());
assertEquals(RetryReason.UNKNOWN, retryEvent.retryReason());
}
Aggregations