Skip to content

CSHARP-3547: CSOT: read-write concern #1752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public BsonDocument CreateCommand(OperationContext operationContext, ICoreSessio
var readConcern = _readConcern != null
? ReadConcernHelper.GetReadConcernForCommand(session, connectionDescription, _readConcern)
: null;
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "aggregate", _collectionNamespace == null ? (BsonValue)1 : _collectionNamespace.CollectionName },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public ClientBulkWriteOperation(
WriteConcern = options?.WriteConcern;
}

protected override BsonDocument CreateCommand(ICoreSessionHandle session, int attempt, long? transactionNumber)
protected override BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session, int attempt, long? transactionNumber)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, WriteConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, WriteConcern);
return new BsonDocument
{
{ "bulkWrite", 1 },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-present MongoDB Inc.
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -211,9 +211,9 @@ public BsonDocument ClusteredIndex
set => _clusteredIndex = value;
}

internal BsonDocument CreateCommand(ICoreSessionHandle session)
internal BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "create", _collectionNamespace.CollectionName },
Expand Down Expand Up @@ -247,7 +247,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
EnsureServerIsValid(channel.ConnectionDescription.MaxWireVersion);
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session);
var operation = CreateOperation(operationContext, channelBinding.Session);
return operation.Execute(operationContext, channelBinding);
}
}
Expand All @@ -264,17 +264,17 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
EnsureServerIsValid(channel.ConnectionDescription.MaxWireVersion);
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session);
var operation = CreateOperation(operationContext, channelBinding.Session);
return await operation.ExecuteAsync(operationContext, channelBinding).ConfigureAwait(false);
}
}
}

private IDisposable BeginOperation() => EventContext.BeginOperation("create");

private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session)
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session)
{
var command = CreateCommand(session);
var command = CreateCommand(operationContext, session);
return new WriteCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
internal BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
{
var maxWireVersion = connectionDescription.MaxWireVersion;
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
if (_commitQuorum != null)
{
Feature.CreateIndexCommitQuorum.ThrowIfNotSupported(maxWireVersion);
Expand Down
14 changes: 7 additions & 7 deletions src/MongoDB.Driver/Core/Operations/CreateViewOperation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2016-present MongoDB Inc.
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,7 +94,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
using (var channel = channelSource.GetChannel(operationContext))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription);
return operation.Execute(operationContext, channelBinding);
}
}
Expand All @@ -107,14 +107,14 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
using (var channel = await channelSource.GetChannelAsync(operationContext).ConfigureAwait(false))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription);
return await operation.ExecuteAsync(operationContext, channelBinding).ConfigureAwait(false);
}
}

public BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
public BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "create", _viewName },
Expand All @@ -125,9 +125,9 @@ public BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescript
};
}

private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session, ConnectionDescription connectionDescription)
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
{
var command = CreateCommand(session, connectionDescription);
var command = CreateCommand(operationContext, session, connectionDescription);
return new WriteCommandOperation<BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/MongoDB.Driver/Core/Operations/DropCollectionOperation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-present MongoDB Inc.
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -104,7 +104,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
using (var channel = channelSource.GetChannel(operationContext))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session);
var operation = CreateOperation(operationContext, channelBinding.Session);
BsonDocument result;
try
{
Expand All @@ -131,7 +131,7 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
using (var channel = await channelSource.GetChannelAsync(operationContext).ConfigureAwait(false))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session);
var operation = CreateOperation(operationContext, channelBinding.Session);
BsonDocument result;
try
{
Expand All @@ -149,9 +149,9 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
}
}

internal BsonDocument CreateCommand(ICoreSessionHandle session)
internal BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "drop", _collectionNamespace.CollectionName },
Expand All @@ -161,9 +161,9 @@ internal BsonDocument CreateCommand(ICoreSessionHandle session)

private IDisposable BeginOperation() => EventContext.BeginOperation("drop");

private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session)
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session)
{
var command = CreateCommand(session);
var command = CreateCommand(operationContext, session);
return new WriteCommandOperation<BsonDocument>(_collectionNamespace.DatabaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
}

Expand Down
14 changes: 7 additions & 7 deletions src/MongoDB.Driver/Core/Operations/DropDatabaseOperation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-present MongoDB Inc.
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,9 +54,9 @@ public WriteConcern WriteConcern
set { _writeConcern = value; }
}

