Skip to content

Commit 082798b

Browse files
authored
CSHARP-6011: Remove Session from IBinding by moving it to OperationContext (mongodb#1985)
1 parent 244774b commit 082798b

173 files changed

Lines changed: 2194 additions & 2374 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -26,14 +26,12 @@ internal sealed class ChannelChannelSource : IChannelSource
2626
private readonly IChannelHandle _channel;
2727
private bool _disposed;
2828
private readonly IServer _server;
29-
private readonly ICoreSessionHandle _session;
3029

3130
// constructors
32-
public ChannelChannelSource(IServer server, IChannelHandle channel, ICoreSessionHandle session)
31+
public ChannelChannelSource(IServer server, IChannelHandle channel)
3332
{
3433
_server = Ensure.IsNotNull(server, nameof(server));
3534
_channel = Ensure.IsNotNull(channel, nameof(channel));
36-
_session = Ensure.IsNotNull(session, nameof(session));
3735
}
3836

3937
// properties
@@ -47,18 +45,12 @@ public ServerDescription ServerDescription
4745
get { return _server.Description; }
4846
}
4947

50-
public ICoreSessionHandle Session
51-
{
52-
get { return _session; }
53-
}
54-
5548
// methods
5649
public void Dispose()
5750
{
5851
if (!_disposed)
5952
{
6053
_channel.Dispose();
61-
_session.Dispose();
6254
_disposed = true;
6355
}
6456
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -27,32 +27,24 @@ internal sealed class ChannelReadBinding : IReadBinding
2727
private bool _disposed;
2828
private readonly ReadPreference _readPreference;
2929
private readonly IServer _server;
30-
private readonly ICoreSessionHandle _session;
3130

32-
public ChannelReadBinding(IServer server, IChannelHandle channel, ReadPreference readPreference, ICoreSessionHandle session)
31+
public ChannelReadBinding(IServer server, IChannelHandle channel, ReadPreference readPreference)
3332
{
3433
_server = Ensure.IsNotNull(server, nameof(server));
3534
_channel = Ensure.IsNotNull(channel, nameof(channel));
3635
_readPreference = Ensure.IsNotNull(readPreference, nameof(readPreference));
37-
_session = Ensure.IsNotNull(session, nameof(session));
3836
}
3937

4038
public ReadPreference ReadPreference
4139
{
4240
get { return _readPreference; }
4341
}
4442

45-
public ICoreSessionHandle Session
46-
{
47-
get { return _session; }
48-
}
49-
5043
public void Dispose()
5144
{
5245
if (!_disposed)
5346
{
5447
_channel.Dispose();
55-
_session.Dispose();
5648
_disposed = true;
5749
}
5850
}
@@ -81,7 +73,7 @@ public Task<IChannelSourceHandle> GetReadChannelSourceAsync(OperationContext ope
8173

8274
private IChannelSourceHandle GetReadChannelSourceHelper()
8375
{
84-
return new ChannelSourceHandle(new ChannelChannelSource(_server, _channel.Fork(), _session.Fork()));
76+
return new ChannelSourceHandle(new ChannelChannelSource(_server, _channel.Fork()));
8577
}
8678

8779
private void ThrowIfDisposed()

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -26,31 +26,23 @@ internal sealed class ChannelReadWriteBinding : IReadWriteBinding
2626
private readonly IChannelHandle _channel;
2727
private bool _disposed;
2828
private readonly IServer _server;
29-
private readonly ICoreSessionHandle _session;
3029

31-
public ChannelReadWriteBinding(IServer server, IChannelHandle channel, ICoreSessionHandle session)
30+
public ChannelReadWriteBinding(IServer server, IChannelHandle channel)
3231
{
3332
_server = Ensure.IsNotNull(server, nameof(server));
3433
_channel = Ensure.IsNotNull(channel, nameof(channel));
35-
_session = Ensure.IsNotNull(session, nameof(session));
3634
}
3735

3836
public ReadPreference ReadPreference
3937
{
4038
get { return ReadPreference.Primary; }
4139
}
4240

43-
public ICoreSessionHandle Session
44-
{
45-
get { return _session; }
46-
}
47-
4841
public void Dispose()
4942
{
5043
if (!_disposed)
5144
{
5245
_channel.Dispose();
53-
_session.Dispose();
5446
_disposed = true;
5547
}
5648
}
@@ -121,7 +113,7 @@ public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(OperationContext op
121113

122114
private IChannelSourceHandle GetChannelSourceHelper()
123115
{
124-
return new ChannelSourceHandle(new ChannelChannelSource(_server, _channel.Fork(), _session.Fork()));
116+
return new ChannelSourceHandle(new ChannelChannelSource(_server, _channel.Fork()));
125117
}
126118

127119
private void ThrowIfDisposed()

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -48,11 +48,6 @@ public ServerDescription ServerDescription
4848
get { return _reference.Instance.ServerDescription; }
4949
}
5050

51-
public ICoreSessionHandle Session
52-
{
53-
get { return _reference.Instance.Session; }
54-
}
55-
5651
// methods
5752
public IChannelHandle GetChannel(OperationContext operationContext)
5853
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ public void CloseAndDispose(IServer server)
127127
}
128128

129129
var endSessionCommand = new BsonDocument("endSessions", new BsonArray(batch.Take(batchSize).Select(s => s.Id)));
130-
var operationContext = OperationContext.NoTimeout;
130+
using var session = NoCoreSession.NewHandle();
131+
using var operationContext = new OperationContext(session);
131132
using var channel = server.GetChannel(operationContext);
132133
channel.Command(
133134
operationContext,
134-
NoCoreSession.Instance,
135135
ReadPreference.PrimaryPreferred,
136136
DatabaseNamespace.Admin,
137137
endSessionCommand,

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ void ICoreSessionInternal.AbortTransaction(AbortTransactionOptions options, Canc
151151
{
152152
EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction));
153153

154-
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), "abortTransaction", "admin", null, _currentTransaction.IsTracingEnabled, cancellationToken);
154+
using var sessionHandle = new NonDisposingCoreSessionHandle(this);
155+
using var operationContext = new OperationContext(sessionHandle, GetTimeout(options?.Timeout), cancellationToken)
156+
{
157+
IsTracingEnabled = _currentTransaction.IsTracingEnabled,
158+
OperationName = "abortTransaction",
159+
DatabaseName = "admin",
160+
CollectionName = null
161+
};
155162
try
156163
{
157164
if (_currentTransaction.IsEmpty)
@@ -189,7 +196,14 @@ async Task ICoreSessionInternal.AbortTransactionAsync(AbortTransactionOptions op
189196
{
190197
EnsureAbortTransactionCanBeCalled(nameof(AbortTransaction));
191198

192-
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), "abortTransaction", "admin", null, _currentTransaction.IsTracingEnabled, cancellationToken);
199+
using var sessionHandle = new NonDisposingCoreSessionHandle(this);
200+
using var operationContext = new OperationContext(sessionHandle, GetTimeout(options?.Timeout), cancellationToken)
201+
{
202+
IsTracingEnabled = _currentTransaction.IsTracingEnabled,
203+
OperationName = "abortTransaction",
204+
DatabaseName = "admin",
205+
CollectionName = null
206+
};
193207
try
194208
{
195209
if (_currentTransaction.IsEmpty)
@@ -275,8 +289,14 @@ public void CommitTransaction(CancellationToken cancellationToken = default)
275289
void ICoreSessionInternal.CommitTransaction(CommitTransactionOptions options, CancellationToken cancellationToken)
276290
{
277291
EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction));
278-
279-
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), "commitTransaction", "admin", null, _currentTransaction.IsTracingEnabled, cancellationToken);
292+
using var sessionHandle = new NonDisposingCoreSessionHandle(this);
293+
using var operationContext = new OperationContext(sessionHandle, GetTimeout(options?.Timeout), cancellationToken)
294+
{
295+
IsTracingEnabled = _currentTransaction.IsTracingEnabled,
296+
OperationName = "commitTransaction",
297+
DatabaseName = "admin",
298+
CollectionName = null
299+
};
280300
try
281301
{
282302
_isCommitTransactionInProgress = true;
@@ -304,8 +324,14 @@ public Task CommitTransactionAsync(CancellationToken cancellationToken = default
304324
async Task ICoreSessionInternal.CommitTransactionAsync(CommitTransactionOptions options, CancellationToken cancellationToken)
305325
{
306326
EnsureCommitTransactionCanBeCalled(nameof(CommitTransaction));
307-
308-
using var operationContext = new OperationContext(GetTimeout(options?.Timeout), "commitTransaction", "admin", null, _currentTransaction.IsTracingEnabled, cancellationToken);
327+
using var sessionHandle = new NonDisposingCoreSessionHandle(this);
328+
using var operationContext = new OperationContext(sessionHandle, GetTimeout(options?.Timeout), cancellationToken)
329+
{
330+
IsTracingEnabled = _currentTransaction.IsTracingEnabled,
331+
OperationName = "commitTransaction",
332+
DatabaseName = "admin",
333+
CollectionName = null
334+
};
309335
try
310336
{
311337
_isCommitTransactionInProgress = true;
@@ -549,7 +575,7 @@ private IReadWriteBinding CreateEndTransactionBinding(ICoreSessionHandle session
549575
// otherwise the captured pinned-channel fork goes stale after UnpinAll.
550576
if (_cluster.Description.Type == ClusterType.LoadBalanced)
551577
{
552-
return new EndTransactionReadWriteBinding(_cluster, session);
578+
return new EndTransactionReadWriteBinding(_cluster);
553579
}
554580

555581
return ChannelPinningHelper.CreateReadWriteBinding(_cluster, session);

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

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,72 +31,111 @@ internal sealed class EndTransactionReadWriteBinding : IReadWriteBinding
3131
#pragma warning restore CA2213 // Disposable fields should be disposed
3232
private bool _disposed;
3333
private IReadWriteBindingHandle _innerBinding;
34-
private readonly ICoreSessionHandle _session;
3534

36-
public EndTransactionReadWriteBinding(IClusterInternal cluster, ICoreSessionHandle session)
35+
public EndTransactionReadWriteBinding(IClusterInternal cluster)
3736
{
3837
_cluster = Ensure.IsNotNull(cluster, nameof(cluster));
39-
_session = Ensure.IsNotNull(session, nameof(session));
40-
_innerBinding = ChannelPinningHelper.CreateReadWriteBinding(_cluster, _session.Fork());
4138
}
4239

4340
public ReadPreference ReadPreference => ReadPreference.Primary;
4441

45-
public ICoreSessionHandle Session => _session;
46-
4742
// Called by EndTransactionOperation.OnRetry between attempts.
4843
public void RebuildInnerBinding()
4944
{
5045
ThrowIfDisposed();
51-
_innerBinding.Dispose();
52-
_innerBinding = ChannelPinningHelper.CreateReadWriteBinding(_cluster, _session.Fork());
46+
_innerBinding?.Dispose();
47+
_innerBinding = null;
5348
}
5449

5550
public void Dispose()
5651
{
5752
if (!_disposed)
5853
{
59-
_innerBinding.Dispose();
60-
_session.Dispose();
54+
_innerBinding?.Dispose();
6155
_disposed = true;
6256
}
6357
}
6458

6559
public IChannelSourceHandle GetReadChannelSource(OperationContext operationContext)
66-
=> _innerBinding.GetReadChannelSource(operationContext);
60+
{
61+
EnsureInnerBinding(operationContext);
62+
return _innerBinding.GetReadChannelSource(operationContext);
63+
}
6764

6865
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(OperationContext operationContext)
69-
=> _innerBinding.GetReadChannelSourceAsync(operationContext);
66+
{
67+
EnsureInnerBinding(operationContext);
68+
return _innerBinding.GetReadChannelSourceAsync(operationContext);
69+
}
7070

7171
public IChannelSourceHandle GetReadChannelSource(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers)
72-
=> _innerBinding.GetReadChannelSource(operationContext, deprioritizedServers);
72+
{
73+
EnsureInnerBinding(operationContext);
74+
return _innerBinding.GetReadChannelSource(operationContext, deprioritizedServers);
75+
}
7376

7477
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers)
75-
=> _innerBinding.GetReadChannelSourceAsync(operationContext, deprioritizedServers);
78+
{
79+
EnsureInnerBinding(operationContext);
80+
return _innerBinding.GetReadChannelSourceAsync(operationContext, deprioritizedServers);
81+
}
7682

7783
public IChannelSourceHandle GetWriteChannelSource(OperationContext operationContext)
78-
=> _innerBinding.GetWriteChannelSource(operationContext);
84+
{
85+
EnsureInnerBinding(operationContext);
86+
return _innerBinding.GetWriteChannelSource(operationContext);
87+
}
7988

8089
public IChannelSourceHandle GetWriteChannelSource(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers)
81-
=> _innerBinding.GetWriteChannelSource(operationContext, deprioritizedServers);
90+
{
91+
EnsureInnerBinding(operationContext);
92+
return _innerBinding.GetWriteChannelSource(operationContext, deprioritizedServers);
93+
}
8294

8395
public IChannelSourceHandle GetWriteChannelSource(OperationContext operationContext, IMayUseSecondaryCriteria mayUseSecondary)
84-
=> _innerBinding.GetWriteChannelSource(operationContext, mayUseSecondary);
96+
{
97+
EnsureInnerBinding(operationContext);
98+
return _innerBinding.GetWriteChannelSource(operationContext, mayUseSecondary);
99+
}
85100

86101
public IChannelSourceHandle GetWriteChannelSource(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers, IMayUseSecondaryCriteria mayUseSecondary)
87-
=> _innerBinding.GetWriteChannelSource(operationContext, deprioritizedServers, mayUseSecondary);
102+
{
103+
EnsureInnerBinding(operationContext);
104+
return _innerBinding.GetWriteChannelSource(operationContext, deprioritizedServers, mayUseSecondary);
105+
}
88106

89107
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(OperationContext operationContext)
90-
=> _innerBinding.GetWriteChannelSourceAsync(operationContext);
108+
{
109+
EnsureInnerBinding(operationContext);
110+
return _innerBinding.GetWriteChannelSourceAsync(operationContext);
111+
}
91112

92113
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers)
93-
=> _innerBinding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers);
114+
{
115+
EnsureInnerBinding(operationContext);
116+
return _innerBinding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers);
117+
}
94118

95119
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(OperationContext operationContext, IMayUseSecondaryCriteria mayUseSecondary)
96-
=> _innerBinding.GetWriteChannelSourceAsync(operationContext, mayUseSecondary);
120+
{
121+
EnsureInnerBinding(operationContext);
122+
return _innerBinding.GetWriteChannelSourceAsync(operationContext, mayUseSecondary);
123+
}
97124

98125
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(OperationContext operationContext, IReadOnlyCollection<ServerDescription> deprioritizedServers, IMayUseSecondaryCriteria mayUseSecondary)
99-
=> _innerBinding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers, mayUseSecondary);
126+
{
127+
EnsureInnerBinding(operationContext);
128+
return _innerBinding.GetWriteChannelSourceAsync(operationContext, deprioritizedServers, mayUseSecondary);
129+
}
130+
131+
private void EnsureInnerBinding(OperationContext operationContext)
132+
{
133+
ThrowIfDisposed();
134+
if (_innerBinding == null)
135+
{
136+
_innerBinding = ChannelPinningHelper.CreateReadWriteBinding(_cluster, operationContext.Session);
137+
}
138+
}
100139

101140
private void ThrowIfDisposed()
102141
{

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@ namespace MongoDB.Driver.Core.Bindings
2222
{
2323
internal interface IBinding : IDisposable
2424
{
25-
ICoreSessionHandle Session { get; }
2625
}
2726

2827
internal interface IReadBinding : IBinding

0 commit comments

Comments
 (0)