Skip to content

Commit de9ab50

Browse files
CSHARP-3671: Simplifying.
1 parent a610696 commit de9ab50

19 files changed

+200
-335
lines changed

src/MongoDB.Driver.Core/Core/Bindings/ReadPreferenceBinding.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public sealed class ReadPreferenceBinding : IReadBinding
3434
private readonly ReadPreference _readPreference;
3535
private readonly IServerSelector _serverSelector;
3636
private readonly ICoreSessionHandle _session;
37-
private readonly TrackedOperationRunContext _trackedOperationRunContext;
3837

3938
// constructors
4039
/// <summary>
@@ -44,17 +43,11 @@ public sealed class ReadPreferenceBinding : IReadBinding
4443
/// <param name="readPreference">The read preference.</param>
4544
/// <param name="session">The session.</param>
4645
public ReadPreferenceBinding(ICluster cluster, ReadPreference readPreference, ICoreSessionHandle session)
47-
: this(cluster, readPreference, session, trackedOperationRunContext: null)
48-
{
49-
}
50-
51-
internal ReadPreferenceBinding(ICluster cluster, ReadPreference readPreference, ICoreSessionHandle session, TrackedOperationRunContext trackedOperationRunContext)
5246
{
5347
_cluster = Ensure.IsNotNull(cluster, nameof(cluster));
5448
_readPreference = Ensure.IsNotNull(readPreference, nameof(readPreference));
5549
_session = Ensure.IsNotNull(session, nameof(session));
5650
_serverSelector = new ReadPreferenceServerSelector(readPreference);
57-
_trackedOperationRunContext = trackedOperationRunContext; // can be null
5851
}
5952

6053
// properties
@@ -89,7 +82,7 @@ public async Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationTo
8982

9083
private IChannelSourceHandle GetChannelSourceHelper(IServer server)
9184
{
92-
return new ChannelSourceHandle(new ServerChannelSource(server, _session.Fork(), _trackedOperationRunContext));
85+
return new ChannelSourceHandle(new ServerChannelSource(server, _session.Fork()));
9386
}
9487

9588
/// <inheritdoc/>

src/MongoDB.Driver.Core/Core/Bindings/ServerChannelSource.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Threading;
1818
using System.Threading.Tasks;
19+
using MongoDB.Driver.Core.Connections;
1920
using MongoDB.Driver.Core.Misc;
2021
using MongoDB.Driver.Core.Servers;
2122

@@ -30,7 +31,6 @@ public sealed class ServerChannelSource : IChannelSource
3031
private bool _disposed;
3132
private readonly IServer _server;
3233
private readonly ICoreSessionHandle _session;
33-
private readonly TrackedOperationRunContext _trackedOperationRunContext;
3434

3535
// constructors
3636
/// <summary>
@@ -39,15 +39,9 @@ public sealed class ServerChannelSource : IChannelSource
3939
/// <param name="server">The server.</param>
4040
/// <param name="session">The session.</param>
4141
public ServerChannelSource(IServer server, ICoreSessionHandle session)
42-
: this(server, session, trackedOperationRunContext: null)
43-
{
44-
}
45-
46-
internal ServerChannelSource(IServer server, ICoreSessionHandle session, TrackedOperationRunContext trackedOperationRunContext)
4742
{
4843
_server = Ensure.IsNotNull(server, nameof(server));
4944
_session = Ensure.IsNotNull(session, nameof(session));
50-
_trackedOperationRunContext = trackedOperationRunContext; // can be null
5145
}
5246

5347
// properties
@@ -84,28 +78,14 @@ public void Dispose()
8478
public IChannelHandle GetChannel(CancellationToken cancellationToken)
8579
{
8680
ThrowIfDisposed();
87-
if (_trackedOperationRunContext != null && _server is IServerWithTrackedGetChannel trackedServer)
88-
{
89-
return trackedServer.GetChannel(_trackedOperationRunContext, cancellationToken);
90-
}
91-
else
92-
{
93-
return _server.GetChannel(cancellationToken);
94-
}
81+
return _server.GetChannel(cancellationToken);
9582
}
9683

9784
/// <inheritdoc/>
9885
public Task<IChannelHandle> GetChannelAsync(CancellationToken cancellationToken)
9986
{
10087
ThrowIfDisposed();
101-
if (_trackedOperationRunContext != null && _server is IServerWithTrackedGetChannel trackedServer)
102-
{
103-
return trackedServer.GetChannelAsync(_trackedOperationRunContext, cancellationToken);
104-
}
105-
else
106-
{
107-
return _server.GetChannelAsync(cancellationToken);
108-
}
88+
return _server.GetChannelAsync(cancellationToken);
10989
}
11090

11191
private void ThrowIfDisposed()

src/MongoDB.Driver.Core/Core/Bindings/TrackedOperationRunContext.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/MongoDB.Driver.Core/Core/Bindings/WritableServerBinding.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public sealed class WritableServerBinding : IReadWriteBinding
3232
private readonly ICluster _cluster;
3333
private bool _disposed;
3434
private readonly ICoreSessionHandle _session;
35-
private readonly TrackedOperationRunContext _trackedOperationRunContext;
3635

3736
// constructors
3837
/// <summary>
@@ -41,15 +40,9 @@ public sealed class WritableServerBinding : IReadWriteBinding
4140
/// <param name="cluster">The cluster.</param>
4241
/// <param name="session">The session.</param>
4342
public WritableServerBinding(ICluster cluster, ICoreSessionHandle session)
44-
: this(cluster, session, trackedOperationRunContext: null)
45-
{
46-
}
47-
48-
internal WritableServerBinding(ICluster cluster, ICoreSessionHandle session, TrackedOperationRunContext trackedOperationRunContext)
4943
{
5044
_cluster = Ensure.IsNotNull(cluster, nameof(cluster));
5145
_session = Ensure.IsNotNull(session, nameof(session));
52-
_trackedOperationRunContext = trackedOperationRunContext; // can be null
5346
}
5447

