Skip to content

Commit 1e4304e

Browse files
committed
CSHARP-1834: Make minimum value of MaxStaleness 90 seconds.
1 parent f9af528 commit 1e4304e

File tree

130 files changed

+453
-1153
lines changed

Some content is hidden

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

130 files changed

+453
-1153
lines changed

src/MongoDB.Driver.Core/Core/Clusters/ServerSelectors/ReadPreferenceServerSelector.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -255,42 +255,30 @@ private void EnsureMaxStalenessIsValid(ClusterDescription cluster)
255255
{
256256
if (_maxStaleness.HasValue)
257257
{
258-
TimeSpan heartbeatInterval;
259-
TimeSpan idleWritePeriod;
260-
261-
var primary = SelectPrimary(cluster.Servers).SingleOrDefault();
262-
if (primary != null)
258+
if (_maxStaleness.Value < TimeSpan.FromSeconds(90))
263259
{
264-
heartbeatInterval = primary.HeartbeatInterval;
265-
idleWritePeriod = primary.IdleWritePeriod;
260+
var message = string.Format(
261+
"Max staleness ({0} seconds) must greater than or equal to 90 seconds.",
262+
(int)_maxStaleness.Value.TotalSeconds);
263+
throw new Exception(message);
266264
}
267-
else
268-
{
269-
ServerDescription secondaryWithGreatestLastUpdateTime = null;
270-
foreach (var secondary in SelectSecondaries(cluster.Servers))
271-
{
272-
if (secondaryWithGreatestLastUpdateTime == null || secondary.LastUpdateTimestamp > secondaryWithGreatestLastUpdateTime.LastUpdateTimestamp)
273-
{
274-
secondaryWithGreatestLastUpdateTime = secondary;
275-
}
276-
}
277-
278-
if (secondaryWithGreatestLastUpdateTime == null)
279-
{
280-
return;
281-
}
282265

283-
heartbeatInterval = secondaryWithGreatestLastUpdateTime.HeartbeatInterval;
284-
idleWritePeriod = secondaryWithGreatestLastUpdateTime.IdleWritePeriod;
266+
var anyServer = cluster.Servers.FirstOrDefault();
267+
if (anyServer == null)
268+
{
269+
return;
285270
}
286271

272+
var heartbeatInterval = anyServer.HeartbeatInterval; // all servers have the same HeartbeatInterval
273+
var idleWritePeriod = TimeSpan.FromSeconds(10);
274+
287275
if (_maxStaleness.Value < heartbeatInterval + idleWritePeriod)
288276
{
289277
var message = string.Format(
290-
"Max staleness ({0} ms) must greater than or equal to heartbeat interval ({1} ms) plus idle write period ({2} ms).",
291-
(int)_maxStaleness.Value.TotalMilliseconds,
292-
(int)heartbeatInterval.TotalMilliseconds,
293-
(int)idleWritePeriod.TotalMilliseconds);
278+
"Max staleness ({0} seconds) must greater than or equal to heartbeat interval ({1} seconds) plus idle write period ({2} seconds).",
279+
(int)_maxStaleness.Value.TotalSeconds,
280+
(int)heartbeatInterval.TotalSeconds,
281+
(int)idleWritePeriod.TotalSeconds);
294282
throw new Exception(message);
295283
}
296284
}

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,6 @@ public ElectionId ElectionId
6363
}
6464
}
6565

