Skip to content

Commit ee358f6

Browse files
CSHARP-3671: Simplifying (v2).
1 parent de9ab50 commit ee358f6

File tree

8 files changed

+46
-90
lines changed

8 files changed

+46
-90
lines changed

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

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,21 @@ public static IReadWriteBindingHandle CreateReadWriteBinding(ICluster cluster, I
9090
return new ReadWriteBindingHandle(readWriteBinding);
9191
}
9292

93-
internal static IChannelSourceHandle CreateGetMoreChannelSource(IChannelSourceHandle channelSource, long cursorId)
93+
internal static IChannelSourceHandle CreateGetMoreChannelSource(IChannelSourceHandle channelSource, IChannelHandle channel, long cursorId)
9494
{
9595
IChannelSource effectiveChannelSource;
9696
if (IsInLoadBalancedMode(channelSource.ServerDescription) && cursorId != 0)
9797
{
98-
var getMoreChannel = channelSource.GetChannel(CancellationToken.None); // no need for cancellation token since we already have channel in the source
99-
var getMoreSession = channelSource.Session.Fork();
100-
if (getMoreChannel.Connection is ITrackedPinningReason trackedConnection)
98+
if (channel.Connection is ITrackedPinningReason trackedConnection)
10199
{
102100
trackedConnection.SetPinningCheckoutReasonIfNotAlreadySet(CheckedOutReason.Cursor);
103101
}
104102

103+
// pinning channel
105104
effectiveChannelSource = new ChannelChannelSource(
106105
channelSource.Server,
107-
getMoreChannel,
108-
getMoreSession);
106+
channel.Fork(),
107+
channelSource.Session.Fork());
109108
}
110109
else
111110
{
@@ -115,41 +114,22 @@ internal static IChannelSourceHandle CreateGetMoreChannelSource(IChannelSourceHa
115114
return new ChannelSourceHandle(effectiveChannelSource);
116115
}
117116

118-
internal static bool PinChannelSourceAndChannelIfRequired(
117+
internal static void PinChannellIfRequired(
119118
IChannelSourceHandle channelSource,
120119
IChannelHandle channel,
121-
ICoreSessionHandle session,
122-
out IChannelSourceHandle pinnedChannelSource,
123-
out IChannelHandle pinnedChannel)
120+
ICoreSessionHandle session)
124121
{
125-
if (IsInLoadBalancedMode(channel.ConnectionDescription))
122+
if (IsInLoadBalancedMode(channel.ConnectionDescription) &&
123+
session.IsInTransaction &&
124+
!IsChannelPinned(session.CurrentTransaction))
126125
{
127-
var server = channelSource.Server;
128-
129-
pinnedChannelSource = new ChannelSourceHandle(
130-
new ChannelChannelSource(
131-
server,
132-
channel.Fork(),
133-
session.Fork()));
134-
135-
if (session.IsInTransaction && !IsChannelPinned(session.CurrentTransaction))
126+
if (channel.Connection is ITrackedPinningReason trackedConnection)
136127
{
137-
if (channel.Connection is ITrackedPinningReason trackedConnection)
138-
{
139-
trackedConnection.SetPinningCheckoutReasonIfNotAlreadySet(CheckedOutReason.Transaction);
140-
}
141-
session.CurrentTransaction.PinChannel(channel.Fork());
142-
session.CurrentTransaction.PinnedServer = server;
128+
trackedConnection.SetPinningCheckoutReasonIfNotAlreadySet(CheckedOutReason.Transaction);
143129
}
144-
145-
pinnedChannel = channel.Fork();
146-
147-
return true;
130+
session.CurrentTransaction.PinChannel(channel.Fork());
131+
session.CurrentTransaction.PinnedServer = channelSource.Server;
148132
}
149-
150-
pinnedChannelSource = null;
151-
pinnedChannel = null;
152-
return false;
153133
}
154134

155135
// private methods

src/MongoDB.Driver.Core/Core/Operations/AggregateOperation.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public IAsyncCursor<TResult> Execute(RetryableReadContext context, CancellationT
298298

299299
context.ChannelSource.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
300300

301-
return CreateCursor(context.ChannelSource, operation.Command, result);
301+
return CreateCursor(context.ChannelSource, context.Channel, operation.Command, result);
302302
}
303303
}
304304

