Search in sources :

Example 1 with WriteBinding

use of com.mongodb.internal.binding.WriteBinding in project mongo-java-driver by mongodb.

the class MixedBulkWriteOperation method executeAsync.

public void executeAsync(final AsyncWriteBinding binding, final SingleResultCallback<BulkWriteResult> callback) {
    // see the comment in `execute(WriteBinding)` explaining the manual tracking of attempts
    RetryState retryState = new RetryState();
    BulkWriteTracker.attachNew(retryState, retryWrites);
    binding.retain();
    AsyncCallbackSupplier<BulkWriteResult> retryingBulkWrite = this.<BulkWriteResult>decorateWriteWithRetries(retryState, funcCallback -> {
        logRetryExecute(retryState);
        withAsyncSourceAndConnection(binding::getWriteConnectionSource, true, funcCallback, (source, connection, releasingCallback) -> {
            ConnectionDescription connectionDescription = connection.getDescription();
            int maxWireVersion = connectionDescription.getMaxWireVersion();
            // attach `maxWireVersion` ASAP because it is used to check whether we can retry
            retryState.attach(AttachmentKeys.maxWireVersion(), maxWireVersion, true);
            BulkWriteTracker bulkWriteTracker = retryState.attachment(AttachmentKeys.bulkWriteTracker()).orElseThrow(Assertions::fail);
            SessionContext sessionContext = binding.getSessionContext();
            WriteConcern writeConcern = getAppliedWriteConcern(sessionContext);
            if (!retryState.isFirstAttempt() && !isRetryableWrite(retryWrites, writeConcern, source.getServerDescription(), connectionDescription, sessionContext)) {
                Throwable prospectiveFailedResult = retryState.exception().orElse(null);
                if (retryState.breakAndCompleteIfRetryAnd(() -> !(prospectiveFailedResult instanceof MongoWriteConcernWithResponseException), releasingCallback)) {
                    return;
                }
                bulkWriteTracker.batch().ifPresent(bulkWriteBatch -> {
                    assertTrue(prospectiveFailedResult instanceof MongoWriteConcernWithResponseException);
                    bulkWriteBatch.addResult((BsonDocument) ((MongoWriteConcernWithResponseException) prospectiveFailedResult).getResponse());
                    BulkWriteTracker.attachNext(retryState, bulkWriteBatch);
                });
            }
            if (validateWriteRequestsAndCompleteIfInvalid(connectionDescription, bypassDocumentValidation, writeRequests, writeConcern, releasingCallback)) {
                return;
            }
            if (writeConcern.isAcknowledged() || serverIsAtLeastVersionThreeDotSix(connectionDescription)) {
                try {
                    if (!bulkWriteTracker.batch().isPresent()) {
                        BulkWriteTracker.attachNew(retryState, BulkWriteBatch.createBulkWriteBatch(namespace, source.getServerDescription(), connectionDescription, ordered, writeConcern, bypassDocumentValidation, retryWrites, writeRequests, sessionContext));
                    }
                } catch (Throwable t) {
                    releasingCallback.onResult(null, t);
                    return;
                }
                logRetryExecute(retryState);
                executeBulkWriteBatchAsync(retryState, binding, connection, maxWireVersion, releasingCallback);
            } else {
                retryState.markAsLastAttempt();
                executeLegacyBatchesAsync(binding, connection, releasingCallback);
            }
        });
    }).whenComplete(binding::release);
    retryingBulkWrite.get(exceptionTransformingCallback(errorHandlingCallback(callback, LOGGER)));
}
Also used : LoopState(com.mongodb.internal.async.function.LoopState) WriteRequest(com.mongodb.internal.bulk.WriteRequest) CommandOperationHelper.addRetryableWriteErrorLabel(com.mongodb.internal.operation.CommandOperationHelper.addRetryableWriteErrorLabel) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) MongoWriteConcernWithResponseException(com.mongodb.internal.connection.MongoWriteConcernWithResponseException) ProtocolHelper(com.mongodb.internal.connection.ProtocolHelper) ConnectionDescription(com.mongodb.connection.ConnectionDescription) BsonArray(org.bson.BsonArray) OperationHelper.validateWriteRequests(com.mongodb.internal.operation.OperationHelper.validateWriteRequests) OperationHelper.withSourceAndConnection(com.mongodb.internal.operation.OperationHelper.withSourceAndConnection) MongoException(com.mongodb.MongoException) AsyncCallbackSupplier(com.mongodb.internal.async.function.AsyncCallbackSupplier) Set(java.util.Set) Collectors(java.util.stream.Collectors) AsyncCallbackRunnable(com.mongodb.internal.async.function.AsyncCallbackRunnable) ServerVersionHelper.serverIsAtLeastVersionThreeDotSix(com.mongodb.internal.operation.ServerVersionHelper.serverIsAtLeastVersionThreeDotSix) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) REPLACE(com.mongodb.internal.bulk.WriteRequest.Type.REPLACE) RetryState(com.mongodb.internal.async.function.RetryState) List(java.util.List) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) Optional(java.util.Optional) RetryingSyncSupplier(com.mongodb.internal.async.function.RetryingSyncSupplier) InsertRequest(com.mongodb.internal.bulk.InsertRequest) RetryingAsyncCallbackSupplier(com.mongodb.internal.async.function.RetryingAsyncCallbackSupplier) CommandOperationHelper.logRetryExecute(com.mongodb.internal.operation.CommandOperationHelper.logRetryExecute) CommandOperationHelper.exceptionTransformingCallback(com.mongodb.internal.operation.CommandOperationHelper.exceptionTransformingCallback) ErrorHandlingResultCallback.errorHandlingCallback(com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback) BsonString(org.bson.BsonString) Supplier(java.util.function.Supplier) BsonDocument(org.bson.BsonDocument) LOGGER(com.mongodb.internal.operation.OperationHelper.LOGGER) Connection(com.mongodb.internal.connection.Connection) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) WriteBinding(com.mongodb.internal.binding.WriteBinding) Assertions(com.mongodb.assertions.Assertions) WriteConcernResult(com.mongodb.WriteConcernResult) AsyncCallbackLoop(com.mongodb.internal.async.function.AsyncCallbackLoop) AsyncWriteBinding(com.mongodb.internal.binding.AsyncWriteBinding) DeleteRequest(com.mongodb.internal.bulk.DeleteRequest) SessionContext(com.mongodb.internal.session.SessionContext) FieldNameValidator(org.bson.FieldNameValidator) OperationHelper.validateWriteRequestsAndCompleteIfInvalid(com.mongodb.internal.operation.OperationHelper.validateWriteRequestsAndCompleteIfInvalid) INSERT(com.mongodb.internal.bulk.WriteRequest.Type.INSERT) MongoNamespace(com.mongodb.MongoNamespace) UpdateRequest(com.mongodb.internal.bulk.UpdateRequest) OperationHelper.isRetryableWrite(com.mongodb.internal.operation.OperationHelper.isRetryableWrite) CommandOperationHelper.transformWriteException(com.mongodb.internal.operation.CommandOperationHelper.transformWriteException) Assertions.isTrueArgument(com.mongodb.assertions.Assertions.isTrueArgument) Assertions.assertTrue(com.mongodb.assertions.Assertions.assertTrue) OperationHelper.withAsyncSourceAndConnection(com.mongodb.internal.operation.OperationHelper.withAsyncSourceAndConnection) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) Nullable(com.mongodb.lang.Nullable) WriteConcern(com.mongodb.WriteConcern) UPDATE(com.mongodb.internal.bulk.WriteRequest.Type.UPDATE) AsyncConnection(com.mongodb.internal.connection.AsyncConnection) AttachmentKeys(com.mongodb.internal.operation.retry.AttachmentKeys) ConnectionDescription(com.mongodb.connection.ConnectionDescription) MongoWriteConcernWithResponseException(com.mongodb.internal.connection.MongoWriteConcernWithResponseException) BsonDocument(org.bson.BsonDocument) WriteConcern(com.mongodb.WriteConcern) SessionContext(com.mongodb.internal.session.SessionContext) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) RetryState(com.mongodb.internal.async.function.RetryState)

