Skip to content

Commit 4deb57a

Browse files
authored
CSHARP-4417: ConnectionId should use longs for LocalValue and ServerValue to match 64-bit integer connectionId returned from the server.(#974)
1 parent d421549 commit 4deb57a

File tree

13 files changed

+82
-53
lines changed

13 files changed

+82
-53
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public sealed class ConnectionId : IEquatable<ConnectionId>
2828
{
2929
// fields
3030
private readonly ServerId _serverId;
31-
private readonly int _localValue;
32-
private readonly int? _serverValue;
31+
private readonly long _localValue;
32+
private readonly long? _serverValue;
3333
private readonly int _hashCode;
3434

3535
// constructors
@@ -38,7 +38,7 @@ public sealed class ConnectionId : IEquatable<ConnectionId>
3838
/// </summary>
3939
/// <param name="serverId">The server identifier.</param>
4040
public ConnectionId(ServerId serverId)
41-
: this(serverId, IdGenerator<ConnectionId>.GetNextId())
41+
: this(serverId, LongIdGenerator<ConnectionId>.GetNextId())
4242
{
4343
}
4444

@@ -47,7 +47,7 @@ public ConnectionId(ServerId serverId)
4747
/// </summary>
4848
/// <param name="serverId">The server identifier.</param>
4949
/// <param name="localValue">The local value.</param>
50-
public ConnectionId(ServerId serverId, int localValue)
50+
public ConnectionId(ServerId serverId, long localValue)
5151
{
5252
_serverId = Ensure.IsNotNull(serverId, nameof(serverId));
5353
_localValue = Ensure.IsGreaterThanOrEqualToZero(localValue, nameof(localValue));
@@ -57,7 +57,7 @@ public ConnectionId(ServerId serverId, int localValue)
5757
.GetHashCode();
5858
}
5959

60-
private ConnectionId(ServerId serverId, int localValue, int serverValue)
60+
private ConnectionId(ServerId serverId, long localValue, long serverValue)
6161
: this(serverId, localValue)
6262
{
6363
_serverValue = Ensure.IsGreaterThanOrEqualToZero(serverValue, nameof(serverValue));
@@ -81,7 +81,19 @@ public ServerId ServerId
8181
/// <value>
8282
/// The local value.
8383
/// </value>
84+
[Obsolete("Use LongLocalValue instead.")]
8485
public int LocalValue
86+
{
87+
get { return (int)_localValue; }
88+
}
89+
90+
/// <summary>
91+
/// Gets the local value.
92+
/// </summary>
93+
/// <value>
94+
/// The local value.
95+
/// </value>
96+
public long LongLocalValue
8597
{
8698
get { return _localValue; }
8799
}
@@ -92,7 +104,19 @@ public int LocalValue
92104
/// <value>
93105
/// The server value.
94106
/// </value>
107+
[Obsolete("Use LongServerValue instead.")]
95108
public int? ServerValue
109+
{
110+
get { return (int)_serverValue; }
111+
}
112+
113+
/// <summary>
114+
/// Gets the server value.
115+
/// </summary>
116+
/// <value>
117+
/// The server value.
118+
/// </value>
119+
public long? LongServerValue
96120
{
97121
get { return _serverValue; }
98122
}
@@ -159,7 +183,7 @@ public override string ToString()
159183
/// </summary>
160184
/// <param name="serverValue">The server value.</param>
161185
/// <returns>A ConnectionId.</returns>
162-
public ConnectionId WithServerValue(int serverValue)
186+
public ConnectionId WithServerValue(long serverValue)
163187
{
164188
return new ConnectionId(_serverId, _localValue, serverValue);
165189
}

src/MongoDB.Driver.Core/Core/Events/Diagnostics/TraceSourceEventHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public static string Label(ClusterId clusterId)
5050

5151
public static string Format(ConnectionId id)
5252
{
53-
if (id.ServerValue.HasValue)
53+
if (id.LongServerValue.HasValue)
5454
{
55-
return id.LocalValue.ToString() + "-" + id.ServerValue.Value.ToString();
55+
return id.LongLocalValue.ToString() + "-" + id.LongServerValue.Value.ToString();
5656
}
57-
return id.LocalValue.ToString();
57+
return id.LongLocalValue.ToString();
5858
}
5959

6060
public static string Format(ServerId serverId)

src/MongoDB.Driver.Core/Core/Logging/StructuredLogTemplateProviders.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,24 @@ public static object[] GetParams(ConnectionId connectionId, object arg1)
131131
{
132132
var (host, port) = connectionId.ServerId.EndPoint.GetHostAndPort();
133133

134-
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LocalValue, host, port, arg1};
134+
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LongLocalValue, host, port, arg1};
135135
}
136136

137137
public static object[] GetParams(ConnectionId connectionId, object arg1, object arg2)
138138
{
139139
var (host, port) = connectionId.ServerId.EndPoint.GetHostAndPort();
140140

141-
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LocalValue, host, port, arg1, arg2 };
141+
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LongLocalValue, host, port, arg1, arg2 };
142142
}
143143