@@ -326,7 +326,7 @@ public async Task<IAsyncCursor<TResult>> ExecuteAsync(RetryableReadContext conte
326326

327327
context.ChannelSource.Session.SetSnapshotTimeIfNeeded(result.AtClusterTime);
328328

329-
return CreateCursor(context.ChannelSource, operation.Command, result);
329+
return CreateCursor(context.ChannelSource, context.Channel,operation.Command, result);
330330
}
331331
}
332332

@@ -387,11 +387,11 @@ private ReadCommandOperation<AggregateResult> CreateOperation(RetryableReadConte
387387
};
388388
}
389389

390-
private AsyncCursor<TResult> CreateCursor(IChannelSourceHandle channelSource, BsonDocument command, AggregateResult result)
390+
private AsyncCursor<TResult> CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument command, AggregateResult result)
391391
{
392392
if (result.CursorId.HasValue)
393393
{
394-
return CreateCursorFromCursorResult(channelSource, command, result);
394+
return CreateCursorFromCursorResult(channelSource, channel, command, result);
395395
}
396396
else
397397
{
@@ -400,10 +400,10 @@ private AsyncCursor<TResult> CreateCursor(IChannelSourceHandle channelSource, Bs
400400
}
401401
}
402402

403-
private AsyncCursor<TResult> CreateCursorFromCursorResult(IChannelSourceHandle channelSource, BsonDocument command, AggregateResult result)
403+
private AsyncCursor<TResult> CreateCursorFromCursorResult(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument command, AggregateResult result)
404404
{
405405
var cursorId = result.CursorId.GetValueOrDefault(0);
406-
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, cursorId);
406+
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, channel, cursorId);
407407
return new AsyncCursor<TResult>(
408408
getMoreChannelSource,
409409
result.CollectionNamespace,

src/MongoDB.Driver.Core/Core/Operations/FindCommandOperation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,12 +479,12 @@ internal BsonDocument CreateCommand(ConnectionDescription connectionDescription,
479479
};
480480
}
481481

482-
private AsyncCursor<TDocument> CreateCursor(IChannelSourceHandle channelSource, BsonDocument commandResult)
482+
private AsyncCursor<TDocument> CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument commandResult)
483483
{
484484
var cursorDocument = commandResult["cursor"].AsBsonDocument;
485485
var collectionNamespace = CollectionNamespace.FromFullName(cursorDocument["ns"].AsString);
486486
var firstBatch = CreateFirstCursorBatch(cursorDocument);
487-
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, firstBatch.CursorId);
487+
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, channel, firstBatch.CursorId);
488488

489489
if (cursorDocument.TryGetValue("atClusterTime", out var atClusterTime))
490490
{
@@ -537,7 +537,7 @@ private CursorBatch<TDocument> CreateFirstCursorBatch(BsonDocument cursorDocumen
537537
{
538538
var operation = CreateOperation(context);
539539
var commandResult = operation.Execute(context, cancellationToken);
540-
return CreateCursor(context.ChannelSource, commandResult);
540+
return CreateCursor(context.ChannelSource, context.Channel, commandResult);
541541
}
542542
}
543543

@@ -562,7 +562,7 @@ private CursorBatch<TDocument> CreateFirstCursorBatch(BsonDocument cursorDocumen
562562
{
563563
var operation = CreateOperation(context);
564564
var commandResult = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false);
565-
return CreateCursor(context.ChannelSource, commandResult);
565+
return CreateCursor(context.ChannelSource, context.Channel, commandResult);
566566
}
567567
}
568568

src/MongoDB.Driver.Core/Core/Operations/FindOpcodeOperation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ public IAsyncCursor<TDocument> Execute(RetryableReadContext context, Cancellatio
467467
var wrappedQuery = CreateWrappedQuery(serverDescription.Type, readPreference, out var secondaryOk);
468468

469469
var batch = ExecuteProtocol(context.Channel, wrappedQuery, secondaryOk, cancellationToken);
470-
return CreateCursor(context.ChannelSource, wrappedQuery, batch);
470+
return CreateCursor(context.ChannelSource, context.Channel, wrappedQuery, batch);
471471
}
472472
}
473473

