Search in sources :

Example 1 with MonoSink

use of reactor.core.publisher.MonoSink in project mongo-java-driver by mongodb.

the class ClientSessionPublisherImpl method commitTransaction.

@Override
public Publisher<Void> commitTransaction() {
    if (transactionState == TransactionState.ABORTED) {
        throw new IllegalStateException("Cannot call commitTransaction after calling abortTransaction");
    }
    if (transactionState == TransactionState.NONE) {
        throw new IllegalStateException("There is no transaction started");
    }
    if (!messageSentInCurrentTransaction) {
        cleanupTransaction(TransactionState.COMMITTED);
        return Mono.create(MonoSink::success);
    } else {
        ReadConcern readConcern = transactionOptions.getReadConcern();
        if (readConcern == null) {
            throw new MongoInternalException("Invariant violated. Transaction options read concern can not be null");
        }
        boolean alreadyCommitted = commitInProgress || transactionState == TransactionState.COMMITTED;
        commitInProgress = true;
        return executor.execute(new CommitTransactionOperation(transactionOptions.getWriteConcern(), alreadyCommitted).recoveryToken(getRecoveryToken()).maxCommitTime(transactionOptions.getMaxCommitTime(MILLISECONDS), MILLISECONDS), readConcern, this).doOnTerminate(() -> {
            commitInProgress = false;
            transactionState = TransactionState.COMMITTED;
        }).doOnError(MongoException.class, this::clearTransactionContextOnError);
    }
}
Also used : MonoSink(reactor.core.publisher.MonoSink) CommitTransactionOperation(com.mongodb.internal.operation.CommitTransactionOperation) ReadConcern(com.mongodb.ReadConcern) MongoInternalException(com.mongodb.MongoInternalException)

Example 2 with MonoSink

use of reactor.core.publisher.MonoSink in project mongo-java-driver by mongodb.

the class Crypt method fetchKeys.

private void fetchKeys(final MongoCryptContext cryptContext, @Nullable final String databaseName, final MonoSink<RawBsonDocument> sink) {
    keyRetriever.find(cryptContext.getMongoOperation()).doOnSuccess(results -> {
        for (BsonDocument result : results) {
            cryptContext.addMongoOperationResult(result);
        }
        cryptContext.completeMongoOperation();
        executeStateMachineWithSink(cryptContext, databaseName, sink);
    }).doOnError(t -> sink.error(MongoException.fromThrowableNonNull(t))).subscribe();
}
Also used : MongoInternalException(com.mongodb.MongoInternalException) MongoClientException(com.mongodb.MongoClientException) EncryptOptions(com.mongodb.client.model.vault.EncryptOptions) MonoSink(reactor.core.publisher.MonoSink) MongoCryptException(com.mongodb.crypt.capi.MongoCryptException) Supplier(java.util.function.Supplier) DataKeyOptions(com.mongodb.client.model.vault.DataKeyOptions) MongoDataKeyOptions(com.mongodb.crypt.capi.MongoDataKeyOptions) MongoClient(com.mongodb.reactivestreams.client.MongoClient) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue) MongoExplicitEncryptOptions(com.mongodb.crypt.capi.MongoExplicitEncryptOptions) MongoCryptContext(com.mongodb.crypt.capi.MongoCryptContext) MongoCrypt(com.mongodb.crypt.capi.MongoCrypt) Logger(com.mongodb.diagnostics.logging.Logger) MongoException(com.mongodb.MongoException) RawBsonDocument(org.bson.RawBsonDocument) MongoKeyDecryptor(com.mongodb.crypt.capi.MongoKeyDecryptor) State(com.mongodb.crypt.capi.MongoCryptContext.State) Mono(reactor.core.publisher.Mono) String.format(java.lang.String.format) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) Loggers(com.mongodb.diagnostics.logging.Loggers) Closeable(java.io.Closeable) Nullable(com.mongodb.lang.Nullable) BsonBinary(org.bson.BsonBinary) BsonDocument(org.bson.BsonDocument) RawBsonDocument(org.bson.RawBsonDocument)

Example 3 with MonoSink

use of reactor.core.publisher.MonoSink in project mongo-java-driver by mongodb.

the class ClientSessionPublisherImpl method abortTransaction.

@Override
public Publisher<Void> abortTransaction() {
    if (transactionState == TransactionState.ABORTED) {
        throw new IllegalStateException("Cannot call abortTransaction twice");
    }
    if (transactionState == TransactionState.COMMITTED) {
        throw new IllegalStateException("Cannot call abortTransaction after calling commitTransaction");
    }
    if (transactionState == TransactionState.NONE) {
        throw new IllegalStateException("There is no transaction started");
    }
    if (!messageSentInCurrentTransaction) {
        cleanupTransaction(TransactionState.ABORTED);
        return Mono.create(MonoSink::success);
    } else {
        ReadConcern readConcern = transactionOptions.getReadConcern();
        if (readConcern == null) {
            throw new MongoInternalException("Invariant violated. Transaction options read concern can not be null");
        }
        return executor.execute(new AbortTransactionOperation(transactionOptions.getWriteConcern()).recoveryToken(getRecoveryToken()), readConcern, this).onErrorResume(Throwable.class, (e) -> Mono.empty()).doOnTerminate(() -> {
            clearTransactionContext();
            cleanupTransaction(TransactionState.ABORTED);
        });
    }
}
Also used : MonoSink(reactor.core.publisher.MonoSink) MongoInternalException(com.mongodb.MongoInternalException) MongoClientException(com.mongodb.MongoClientException) MonoSink(reactor.core.publisher.MonoSink) AsyncClientSession(com.mongodb.internal.async.client.AsyncClientSession) MongoClient(com.mongodb.reactivestreams.client.MongoClient) TransactionOptions(com.mongodb.TransactionOptions) TRANSIENT_TRANSACTION_ERROR_LABEL(com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL) BaseClientSessionImpl(com.mongodb.internal.session.BaseClientSessionImpl) Assertions.isTrue(com.mongodb.assertions.Assertions.isTrue) ServerSessionPool(com.mongodb.internal.session.ServerSessionPool) ReadConcern(com.mongodb.ReadConcern) CommitTransactionOperation(com.mongodb.internal.operation.CommitTransactionOperation) MongoException(com.mongodb.MongoException) AsyncWriteOperation(com.mongodb.internal.operation.AsyncWriteOperation) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ClientSession(com.mongodb.reactivestreams.client.ClientSession) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) ClientSessionOptions(com.mongodb.ClientSessionOptions) Assertions.assertTrue(com.mongodb.assertions.Assertions.assertTrue) AsyncReadOperation(com.mongodb.internal.operation.AsyncReadOperation) UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL(com.mongodb.MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL) AbortTransactionOperation(com.mongodb.internal.operation.AbortTransactionOperation) WriteConcern(com.mongodb.WriteConcern) ReadConcern(com.mongodb.ReadConcern) AbortTransactionOperation(com.mongodb.internal.operation.AbortTransactionOperation) MongoInternalException(com.mongodb.MongoInternalException)

Aggregations

MongoInternalException (com.mongodb.MongoInternalException)3 MonoSink (reactor.core.publisher.MonoSink)3 MongoClientException (com.mongodb.MongoClientException)2 MongoException (com.mongodb.MongoException)2 ReadConcern (com.mongodb.ReadConcern)2 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)2 CommitTransactionOperation (com.mongodb.internal.operation.CommitTransactionOperation)2 MongoClient (com.mongodb.reactivestreams.client.MongoClient)2 Mono (reactor.core.publisher.Mono)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 TRANSIENT_TRANSACTION_ERROR_LABEL (com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL)1 UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL (com.mongodb.MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)1 TransactionOptions (com.mongodb.TransactionOptions)1 WriteConcern (com.mongodb.WriteConcern)1 Assertions.assertTrue (com.mongodb.assertions.Assertions.assertTrue)1 Assertions.isTrue (com.mongodb.assertions.Assertions.isTrue)1 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)1 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)1 MongoCrypt (com.mongodb.crypt.capi.MongoCrypt)1 MongoCryptContext (com.mongodb.crypt.capi.MongoCryptContext)1