Beispiel #1
0
 public static bool ShouldConnectionAcquireBeRetried(RetryableWriteContext context, ServerDescription serverDescription)
 {
     return(context.RetryRequested &&
            AreRetryableWritesSupported(serverDescription) &&
            context.Binding.Session.Id != null &&
            !context.Binding.Session.IsInTransaction);
 }
Beispiel #2
0
        public Task <BulkWriteOperationResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            EnsureCollationIsSupportedIfAnyRequestHasCollation(context, _requests);
            EnsureHintIsSupportedIfAnyRequestHasHint(context);

            return(ExecuteBatchesAsync(context, cancellationToken));
        }
 // public methods
 /// <inheritdoc />
 public virtual BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (var context = RetryableWriteContext.Create(binding, _retryRequested, cancellationToken))
     {
         return(Execute(context, cancellationToken));
     }
 }
 /// <inheritdoc />
 public virtual async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (var context = await RetryableWriteContext.CreateAsync(binding, _retryRequested, cancellationToken).ConfigureAwait(false))
     {
         return(Execute(context, cancellationToken));
     }
 }
 // public static methods
 public static TResult Execute <TResult>(IRetryableWriteOperation <TResult> operation, IWriteBinding binding, bool retryRequested, CancellationToken cancellationToken)
 {
     using (var context = RetryableWriteContext.Create(binding, retryRequested, cancellationToken))
     {
         return(Execute(operation, context, cancellationToken));
     }
 }
Beispiel #6
0
        /// <summary>
        /// Creates and initializes a retryable write operation context.
        /// </summary>
        /// <param name="binding">The binding.</param>
        /// <param name="retryRequested">if set to <c>true</c> [retry requested].</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A retryable write context.</returns>
        public static async Task <RetryableWriteContext> CreateAsync(IWriteBinding binding, bool retryRequested, CancellationToken cancellationToken)
        {
            var context = new RetryableWriteContext(binding, retryRequested);

            try
            {
                await context.InitializeAsync(cancellationToken).ConfigureAwait(false);

                if (context.Binding.Session.IsInTransaction &&
                    ChannelPinningHelper.PinChannelSourceAndChannelIfRequired(
                        context.ChannelSource,
                        context.Channel,
                        context.Binding.Session,
                        out var pinnedChannelSource,
                        out var pinnedChannel))
                {
                    context.ReplaceChannelSource(pinnedChannelSource);
                    context.ReplaceChannel(pinnedChannel);
                }

                return(context);
            }
            catch
            {
                context.Dispose();
                throw;
            }
        }
        private async Task <BulkWriteBatchResult> ExecuteBatchAsync(RetryableWriteContext context, Batch batch, CancellationToken cancellationToken)
        {
            var operation       = CreateBatchOperation(batch);
            var operationResult = await RetryableWriteOperationExecutor.ExecuteAsync(operation, context, cancellationToken).ConfigureAwait(false);

            return(CreateBatchResult(batch, operationResult));
        }
        private BulkWriteBatchResult ExecuteBatch(RetryableWriteContext context, Batch batch, CancellationToken cancellationToken)
        {
            var operation       = CreateBatchOperation(batch);
            var operationResult = RetryableWriteOperationExecutor.Execute(operation, context, cancellationToken);

            return(CreateBatchResult(batch, operationResult));
        }
Beispiel #9
0
 private static bool DoesContextAllowRetries(RetryableWriteContext context)
 {
     return
         (context.RetryRequested &&
          AreRetryableWritesSupported(context.Channel.ConnectionDescription) &&
          !context.Binding.Session.IsInTransaction);
 }
 public async static Task <TResult> ExecuteAsync <TResult>(IRetryableWriteOperation <TResult> operation, IWriteBinding binding, bool retryRequested, CancellationToken cancellationToken)
 {
     using (var context = await RetryableWriteContext.CreateAsync(binding, retryRequested, cancellationToken).ConfigureAwait(false))
     {
         return(await ExecuteAsync(operation, context, cancellationToken).ConfigureAwait(false));
     }
 }
 /// <inheritdoc/>
 public async Task <WriteConcernResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         var emulator = CreateEmulator();
         return(await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false));
     }
 }
 /// <inheritdoc/>
 public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
     {
         var emulator = CreateEmulator();
         return(emulator.Execute(context, cancellationToken));
     }
 }
        // public methods
        /// <inheritdoc/>
        public WriteConcernResult Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var context = RetryableWriteContext.Create(binding, false, cancellationToken))
            {
                return(Execute(context, cancellationToken));
            }
        }
        /// <inheritdoc/>
        public async Task <WriteConcernResult> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var context = await RetryableWriteContext.CreateAsync(binding, false, cancellationToken).ConfigureAwait(false))
            {
                return(await ExecuteAsync(context, cancellationToken).ConfigureAwait(false));
            }
        }
 public BulkWriteOperationResult Execute(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
         using (var context = RetryableWriteContext.Create(binding, _retryRequested, cancellationToken))
         {
             context.DisableRetriesIfAnyWriteRequestIsNotRetryable(_requests);
             return(Execute(context, cancellationToken));
         }
 }
 public async Task <BulkWriteOperationResult> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
         using (var context = await RetryableWriteContext.CreateAsync(binding, _retryRequested, cancellationToken).ConfigureAwait(false))
         {
             context.DisableRetriesIfAnyWriteRequestIsNotRetryable(_requests);
             return(await ExecuteAsync(context, cancellationToken).ConfigureAwait(false));
         }
 }
