Skip to content

Commit 0fa0995

Browse files
CSHARP-2571: Prefer connectionId from isMaster response to connectionid from getLastError response
1 parent e6b3e5a commit 0fa0995

File tree

3 files changed

+86
-18
lines changed

3 files changed

+86
-18
lines changed

src/MongoDB.Driver.Core/Core/Connections/ConnectionInitializer.cs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,24 @@ public ConnectionDescription InitializeConnection(IConnection connection, Cancel
5353

5454
AuthenticationHelper.Authenticate(connection, description, cancellationToken);
5555

56-
try
56+
var connectionIdServerValue = isMasterResult.ConnectionIdServerValue;
57+
if (connectionIdServerValue.HasValue)
5758
{
58-
var getLastErrorProtocol = CreateGetLastErrorProtocol();
59-
var getLastErrorResult = getLastErrorProtocol.Execute(connection, cancellationToken);
60-
61-
description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
59+
description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
6260
}
63-
catch
61+
else
6462
{
65-
// if we couldn't get the server's connection id, so be it.
63+
try
64+
{
65+
var getLastErrorProtocol = CreateGetLastErrorProtocol();
66+
var getLastErrorResult = getLastErrorProtocol.Execute(connection, cancellationToken);
67+
68+
description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
69+
}
70+
catch
71+
{
72+
// if we couldn't get the server's connection id, so be it.
73+
}
6674
}
6775

6876
return description;
@@ -82,16 +90,26 @@ public async Task<ConnectionDescription> InitializeConnectionAsync(IConnection c
8290

8391
await AuthenticationHelper.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);
8492

85-
try
93+
var connectionIdServerValue = isMasterResult.ConnectionIdServerValue;
94+
if (connectionIdServerValue.HasValue)
8695
{
87-
var getLastErrorProtocol = CreateGetLastErrorProtocol();
88-
var getLastErrorResult = await getLastErrorProtocol.ExecuteAsync(connection, cancellationToken).ConfigureAwait(false);
89-
90-
description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
96+
description = UpdateConnectionIdWithServerValue(description, connectionIdServerValue.Value);
9197
}
92-
catch
98+
else
9399
{
94-
// if we couldn't get the server's connection id, so be it.
100+
try
101+
{
102+
var getLastErrorProtocol = CreateGetLastErrorProtocol();
103+
var getLastErrorResult = await getLastErrorProtocol
104+
.ExecuteAsync(connection, cancellationToken)
105+
.ConfigureAwait(false);
106+
107+
description = UpdateConnectionIdWithServerValue(description, getLastErrorResult);
108+
}
109+
catch
110+
{
111+
// if we couldn't get the server's connection id, so be it.
112+
}
95113
}
96114

97115
return description;
@@ -132,14 +150,20 @@ private BsonDocument CreateInitialIsMasterCommand(IReadOnlyList<IAuthenticator>
132150

133151
private ConnectionDescription UpdateConnectionIdWithServerValue(ConnectionDescription description, BsonDocument getLastErrorResult)
134152
{
135-
BsonValue connectionIdBsonValue;
136-
if (getLastErrorResult.TryGetValue("connectionId", out connectionIdBsonValue))
153+
if (getLastErrorResult.TryGetValue("connectionId", out var connectionIdBsonValue))
137154
{
138-
var connectionId = description.ConnectionId.WithServerValue(connectionIdBsonValue.ToInt32());
139-
description = description.WithConnectionId(connectionId);
155+
description = UpdateConnectionIdWithServerValue(description, connectionIdBsonValue.ToInt32());
140156
}
141157

142158
return description;
143159
}
160+
161+
private ConnectionDescription UpdateConnectionIdWithServerValue(ConnectionDescription description, int serverValue)
162+
{
163+
var connectionId = description.ConnectionId.WithServerValue(serverValue);
164+
description = description.WithConnectionId(connectionId);
165+
166+
return description;
167+
}
144168
}
145169
}

src/MongoDB.Driver.Core/Core/Connections/IsMasterResult.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ public IReadOnlyList<CompressorType> Compressions
6969
}
7070
}
7171

72+
/// <summary>
73+
/// Gets the connection id server value.
74+
/// </summary>
75+
public int? ConnectionIdServerValue
76+
{
77+
get
78+
{
79+
if (_wrapped.TryGetValue("connectionId", out var connectionIdBsonValue))
80+
{
81+
return connectionIdBsonValue.ToInt32();
82+
}
83+
84+
return null;
85+
}
86+
}
87+
7288
/// <summary>
7389
/// Gets the election identifier.
7490
/// </summary>

tests/MongoDB.Driver.Core.Tests/Core/Connections/ConnectionInitializerTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ public ConnectionInitializerTests()
3939
_subject = new ConnectionInitializer("test", new [] { new CompressorConfiguration(CompressorType.Zlib) });
4040
}
4141

42+
[Theory]
43+
[ParameterAttributeData]
44+
public void InitializeConnection_should_acquire_connectionId_from_isMaster_response([Values(false, true)] bool async)
45+
{
46+
var isMasterReply = MessageHelper.BuildReply(
47+
RawBsonDocumentHelper.FromJson("{ ok : 1, connectionId : 1 }"));
48+
var buildInfoReply = MessageHelper.BuildReply(
49+
RawBsonDocumentHelper.FromJson("{ ok : 1, version : \"4.2.0\" }"));
50+
51+
var connection = new MockConnection(__serverId);
52+
connection.EnqueueReplyMessage(isMasterReply);
53+
connection.EnqueueReplyMessage(buildInfoReply);
54+
55+
ConnectionDescription result;
56+
if (async)
57+
{
58+
result = _subject.InitializeConnectionAsync(connection, CancellationToken.None).GetAwaiter().GetResult();
59+
}
60+
else
61+
{
62+
result = _subject.InitializeConnection(connection, CancellationToken.None);
63+
}
64+
65+
var sentMessages = connection.GetSentMessages();
66+
sentMessages.Should().HaveCount(2);
67+
result.ConnectionId.ServerValue.Should().Be(1);
68+
}
69+
4270
[Theory]
4371
[ParameterAttributeData]
4472
public void InitializeConnection_should_throw_an_ArgumentNullException_if_the_connection_is_null(

0 commit comments

Comments
 (0)