/// <summary>
        /// Create a readwrite binding handle.
        /// </summary>
        /// <param name="cluster">The cluster.</param>
        /// <param name="session">The session.</param>
        /// <returns>An effective read write binging.</returns>
        public static IReadWriteBindingHandle CreateReadWriteBinding(ICluster cluster, ICoreSessionHandle session)
        {
            IReadWriteBinding readWriteBinding;

            if (session.IsInTransaction &&
                IsChannelPinned(session.CurrentTransaction) &&
                session.CurrentTransaction.State != CoreTransactionState.Starting)
            {
                readWriteBinding = new ChannelReadWriteBinding(
                    session.CurrentTransaction.PinnedServer,
                    session.CurrentTransaction.PinnedChannel.Fork(),
                    session);
            }
            else
            {
                if (IsInLoadBalancedMode(cluster.Description) && IsChannelPinned(session.CurrentTransaction))
                {
                    // unpin if the next operation is not under transaction
                    session.CurrentTransaction.UnpinAll();
                }
                readWriteBinding = new WritableServerBinding(cluster, session);
            }

            return(new ReadWriteBindingHandle(readWriteBinding));
        }
Пример #2
0
        // public methods
        /// <inheritdoc/>
        public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var          operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
                        BsonDocument result;
                        try
                        {
                            result = operation.Execute(channelBinding, cancellationToken);
                        }
                        catch (MongoCommandException ex)
                        {
                            if (!ShouldIgnoreException(ex))
                            {
                                throw;
                            }
                            result = ex.Result;
                        }
                        WriteConcernErrorHelper.ThrowIfHasWriteConcernError(channel.ConnectionDescription.ConnectionId, result);
                        return(result);
                    }
        }
        /// <inheritdoc/>
        public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var          operation = CreateOperation(channelBinding.Session);
                        BsonDocument result;
                        try
                        {
                            result = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);
                        }
                        catch (MongoCommandException ex)
                        {
                            if (!ShouldIgnoreException(ex))
                            {
                                throw;
                            }
                            result = ex.Result;
                        }
                        return(result);
                    }
        }
Пример #4
0
 /// <inheritdoc/>
 public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
         using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
             using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session))
             {
                 var operation = CreateWriteOperation(channel.ConnectionDescription, binding.Session);
                 return(await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false));
             }
 }
Пример #5
0
 /// <inheritdoc/>
 public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
         using (var channel = channelSource.GetChannel(cancellationToken))
             using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session))
             {
                 var operation = CreateWriteOperation(channel.ConnectionDescription, binding.Session);
                 return(operation.Execute(channelBinding, cancellationToken));
             }
 }
 // public methods
 /// <inheritdoc/>
 public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
         using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
             using (var channel = channelSource.GetChannel(cancellationToken))
                 using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                 {
                     var operation = CreateOperation(channel);
                     return(operation.Execute(channelBinding, cancellationToken));
                 }
 }
        /// <inheritdoc/>
        public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var operation = CreateOperation(channelBinding.Session);
                        return(await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false));
                    }
        }
Пример #8
0
        // public methods
        /// <inheritdoc/>
        public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                        return(operation.Execute(channelBinding, cancellationToken));
                    }
        }
Пример #9
0
 // public methods
 /// <inheritdoc/>
 public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
 {
     using (EventContext.BeginOperation())
         using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
             using (var channel = channelSource.GetChannel(cancellationToken))
                 using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                 {
                     var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                     var result    = operation.Execute(channelBinding, cancellationToken);
                     WriteConcernErrorHelper.ThrowIfHasWriteConcernError(channel.ConnectionDescription.ConnectionId, result);
                     return(result);
                 }
 }
        public void Execute_unacknowledged_with_an_error_in_the_first_batch_and_ordered_is_true(
            [Values(false, true)]
            bool async)
        {
            var keys = new BsonDocument("x", 1);
            var createIndexRequests = new[] { new CreateIndexRequest(keys)
                                              {
                                                  Unique = true
                                              } };
            var createIndexOperation = new CreateIndexesOperation(_collectionNamespace, createIndexRequests, _messageEncoderSettings);

            ExecuteOperation(createIndexOperation, async);

            var requests = new[]
            {
                new InsertRequest(new BsonDocument {
                    { "_id", 1 }
                }),
                new InsertRequest(new BsonDocument {
                    { "_id", 1 }
                }),                                                  // will fail
                new InsertRequest(new BsonDocument {
                    { "_id", 3 }
                }),
                new InsertRequest(new BsonDocument {
                    { "_id", 1 }
                }),                                                  // will fail
                new InsertRequest(new BsonDocument {
                    { "_id", 5 }
                }),
            };

            var subject = new BulkMixedWriteOperation(_collectionNamespace, requests, _messageEncoderSettings)
            {
                IsOrdered    = true,
                WriteConcern = WriteConcern.Unacknowledged
            };

            using (var readWriteBinding = CoreTestConfiguration.GetReadWriteBinding())
                using (var channelSource = readWriteBinding.GetWriteChannelSource(CancellationToken.None))
                    using (var channel = channelSource.GetChannel(CancellationToken.None))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                        {
                            var result = ExecuteOperation(subject, channelBinding, async);
                            result.ProcessedRequests.Should().HaveCount(5);
                            result.RequestCount.Should().Be(5);

                            var list = ReadAllFromCollection(channelBinding);
                            list.Should().HaveCount(1);
                        }
        }
