Skip to content

Commit c0a05b4

Browse files
committed
The MongoTimeoutException thrown from BaseCluster methods now includes a short description of the cluster state,
which includes a short description of each server being monitored, which includes a short description of any exception thrown while monitoring. JAVA-1477 Conflicts: src/main/com/mongodb/ServerDescription.java src/test/com/mongodb/ServerDescriptionTest.java
1 parent e66bda8 commit c0a05b4

File tree

6 files changed

+290
-114
lines changed

6 files changed

+290
-114
lines changed

src/main/com/mongodb/BaseCluster.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ public Server getServer(final ServerSelector serverSelector, final long maxWaitT
7979
}
8080

8181
if (curTimeNanos > endTimeNanos) {
82-
throw new MongoTimeoutException(format("Timed out while waiting for a server that matches %s after %d ms",
83-
serverSelector, MILLISECONDS.convert(maxWaitTime, timeUnit)));
82+
throw new MongoTimeoutException(format("Timed out after %d ms while waiting for a server that matches %s. " +
83+
"Client view of cluster state is %s",
84+
MILLISECONDS.convert(maxWaitTime, timeUnit), serverSelector,
85+
curDescription.getShortDescription()));
8486
}
8587

8688
if (!selectionFailureLogged) {
@@ -123,8 +125,10 @@ public ClusterDescription getDescription(final long maxWaitTime, final TimeUnit
123125
while (curDescription.getType() == ClusterType.Unknown) {
124126

125127
if (curTimeNanos > endTimeNanos) {
126-
throw new MongoTimeoutException(format("Timed out while waiting to connect after %d ms",
127-
MILLISECONDS.convert(maxWaitTime, timeUnit)));
128+
throw new MongoTimeoutException(format("Timed out after %d ms while waiting to connect. Client view of cluster state " +
129+
"is %s",
130+
MILLISECONDS.convert(maxWaitTime, timeUnit),
131+
curDescription.getShortDescription()));
128132
}
129133

130134
if (!selectionFailureLogged) {

src/main/com/mongodb/ServerDescription.java

+47-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class ServerDescription {
7272
private final int minWireVersion;
7373
private final int maxWireVersion;
7474

75+
private Throwable exception;
76+
7577
static class Builder {
7678
private ServerAddress address;
7779
private ServerType type = Unknown;
@@ -90,6 +92,7 @@ static class Builder {
9092
private ServerVersion version = new ServerVersion();
9193
private int minWireVersion = 0;
9294
private int maxWireVersion = 0;
95+
private Throwable exception;
9396

9497
// CHECKSTYLE:OFF
9598
public Builder address(final ServerAddress address) {
@@ -178,6 +181,11 @@ public Builder maxWireVersion(final int maxWireVersion) {
178181
return this;
179182
}
180183

184+
public Builder exception(final Throwable exception) {
185+
this.exception = exception;
186+
return this;
187+
}
188+
181189
public ServerDescription build() {
182190
return new ServerDescription(this);
183191
}
@@ -349,6 +357,10 @@ public long getAverageLatencyNanos() {
349357
return averageLatencyNanos;
350358
}
351359

360+
public Throwable getException() {
361+
return exception;
362+
}
363+
352364
/**
353365
* Returns true if this instance is equals to @code{o}. Note that equality is defined to NOT include the average ping time.
354366
*
@@ -415,6 +427,19 @@ public boolean equals(final Object o) {
415427
return false;
416428
}
417429

430+
// Compare class equality and message as exceptions rarely override equals
431+
Class thisExceptionClass = exception != null ? exception.getClass() : null;
432+
Class thatExceptionClass = that.exception != null ? that.exception.getClass() : null;
433+
if (thisExceptionClass != null ? !thisExceptionClass.equals(thatExceptionClass) : thatExceptionClass != null) {
434+
return false;
435+
}
436+
437+
String thisExceptionMessage = exception != null ? exception.getMessage() : null;
438+
String thatExceptionMessage= that.exception != null ? that.exception.getMessage() : null;
439+
if (thisExceptionMessage != null ? !thisExceptionMessage.equals(thatExceptionMessage) : thatExceptionMessage != null) {
440+
return false;
441+
}
442+
418443
return true;
419444
}
420445

@@ -437,6 +462,8 @@ public int hashCode() {
437462
result = 31 * result + version.hashCode();
438463
result = 31 * result + minWireVersion;
439464
result = 31 * result + maxWireVersion;
465+
result = 31 * result + (exception == null ? 0 : exception.getClass().hashCode());
466+
result = 31 * result + (exception == null ? 0 : exception.getMessage().hashCode());
440467
return result;
441468
}
442469

@@ -467,12 +494,30 @@ public String getShortDescription() {
467494
return "{"
468495
+ "address=" + address
469496
+ ", type=" + type
470-
+ (tags.isEmpty() ? "" : tags)
497+
+ (tags.isEmpty() ? "" : ", " + tags)
471498
+ (state == Connected ? (", averageLatency=" + getAverageLatencyFormattedInMilliseconds() + " ms") : "")
472499
+ ", state=" + state
500+
+ (exception == null ? "" : ", exception=" + translateExceptionToString())
473501
+ '}';
474502
}
475503

504+
private String translateExceptionToString() {
505+
StringBuilder builder = new StringBuilder();
506+
builder.append("{");
507+
builder.append(exception);
508+
builder.append("}");
509+
Throwable cur = exception.getCause();
510+
while (cur != null) {
511+
builder.append(", caused by ");
512+
builder.append("{");
513+
builder.append(cur);
514+
builder.append("}");
515+
cur = cur.getCause();
516+
}
517+
518+
return builder.toString();
519+
}
520+
476521
private String getAverageLatencyFormattedInMilliseconds() {
477522
return new DecimalFormat("#0.0").format(averageLatencyNanos / 1000.0 / 1000.0);
478523
}
@@ -495,5 +540,6 @@ private String getAverageLatencyFormattedInMilliseconds() {
495540
ok = builder.ok;
496541
minWireVersion = builder.minWireVersion;
497542
maxWireVersion = builder.maxWireVersion;
543+
exception = builder.exception;
498544
}
499545
}

src/main/com/mongodb/ServerMonitor.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ServerMonitorRunnable implements Runnable {
7777
public void run() {
7878
DBPort connection = null;
7979
try {
80-
ServerDescription currentServerDescription = getConnectingServerDescription();
80+
ServerDescription currentServerDescription = getConnectingServerDescription(null);
8181
Throwable currentException = null;
8282
while (!isClosed) {
8383
ServerDescription previousServerDescription = currentServerDescription;
@@ -109,7 +109,7 @@ public void run() {
109109
}
110110
} catch (Throwable t) {
111111
currentException = t;
112-
currentServerDescription = getConnectingServerDescription();
112+
currentServerDescription = getConnectingServerDescription(t);
113113
}
114114

115115
if (!isClosed) {
@@ -203,7 +203,7 @@ private MongoOptions getOptions() {
203203
}
204204

205205
static boolean descriptionHasChanged(final ServerDescription previousServerDescription,
206-
final ServerDescription currentServerDescription) {
206+
final ServerDescription currentServerDescription) {
207207
return !previousServerDescription.equals(currentServerDescription);
208208
}
209209

@@ -317,7 +317,11 @@ private static Tags getTagsFromDocument(final DBObject tagsDocuments) {
317317
return tags;
318318
}
319319

320-
private ServerDescription getConnectingServerDescription() {
321-
return ServerDescription.builder().type(ServerType.Unknown).state(ServerConnectionState.Connecting).address(serverAddress).build();
320+
private ServerDescription getConnectingServerDescription(final Throwable throwable) {
321+
return ServerDescription.builder().type(ServerType.Unknown)
322+
.state(ServerConnectionState.Connecting)
323+
.address(serverAddress)
324+
.exception(throwable)
325+
.build();
322326
}
323327
}

src/main/com/mongodb/Tags.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public int hashCode() {
158158

159159
@Override
160160
public String toString() {
161-
return wrapped.toString();
161+
return "TagSet" + wrapped.toString();
162162
}
163163
}
164164

0 commit comments

Comments
 (0)