144144
public static object[] GetParamsOmitNull(ConnectionId connectionId, object arg1, object arg2, object arg3, object arg4, object arg5, object arg6, object arg7, object ommitableParam)
145145
{
146146
var (host, port) = connectionId.ServerId.EndPoint.GetHostAndPort();
147147

148148
if (ommitableParam == null)
149-
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LocalValue, host, port, arg1, arg2, arg3, arg4, arg5, arg6, arg7, };
149+
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LongLocalValue, host, port, arg1, arg2, arg3, arg4, arg5, arg6, arg7, };
150150
else
151-
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LocalValue, host, port, arg1, arg2, arg3, arg4, arg5, arg6, arg7, ommitableParam };
151+
return new object[] { connectionId.ServerId.ClusterId.Value, connectionId.LongLocalValue, host, port, arg1, arg2, arg3, arg4, arg5, arg6, arg7, ommitableParam };
152152
}
153153

154154
private static void AddTemplateProvider<TEvent>(LogLevel logLevel, string template, Func<TEvent, EventLogFormattingOptions, object[]> extractor) where TEvent : struct, IEvent =>

src/MongoDB.Driver.Core/Core/Logging/StructuredLogTemplateProvidersCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private static void AddCommandTemplates()
4949
CommandCommonParams(DatabaseName, Command),
5050
(e, o) => GetParamsOmitNull(
5151
e.ConnectionId,
52-
e.ConnectionId.ServerValue,
52+
e.ConnectionId.LongServerValue,
5353
e.RequestId,
5454
e.OperationId,
5555
"Command started",
@@ -64,7 +64,7 @@ private static void AddCommandTemplates()
6464
CommandCommonParams(DurationMS, Reply),
6565
(e, o) => GetParamsOmitNull(
6666
e.ConnectionId,
67-
e.ConnectionId.ServerValue,
67+
e.ConnectionId.LongServerValue,
6868
e.RequestId,
6969
e.OperationId,
7070
"Command succeeded",
@@ -79,7 +79,7 @@ private static void AddCommandTemplates()
7979
CommandCommonParams(DurationMS, Failure),
8080
(e, o) => GetParamsOmitNull(
8181
e.ConnectionId,
82-
e.ConnectionId.ServerValue,
82+
e.ConnectionId.LongServerValue,
8383
e.RequestId,
8484
e.OperationId,
8585
"Command failed",

src/MongoDB.Driver.Core/Core/Servers/RoundTripTimeMonitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private void MonitorServer()
150150
var connectionId = toDispose?.ConnectionId;
151151
toDispose?.Dispose();
152152

153-
_logger?.LogDebug(ex, StructuredLogTemplateProviders.DriverConnectionId_Message, connectionId?.LocalValue, "Monitoring exception");
153+
_logger?.LogDebug(ex, StructuredLogTemplateProviders.DriverConnectionId_Message, connectionId?.LongLocalValue, "Monitoring exception");
154154
}
155155
ThreadHelper.Sleep(_heartbeatInterval, _cancellationToken);
156156
}

src/MongoDB.Driver.Core/MongoCursorNotFoundException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static string FormatMessage(ConnectionId connectionId, long cursorId)
3535
"Cursor {0} not found on server {1} using connection {2}.",
3636
cursorId,
3737
EndPointHelper.ToString(connectionId.ServerId.EndPoint),
38-
connectionId.ServerValue);
38+
connectionId.LongServerValue);
3939
}
4040
#endregion
4141

