Skip to content

Commit d8aaf49

Browse files
committed
JAVA-459: smooth the latency measurements
1 parent 3e96c2f commit d8aaf49

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ ServerAddress getASecondary( String tagKey, String tagValue ){
179179
continue;
180180
}
181181

182-
long diff = best._pingTime - n._pingTime;
182+
float diff = best._pingTime - n._pingTime;
183183
if ( diff > slaveAcceptableLatencyMS ||
184184
// this is a complex way to make sure we get a random distribution of slaves
185185
( ( badBeforeBest - mybad ) / ( _all.size() - 1 ) ) > _random.nextDouble() )
@@ -235,8 +235,14 @@ synchronized void update(Set<Node> seenNodes){
235235
try {
236236
long start = System.currentTimeMillis();
237237
CommandResult res = _port.runCommand( _mongo.getDB("admin") , _isMasterCmd );
238+
boolean first = (_lastCheck == 0);
238239
_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 );
240246

241247
if ( res == null ){
242248
throw new MongoInternalException("Invalid null value returned from isMaster");
@@ -365,7 +371,7 @@ public void close() {
365371

366372
boolean _ok = false;
367373
long _lastCheck = 0;
368-
long _pingTime = 0;
374+
float _pingTime = 0;
369375

370376
boolean _isMaster = false;
371377
boolean _isSecondary = false;
@@ -533,13 +539,15 @@ public int getMaxBsonObjectSize() {
533539
static int updaterIntervalMS;
534540
static int slaveAcceptableLatencyMS;
535541
static int inetAddrCacheMS;
542+
static float latencySmoothFactor;
536543

537544
static final MongoOptions _mongoOptions = new MongoOptions();
538545

539546
static {
540547
updaterIntervalMS = Integer.parseInt(System.getProperty("com.mongodb.updaterIntervalMS", "5000"));
541548
slaveAcceptableLatencyMS = Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
542549
inetAddrCacheMS = Integer.parseInt(System.getProperty("com.mongodb.inetAddrCacheMS", "300000"));
550+
latencySmoothFactor = Float.parseFloat(System.getProperty("com.mongodb.latencySmoothFactor", "4"));
543551
_mongoOptions.connectTimeout = Integer.parseInt(System.getProperty("com.mongodb.updaterConnectTimeoutMS", "20000"));
544552
_mongoOptions.socketTimeout = Integer.parseInt(System.getProperty("com.mongodb.updaterSocketTimeoutMS", "20000"));
545553
}

0 commit comments

Comments
 (0)