Example 2 with WriteBinding

use of com.mongodb.internal.binding.WriteBinding in project mongo-java-driver by mongodb.

the class CommandOperationHelper method executeRetryableWrite.

static <T, R> R executeRetryableWrite(final WriteBinding binding, final String database, final ReadPreference readPreference, final FieldNameValidator fieldNameValidator, final Decoder<T> commandResultDecoder, final CommandCreator commandCreator, final CommandWriteTransformer<T, R> transformer, final Function<BsonDocument, BsonDocument> retryCommandModifier) {
    RetryState retryState = initialRetryState(true);
    Supplier<R> retryingWrite = decorateWriteWithRetries(retryState, () -> {
        logRetryExecute(retryState);
        boolean firstAttempt = retryState.isFirstAttempt();
        if (!firstAttempt && binding.getSessionContext().hasActiveTransaction()) {
            binding.getSessionContext().clearTransactionContext();
        }
        return withSourceAndConnection(binding::getWriteConnectionSource, true, (source, connection) -> {
            int maxWireVersion = connection.getDescription().getMaxWireVersion();
            try {
                retryState.breakAndThrowIfRetryAnd(() -> !canRetryWrite(source.getServerDescription(), connection.getDescription(), binding.getSessionContext()));
                BsonDocument command = retryState.attachment(AttachmentKeys.command()).map(previousAttemptCommand -> {
                    assertFalse(firstAttempt);
                    return retryCommandModifier.apply(previousAttemptCommand);
                }).orElseGet(() -> commandCreator.create(source.getServerDescription(), connection.getDescription()));
                // attach `maxWireVersion`, `retryableCommandFlag` ASAP because they are used to check whether we should retry
                retryState.attach(AttachmentKeys.maxWireVersion(), maxWireVersion, true).attach(AttachmentKeys.retryableCommandFlag(), isRetryWritesEnabled(command), true).attach(AttachmentKeys.commandDescriptionSupplier(), command::getFirstKey, true).attach(AttachmentKeys.command(), command, false);
                logRetryExecute(retryState);
                return transformer.apply(connection.command(database, command, fieldNameValidator, readPreference, commandResultDecoder, binding.getSessionContext(), binding.getServerApi(), binding.getRequestContext()), connection);
            } catch (MongoException e) {
                if (!firstAttempt) {
                    addRetryableWriteErrorLabel(e, maxWireVersion);
                }
                throw e;
            }
        });
    });
    try {
        return retryingWrite.get();
    } catch (MongoException e) {
        throw transformWriteException(e);
    }
}
Also used : AsyncReadBinding(com.mongodb.internal.binding.AsyncReadBinding) ServerDescription(com.mongodb.connection.ServerDescription) MongoClientException(com.mongodb.MongoClientException) Function(com.mongodb.Function) MongoSocketException(com.mongodb.MongoSocketException) ConnectionDescription(com.mongodb.connection.ConnectionDescription) Arrays.asList(java.util.Arrays.asList) MongoNotPrimaryException(com.mongodb.MongoNotPrimaryException) OperationHelper.canRetryRead(com.mongodb.internal.operation.OperationHelper.canRetryRead) MongoNodeIsRecoveringException(com.mongodb.MongoNodeIsRecoveringException) OperationHelper.withSourceAndConnection(com.mongodb.internal.operation.OperationHelper.withSourceAndConnection) MongoException(com.mongodb.MongoException) ConnectionSource(com.mongodb.internal.binding.ConnectionSource) AsyncCallbackSupplier(com.mongodb.internal.async.function.AsyncCallbackSupplier) ReadBinding(com.mongodb.internal.binding.ReadBinding) Assertions.assertFalse(com.mongodb.assertions.Assertions.assertFalse) MongoServerException(com.mongodb.MongoServerException) String.format(java.lang.String.format) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Decoder(org.bson.codecs.Decoder) PRIVATE(com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE) RetryState(com.mongodb.internal.async.function.RetryState) List(java.util.List) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) ResourceSupplierInternalException(com.mongodb.internal.operation.OperationHelper.ResourceSupplierInternalException) ReadPreference.primary(com.mongodb.ReadPreference.primary) RetryingSyncSupplier(com.mongodb.internal.async.function.RetryingSyncSupplier) ReadPreference(com.mongodb.ReadPreference) RetryingAsyncCallbackSupplier(com.mongodb.internal.async.function.RetryingAsyncCallbackSupplier) MongoCommandException(com.mongodb.MongoCommandException) ErrorHandlingResultCallback.errorHandlingCallback(com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback) Supplier(java.util.function.Supplier) BsonDocument(org.bson.BsonDocument) LOGGER(com.mongodb.internal.operation.OperationHelper.LOGGER) Connection(com.mongodb.internal.connection.Connection) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) WriteBinding(com.mongodb.internal.binding.WriteBinding) Assertions(com.mongodb.assertions.Assertions) MongoConnectionPoolClearedException(com.mongodb.MongoConnectionPoolClearedException) VisibleForTesting(com.mongodb.internal.VisibleForTesting) AsyncWriteBinding(com.mongodb.internal.binding.AsyncWriteBinding) AsyncConnectionSource(com.mongodb.internal.binding.AsyncConnectionSource) FieldNameValidator(org.bson.FieldNameValidator) OperationHelper.canRetryWrite(com.mongodb.internal.operation.OperationHelper.canRetryWrite) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) OperationHelper.withAsyncSourceAndConnection(com.mongodb.internal.operation.OperationHelper.withAsyncSourceAndConnection) Nullable(com.mongodb.lang.Nullable) AsyncConnection(com.mongodb.internal.connection.AsyncConnection) AttachmentKeys(com.mongodb.internal.operation.retry.AttachmentKeys) LOGGER(com.mongodb.internal.operation.OperationHelper.LOGGER) MongoException(com.mongodb.MongoException) BsonDocument(org.bson.BsonDocument) RetryState(com.mongodb.internal.async.function.RetryState)