tests/AstrolabeWorkloadExecutor/AstrolabeEventFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private BsonDocument CreateCmapEventDocument(string eventName, DateTime timestam
121121

122122
private BsonDocument CreateCmapEventDocument(string eventName, DateTime timestamp, ConnectionId connectionId) =>
123123
CreateCmapEventDocument(eventName, timestamp, connectionId.ServerId)
124-
.Add("connectionId", connectionId.LocalValue);
124+
.Add("connectionId", connectionId.LongLocalValue);
125125

126126
private BsonDocument CreateCommandEventDocument(string eventName, DateTime timestamp, string commandName, int requestId) =>
127127
new BsonDocument

tests/MongoDB.Driver.Core.Tests/Core/ConnectionPools/ExclusiveConnectionPoolTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,18 +1343,18 @@ public void Prune_should_respect_generation_when_closing_inUse_connections(
13431343
subject.Clear(true);
13441344

13451345
capturedEvents.WaitForOrThrowIfTimeout(events => events.Count() >= connectionsToBeRemovedCount, TimeSpan.FromSeconds(5));
1346-
var removedConnections = new HashSet<int>(
1346+
var removedConnections = new HashSet<long>(
13471347
capturedEvents
13481348
.Events
13491349
.OfType<ConnectionPoolRemovedConnectionEvent>()
1350-
.Select(c => c.ConnectionId.LocalValue));
1350+
.Select(c => c.ConnectionId.LongLocalValue));
13511351

13521352
foreach (var connection in allConnections)
13531353
{
13541354
connection.IsExpired.Should().BeTrue();
13551355

13561356
removedConnections
1357-
.Contains(connection.ConnectionId.LocalValue)
1357+
.Contains(connection.ConnectionId.LongLocalValue)
13581358
.Should().Be(connection.Generation <= maxExpiredGeneration);
13591359
}
13601360

@@ -1446,14 +1446,14 @@ public void PrunePoolAsync_should_remove_all_expired_connections([RandomSeed] in
14461446
_capturedEvents.Events
14471447
.Take(connectionsCount * 2)
14481448
.OfType<ConnectionPoolRemovingConnectionEvent>()
1449-
.Select(e => e.ConnectionId.LocalValue)
1450-
.ShouldBeEquivalentTo(connectionsExpired.Select(c => c.LocalValue));
1449+
.Select(e => e.ConnectionId.LongLocalValue)
1450+
.ShouldBeEquivalentTo(connectionsExpired.Select(c => c.LongLocalValue));
14511451

14521452
_capturedEvents.Events
14531453
.Take(connectionsCount * 2)
14541454
.OfType<ConnectionPoolRemovedConnectionEvent>()
1455-
.Select(e => e.ConnectionId.LocalValue)
1456-
.ShouldAllBeEquivalentTo(connectionsExpired.Select(c => c.LocalValue));
1455+
.Select(e => e.ConnectionId.LongLocalValue)
1456+
.ShouldAllBeEquivalentTo(connectionsExpired.Select(c => c.LongLocalValue));
14571457
}
14581458

14591459
[Theory]

tests/MongoDB.Driver.Core.Tests/Core/ConnectionPools/MaintenanceHelperTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void Stop_should_trigger_immidiate_maintenace_call(
159159

160160
var maitenanceInPlayTimeout = TimeSpan.FromMilliseconds(50);
161161
eventCapturer.WaitForEventOrThrowIfTimeout<ConnectionPoolAddedConnectionEvent>(maitenanceInPlayTimeout);
162-
eventCapturer.Next().Should().BeOfType<ConnectionPoolAddedConnectionEvent>().Which.ConnectionId.LocalValue.Should().Be(1); // minPoolSize has been enrolled
162+
eventCapturer.Next().Should().BeOfType<ConnectionPoolAddedConnectionEvent>().Which.ConnectionId.LongLocalValue.Should().Be(1); // minPoolSize has been enrolled
163163
eventCapturer.Any().Should().BeFalse();
164164

165165
SpinWait.SpinUntil(() => pool.ConnectionHolder._connections().Count > 0, TimeSpan.FromSeconds(1)).Should().BeTrue(); // wait until connection 1 has been returned to the pool after minPoolSize logic
@@ -168,7 +168,7 @@ public void Stop_should_trigger_immidiate_maintenace_call(
168168
if (checkOutConnection)
169169
{
170170
acquiredConnection = pool.AcquireConnection(CancellationToken.None);
171-
acquiredConnection.ConnectionId.LocalValue.Should().Be(1);
171+
acquiredConnection.ConnectionId.LongLocalValue.Should().Be(1);
172172
}
173173

174174
IncrementGeneration(pool);

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Net;
1818
using FluentAssertions;
1919
using MongoDB.Driver.Core.Clusters;
20-
using MongoDB.Driver.Core.Connections;
2120
using MongoDB.Driver.Core.Servers;
2221
using Xunit;
2322

@@ -74,30 +73,36 @@ public void Equals_should_return_expected_result(
7473
}
7574

7675
[Fact]
77-
public void LocalValues_of_2_ids_should_not_be_the_same_when_automically_constructed()
76+
public void LongLocalValues_of_2_ids_should_not_be_the_same_when_automatically_constructed()
7877
{
7978
var subject = new ConnectionId(__serverId);
8079
var subject2 = new ConnectionId(__serverId);
8180

82-
subject.LocalValue.Should().NotBe(subject2.LocalValue);
81+
subject.LongLocalValue.Should().NotBe(subject2.LongLocalValue);
8382
}
8483

85-
[Fact]
86-
public void LocalValue_should_be_what_was_specified_in_the_constructor()
84+
[Theory]
85+
[InlineData(0)]
86+
[InlineData(int.MaxValue)]
87+
[InlineData((long)int.MaxValue+1)]
88+
public void LongLocalValue_should_be_what_was_specified_in_the_constructor(long localValue)
8789
{
88-
var subject = new ConnectionId(__serverId, 10);
90+
var subject = new ConnectionId(__serverId, localValue);
8991

90-
subject.LocalValue.Should().Be(10);
92+
subject.LongLocalValue.Should().Be(localValue);
9193
}
9294

93-
[Fact]
94-
public void WithServerValue_should_set_the_server_value_and_leave_the_LocalValue_alone()
95+
[Theory]
96+
[InlineData(0)]
97+
[InlineData(int.MaxValue)]
98+
[InlineData((long)int.MaxValue+1)]
99+
public void WithServerValue_should_set_the_server_value_and_leave_the_LocalValue_alone(long serverValue)
95100
{
96101
var subject = new ConnectionId(__serverId, 10)
97-
.WithServerValue(11);
102+
.WithServerValue(serverValue);
98103

99-
subject.LocalValue.Should().Be(10);
100-
subject.ServerValue.Should().Be(11);
104+
subject.LongLocalValue.Should().Be(10);
105+
subject.LongServerValue.Should().Be(serverValue);
101106
}
102107
}
103108
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void InitializeConnection_should_acquire_connectionId_from_hello_response
130130

131131
var sentMessages = connection.GetSentMessages();
132132
sentMessages.Should().HaveCount(1);
133-
result.ConnectionId.ServerValue.Should().Be(1);
133+
result.ConnectionId.LongServerValue.Should().Be(1);
134134
}
135135

136136
[Theory]
@@ -148,7 +148,7 @@ public void InitializeConnection_should_acquire_connectionId_from_legacy_hello_r
148148

149149
var sentMessages = connection.GetSentMessages();
150150
sentMessages.Should().HaveCount(1);
151-
result.ConnectionId.ServerValue.Should().Be(1);
151+
result.ConnectionId.LongServerValue.Should().Be(1);
152152
}
153153

154154
[Theory]
@@ -204,7 +204,7 @@ public void InitializeConnection_with_serverApi_should_send_hello([Values(false,
204204

205205
var result = InitializeConnection(subject, connection, async, CancellationToken.None);
206206

207-
result.ConnectionId.ServerValue.Should().Be(1);
207+
result.ConnectionId.LongServerValue.Should().Be(1);
208208

209209
SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 1, TimeSpan.FromSeconds(5)).Should().BeTrue();
210210

@@ -231,7 +231,7 @@ public void InitializeConnection_without_serverApi_should_send_legacy_hello([Val
231231

232232
var result = InitializeConnection(subject, connection, async, CancellationToken.None);
233233

234-
result.ConnectionId.ServerValue.Should().Be(1);
234+
result.ConnectionId.LongServerValue.Should().Be(1);
235235

236236
SpinWait.SpinUntil(() => connection.GetSentMessages().Count >= 1, TimeSpan.FromSeconds(5)).Should().BeTrue();
237237

@@ -264,7 +264,7 @@ public void InitializeConnection_should_build_the_ConnectionDescription_correctl
264264
var result = InitializeConnection(subject, connection, async, CancellationToken.None);
265265

266266
result.MaxWireVersion.Should().Be(6);
267-
result.ConnectionId.ServerValue.Should().Be(10);
267+
result.ConnectionId.LongServerValue.Should().Be(10);
268268
result.AvailableCompressors.Count.Should().Be(1);
269269
result.AvailableCompressors.Should().Contain(ToCompressorTypeEnum(compressorType));
270270

tests/MongoDB.Driver.Core.Tests/Specifications/connection-monitoring-and-pooling/ConnectionMonitoringAndPoolingTestRunner.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private void AssertEvent(object actualEvent, BsonDocument expectedEvent)
210210
}
211211
else
212212
{
213-
actualEvent.ConnectionId().LocalValue.Should().Be(expectedConnectionId);
213+
actualEvent.ConnectionId().LongLocalValue.Should().Be(expectedConnectionId);
214214
}
215215
}
216216

@@ -618,7 +618,7 @@ private void ParseSettings(
618618

619619
private void ResetConnectionId()
620620
{
621-
IdGeneratorReflector.__lastId(0);
621+
LongIdGeneratorReflector.__lastId(0);
622622
}
623623

624624
private (IConnectionPool, FailPoint, ICluster, Func<object, bool>) SetupConnectionData(BsonDocument test, EventCapturer eventCapturer, bool isUnit)
@@ -691,9 +691,9 @@ private void ResetConnectionId()
691691
{
692692
eventCapturer.WaitForOrThrowIfTimeout(events => events.Any(e => e is ConnectionCreatedEvent), TimeSpan.FromMilliseconds(500));
693693

694-
var connectionIdsToIgnore = new HashSet<int>(eventCapturer.Events
694+
var connectionIdsToIgnore = new HashSet<long>(eventCapturer.Events
695695
.OfType<ConnectionCreatedEvent>()
696-
.Select(c => c.ConnectionId.LocalValue)
696+
.Select(c => c.ConnectionId.LongLocalValue)
697697
.ToList());
698698

699699
eventsFilter = o =>
@@ -704,7 +704,7 @@ or ConnectionCreatedEvent
704704
or ConnectionFailedEvent)
705705
{
706706
var connectionId = o.ConnectionId();
707-
return !connectionIdsToIgnore.Contains(connectionId.LocalValue) &&
707+
return !connectionIdsToIgnore.Contains(connectionId.LongLocalValue) &&
708708
EndPointHelper.Equals(connectionId.ServerId.EndPoint, server.EndPoint);
709709
}
710710

@@ -844,9 +844,9 @@ public static ServerId ServerId(this object @event)
844844
}
845845
}
846846

847-
internal static class IdGeneratorReflector
847+
internal static class LongIdGeneratorReflector
848848
{
849-
public static void __lastId(int value) => Reflector.SetStaticFieldValue(typeof(IdGenerator<ConnectionId>), nameof(__lastId), value);
849+
public static void __lastId(int value) => Reflector.SetStaticFieldValue(typeof(LongIdGenerator<ConnectionId>), nameof(__lastId), value);
850850
}
851851

852852
internal static class IServerReflector

0 commit comments

Comments
 (0)