Skip to content

Commit fd2161e

Browse files
committed
CSHARP-3721: Disable writes on snapshot sessions
1 parent b947f6e commit fd2161e

15 files changed

+1001
-50
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,9 @@ public static BsonDocument GetReadConcernForFirstCommandInTransaction(ICoreSessi
3333
return ToBsonDocument(session, connectionDescription, readConcern);
3434
}
3535

36-
// private static methods
37-
private static BsonDocument ToBsonDocument(ICoreSession session, ConnectionDescription connectionDescription, ReadConcern readConcern)
36+
public static BsonDocument GetReadConcernForSnapshotSesssion(ICoreSession session, ConnectionDescription connectionDescription)
3837
{
39-
var sessionsAreSupported = connectionDescription.IsMasterResult.LogicalSessionTimeout != null || connectionDescription.ServiceId.HasValue;
40-
41-
// snapshot
42-
if (sessionsAreSupported && session.IsSnapshot)
38+
if (AreSessionsSupported(connectionDescription) && session.IsSnapshot)
4339
{
4440
Feature.SnapshotReads.ThrowIfNotSupported(connectionDescription.ServerVersion);
4541

@@ -48,11 +44,18 @@ private static BsonDocument ToBsonDocument(ICoreSession session, ConnectionDescr
4844
{
4945
readConcernDocument.Add("atClusterTime", session.SnapshotTime);
5046
}
47+
5148
return readConcernDocument;
5249
}
5350

51+
return null;
52+
}
53+
54+
// private static methods
55+
private static BsonDocument ToBsonDocument(ICoreSession session, ConnectionDescription connectionDescription, ReadConcern readConcern)
56+
{
5457
// causal consistency
55-
var shouldSendAfterClusterTime = sessionsAreSupported && session.IsCausallyConsistent && session.OperationTime != null;
58+
var shouldSendAfterClusterTime = AreSessionsSupported(connectionDescription) && session.IsCausallyConsistent && session.OperationTime != null;
5659
var shouldSendReadConcern = !readConcern.IsServerDefault || shouldSendAfterClusterTime;
5760

5861
if (shouldSendReadConcern)
@@ -67,5 +70,8 @@ private static BsonDocument ToBsonDocument(ICoreSession session, ConnectionDescr
6770

6871
return null;
6972
}
73+
74+
private static bool AreSessionsSupported(ConnectionDescription connectionDescription) =>
75+
connectionDescription?.IsMasterResult.LogicalSessionTimeout != null || connectionDescription?.ServiceId != null;
7076
}
7177
}

src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingCommandMessageWireProtocol.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ private Type0CommandMessageSection<BsonDocument> CreateType0Section(ConnectionDe
351351
}
352352
}
353353

354+
355+
var snapshotReadConcernDocument = ReadConcernHelper.GetReadConcernForSnapshotSesssion(_session, connectionDescription);
356+
if (snapshotReadConcernDocument != null)
357+
{
358+
extraElements.Add(new BsonElement("readConcern", snapshotReadConcernDocument));
359+
}
360+
354361
if (_session.ClusterTime != null)
355362
{
356363
AddIfNotAlreadyAdded("$clusterTime", _session.ClusterTime);

0 commit comments

Comments
 (0)