66-
/// <summary>
67-
/// Gets the idle write period.
68-
/// </summary>
69-
public TimeSpan IdleWritePeriod
70-
{
71-
get
72-
{
73-
BsonValue value;
74-
if (_wrapped.TryGetValue("idleWritePeriodMS", out value))
75-
{
76-
return TimeSpan.FromMilliseconds(value.ToDouble());
77-
}
78-
else
79-
{
80-
return TimeSpan.FromSeconds(10);
81-
}
82-
}
83-
}
84-
8566
/// <summary>
8667
/// Gets a value indicating whether this instance is an arbiter.
8768
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static BsonDocument CreateReadPreferenceDocument(ServerType serverType, R
8181
{
8282
{ "mode", modeString },
8383
{ "tags", tagSets, tagSets != null },
84-
{ "maxStalenessSeconds", () => readPreference.MaxStaleness.Value.TotalSeconds, readPreference.MaxStaleness.HasValue }
84+
{ "maxStalenessSeconds", () => (int)readPreference.MaxStaleness.Value.TotalSeconds, readPreference.MaxStaleness.HasValue }
8585
};
8686
}
8787
}

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public sealed class ServerDescription : IEquatable<ServerDescription>
4040
private readonly EndPoint _endPoint;
4141
private readonly Exception _heartbeatException;
4242
private readonly TimeSpan _heartbeatInterval;
43-
private readonly TimeSpan _idleWritePeriod;
4443
private readonly DateTime _lastUpdateTimestamp;
4544
private readonly DateTime? _lastWriteTimestamp;
4645
private readonly int _maxBatchCount;
@@ -66,7 +65,6 @@ public sealed class ServerDescription : IEquatable<ServerDescription>
6665
/// <param name="electionId">The election identifier.</param>
6766
/// <param name="heartbeatException">The heartbeat exception.</param>
6867
/// <param name="heartbeatInterval">The heartbeat interval.</param>
69-
/// <param name="idleWritePeriod">The idle write period.</param>
7068
/// <param name="lastUpdateTimestamp">The last update timestamp.</param>
7169
/// <param name="lastWriteTimestamp">The last write timestamp.</param>
7270
/// <param name="maxBatchCount">The maximum batch count.</param>
@@ -87,7 +85,6 @@ public ServerDescription(
8785
Optional<ElectionId> electionId = default(Optional<ElectionId>),
8886
Optional<Exception> heartbeatException = default(Optional<Exception>),
8987
Optional<TimeSpan> heartbeatInterval = default(Optional<TimeSpan>),
90-
Optional<TimeSpan> idleWritePeriod = default(Optional<TimeSpan>),
9188
Optional<DateTime> lastUpdateTimestamp = default(Optional<DateTime>),
9289
Optional<DateTime?> lastWriteTimestamp = default(Optional<DateTime?>),
9390
Optional<int> maxBatchCount = default(Optional<int>),
@@ -114,7 +111,6 @@ public ServerDescription(
114111
_endPoint = endPoint;
115112
_heartbeatException = heartbeatException.WithDefault(null);
116113
_heartbeatInterval = heartbeatInterval.WithDefault(TimeSpan.Zero);
117-
_idleWritePeriod = idleWritePeriod.WithDefault(TimeSpan.FromSeconds(10));
118114
_lastUpdateTimestamp = lastUpdateTimestamp.WithDefault(DateTime.UtcNow);
119115
_lastWriteTimestamp = lastWriteTimestamp.WithDefault(null);
120116
_maxBatchCount = maxBatchCount.WithDefault(1000);
@@ -193,17 +189,6 @@ public TimeSpan HeartbeatInterval
193189
get { return _heartbeatInterval; }
194190
}
195191

196-
/// <summary>
197-
/// Gets the idle write period.
198-
/// </summary>
199-
/// <value>
200-
/// The idle write period.
201-
/// </value>
202-
public TimeSpan IdleWritePeriod
203-
{
204-
get { return _idleWritePeriod; }
205-
}
206-
207192
/// <summary>
208193
/// Gets the last update timestamp (when the ServerDescription itself was last updated).
209194
/// </summary>
@@ -369,7 +354,6 @@ public bool Equals(ServerDescription other)
369354
EndPointHelper.Equals(_endPoint, other._endPoint) &&
370355
object.Equals(_heartbeatException, other._heartbeatException) &&
371356
_heartbeatInterval == other._heartbeatInterval &&
372-
_idleWritePeriod == other._idleWritePeriod &&
373357
_lastUpdateTimestamp == other._lastUpdateTimestamp &&
374358
_lastWriteTimestamp == other._lastWriteTimestamp &&
375359
_maxBatchCount == other._maxBatchCount &&
@@ -396,7 +380,6 @@ public override int GetHashCode()
396380
.Hash(_endPoint)
397381
.Hash(_heartbeatException)
398382
.Hash(_heartbeatInterval)
399-
.Hash(_idleWritePeriod)
400383
.Hash(_lastUpdateTimestamp)
401384
.Hash(_lastWriteTimestamp)
402385
.Hash(_maxBatchCount)
@@ -438,7 +421,6 @@ public override string ToString()
438421
/// <param name="electionId">The election identifier.</param>
439422
/// <param name="heartbeatException">The heartbeat exception.</param>
440423
/// <param name="heartbeatInterval">The heartbeat interval.</param>
441-
/// <param name="idleWritePeriod">The idle write period.</param>
442424
/// <param name="lastUpdateTimestamp">The last update timestamp.</param>
443425
/// <param name="lastWriteTimestamp">The last write timestamp.</param>
444426
/// <param name="maxBatchCount">The maximum batch count.</param>
@@ -460,7 +442,6 @@ public ServerDescription With(
460442
Optional<ElectionId> electionId = default(Optional<ElectionId>),
461443
Optional<Exception> heartbeatException = default(Optional<Exception>),
462444
Optional<TimeSpan> heartbeatInterval = default(Optional<TimeSpan>),
463-
Optional<TimeSpan> idleWritePeriod = default(Optional<TimeSpan>),
464445
Optional<DateTime> lastUpdateTimestamp = default(Optional<DateTime>),
465446
Optional<DateTime?> lastWriteTimestamp = default(Optional<DateTime?>),
466447
Optional<int> maxBatchCount = default(Optional<int>),
@@ -485,7 +466,6 @@ public ServerDescription With(
485466
electionId.Replaces(_electionId) ||
486467
heartbeatException.Replaces(_heartbeatException) ||
487468
heartbeatInterval.Replaces(_heartbeatInterval) ||
488-
idleWritePeriod.Replaces(_idleWritePeriod) ||
489469
lastUpdateTimestamp.Replaces(_lastUpdateTimestamp) ||
490470
lastWriteTimestamp.Replaces(_lastWriteTimestamp) ||
491471
maxBatchCount.Replaces(_maxBatchCount) ||
@@ -507,7 +487,6 @@ public ServerDescription With(
507487
electionId: electionId.WithDefault(_electionId),
508488
heartbeatException: heartbeatException.WithDefault(_heartbeatException),
509489
heartbeatInterval: heartbeatInterval.WithDefault(_heartbeatInterval),
510-
idleWritePeriod: idleWritePeriod.WithDefault(_idleWritePeriod),
511490
lastUpdateTimestamp: lastUpdateTimestamp.WithDefault(_lastUpdateTimestamp),
512491
lastWriteTimestamp: lastWriteTimestamp.WithDefault(_lastWriteTimestamp),
513492
maxBatchCount: maxBatchCount.WithDefault(_maxBatchCount),

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ private async Task<bool> HeartbeatAsync(CancellationToken cancellationToken)
177177
canonicalEndPoint: isMasterResult.Me,
178178
electionId: isMasterResult.ElectionId,
179179
lastWriteTimestamp: isMasterResult.LastWriteTimestamp,
180-
idleWritePeriod: isMasterResult.IdleWritePeriod,
181180
maxBatchCount: isMasterResult.MaxBatchCount,
182181
maxDocumentSize: isMasterResult.MaxDocumentSize,
183182
maxMessageSize: isMasterResult.MaxMessageSize,

src/MongoDB.Driver.Core/ReadPreference.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public ReadPreference(
119119
if (maxStaleness.HasValue)
120120
{
121121
Ensure.IsInfiniteOrGreaterThanZero(maxStaleness.Value, nameof(maxStaleness));
122+
if (maxStaleness.Value > TimeSpan.Zero)
123+
{
124+
Ensure.That(maxStaleness.Value.Ticks % TimeSpan.TicksPerMillisecond == 0, "MaxStaleness must not have fractional seconds.", nameof(maxStaleness));
125+
}
122126
Ensure.That(mode != ReadPreferenceMode.Primary, "MaxStaleness cannot be used with ReadPreferenceMode Primary.", nameof(maxStaleness));
123127
}
124128

tests/MongoDB.Driver.Core.Tests.Dotnet/Specifications/max-staleness/tests/ReplicaSetNoPrimary/IdleWritePeriod.json

Lines changed: 0 additions & 81 deletions
This file was deleted.

tests/MongoDB.Driver.Core.Tests.Dotnet/Specifications/max-staleness/tests/ReplicaSetNoPrimary/IdleWritePeriod.yml

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/MongoDB.Driver.Core.Tests.Dotnet/Specifications/max-staleness/tests/ReplicaSetNoPrimary/IdleWritePeriod2.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

tests/MongoDB.Driver.Core.Tests.Dotnet/Specifications/max-staleness/tests/ReplicaSetNoPrimary/Incompatible.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# During server selection,
2-
# clients (drivers or mongos) MUST raise an error if ``maxStalenessSeconds`` is not zero or null,
3-
# and any server's ``maxWireVersion`` is less than 5 (`SERVER-23893`_).
1+
# During server selection, clients (drivers or mongos) MUST raise an error if
2+
# maxStalenessSeconds is defined and not -1 and any server's ``maxWireVersion``
3+
# is less than 5 (`SERVER-23893`_).
44
---
55
topology_description:
66
type: ReplicaSetNoPrimary

0 commit comments

Comments
 (0)