Skip to content

Commit 5ab8d22

Browse files
committed
JAVA-1159: If the connection wasn't already null, retry once before declaring a server down.
1 parent 2d69e07 commit 5ab8d22

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/main/com/mongodb/ServerStateNotifier.java

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3232

3333
@ThreadSafe
34+
@SuppressWarnings("deprecation")
3435
class ServerStateNotifier implements Runnable {
3536

3637
private static final Logger LOGGER = Loggers.getLogger("cluster");
@@ -43,7 +44,7 @@ class ServerStateNotifier implements Runnable {
4344
private long elapsedNanosSum;
4445
private volatile ServerDescription serverDescription;
4546
private volatile boolean isClosed;
46-
DBPort connection;
47+
private DBPort connection;
4748

4849
ServerStateNotifier(final ServerAddress serverAddress, final ChangeListener<ServerDescription> serverStateListener,
4950
final SocketSettings socketSettings, final Mongo mongo) {
@@ -68,21 +69,20 @@ public synchronized void run() {
6869
connection = new DBPort(serverAddress, null, getOptions(), 0);
6970
}
7071
try {
71-
LOGGER.fine(format("Checking status of %s", serverAddress));
72-
long startNanoTime = System.nanoTime();
73-
final CommandResult isMasterResult = connection.runCommand(mongo.getDB("admin"), new BasicDBObject("ismaster", 1));
74-
count++;
75-
elapsedNanosSum += System.nanoTime() - startNanoTime;
76-
77-
final CommandResult buildInfoResult = connection.runCommand(mongo.getDB("admin"), new BasicDBObject("buildinfo", 1));
78-
serverDescription = createDescription(isMasterResult, buildInfoResult, elapsedNanosSum / count);
72+
serverDescription = lookupServerDescription();
7973
} catch (IOException e) {
80-
if (!isClosed) {
74+
// in case the connection has been reset since the last run, do one retry immediately before reporting that the server is
75+
// down
76+
count = 0;
77+
elapsedNanosSum = 0;
78+
connection.close(); // generating a warning in IDEA about possible NPE, but I don't think it can happen
79+
connection = new DBPort(serverAddress, null, getOptions(), 0);
80+
try {
81+
serverDescription = lookupServerDescription();
82+
} catch (IOException e1) {
8183
connection.close();
8284
connection = null;
83-
count = 0;
84-
elapsedNanosSum = 0;
85-
throw e;
85+
throw e1;
8686
}
8787
}
8888
} catch (Throwable t) {
@@ -98,8 +98,7 @@ public synchronized void run() {
9898
if (throwable != null) {
9999
LOGGER.log(Level.INFO, format("Exception in monitor thread while connecting to server %s", serverAddress),
100100
throwable);
101-
}
102-
else {
101+
} else {
103102
LOGGER.info(format("Monitor thread successfully connected to server with description %s", serverDescription));
104103
}
105104
}
@@ -126,6 +125,17 @@ private MongoOptions getOptions() {
126125
return options;
127126
}
128127

128+
private ServerDescription lookupServerDescription() throws IOException {
129+
LOGGER.fine(format("Checking status of %s", serverAddress));
130+
long startNanoTime = System.nanoTime();
131+
final CommandResult isMasterResult = connection.runCommand(mongo.getDB("admin"), new BasicDBObject("ismaster", 1));
132+
count++;
133+
elapsedNanosSum += System.nanoTime() - startNanoTime;
134+
135+
final CommandResult buildInfoResult = connection.runCommand(mongo.getDB("admin"), new BasicDBObject("buildinfo", 1));
136+
return createDescription(isMasterResult, buildInfoResult, elapsedNanosSum / count);
137+
}
138+
129139
@SuppressWarnings("unchecked")
130140
private ServerDescription createDescription(final CommandResult commandResult, final CommandResult buildInfoResult,
131141
final long averagePingTimeNanos) {
@@ -159,8 +169,7 @@ private static ServerVersion getVersion(final CommandResult buildInfoResult) {
159169
private Set<String> listToSet(final List<String> list) {
160170
if (list == null || list.isEmpty()) {
161171
return Collections.emptySet();
162-
}
163-
else {
172+
} else {
164173
return new HashSet<String>(list);
165174
}
166175
}

0 commit comments

Comments
 (0)