use of com.mongodb.MongoConnectionPoolClearedException in project mongo-java-driver by mongodb.
the class DefaultConnectionPoolTest method useConcurrently.
private static void useConcurrently(final DefaultConnectionPool pool, final int concurrentUsersCount, final boolean checkoutSync, final boolean checkoutAsync, final float invalidateAndReadyProb, final float invalidateProb, final float readyProb, final ExecutorService executor, final long durationNanos) throws ExecutionException, InterruptedException, TimeoutException {
assertTrue(invalidateAndReadyProb >= 0 && invalidateAndReadyProb <= 1);
Runnable spontaneouslyInvalidateReady = () -> {
if (ThreadLocalRandom.current().nextFloat() < invalidateAndReadyProb) {
pool.invalidate(null);
pool.ready();
}
if (ThreadLocalRandom.current().nextFloat() < invalidateProb) {
pool.invalidate(null);
}
if (ThreadLocalRandom.current().nextFloat() < readyProb) {
pool.ready();
}
};
Collection<Future<?>> tasks = new ArrayList<>();
Timeout duration = Timeout.startNow(durationNanos);
for (int i = 0; i < concurrentUsersCount; i++) {
if ((checkoutSync && checkoutAsync) ? i % 2 == 0 : checkoutSync) {
// check out synchronously and check in
tasks.add(executor.submit(() -> {
while (!(duration.expired() || Thread.currentThread().isInterrupted())) {
spontaneouslyInvalidateReady.run();
InternalConnection conn = null;
try {
conn = pool.get(TEST_WAIT_TIMEOUT_MILLIS, MILLISECONDS);
} catch (MongoConnectionPoolClearedException e) {
// expected because we spontaneously invalidate `pool`
} finally {
if (conn != null) {
conn.close();
}
}
}
}));
} else if (checkoutAsync) {
// check out asynchronously and check in
tasks.add(executor.submit(() -> {
while (!(duration.expired() || Thread.currentThread().isInterrupted())) {
spontaneouslyInvalidateReady.run();
CompletableFuture<InternalConnection> futureCheckOutCheckIn = new CompletableFuture<>();
pool.getAsync((conn, t) -> {
if (t != null) {
if (t instanceof MongoConnectionPoolClearedException) {
// expected because we spontaneously invalidate `pool`
futureCheckOutCheckIn.complete(null);
} else {
futureCheckOutCheckIn.completeExceptionally(t);
}
} else {
conn.close();
futureCheckOutCheckIn.complete(null);
}
});
try {
futureCheckOutCheckIn.get(TEST_WAIT_TIMEOUT_MILLIS, MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
} catch (ExecutionException | TimeoutException e) {
throw new AssertionError(e);
}
}
}));
}
}
for (Future<?> task : tasks) {
task.get(durationNanos + MILLISECONDS.toNanos(TEST_WAIT_TIMEOUT_MILLIS), NANOSECONDS);
}
}
use of com.mongodb.MongoConnectionPoolClearedException in project mongo-java-driver by mongodb.
the class CommandOperationHelper method shouldAttemptToRetryWrite.
static boolean shouldAttemptToRetryWrite(final RetryState retryState, final Throwable attemptFailure) {
Throwable failure = attemptFailure instanceof ResourceSupplierInternalException ? attemptFailure.getCause() : attemptFailure;
boolean decision = false;
MongoException exceptionRetryableRegardlessOfCommand = null;
if (failure instanceof MongoConnectionPoolClearedException) {
decision = true;
exceptionRetryableRegardlessOfCommand = (MongoException) failure;
}
if (retryState.attachment(AttachmentKeys.retryableCommandFlag()).orElse(false)) {
if (exceptionRetryableRegardlessOfCommand != null) {
/* We are going to retry even if `retryableCommand` is false,
* but we add the retryable label only if `retryableCommand` is true. */
exceptionRetryableRegardlessOfCommand.addLabel(RETRYABLE_WRITE_ERROR_LABEL);
} else if (decideRetryableAndAddRetryableWriteErrorLabel(failure, retryState.attachment(AttachmentKeys.maxWireVersion()).orElse(null))) {
decision = true;
} else {
logUnableToRetry(retryState.attachment(AttachmentKeys.commandDescriptionSupplier()).orElse(null), failure);
}
}
return decision;
}
Aggregations