@@ -495,7 +495,7 @@ public async Task<IAsyncCursor<TDocument>> ExecuteAsync(RetryableReadContext con
495495
var wrappedQuery = CreateWrappedQuery(serverDescription.Type, readPreference, out var secondaryOk);
496496

497497
var batch = await ExecuteProtocolAsync(context.Channel, wrappedQuery, secondaryOk, cancellationToken).ConfigureAwait(false);
498-
return CreateCursor(context.ChannelSource, wrappedQuery, batch);
498+
return CreateCursor(context.ChannelSource, context.Channel, wrappedQuery, batch);
499499
}
500500
}
501501

@@ -538,9 +538,9 @@ public IReadOperation<BsonDocument> ToExplainOperation(ExplainVerbosity verbosit
538538
}
539539

540540
// private methods
541-
private IAsyncCursor<TDocument> CreateCursor(IChannelSourceHandle channelSource, BsonDocument query, CursorBatch<TDocument> batch)
541+
private IAsyncCursor<TDocument> CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument query, CursorBatch<TDocument> batch)
542542
{
543-
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, batch.CursorId);
543+
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, channel, batch.CursorId);
544544

545545
return new AsyncCursor<TDocument>(
546546
getMoreChannelSource,

src/MongoDB.Driver.Core/Core/Operations/ListCollectionsUsingCommandOperation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public IAsyncCursor<BsonDocument> Execute(RetryableReadContext context, Cancella
144144
{
145145
var operation = CreateOperation();
146146
var result = operation.Execute(context, cancellationToken);
147-
return CreateCursor(context.ChannelSource, operation.Command, result);
147+
return CreateCursor(context.ChannelSource, context.Channel, operation.Command, result);
148148
}
149149
}
150150

@@ -168,7 +168,7 @@ public async Task<IAsyncCursor<BsonDocument>> ExecuteAsync(RetryableReadContext
168168
{
169169
var operation = CreateOperation();
170170
var result = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false);
171-
return CreateCursor(context.ChannelSource, operation.Command, result);
171+
return CreateCursor(context.ChannelSource, context.Channel, operation.Command, result);
172172
}
173173
}
174174

@@ -188,11 +188,11 @@ private ReadCommandOperation<BsonDocument> CreateOperation()
188188
};
189189
}
190190

