@@ -179,7 +179,7 @@ ServerAddress getASecondary( String tagKey, String tagValue ){
179
179
continue ;
180
180
}
181
181
182
- long diff = best ._pingTime - n ._pingTime ;
182
+ float diff = best ._pingTime - n ._pingTime ;
183
183
if ( diff > slaveAcceptableLatencyMS ||
184
184
// this is a complex way to make sure we get a random distribution of slaves
185
185
( ( badBeforeBest - mybad ) / ( _all .size () - 1 ) ) > _random .nextDouble () )
@@ -235,8 +235,14 @@ synchronized void update(Set<Node> seenNodes){
235
235
try {
236
236
long start = System .currentTimeMillis ();
237
237
CommandResult res = _port .runCommand ( _mongo .getDB ("admin" ) , _isMasterCmd );
238
+ boolean first = (_lastCheck == 0 );
238
239
_lastCheck = System .currentTimeMillis ();
239
- _pingTime = _lastCheck - start ;
240
+ float newPing = _lastCheck - start ;
241
+ if (first )
242
+ _pingTime = newPing ;
243
+ else
244
+ _pingTime = _pingTime + ((newPing - _pingTime ) / latencySmoothFactor );
245
+ _rootLogger .log ( Level .FINE , "Latency to " + _addr + " actual=" + newPing + " smoothed=" + _pingTime );
240
246
241
247
if ( res == null ){
242
248
throw new MongoInternalException ("Invalid null value returned from isMaster" );
@@ -365,7 +371,7 @@ public void close() {
365
371
366
372
boolean _ok = false ;
367
373
long _lastCheck = 0 ;
368
- long _pingTime = 0 ;
374
+ float _pingTime = 0 ;
369
375
370
376
boolean _isMaster = false ;
371
377
boolean _isSecondary = false ;
@@ -533,13 +539,15 @@ public int getMaxBsonObjectSize() {
533
539
static int updaterIntervalMS ;
534
540
static int slaveAcceptableLatencyMS ;
535
541
static int inetAddrCacheMS ;
542
+ static float latencySmoothFactor ;
536
543
537
544
static final MongoOptions _mongoOptions = new MongoOptions ();
538
545
539
546
static {
540
547
updaterIntervalMS = Integer .parseInt (System .getProperty ("com.mongodb.updaterIntervalMS" , "5000" ));
541
548
slaveAcceptableLatencyMS = Integer .parseInt (System .getProperty ("com.mongodb.slaveAcceptableLatencyMS" , "15" ));
542
549
inetAddrCacheMS = Integer .parseInt (System .getProperty ("com.mongodb.inetAddrCacheMS" , "300000" ));
550
+ latencySmoothFactor = Float .parseFloat (System .getProperty ("com.mongodb.latencySmoothFactor" , "4" ));
543
551
_mongoOptions .connectTimeout = Integer .parseInt (System .getProperty ("com.mongodb.updaterConnectTimeoutMS" , "20000" ));
544
552
_mongoOptions .socketTimeout = Integer .parseInt (System .getProperty ("com.mongodb.updaterSocketTimeoutMS" , "20000" ));
545
553
}
0 commit comments