Aggregations

MongoException (com.mongodb.MongoException)2 Assertions (com.mongodb.assertions.Assertions)2 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)2 ConnectionDescription (com.mongodb.connection.ConnectionDescription)2 ErrorHandlingResultCallback.errorHandlingCallback (com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback)2 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)2 AsyncCallbackSupplier (com.mongodb.internal.async.function.AsyncCallbackSupplier)2 RetryState (com.mongodb.internal.async.function.RetryState)2 RetryingAsyncCallbackSupplier (com.mongodb.internal.async.function.RetryingAsyncCallbackSupplier)2 RetryingSyncSupplier (com.mongodb.internal.async.function.RetryingSyncSupplier)2 AsyncWriteBinding (com.mongodb.internal.binding.AsyncWriteBinding)2 WriteBinding (com.mongodb.internal.binding.WriteBinding)2 AsyncConnection (com.mongodb.internal.connection.AsyncConnection)2 Connection (com.mongodb.internal.connection.Connection)2 LOGGER (com.mongodb.internal.operation.OperationHelper.LOGGER)2 Function (com.mongodb.Function)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 MongoClientException (com.mongodb.MongoClientException)1 MongoCommandException (com.mongodb.MongoCommandException)1 MongoConnectionPoolClearedException (com.mongodb.MongoConnectionPoolClearedException)1