Пример #11
0
        /// <inheritdoc/>
        public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                    using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                        {
                            var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                            var result    = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);

                            WriteConcernErrorHelper.ThrowIfHasWriteConcernError(channel.ConnectionDescription.ConnectionId, result);
                            return(result);
                        }
        }
        /// <inheritdoc/>
        public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            var mayUseSecondary = new MayUseSecondary(_readPreference);

            using (var channelSource = await binding.GetWriteChannelSourceAsync(mayUseSecondary, cancellationToken).ConfigureAwait(false))
                using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription, mayUseSecondary.EffectiveReadPreference);
                        return(await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false));
                    }
        }
Пример #13
0
        // methods
        public bool Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var operation = new DropUserUsingUserManagementCommandsOperation(
                            _databaseNamespace,
                            _username,
                            _messageEncoderSettings);

                        return(operation.Execute(channelBinding, cancellationToken));
                    }
        }
Пример #14
0
        // public methods
        /// <inheritdoc/>
        public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                    {
                        var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                        var result    = operation.Execute(binding, cancellationToken);
                        WriteConcernErrorHelper.ThrowIfHasWriteConcernError(channel.ConnectionDescription.ConnectionId, result);
                        return(result);
                    }
        }
        // methods
        /// <inheritdoc/>
        public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            var mayUseSecondary = new MayUseSecondary(_readPreference);

            using (var channelSource = binding.GetWriteChannelSource(mayUseSecondary, cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription, mayUseSecondary.EffectiveReadPreference);
                        return(operation.Execute(channelBinding, cancellationToken));
                    }
        }
Пример #16
0
        /// <inheritdoc />
        public virtual async Task <BsonDocument> ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = await binding.GetReadChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        var operation = CreateOperation();
                        var result    = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);

                        WriteConcernErrorHelper.ThrowIfHasWriteConcernError(channel.ConnectionDescription.ConnectionId, result);
                        return(result);
                    }
        }
        /// <inheritdoc/>
        public async Task <TResult> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                    {
                        var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                        using (var rawBsonDocument = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false))
                        {
                            return(ProcessCommandResult(channel.ConnectionDescription.ConnectionId, rawBsonDocument));
                        }
                    }
        }
Пример #18
0
        /// <inheritdoc/>
        public async Task <TResult> ExecuteAttemptAsync(RetryableWriteContext context, int attempt, long?transactionNumber, CancellationToken cancellationToken)
        {
            var binding       = context.Binding;
            var channelSource = context.ChannelSource;
            var channel       = context.Channel;

            using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
            {
                var operation = CreateOperation(channel.ConnectionDescription.ServerVersion, transactionNumber);
                using (var rawBsonDocument = await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false))
                {
                    return(ProcessCommandResult(channel.ConnectionDescription.ConnectionId, rawBsonDocument));
                }
            }
        }
        // public methods
        /// <inheritdoc/>
        public TResult Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(binding, nameof(binding));

            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                    {
                        var operation = CreateOperation(channel.ConnectionDescription.ServerVersion);
                        using (var rawBsonDocument = operation.Execute(channelBinding, cancellationToken))
                        {
                            return(ProcessCommandResult(channel.ConnectionDescription.ConnectionId, rawBsonDocument));
                        }
                    }
        }
        // public methods
        /// <inheritdoc/>
        public BsonDocument Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (EventContext.BeginOperation())
                using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                    using (var channel = channelSource.GetChannel(cancellationToken))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                        {
                            foreach (var createIndexRequest in _requests)
                            {
                                var operation = CreateOperation(channel.ConnectionDescription.ServerVersion, createIndexRequest);
                                operation.Execute(channelBinding, cancellationToken);
                            }

                            return(new BsonDocument("ok", 1));
                        }
        }
        /// <inheritdoc/>
        public async Task <BsonDocument> ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (EventContext.BeginOperation())
                using (var channelSource = await binding.GetWriteChannelSourceAsync(cancellationToken).ConfigureAwait(false))
                    using (var channel = await channelSource.GetChannelAsync(cancellationToken).ConfigureAwait(false))
                        using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                        {
                            foreach (var createIndexRequest in _requests)
                            {
                                var operation = CreateOperation(channel.ConnectionDescription.ServerVersion, createIndexRequest);
                                await operation.ExecuteAsync(channelBinding, cancellationToken).ConfigureAwait(false);
                            }

                            return(new BsonDocument("ok", 1));
                        }
        }
Пример #22
0
        // methods
        public bool Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel))
                    {
                        IWriteOperation <bool> operation;
                        if (channel.ConnectionDescription.ServerVersion >= __serverVersionSupportingUserManagementCommands)
                        {
                            operation = new DropUserUsingUserManagementCommandsOperation(_databaseNamespace, _username, _messageEncoderSettings);
                        }
                        else
                        {
                            operation = new DropUserUsingSystemUsersCollectionOperation(_databaseNamespace, _username, _messageEncoderSettings);
                        }

                        return(operation.Execute(channelBinding, cancellationToken));
                    }
        }
Пример #23
0
        // methods
        public bool Execute(IWriteBinding binding, CancellationToken cancellationToken)
        {
            using (var channelSource = binding.GetWriteChannelSource(cancellationToken))
                using (var channel = channelSource.GetChannel(cancellationToken))
                    using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
                    {
                        IWriteOperation <bool> operation;
                        if (Feature.UserManagementCommands.IsSupported(channel.ConnectionDescription.ServerVersion))
                        {
                            operation = new AddUserUsingUserManagementCommandsOperation(_databaseNamespace, _username, _passwordHash, _readOnly, _messageEncoderSettings);
                        }
                        else
                        {
                            operation = new AddUserUsingSystemUsersCollectionOperation(_databaseNamespace, _username, _passwordHash, _readOnly, _messageEncoderSettings);
                        }

                        return(operation.Execute(channelBinding, cancellationToken));
                    }
        }