5548
// properties
@@ -101,7 +94,7 @@ public async Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationT
10194

10295
private IChannelSourceHandle GetChannelSourceHelper(IServer server)
10396
{
104-
return new ChannelSourceHandle(new ServerChannelSource(server, _session.Fork(), _trackedOperationRunContext));
97+
return new ChannelSourceHandle(new ServerChannelSource(server, _session.Fork()));
10598
}
10699

107100
/// <inheritdoc/>

src/MongoDB.Driver.Core/Core/ChannelPinningHelper.cs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Threading;
1717
using MongoDB.Driver.Core.Bindings;
1818
using MongoDB.Driver.Core.Clusters;
19+
using MongoDB.Driver.Core.ConnectionPools;
1920
using MongoDB.Driver.Core.Connections;
2021
using MongoDB.Driver.Core.Servers;
2122

@@ -32,9 +33,8 @@ public static class ChannelPinningHelper
3233
/// <param name="cluster">The cluster,</param>
3334
/// <param name="session">The session.</param>
3435
/// <param name="readPreference">The read preference.</param>
35-
/// <param name="doesInitiateCursor">The flag whether operation initiates cursor.</param>
3636
/// <returns>An effective read binging.</returns>
37-
public static IReadBindingHandle CreateReadBinding(ICluster cluster, ICoreSessionHandle session, ReadPreference readPreference, bool doesInitiateCursor)
37+
public static IReadBindingHandle CreateReadBinding(ICluster cluster, ICoreSessionHandle session, ReadPreference readPreference)
3838
{
3939
IReadBinding readBinding;
4040
if (session.IsInTransaction &&
@@ -48,21 +48,12 @@ public static IReadBindingHandle CreateReadBinding(ICluster cluster, ICoreSessio
4848
}
4949
else
5050
{
51-
TrackedOperationRunContext trackedOperationRunContext;
52-
if (IsInLoadBalancedMode(cluster.Description))
51+
if (IsInLoadBalancedMode(cluster.Description) && IsChannelPinned(session.CurrentTransaction))
5352
{
54-
if (IsChannelPinned(session.CurrentTransaction))
55-
{
56-
// unpin if the next operation is not under transaction
57-
session.CurrentTransaction.UnpinAll();
58-
}
59-
trackedOperationRunContext = new TrackedOperationRunContext(session.IsInTransaction, doesInitiateCursor);
60-
}
61-
else
62-
{
63-
trackedOperationRunContext = TrackedOperationRunContext.CreateEmpty();
53+
// unpin if the next operation is not under transaction
54+
session.CurrentTransaction.UnpinAll();
6455
}
65-
readBinding = new ReadPreferenceBinding(cluster, readPreference, session, trackedOperationRunContext);
56+
readBinding = new ReadPreferenceBinding(cluster, readPreference, session);
6657
}
6758

6859
return new ReadBindingHandle(readBinding);
@@ -88,23 +79,12 @@ public static IReadWriteBindingHandle CreateReadWriteBinding(ICluster cluster, I
8879
}
8980
else
9081
{
91-
TrackedOperationRunContext trackedOperationRunContext;
92-
if (IsInLoadBalancedMode(cluster.Description))
93-
{
94-
if (IsChannelPinned(session.CurrentTransaction))
95-
{
96-
// unpin if the next operation is not under transaction
97-
session.CurrentTransaction.UnpinAll();
98-
}
99-
100-
trackedOperationRunContext = new TrackedOperationRunContext(session.IsInTransaction, withCursorResult: false);
101-
}
102-
else
82+
if (IsInLoadBalancedMode(cluster.Description) && IsChannelPinned(session.CurrentTransaction))
10383
{
104-
trackedOperationRunContext = TrackedOperationRunContext.CreateEmpty();
84+
// unpin if the next operation is not under transaction
85+
session.CurrentTransaction.UnpinAll();
10586
}
106-
107-
readWriteBinding = new WritableServerBinding(cluster, session, trackedOperationRunContext);
87+
readWriteBinding = new WritableServerBinding(cluster, session);
10888
}
10989

11090
return new ReadWriteBindingHandle(readWriteBinding);
@@ -117,6 +97,10 @@ internal static IChannelSourceHandle CreateGetMoreChannelSource(IChannelSourceHa
11797
{
11898
var getMoreChannel = channelSource.GetChannel(CancellationToken.None); // no need for cancellation token since we already have channel in the source
11999
var getMoreSession = channelSource.Session.Fork();
100+
if (getMoreChannel.Connection is ITrackedPinningReason trackedConnection)
101+
{
102+
trackedConnection.SetPinningCheckoutReasonIfNotAlreadySet(CheckedOutReason.Cursor);
103+
}
120104

121105
effectiveChannelSource = new ChannelChannelSource(
122106
channelSource.Server,
@@ -150,6 +134,10 @@ internal static bool PinChannelSourceAndChannelIfRequired(
150134

151135
if (session.IsInTransaction && !IsChannelPinned(session.CurrentTransaction))
152136
{
137+
if (channel.Connection is ITrackedPinningReason trackedConnection)
138+
{
139+
trackedConnection.SetPinningCheckoutReasonIfNotAlreadySet(CheckedOutReason.Transaction);
140+
}
153141
session.CurrentTransaction.PinChannel(channel.Fork());
154142
session.CurrentTransaction.PinnedServer = server;
155143
}

0 commit comments

Comments
 (0)