public BsonDocument CreateCommand(ICoreSessionHandle session)
public BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "dropDatabase", 1 },
Expand All @@ -73,7 +73,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
using (var channel = channelSource.GetChannel(operationContext))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session);
var operation = CreateOperation(operationContext, channelBinding.Session);
return operation.Execute(operationContext, channelBinding);
}
}
Expand All @@ -87,16 +87,16 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
using (var channel = await channelSource.GetChannelAsync(operationContext).ConfigureAwait(false))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session);
var operation = CreateOperation(operationContext, channelBinding.Session);
return await operation.ExecuteAsync(operationContext, channelBinding).ConfigureAwait(false);
}
}

private IDisposable BeginOperation() => EventContext.BeginOperation("dropDatabase");

private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session)
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session)
{
var command = CreateCommand(session);
var command = CreateCommand(operationContext, session);
return new WriteCommandOperation<BsonDocument>(_databaseNamespace, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver/Core/Operations/DropIndexOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public TimeSpan? MaxTime

public BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "dropIndexes", _collectionNamespace.CollectionName },
Expand Down
10 changes: 8 additions & 2 deletions src/MongoDB.Driver/Core/Operations/EndTransactionOperation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2018-present MongoDB Inc.
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,10 +78,16 @@ public virtual async Task<BsonDocument> ExecuteAsync(OperationContext operationC

protected virtual BsonDocument CreateCommand(OperationContext operationContext)
{
var writeConcern = _writeConcern;
if (operationContext.IsRootContextTimeoutConfigured())
{
writeConcern = writeConcern.With(wTimeout: null);
}

return new BsonDocument
{
{ CommandName, 1 },
{ "writeConcern", () => _writeConcern.ToBsonDocument(), !_writeConcern.IsServerDefault },
{ "writeConcern", () => _writeConcern.ToBsonDocument(), !writeConcern.IsServerDefault },
{ "recoveryToken", _recoveryToken, _recoveryToken != null }
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public override BsonDocument CreateCommand(OperationContext operationContext, IC
}
}

var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, WriteConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, WriteConcern);
return new BsonDocument
{
{ "findAndModify", CollectionNamespace.CollectionName },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override BsonDocument CreateCommand(OperationContext operationContext, IC
}
}

var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, WriteConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, WriteConcern);
return new BsonDocument
{
{ "findAndModify", CollectionNamespace.CollectionName },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public override BsonDocument CreateCommand(OperationContext operationContext, IC
}
}

var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, WriteConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, WriteConcern);
return new BsonDocument
{
{ "findAndModify", CollectionNamespace.CollectionName },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected internal override BsonDocument CreateCommand(OperationContext operatio
{
command.Add("bypassDocumentValidation", _bypassDocumentValidation.Value);
}
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
if (writeConcern != null)
{
command.Add("writeConcern", writeConcern.ToBsonDocument());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2013-present MongoDB Inc.
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,9 +70,9 @@ public WriteConcern WriteConcern
set { _writeConcern = value; }
}

public BsonDocument CreateCommand(ICoreSessionHandle session, ConnectionDescription connectionDescription)
public BsonDocument CreateCommand(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
{
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(session, _writeConcern);
var writeConcern = WriteConcernHelper.GetEffectiveWriteConcern(operationContext, session, _writeConcern);
return new BsonDocument
{
{ "renameCollection", _collectionNamespace.FullName },
Expand All @@ -91,7 +91,7 @@ public BsonDocument Execute(OperationContext operationContext, IWriteBinding bin
using (var channel = channelSource.GetChannel(operationContext))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription);
return operation.Execute(operationContext, channelBinding);
}
}
Expand All @@ -105,16 +105,16 @@ public async Task<BsonDocument> ExecuteAsync(OperationContext operationContext,
using (var channel = await channelSource.GetChannelAsync(operationContext).ConfigureAwait(false))
using (var channelBinding = new ChannelReadWriteBinding(channelSource.Server, channel, binding.Session.Fork()))
{
var operation = CreateOperation(channelBinding.Session, channel.ConnectionDescription);
var operation = CreateOperation(operationContext, channelBinding.Session, channel.ConnectionDescription);
return await operation.ExecuteAsync(operationContext, channelBinding).ConfigureAwait(false);
}
}

private IDisposable BeginOperation() => EventContext.BeginOperation("renameCollection");

private WriteCommandOperation<BsonDocument> CreateOperation(ICoreSessionHandle session, ConnectionDescription connectionDescription)
private WriteCommandOperation<BsonDocument> CreateOperation(OperationContext operationContext, ICoreSessionHandle session, ConnectionDescription connectionDescription)
{
var command = CreateCommand(session, connectionDescription);
var command = CreateCommand(operationContext, session, connectionDescription);
return new WriteCommandOperation<BsonDocument>(DatabaseNamespace.Admin, command, BsonDocumentSerializer.Instance, _messageEncoderSettings);
}
}
Expand Down
Loading