Skip to content

Commit dcb0c90

Browse files
committed
JAVA-1207: Fixed log message to correctly convert nanos to millis. Also stopped using the word "ping" and changed to "latency", so it is clear the driver is not actually using the ping protocol.
1 parent 65e8e30 commit dcb0c90

9 files changed

+40
-37
lines changed

src/main/com/mongodb/LatencyMinimizingServerSelector.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class LatencyMinimizingServerSelector implements ServerSelector {
3434

3535
@Override
3636
public List<ServerDescription> choose(final ClusterDescription clusterDescription) {
37-
return getServersWithAcceptableLatencyDifference(clusterDescription.getAll(), getBestPingTimeNanos(clusterDescription.getAll()));
37+
return getServersWithAcceptableLatencyDifference(clusterDescription.getAll(), getBestLatencyNanos(clusterDescription.getAll()));
3838
}
3939

4040
@Override
@@ -44,14 +44,14 @@ public String toString() {
4444
+ '}';
4545
}
4646

47-
private long getBestPingTimeNanos(final Set<ServerDescription> members) {
47+
private long getBestLatencyNanos(final Set<ServerDescription> members) {
4848
long bestPingTime = Long.MAX_VALUE;
4949
for (final ServerDescription cur : members) {
5050
if (!cur.isOk()) {
5151
continue;
5252
}
53-
if (cur.getAveragePingTimeNanos() < bestPingTime) {
54-
bestPingTime = cur.getAveragePingTimeNanos();
53+
if (cur.getAverageLatencyNanos() < bestPingTime) {
54+
bestPingTime = cur.getAverageLatencyNanos();
5555
}
5656
}
5757
return bestPingTime;
@@ -64,7 +64,7 @@ private List<ServerDescription> getServersWithAcceptableLatencyDifference(final
6464
if (!cur.isOk()) {
6565
continue;
6666
}
67-
if (cur.getAveragePingTimeNanos() - acceptableLatencyDifferenceNanos <= bestPingTime) {
67+
if (cur.getAverageLatencyNanos() - acceptableLatencyDifferenceNanos <= bestPingTime) {
6868
goodSecondaries.add(cur);
6969
}
7070
}

src/main/com/mongodb/MongosHAServerSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public List<ServerDescription> choose(final ClusterDescription clusterDescriptio
4343
}
4444
ServerDescription fastestServer = null;
4545
for (ServerDescription cur : clusterDescription.getAny()) {
46-
if (fastestServer == null || cur.getAveragePingTimeNanos() < fastestServer.getAveragePingTimeNanos()) {
46+
if (fastestServer == null || cur.getAverageLatencyNanos() < fastestServer.getAverageLatencyNanos()) {
4747
fastestServer = cur;
4848
}
4949
}

src/main/com/mongodb/ServerDescription.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.bson.util.annotations.Immutable;
2020

21+
import java.text.DecimalFormat;
2122
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.Map;
@@ -32,8 +33,6 @@
3233
import static com.mongodb.ServerType.ShardRouter;
3334
import static com.mongodb.ServerType.StandAlone;
3435
import static com.mongodb.ServerType.Unknown;
35-
import static java.util.concurrent.TimeUnit.MILLISECONDS;
36-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
3736
import static org.bson.util.Assertions.notNull;
3837

3938

@@ -64,7 +63,7 @@ class ServerDescription {
6463
private final int maxMessageSize;
6564
private final Tags tags;
6665
private final String setName;
67-
private final long averagePingTimeNanos;
66+
private final long averageLatencyNanos;
6867
private final boolean ok;
6968
private final ServerConnectionState state;
7069
private final ServerVersion version;
@@ -86,7 +85,7 @@ static class Builder {
8685
private Tags tags = Tags.freeze(new Tags());
8786
private String setName;
8887
private Integer setVersion;
89-
private long averagePingTimeNanos;
88+
private long averageLatency;
9089
private boolean ok;
9190
private ServerConnectionState state;
9291
private ServerVersion version = new ServerVersion();
@@ -144,8 +143,8 @@ public Builder tags(final Tags tags) {
144143
return this;
145144
}
146145

147-
public Builder averagePingTime(final long averagePingTime, final TimeUnit timeUnit) {
148-
this.averagePingTimeNanos = timeUnit.toNanos(averagePingTime);
146+
public Builder averageLatency(final long averageLatency, final TimeUnit timeUnit) {
147+
this.averageLatency = timeUnit.toNanos(averageLatency);
149148
return this;
150149
}
151150

@@ -355,8 +354,8 @@ public Integer getSetVersion() {
355354
return setVersion;
356355
}
357356

358-
public long getAveragePingTimeNanos() {
359-
return averagePingTimeNanos;
357+
public long getAverageLatencyNanos() {
358+
return averageLatencyNanos;
360359
}
361360

362361
/**
@@ -469,7 +468,7 @@ public String toString() {
469468
+ ", tags=" + tags
470469
+ ", setName='" + setName + '\''
471470
+ ", setVersion='" + setVersion + '\''
472-
+ ", averagePingTimeNanos=" + averagePingTimeNanos
471+
+ ", averageLatencyNanos=" + averageLatencyNanos
473472
+ ", ok=" + ok
474473
+ ", state=" + state
475474
+ ", version=" + version
@@ -483,11 +482,15 @@ public String getShortDescription() {
483482
+ "address=" + address
484483
+ ", type=" + type
485484
+ (tags.isEmpty() ? "" : tags)
486-
+ (state == Connected ? (", averagePingTime=" + NANOSECONDS.convert(averagePingTimeNanos, MILLISECONDS) + " ms") : "")
485+
+ (state == Connected ? (", averageLatency=" + getAverageLatencyFormattedInMilliseconds() + " ms") : "")
487486
+ ", state=" + state
488487
+ '}';
489488
}
490489

490+
private String getAverageLatencyFormattedInMilliseconds() {
491+
return new DecimalFormat("#0.0").format(averageLatencyNanos / 1000.0 / 1000.0);
492+
}
493+
491494
ServerDescription(final Builder builder) {
492495
address = notNull("address", builder.address);
493496
type = notNull("type", builder.type);
@@ -503,7 +506,7 @@ public String getShortDescription() {
503506
tags = builder.tags;
504507
setName = builder.setName;
505508
setVersion = builder.setVersion;
506-
averagePingTimeNanos = builder.averagePingTimeNanos;
509+
averageLatencyNanos = builder.averageLatency;
507510
ok = builder.ok;
508511
minWireVersion = builder.minWireVersion;
509512
maxWireVersion = builder.maxWireVersion;

src/main/com/mongodb/ServerStateNotifier.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void run() {
9595

9696
if (!isClosed) {
9797
try {
98-
// Note that the ServerDescription.equals method does not include the average ping time as part of the comparison,
98+
// Note that the ServerDescription.equals method does not include the average latency as part of the comparison,
9999
// so this will not spam the logs too hard.
100100
if (!currentServerDescription.equals(serverDescription)) {
101101
if (throwable != null) {
@@ -141,7 +141,7 @@ private ServerDescription lookupServerDescription() throws IOException {
141141

142142
@SuppressWarnings("unchecked")
143143
private ServerDescription createDescription(final CommandResult commandResult, final CommandResult buildInfoResult,
144-
final long averagePingTimeNanos) {
144+
final long averageLatencyNanos) {
145145
return ServerDescription.builder()
146146
.state(ServerConnectionState.Connected)
147147
.version(getVersion(buildInfoResult))
@@ -160,7 +160,7 @@ private ServerDescription createDescription(final CommandResult commandResult, f
160160
.setVersion((Integer) commandResult.get("setVersion"))
161161
.minWireVersion(commandResult.getInt("minWireVersion", ServerDescription.getDefaultMinWireVersion()))
162162
.maxWireVersion(commandResult.getInt("maxWireVersion", ServerDescription.getDefaultMaxWireVersion()))
163-
.averagePingTime(averagePingTimeNanos, TimeUnit.NANOSECONDS)
163+
.averageLatency(averageLatencyNanos, TimeUnit.NANOSECONDS)
164164
.ok(commandResult.ok()).build();
165165
}
166166

src/test/com/mongodb/CompositeServerSelectorTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ public void setUp() throws UnknownHostException {
4040
.state(Connected)
4141
.address(new ServerAddress())
4242
.ok(true)
43-
.averagePingTime(5, MILLISECONDS)
43+
.averageLatency(5, MILLISECONDS)
4444
.type(ServerType.ReplicaSetPrimary)
4545
.build();
4646

4747
second = ServerDescription.builder()
4848
.state(Connected)
4949
.address(new ServerAddress("localhost:27018"))
5050
.ok(true)
51-
.averagePingTime(30, MILLISECONDS)
51+
.averageLatency(30, MILLISECONDS)
5252
.type(ServerType.ReplicaSetSecondary)
5353
.build();
5454

5555
third = ServerDescription.builder()
5656
.state(Connected)
5757
.address(new ServerAddress("localhost:27019"))
5858
.ok(true)
59-
.averagePingTime(35, MILLISECONDS)
59+
.averageLatency(35, MILLISECONDS)
6060
.type(ServerType.ReplicaSetSecondary)
6161
.build();
6262
}

src/test/com/mongodb/LatencyMinimizingServerSelectorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,28 @@ public void testLatencyDifferentialMinimization() throws UnknownHostException {
3838
.address(new ServerAddress())
3939
.ok(true)
4040
.type(ServerType.ReplicaSetPrimary)
41-
.averagePingTime(10, TimeUnit.MILLISECONDS)
41+
.averageLatency(10, TimeUnit.MILLISECONDS)
4242
.build();
4343
final ServerDescription secondaryOne = ServerDescription.builder()
4444
.state(Connected)
4545
.address(new ServerAddress("localhost:27018"))
4646
.ok(true)
4747
.type(ServerType.ReplicaSetSecondary)
48-
.averagePingTime(15, TimeUnit.MILLISECONDS)
48+
.averageLatency(15, TimeUnit.MILLISECONDS)
4949
.build();
5050
final ServerDescription secondaryTwo = ServerDescription.builder()
5151
.state(Connected)
5252
.address(new ServerAddress("localhost:27019"))
5353
.ok(true)
5454
.type(ServerType.ReplicaSetSecondary)
55-
.averagePingTime(31, TimeUnit.MILLISECONDS)
55+
.averageLatency(31, TimeUnit.MILLISECONDS)
5656
.build();
5757
final ServerDescription secondaryThree = ServerDescription.builder()
5858
.state(Connected)
5959
.address(new ServerAddress("localhost:27020"))
6060
.ok(true)
6161
.type(ServerType.ReplicaSetSecondary)
62-
.averagePingTime(30, TimeUnit.MILLISECONDS)
62+
.averageLatency(30, TimeUnit.MILLISECONDS)
6363
.build();
6464
assertEquals(Arrays.asList(primary, secondaryOne, secondaryThree),
6565
selector.choose(new ClusterDescription(Multiple, ReplicaSet,
@@ -74,14 +74,14 @@ public void testZeroLatencyDifferentialTolerance() throws UnknownHostException {
7474
.address(new ServerAddress())
7575
.ok(true)
7676
.type(ServerType.ReplicaSetPrimary)
77-
.averagePingTime(10, TimeUnit.NANOSECONDS)
77+
.averageLatency(10, TimeUnit.NANOSECONDS)
7878
.build();
7979
final ServerDescription secondaryOne = ServerDescription.builder()
8080
.state(Connected)
8181
.address(new ServerAddress("localhost:27018"))
8282
.ok(true)
8383
.type(ServerType.ReplicaSetSecondary)
84-
.averagePingTime(11, TimeUnit.NANOSECONDS)
84+
.averageLatency(11, TimeUnit.NANOSECONDS)
8585
.build();
8686
assertEquals(Arrays.asList(primary), selector.choose(new ClusterDescription(Multiple, ReplicaSet,
8787
Arrays.asList(primary, secondaryOne))));

src/test/com/mongodb/MongosHAServerSelectorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void setUp() throws UnknownHostException {
4848
.state(Connected)
4949
.address(new ServerAddress())
5050
.ok(true)
51-
.averagePingTime(10, MILLISECONDS)
51+
.averageLatency(10, MILLISECONDS)
5252
.type(ServerType.ShardRouter)
5353
.build();
5454

@@ -63,7 +63,7 @@ public void setUp() throws UnknownHostException {
6363
.state(Connected)
6464
.address(new ServerAddress("localhost:27018"))
6565
.ok(true)
66-
.averagePingTime(8, MILLISECONDS)
66+
.averageLatency(8, MILLISECONDS)
6767
.type(ServerType.ShardRouter)
6868
.build();
6969
}

src/test/com/mongodb/ReadPreferenceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,28 @@ public void setUp() throws IOException {
5454
final long unacceptablePingTime = bestPingTime + acceptableLatencyMS + 1;
5555

5656
primary = ServerDescription.builder().state(Connected).address(new ServerAddress(HOST, 27017))
57-
.averagePingTime(acceptablePingTime * 1000000L, NANOSECONDS)
57+
.averageLatency(acceptablePingTime * 1000000L, NANOSECONDS)
5858
.ok(true)
5959
.type(ServerType.ReplicaSetPrimary)
6060
.tags(tags1)
6161
.maxDocumentSize(FOUR_MEG).build();
6262

6363
secondary = ServerDescription.builder().state(Connected).address(new ServerAddress(HOST, 27018))
64-
.averagePingTime(bestPingTime * 1000000L, NANOSECONDS)
64+
.averageLatency(bestPingTime * 1000000L, NANOSECONDS)
6565
.ok(true)
6666
.type(ServerType.ReplicaSetSecondary)
6767
.tags(tags2)
6868
.maxDocumentSize(FOUR_MEG).build();
6969

7070
otherSecondary = ServerDescription.builder().state(Connected).address(new ServerAddress(HOST, 27019))
71-
.averagePingTime(unacceptablePingTime * 1000000L, NANOSECONDS)
71+
.averageLatency(unacceptablePingTime * 1000000L, NANOSECONDS)
7272
.ok(true)
7373
.type(ServerType.ReplicaSetSecondary)
7474
.tags(tags3)
7575
.maxDocumentSize(FOUR_MEG)
7676
.build();
7777
ServerDescription uninitiatedMember = ServerDescription.builder().state(Connected).address(new ServerAddress(HOST, 27020))
78-
.averagePingTime(unacceptablePingTime * 1000000L, NANOSECONDS)
78+
.averageLatency(unacceptablePingTime * 1000000L, NANOSECONDS)
7979
.ok(true)
8080
.type(ServerType.ReplicaSetOther)
8181
.maxDocumentSize(FOUR_MEG)

src/test/com/mongodb/ServerDescriptionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void testDefaults() throws UnknownHostException {
6767
assertFalse(serverDescription.isPrimary());
6868
assertFalse(serverDescription.isSecondary());
6969

70-
assertEquals(0F, serverDescription.getAveragePingTimeNanos(), 0L);
70+
assertEquals(0F, serverDescription.getAverageLatencyNanos(), 0L);
7171

7272
assertEquals(0x1000000, serverDescription.getMaxDocumentSize());
7373
assertEquals(0x2000000, serverDescription.getMaxMessageSize());
@@ -96,7 +96,7 @@ public void testObjectOverrides() throws UnknownHostException {
9696
.maxDocumentSize(100)
9797
.maxMessageSize(200)
9898
.maxWriteBatchSize(1024)
99-
.averagePingTime(50000, java.util.concurrent.TimeUnit.NANOSECONDS)
99+
.averageLatency(50000, java.util.concurrent.TimeUnit.NANOSECONDS)
100100
.primary("localhost:27017")
101101
.hosts(new HashSet<String>(Arrays.asList("localhost:27017",
102102
"localhost:27018")))

0 commit comments

Comments
 (0)