191-
private IAsyncCursor<BsonDocument> CreateCursor(IChannelSourceHandle channelSource, BsonDocument command, BsonDocument result)
191+
private IAsyncCursor<BsonDocument> CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument command, BsonDocument result)
192192
{
193193
var cursorDocument = result["cursor"].AsBsonDocument;
194194
var cursorId = cursorDocument["id"].ToInt64();
195-
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, cursorId);
195+
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, channel, cursorId);
196196
var cursor = new AsyncCursor<BsonDocument>(
197197
getMoreChannelSource,
198198
CollectionNamespace.FromFullName(cursorDocument["ns"].AsString),

src/MongoDB.Driver.Core/Core/Operations/ListIndexesUsingCommandOperation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public IAsyncCursor<BsonDocument> Execute(RetryableReadContext context, Cancella
123123
try
124124
{
125125
var result = operation.Execute(context, cancellationToken);
126-
return CreateCursor(context.ChannelSource, result, operation.Command);
126+
return CreateCursor(context.ChannelSource, context.Channel, result, operation.Command);
127127
}
128128
catch (MongoCommandException ex) when (IsCollectionNotFoundException(ex))
129129
{
@@ -154,7 +154,7 @@ public async Task<IAsyncCursor<BsonDocument>> ExecuteAsync(RetryableReadContext
154154
try
155155
{
156156
var result = await operation.ExecuteAsync(context, cancellationToken).ConfigureAwait(false);
157-
return CreateCursor(context.ChannelSource, result, operation.Command);
157+
return CreateCursor(context.ChannelSource, context.Channel, result, operation.Command);
158158
}
159159
catch (MongoCommandException ex) when (IsCollectionNotFoundException(ex))
160160
{
@@ -178,11 +178,11 @@ private ReadCommandOperation<BsonDocument> CreateOperation()
178178
};
179179
}
180180

181-
private IAsyncCursor<BsonDocument> CreateCursor(IChannelSourceHandle channelSource, BsonDocument result, BsonDocument command)
181+
private IAsyncCursor<BsonDocument> CreateCursor(IChannelSourceHandle channelSource, IChannelHandle channel, BsonDocument result, BsonDocument command)
182182
{
183183
var cursorDocument = result["cursor"].AsBsonDocument;
184184
var cursorId = cursorDocument["id"].ToInt64();
185-
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, cursorId);
185+
var getMoreChannelSource = ChannelPinningHelper.CreateGetMoreChannelSource(channelSource, channel, cursorId);
186186
var cursor = new AsyncCursor<BsonDocument>(
187187
getMoreChannelSource,
188188
CollectionNamespace.FromFullName(cursorDocument["ns"].AsString),

src/MongoDB.Driver.Core/Core/Operations/RetryableReadContext.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,11 @@ public static RetryableReadContext Create(IReadBinding binding, bool retryReques
4343
{
4444
context.Initialize(cancellationToken);
4545

46-
if (ChannelPinningHelper.PinChannelSourceAndChannelIfRequired(
46+
ChannelPinningHelper.PinChannellIfRequired(
4747
context.ChannelSource,
4848
context.Channel,
49-
context.Binding.Session,
50-
out var pinnedChannelSource,
51-
out var pinnedChannel))
52-
{
53-
context.ReplaceChannelSource(pinnedChannelSource);
54-
context.ReplaceChannel(pinnedChannel);
55-
}
49+
context.Binding.Session);
50+
5651
return context;
5752
}
5853
catch
@@ -76,16 +71,11 @@ public static async Task<RetryableReadContext> CreateAsync(IReadBinding binding,
7671
{
7772
await context.InitializeAsync(cancellationToken).ConfigureAwait(false);
7873

79-
if (ChannelPinningHelper.PinChannelSourceAndChannelIfRequired(
74+
ChannelPinningHelper.PinChannellIfRequired(
8075
context.ChannelSource,
8176
context.Channel,
82-
context.Binding.Session,
83-
out var pinnedChannelSource,
84-
out var pinnedChannel))
85-
{
86-
context.ReplaceChannelSource(pinnedChannelSource);
87-
context.ReplaceChannel(pinnedChannel);
88-
}
77+
context.Binding.Session);
78+
8979
return context;
9080
}
9181
catch

src/MongoDB.Driver.Core/Core/Operations/RetryableWriteContext.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,10 @@ public static RetryableWriteContext Create(IWriteBinding binding, bool retryRequ
4545
{
4646
context.Initialize(cancellationToken);
4747

48-
if (context.Binding.Session.IsInTransaction &&
49-
ChannelPinningHelper.PinChannelSourceAndChannelIfRequired(
48+
ChannelPinningHelper.PinChannellIfRequired(
5049
context.ChannelSource,
5150
context.Channel,
52-
context.Binding.Session,
53-
out var pinnedChannelSource,
54-
out var pinnedChannel))
55-
{
56-
context.ReplaceChannelSource(pinnedChannelSource);
57-
context.ReplaceChannel(pinnedChannel);
58-
}
51+
context.Binding.Session);
5952

6053
return context;
6154
}
@@ -80,17 +73,10 @@ public static async Task<RetryableWriteContext> CreateAsync(IWriteBinding bindin
8073
{
8174
await context.InitializeAsync(cancellationToken).ConfigureAwait(false);
8275

83-
if (context.Binding.Session.IsInTransaction &&
84-
ChannelPinningHelper.PinChannelSourceAndChannelIfRequired(
76+
ChannelPinningHelper.PinChannellIfRequired(
8577
context.ChannelSource,
8678
context.Channel,
87-
context.Binding.Session,
88-
out var pinnedChannelSource,
89-
out var pinnedChannel))
90-
{
91-
context.ReplaceChannelSource(pinnedChannelSource);
92-
context.ReplaceChannel(pinnedChannel);
93-
}
79+
context.Binding.Session);
9480

9581
return context;
9682
}

0 commit comments

Comments
 (0)