Beispiel #17
0
        public async Task <BulkWriteOperationResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            var helper = new BatchHelper(this, context.Channel);

            foreach (var batch in helper.GetBatches())
            {
                batch.Result = await EmulateSingleRequestAsync(context.Channel, batch.Request, batch.OriginalIndex, cancellationToken).ConfigureAwait(false);
            }
            return(helper.GetFinalResultOrThrow());
        }
        private async Task <BulkWriteOperationResult> ExecuteBatchesAsync(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            var helper = new BatchHelper(_requests, _writeConcern, _isOrdered);

            foreach (var batch in helper.GetBatches())
            {
                batch.Result = await ExecuteBatchAsync(context, batch, cancellationToken).ConfigureAwait(false);
            }
            return(helper.CreateFinalResultOrThrow(context.Channel));
        }
        private BulkWriteOperationResult ExecuteBatches(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            var helper = new BatchHelper(_requests, _writeConcern, _isOrdered);

            foreach (var batch in helper.GetBatches())
            {
                batch.Result = ExecuteBatch(context, batch, cancellationToken);
            }
            return(helper.CreateFinalResultOrThrow(context.Channel));
        }
Beispiel #20
0
        // public methods
        public BulkWriteOperationResult Execute(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            var helper = new BatchHelper(this, context.Channel);

            foreach (var batch in helper.GetBatches())
            {
                batch.Result = EmulateSingleRequest(context.Channel, batch.Request, batch.OriginalIndex, cancellationToken);
            }
            return(helper.GetFinalResultOrThrow());
        }
        public static bool ShouldConnectionAcquireBeRetried(RetryableWriteContext context, ServerDescription serverDescription, Exception exception)
        {
            var innerException = exception is MongoAuthenticationException mongoAuthenticationException ? mongoAuthenticationException.InnerException : exception;

            // According the spec error during handshake should be handle according to RetryableReads logic
            return(context.RetryRequested &&
                   AreRetryableWritesSupported(serverDescription) &&
                   context.Binding.Session.Id != null &&
                   !context.Binding.Session.IsInTransaction &&
                   RetryabilityHelper.IsRetryableReadException(innerException));
        }
        private CommandArgs GetCommandArgs(RetryableWriteContext context, int attempt, long?transactionNumber)
        {
            var args = new CommandArgs();

            args.Command                = CreateCommand(context.Binding.Session, context.Channel.ConnectionDescription, attempt, transactionNumber);
            args.CommandPayloads        = CreateCommandPayloads(context.Channel, attempt).ToList();
            args.PostWriteAction        = GetPostWriteAction(args.CommandPayloads);
            args.ResponseHandling       = GetResponseHandling();
            args.MessageEncoderSettings = CreateMessageEncoderSettings(context.Channel);
            return(args);
        }
Beispiel #23
0
 /// <inheritdoc/>
 public WriteConcernResult Execute(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
     {
         var emulator = CreateEmulator();
         return(emulator.Execute(context, cancellationToken));
     }
     else
     {
         return(ExecuteProtocol(context.Channel, cancellationToken));
     }
 }
Beispiel #24
0
 /// <inheritdoc/>
 public async Task <WriteConcernResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
     {
         var emulator = CreateEmulator();
         return(await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false));
     }
     else
     {
         return(await ExecuteProtocolAsync(context.Channel, cancellationToken).ConfigureAwait(false));
     }
 }
Beispiel #25
0
        private void EnsureHintIsSupportedIfAnyRequestHasHint(RetryableWriteContext context)
        {
            var serverVersion = context.Channel.ConnectionDescription.ServerVersion;

            foreach (var request in _requests)
            {
                if (RequestHasHint(request) && !IsHintSupportedForRequestWithHint(request, serverVersion))
                {
                    throw new NotSupportedException($"Server version {serverVersion} does not support hints.");
                }
            }
        }
Beispiel #26
0
        // public methods
        /// <inheritdoc/>
        public IEnumerable <WriteConcernResult> Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var context = RetryableWriteContext.Create(binding, false, cancellationToken))
                {
                    var emulator = CreateEmulator();
                    var result   = emulator.Execute(context, cancellationToken);
                    return(result != null ? new[] { result } : null);
                }
        }
 public Task <BulkWriteOperationResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion))
     {
         return(ExecuteBatchesAsync(context, cancellationToken));
     }
     else
     {
         var emulator = CreateEmulator();
         return(emulator.ExecuteAsync(context, cancellationToken));
     }
 }
Beispiel #28
0
        /// <inheritdoc/>
        public async Task <IEnumerable <WriteConcernResult> > ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (EventContext.BeginOperation())
                using (var context = await RetryableWriteContext.CreateAsync(binding, false, cancellationToken).ConfigureAwait(false))
                {
                    var emulator = CreateEmulator();
                    var result   = await emulator.ExecuteAsync(context, cancellationToken).ConfigureAwait(false);

                    return(result != null ? new[] { result } : null);
                }
        }
 // public methods
 public BulkWriteOperationResult Execute(RetryableWriteContext context, CancellationToken cancellationToken)
 {
     EnsureCollationIsSupportedIfAnyRequestHasCollation(context, _requests);
     if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion))
     {
         return(ExecuteBatches(context, cancellationToken));
     }
     else
     {
         var emulator = CreateEmulator();
         return(emulator.Execute(context, cancellationToken));
     }
 }
Beispiel #30
0
        /// <inheritdoc/>
        public Task <WriteConcernResult> ExecuteAsync(RetryableWriteContext context, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(context, nameof(context));

            if (Feature.WriteCommands.IsSupported(context.Channel.ConnectionDescription.ServerVersion) && _writeConcern.IsAcknowledged)
            {
                var emulator = CreateEmulator();
                return(emulator.ExecuteAsync(context, cancellationToken));
            }
            else
            {
                return(ExecuteProtocolAsync(context.Channel, cancellationToken));
            }
        }