Skip to content

Conversation

sanych-sun
Copy link
Member

No description provided.

@sanych-sun sanych-sun requested a review from a team as a code owner February 18, 2025 22:22
@sanych-sun sanych-sun requested review from BorisDog and papafe and removed request for a team February 18, 2025 22:22
private ClusterDescription _description;
private TaskCompletionSource<bool> _descriptionChangedTaskCompletionSource;
private readonly object _descriptionLock = new object();
private Tuple<ClusterDescription, TaskCompletionSource<bool>> _descriptionWithChangedTaskCompletionSource;
Copy link
Member Author

@sanych-sun sanych-sun Feb 18, 2025

Choose a reason for hiding this comment

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

I've decided to use Tuple instead of 2 fields, to let me remove lock in UpdateClusterDescription method.

Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks good!

oldClusterDescription = _description;
_description = newClusterDescription;
var oldClusterDescription = Interlocked.Exchange(ref _descriptionWithChangedTaskCompletionSource,
Tuple.Create(newClusterDescription, new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously)));
Copy link
Member Author

Choose a reason for hiding this comment

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

We can use TaskCreationOptions.RunContinuationsAsynchronously and remove old workaround with explicitly running new task (see line 311 before the changes).

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(newClusterDescription, new(TaskCreationOptions..)) ?

TrackInUseConnection(result);
}

// This connection can be expired and not disposed by Prune. Dispose if needed
Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like there is no way to get expired connection here, because we just checked that in line 763.

@sanych-sun sanych-sun marked this pull request as draft February 19, 2025 23:30
@sanych-sun sanych-sun changed the title [WIP] CSHARP-5496: Reduce locks contention on server selection and connection checkout CSHARP-5496: Reduce locks contention on server selection and connection checkout Mar 5, 2025
@sanych-sun sanych-sun marked this pull request as ready for review March 5, 2025 22:21
Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

Just a few minor comments for your consideration.

private ClusterDescription _description;
private TaskCompletionSource<bool> _descriptionChangedTaskCompletionSource;
private readonly object _descriptionLock = new object();
private Tuple<ClusterDescription, TaskCompletionSource<bool>> _descriptionWithChangedTaskCompletionSource;
Copy link
Contributor

Choose a reason for hiding this comment

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

This change looks good!

_clusterId = new ClusterId();
_description = ClusterDescription.CreateInitial(_clusterId, _settings.DirectConnection);
_descriptionChangedTaskCompletionSource = new TaskCompletionSource<bool>();
_descriptionWithChangedTaskCompletionSource = new(ClusterDescription.CreateInitial(_clusterId, _settings.DirectConnection), new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously));
Copy link
Contributor

Choose a reason for hiding this comment

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

minor:

_descriptionWithChangedTaskCompletionSource = new(ClusterDescription.CreateInitial(_clusterId, _settings.DirectConnection), new(TaskCreationOptions.RunContinuationsAsynchronously));

oldClusterDescription = _description;
_description = newClusterDescription;
var oldClusterDescription = Interlocked.Exchange(ref _descriptionWithChangedTaskCompletionSource,
Tuple.Create(newClusterDescription, new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously)));
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(newClusterDescription, new(TaskCreationOptions..)) ?


// TODO: use RunContinuationsAsynchronously instead once we require a new enough .NET Framework
Task.Run(() => oldDescriptionChangedTaskCompletionSource.TrySetResult(true));
oldClusterDescription.Item2.TrySetResult(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider defining a 2 liner private class to improve readability of Item1 and Item2.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

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

Apart from the comments that Boris already gave, LGTM!

@sanych-sun sanych-sun requested a review from BorisDog March 7, 2025 01:19
Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

LGTM!

}

// nested classes
internal class ClusterDescriptionChangeSource
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: sealed.


public ClusterDescriptionChangeSource(ClusterDescription clusterDescription)
{
_changedTaskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to hide this ceremony of source creation.

@sanych-sun sanych-sun merged commit f78ad53 into mongodb:main Mar 11, 2025
30 checks passed
@sanych-sun sanych-sun deleted the csharp5496 branch March 11, 2025 15:53
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