Search in sources :

Example 1 with AsyncReadOperation

use of com.mongodb.internal.operation.AsyncReadOperation in project mongo-java-driver by mongodb.

the class OperationExecutorImpl method execute.

@Override
public <T> Mono<T> execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final ReadConcern readConcern, @Nullable final ClientSession session) {
    notNull("operation", operation);
    notNull("readPreference", readPreference);
    notNull("readConcern", readConcern);
    if (session != null) {
        session.notifyOperationInitiated(operation);
    }
    return Mono.from(subscriber -> clientSessionHelper.withClientSession(session, OperationExecutorImpl.this).map(clientSession -> getReadWriteBinding(getContext(subscriber), readPreference, readConcern, clientSession, session == null && clientSession != null)).switchIfEmpty(Mono.fromCallable(() -> getReadWriteBinding(getContext(subscriber), readPreference, readConcern, session, false))).flatMap(binding -> {
        if (session != null && session.hasActiveTransaction() && !binding.getReadPreference().equals(primary())) {
            binding.release();
            return Mono.error(new MongoClientException("Read preference in a transaction must be primary"));
        } else {
            return Mono.<T>create(sink -> operation.executeAsync(binding, (result, t) -> {
                try {
                    binding.release();
                } finally {
                    sinkToCallback(sink).onResult(result, t);
                }
            })).doOnError((t) -> {
                labelException(session, t);
                unpinServerAddressOnTransientTransactionError(session, t);
            });
        }
    }).subscribe(subscriber));
}
Also used : MongoInternalException(com.mongodb.MongoInternalException) ReadPreference(com.mongodb.ReadPreference) AsyncReadWriteBinding(com.mongodb.internal.binding.AsyncReadWriteBinding) MongoClientException(com.mongodb.MongoClientException) MongoSocketException(com.mongodb.MongoSocketException) MongoQueryException(com.mongodb.MongoQueryException) CryptBinding(com.mongodb.reactivestreams.client.internal.crypt.CryptBinding) TRANSIENT_TRANSACTION_ERROR_LABEL(com.mongodb.MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL) IgnorableRequestContext(com.mongodb.internal.IgnorableRequestContext) ReadConcern(com.mongodb.ReadConcern) Subscriber(org.reactivestreams.Subscriber) MongoException(com.mongodb.MongoException) AsyncWriteOperation(com.mongodb.internal.operation.AsyncWriteOperation) Mono(reactor.core.publisher.Mono) RequestContext(com.mongodb.RequestContext) ClientSessionBinding(com.mongodb.internal.async.client.ClientSessionBinding) ClientSession(com.mongodb.reactivestreams.client.ClientSession) ReactiveContextProvider(com.mongodb.reactivestreams.client.ReactiveContextProvider) MongoTimeoutException(com.mongodb.MongoTimeoutException) Crypt(com.mongodb.reactivestreams.client.internal.crypt.Crypt) Assertions.notNull(com.mongodb.assertions.Assertions.notNull) ContextProvider(com.mongodb.ContextProvider) AsyncReadOperation(com.mongodb.internal.operation.AsyncReadOperation) AsyncClusterBinding(com.mongodb.internal.binding.AsyncClusterBinding) UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL(com.mongodb.MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL) ReadPreference.primary(com.mongodb.ReadPreference.primary) MongoOperationPublisher.sinkToCallback(com.mongodb.reactivestreams.client.internal.MongoOperationPublisher.sinkToCallback) Nullable(com.mongodb.lang.Nullable) AsyncClusterAwareReadWriteBinding(com.mongodb.internal.binding.AsyncClusterAwareReadWriteBinding) MongoClientException(com.mongodb.MongoClientException)

Aggregations

ContextProvider (com.mongodb.ContextProvider)1 MongoClientException (com.mongodb.MongoClientException)1 MongoException (com.mongodb.MongoException)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 MongoInternalException (com.mongodb.MongoInternalException)1 MongoQueryException (com.mongodb.MongoQueryException)1 MongoSocketException (com.mongodb.MongoSocketException)1 MongoTimeoutException (com.mongodb.MongoTimeoutException)1 ReadConcern (com.mongodb.ReadConcern)1 ReadPreference (com.mongodb.ReadPreference)1 ReadPreference.primary (com.mongodb.ReadPreference.primary)1 RequestContext (com.mongodb.RequestContext)1 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)1 IgnorableRequestContext (com.mongodb.internal.IgnorableRequestContext)1 ClientSessionBinding (com.mongodb.internal.async.client.ClientSessionBinding)1 AsyncClusterAwareReadWriteBinding (com.mongodb.internal.binding.AsyncClusterAwareReadWriteBinding)1 AsyncClusterBinding (com.mongodb.internal.binding.AsyncClusterBinding)1 AsyncReadWriteBinding (com.mongodb.internal.binding.AsyncReadWriteBinding)1 AsyncReadOperation (com.mongodb.internal.operation.AsyncReadOperation)1