Skip to content

CSHARP-5560: CSOT: Refactor IOperationExecutor to use operation context #1676

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner April 24, 2025 00:08
@sanych-sun sanych-sun requested review from BorisDog and adelinowona and removed request for a team April 24, 2025 00:08
{
return operation.Execute(binding, cancellationToken);
bool isOwnSession = session == null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use var?

here and other methods in this file

{
return _client.StartImplicitSessionAsync(cancellationToken);
var options = new ClientSessionOptions { CausalConsistency = false, Snapshot = false };
ICoreSessionHandle coreSession = _client.GetClusterInternal().StartSession(options.ToCore(isImplicit: true));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use var?


namespace MongoDB.Driver
{
internal record class ReadOperationOptions(ReadPreference ExplicitReadPreference = null, ReadPreference DefaultReadPreference = null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for specifying class? I believe record is equivalent to record class

Comment on lines -225 to +200
return UsingImplicitSession(session => BulkWrite(session, requests, options, cancellationToken), cancellationToken);
using var session = OperationExecutor.StartImplicitSession(cancellationToken);
return BulkWrite(session, requests, options, cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that for some of the other methods above (like AggregateToCollection) you did not use OperationExecutor to create the implicit session and just used the existing UsingImplicitSession mechanism. Should we update all those use cases as well? or am I missing something?

Copy link
Contributor

@adelinowona adelinowona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! left some minor comments


var lastStage = renderedPipeline.Documents.LastOrDefault();
var lastStageName = lastStage?.GetElement(0).Name;
isAggregateToCollection = lastStage != null && (lastStageName == "$out" || lastStageName == "$merge");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lastStage != null check is redundant?

@@ -66,6 +67,9 @@ public MongoClient(MongoClientSettings settings)

_cluster = _settings.ClusterSource.Get(_settings.ToClusterKey());
_operationExecutor = new OperationExecutor(this);
_readOperationOptions = new ReadOperationOptions(DefaultReadPreference: _settings.ReadPreference);
_writeOperationOptions = new WriteOperationOptions();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider

_readOperationOptions = new (DefaultReadPreference: _settings.ReadPreference);
_writeOperationOptions = new WriteOperationOptions();

@@ -117,8 +121,6 @@ internal MongoClient(IOperationExecutor operationExecutor, MongoClientSettings s
// internal methods
internal void ConfigureAutoEncryptionMessageEncoderSettings(MessageEncoderSettings messageEncoderSettings)
{
ThrowIfDisposed();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need?

{
return operation.Execute(binding, cancellationToken);
bool isOwnSession = session == null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: var

@@ -94,160 +98,117 @@ public override MongoCollectionSettings Settings
}

// public methods
public override IAsyncCursor<TResult> Aggregate<TResult>(PipelineDefinition<TDocument, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken = default(CancellationToken))
public override IAsyncCursor<TResult> Aggregate<TResult>(PipelineDefinition<TDocument, TResult> pipeline, AggregateOptions options, CancellationToken cancellationToken = default)
{
return UsingImplicitSession(session => Aggregate(session, pipeline, options, cancellationToken), cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to reuse the newer pattern without UsingImplicitSession here (like in MongoDatabase)?

}

// public properties
public IMongoClient Client => _client;
public DatabaseNamespace DatabaseNamespace => _databaseNamespace;
private IOperationExecutor OperationExecutor => _operationExecutor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this?

return UsingImplicitSession(session => ListCollectionNames(session, options, cancellationToken), cancellationToken);
var cursor = OperationExecutor.ExecuteReadOperation(CreateListCollectionNamesOperation(options),
_readOperationOptions with { DefaultReadPreference = ReadPreference.Primary },
null, cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: New line for each param?

@@ -778,9 +717,10 @@ private async Task<IWriteOperation<BsonDocument>> CreateDropCollectionOperationA
});
}

private ListCollectionsOperation CreateListCollectionNamesOperation(ListCollectionNamesOptions options, RenderArgs<BsonDocument> renderArgs)
private ListCollectionsOperation CreateListCollectionNamesOperation(ListCollectionNamesOptions options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe refactoring to

CreateDropCollectionOperation
GetEncryptedFieldsMap
GetEncryptedFieldsMapAsync

Would be cleaner.


namespace MongoDB.Driver.Tests
{
public class ReadOperationOptionsTests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


[Theory]
[MemberData(nameof(ImplicitSessionTestCases))]
public async Task ExecuteReadOperationShouldStartAndDisposeImplicitSessionIfNeeded(bool shouldCreateSession, bool isAsync, IClientSessionHandle session)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we use this format:
ExecuteReadOperation_should_start_and.. ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants