@@ -34,13 +34,15 @@ public sealed class ServerDescription : IEquatable<ServerDescription>
34
34
private readonly EndPoint _endPoint ;
35
35
private readonly Exception _heartbeatException ;
36
36
private readonly TimeSpan _heartbeatInterval ;
37
+ private readonly DateTime ? _lastHeartbeatTimestamp ;
37
38
private readonly DateTime _lastUpdateTimestamp ;
38
39
private readonly DateTime ? _lastWriteTimestamp ;
39
40
private readonly TimeSpan ? _logicalSessionTimeout ;
40
41
private readonly int _maxBatchCount ;
41
42
private readonly int _maxDocumentSize ;
42
43
private readonly int _maxMessageSize ;
43
44
private readonly int _maxWireDocumentSize ;
45
+ private readonly string _reasonChanged ;
44
46
private readonly ReplicaSetConfig _replicaSetConfig ;
45
47
private readonly ServerId _serverId ;
46
48
private readonly ServerState _state ;
@@ -55,11 +57,13 @@ public sealed class ServerDescription : IEquatable<ServerDescription>
55
57
/// </summary>
56
58
/// <param name="serverId">The server identifier.</param>
57
59
/// <param name="endPoint">The end point.</param>
60
+ /// <param name="reasonChanged">The reason the server description was last changed.</param>
58
61
/// <param name="averageRoundTripTime">The average round trip time.</param>
59
62
/// <param name="canonicalEndPoint">The canonical end point.</param>
60
63
/// <param name="electionId">The election identifier.</param>
61
64
/// <param name="heartbeatException">The heartbeat exception.</param>
62
65
/// <param name="heartbeatInterval">The heartbeat interval.</param>
66
+ /// <param name="lastHeartbeatTimestamp">The last heartbeat timestamp.</param>
63
67
/// <param name="lastUpdateTimestamp">The last update timestamp.</param>
64
68
/// <param name="lastWriteTimestamp">The last write timestamp.</param>
65
69
/// <param name="logicalSessionTimeout">The logical session timeout.</param>
@@ -77,11 +81,13 @@ public sealed class ServerDescription : IEquatable<ServerDescription>
77
81
public ServerDescription (
78
82
ServerId serverId ,
79
83
EndPoint endPoint ,
84
+ Optional < string > reasonChanged = default ( Optional < string > ) ,
80
85
Optional < TimeSpan > averageRoundTripTime = default ( Optional < TimeSpan > ) ,
81
86
Optional < EndPoint > canonicalEndPoint = default ( Optional < EndPoint > ) ,
82
87
Optional < ElectionId > electionId = default ( Optional < ElectionId > ) ,
83
88
Optional < Exception > heartbeatException = default ( Optional < Exception > ) ,
84
89
Optional < TimeSpan > heartbeatInterval = default ( Optional < TimeSpan > ) ,
90
+ Optional < DateTime ? > lastHeartbeatTimestamp = default ( Optional < DateTime ? > ) ,
85
91
Optional < DateTime > lastUpdateTimestamp = default ( Optional < DateTime > ) ,
86
92
Optional < DateTime ? > lastWriteTimestamp = default ( Optional < DateTime ? > ) ,
87
93
Optional < TimeSpan ? > logicalSessionTimeout = default ( Optional < TimeSpan ? > ) ,
@@ -109,13 +115,15 @@ public ServerDescription(
109
115
_endPoint = endPoint ;
110
116
_heartbeatException = heartbeatException . WithDefault ( null ) ;
111
117
_heartbeatInterval = heartbeatInterval . WithDefault ( TimeSpan . Zero ) ;
118
+ _lastHeartbeatTimestamp = lastHeartbeatTimestamp . WithDefault ( null ) ;
112
119
_lastUpdateTimestamp = lastUpdateTimestamp . WithDefault ( DateTime . UtcNow ) ;
113
120
_lastWriteTimestamp = lastWriteTimestamp . WithDefault ( null ) ;
114
121
_logicalSessionTimeout = logicalSessionTimeout . WithDefault ( null ) ;
115
122
_maxBatchCount = maxBatchCount . WithDefault ( 1000 ) ;
116
123
_maxDocumentSize = maxDocumentSize . WithDefault ( 4 * 1024 * 1024 ) ;
117
124
_maxMessageSize = maxMessageSize . WithDefault ( Math . Max ( _maxDocumentSize + 1024 , 16000000 ) ) ;
118
125
_maxWireDocumentSize = maxWireDocumentSize . WithDefault ( _maxDocumentSize + 16 * 1024 ) ;
126
+ _reasonChanged = reasonChanged . WithDefault ( "NotSpecified" ) ;
119
127
_replicaSetConfig = replicaSetConfig . WithDefault ( null ) ;
120
128
_serverId = serverId ;
121
129
_state = state . WithDefault ( ServerState . Disconnected ) ;
@@ -223,6 +231,17 @@ public bool IsDataBearing
223
231
}
224
232
}
225
233
234
+ /// <summary>
235
+ /// Gets the last heartbeat timestamp.
236
+ /// </summary>
237
+ /// <value>
238
+ /// The last heartbeat timestamp.
239
+ /// </value>
240
+ public DateTime ? LastHeartbeatTimestamp
241
+ {
242
+ get { return _lastHeartbeatTimestamp ; }
243
+ }
244
+
226
245
/// <summary>
227
246
/// Gets the last update timestamp (when the ServerDescription itself was last updated).
228
247
/// </summary>
@@ -300,6 +319,12 @@ public int MaxWireDocumentSize
300
319
get { return _maxWireDocumentSize ; }
301
320
}
302
321
322
+ /// <summary>
323
+ /// The reason the server description was last changed.
324
+ /// </summary>
325
+ /// <value>The reason the server description was last changed.</value>
326
+ public string ReasonChanged => _reasonChanged ;
327
+
303
328
/// <summary>
304
329
/// Gets the replica set configuration.
305
330
/// </summary>
@@ -399,13 +424,15 @@ public bool Equals(ServerDescription other)
399
424
EndPointHelper . Equals ( _endPoint , other . _endPoint ) &&
400
425
object . Equals ( _heartbeatException , other . _heartbeatException ) &&
401
426
_heartbeatInterval == other . _heartbeatInterval &&
427
+ _lastHeartbeatTimestamp == other . LastHeartbeatTimestamp &&
402
428
_lastUpdateTimestamp == other . _lastUpdateTimestamp &&
403
429
_lastWriteTimestamp == other . _lastWriteTimestamp &&
404
430
_logicalSessionTimeout == other . _logicalSessionTimeout &&
405
431
_maxBatchCount == other . _maxBatchCount &&
406
432
_maxDocumentSize == other . _maxDocumentSize &&
407
433
_maxMessageSize == other . _maxMessageSize &&
408
434
_maxWireDocumentSize == other . _maxWireDocumentSize &&
435
+ _reasonChanged . Equals ( other . _reasonChanged , StringComparison . Ordinal ) &&
409
436
object . Equals ( _replicaSetConfig , other . _replicaSetConfig ) &&
410
437
_serverId . Equals ( other . _serverId ) &&
411
438
_state == other . _state &&
@@ -426,13 +453,15 @@ public override int GetHashCode()
426
453
. Hash ( _endPoint )
427
454
. Hash ( _heartbeatException )
428
455
. Hash ( _heartbeatInterval )
456
+ . Hash ( _lastHeartbeatTimestamp )
429
457
. Hash ( _lastUpdateTimestamp )
430
458
. Hash ( _lastWriteTimestamp )
431
459
. Hash ( _logicalSessionTimeout )
432
460
. Hash ( _maxBatchCount )
433
461
. Hash ( _maxDocumentSize )
434
462
. Hash ( _maxMessageSize )
435
463
. Hash ( _maxWireDocumentSize )
464
+ . Hash ( _reasonChanged )
436
465
. Hash ( _replicaSetConfig )
437
466
. Hash ( _serverId )
438
467
. Hash ( _state )
@@ -450,12 +479,14 @@ public override string ToString()
450
479
. Append ( "{ " )
451
480
. AppendFormat ( "ServerId: \" {0}\" " , _serverId )
452
481
. AppendFormat ( ", EndPoint: \" {0}\" " , _endPoint )
482
+ . AppendFormat ( ", ReasonChanged: \" {0}\" " , _reasonChanged )
453
483
. AppendFormat ( ", State: \" {0}\" " , _state )
454
484
. AppendFormat ( ", Type: \" {0}\" " , _type )
455
485
. AppendFormatIf ( _tags != null && ! _tags . IsEmpty , ", Tags: \" {0}\" " , _tags )
456
486
. AppendFormatIf ( _state == ServerState . Connected , ", WireVersionRange: \" {0}\" " , _wireVersionRange )
457
487
. AppendFormatIf ( _electionId != null , ", ElectionId: \" {0}\" " , _electionId )
458
488
. AppendFormatIf ( _heartbeatException != null , ", HeartbeatException: \" {0}\" " , _heartbeatException )
489
+ . AppendFormat ( ", LastHeartbeatTimestamp: {0}" , _lastHeartbeatTimestamp . HasValue ? "\" " + LastHeartbeatTimestamp . Value . ToString ( "yyyy-MM-ddTHH:mm:ss.fffffffK" ) + "\" " : "null" )
459
490
. AppendFormat ( ", LastUpdateTimestamp: \" {0:yyyy-MM-ddTHH:mm:ss.fffffffK}\" " , _lastUpdateTimestamp )
460
491
. Append ( " }" )
461
492
. ToString ( ) ;
@@ -464,11 +495,13 @@ public override string ToString()
464
495
/// <summary>
465
496
/// Returns a new instance of ServerDescription with some values changed.
466
497
/// </summary>
498
+ /// <param name="reasonChanged">The reason the server description changed.</param>
467
499
/// <param name="averageRoundTripTime">The average round trip time.</param>
468
500
/// <param name="canonicalEndPoint">The canonical end point.</param>
469
501
/// <param name="electionId">The election identifier.</param>
470
502
/// <param name="heartbeatException">The heartbeat exception.</param>
471
503
/// <param name="heartbeatInterval">The heartbeat interval.</param>
504
+ /// <param name="lastHeartbeatTimestamp">The last heartbeat timestamp.</param>
472
505
/// <param name="lastUpdateTimestamp">The last update timestamp.</param>
473
506
/// <param name="lastWriteTimestamp">The last write timestamp.</param>
474
507
/// <param name="logicalSessionTimeout">The logical session timeout.</param>
@@ -486,11 +519,13 @@ public override string ToString()
486
519
/// A new instance of ServerDescription.
487
520
/// </returns>
488
521
public ServerDescription With (
522
+ Optional < string > reasonChanged = default ( Optional < string > ) ,
489
523
Optional < TimeSpan > averageRoundTripTime = default ( Optional < TimeSpan > ) ,
490
524
Optional < EndPoint > canonicalEndPoint = default ( Optional < EndPoint > ) ,
491
525
Optional < ElectionId > electionId = default ( Optional < ElectionId > ) ,
492
526
Optional < Exception > heartbeatException = default ( Optional < Exception > ) ,
493
527
Optional < TimeSpan > heartbeatInterval = default ( Optional < TimeSpan > ) ,
528
+ Optional < DateTime ? > lastHeartbeatTimestamp = default ( Optional < DateTime ? > ) ,
494
529
Optional < DateTime > lastUpdateTimestamp = default ( Optional < DateTime > ) ,
495
530
Optional < DateTime ? > lastWriteTimestamp = default ( Optional < DateTime ? > ) ,
496
531
Optional < TimeSpan ? > logicalSessionTimeout = default ( Optional < TimeSpan ? > ) ,
@@ -508,11 +543,13 @@ public ServerDescription With(
508
543
return new ServerDescription (
509
544
_serverId ,
510
545
_endPoint ,
546
+ reasonChanged ,
511
547
averageRoundTripTime : averageRoundTripTime . WithDefault ( _averageRoundTripTime ) ,
512
548
canonicalEndPoint : canonicalEndPoint . WithDefault ( _canonicalEndPoint ) ,
513
549
electionId : electionId . WithDefault ( _electionId ) ,
514
550
heartbeatException : heartbeatException . WithDefault ( _heartbeatException ) ,
515
551
heartbeatInterval : heartbeatInterval . WithDefault ( _heartbeatInterval ) ,
552
+ lastHeartbeatTimestamp : lastHeartbeatTimestamp . WithDefault ( _lastHeartbeatTimestamp ) ,
516
553
lastUpdateTimestamp : lastUpdateTimestamp . WithDefault ( DateTime . UtcNow ) ,
517
554
lastWriteTimestamp : lastWriteTimestamp . WithDefault ( _lastWriteTimestamp ) ,
518
555
logicalSessionTimeout : logicalSessionTimeout . WithDefault ( _logicalSessionTimeout ) ,
@@ -540,11 +577,13 @@ public ServerDescription WithHeartbeatException(Exception heartbeatException)
540
577
return new ServerDescription (
541
578
_serverId ,
542
579
_endPoint ,
580
+ reasonChanged : "HeartbeatFailed" ,
543
581
averageRoundTripTime : _averageRoundTripTime ,
544
582
canonicalEndPoint : _canonicalEndPoint ,
545
583
electionId : _electionId ,
546
584
heartbeatException : heartbeatException ,
547
585
heartbeatInterval : _heartbeatInterval ,
586
+ lastHeartbeatTimestamp : DateTime . UtcNow ,
548
587
lastUpdateTimestamp : DateTime . UtcNow ,
549
588
lastWriteTimestamp : _lastWriteTimestamp ,
550
589
logicalSessionTimeout : _logicalSessionTimeout ,
0 commit comments