Skip to content

Commit 65e8e30

Browse files
committed
JAVA-1202: Made connection volatile to remove race condition, and removed isRunning, since the executor is single threaded anyway.
1 parent 7d59cc8 commit 65e8e30

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

src/main/com/mongodb/DefaultServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
class DefaultServer implements ClusterableServer {
3434

35-
private final String clusterId;
36-
3735
private enum HeartbeatFrequency {
3836
NORMAL {
3937
@Override
@@ -52,6 +50,7 @@ long getFrequencyMS(final ServerSettings settings) {
5250
abstract long getFrequencyMS(final ServerSettings settings);
5351
}
5452

53+
private final String clusterId;
5554
private final ScheduledExecutorService scheduledExecutorService;
5655
private final ServerAddress serverAddress;
5756
private final ServerStateNotifier stateNotifier;

src/main/com/mongodb/ServerStateNotifier.java

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ class ServerStateNotifier implements Runnable {
4444
private long elapsedNanosSum;
4545
private volatile ServerDescription serverDescription;
4646
private volatile boolean isClosed;
47-
private DBPort connection;
48-
private volatile boolean isRunning;
47+
private volatile DBPort connection;
4948

5049
ServerStateNotifier(final ServerAddress serverAddress, final ChangeListener<ServerDescription> serverStateListener,
5150
final SocketSettings socketSettings, final Mongo mongo) {
@@ -63,64 +62,53 @@ public void run() {
6362
return;
6463
}
6564

66-
synchronized (this) {
67-
if (isRunning) {
68-
return;
69-
}
70-
isRunning = true;
71-
}
72-
65+
final ServerDescription currentServerDescription = serverDescription;
66+
Throwable throwable = null;
7367
try {
74-
final ServerDescription currentServerDescription = serverDescription;
75-
Throwable throwable = null;
68+
if (connection == null) {
69+
connection = new DBPort(serverAddress, null, getOptions(), 0);
70+
}
7671
try {
77-
if (connection == null) {
78-
connection = new DBPort(serverAddress, null, getOptions(), 0);
72+
serverDescription = lookupServerDescription();
73+
} catch (IOException e) {
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+
if (connection != null) {
79+
connection.close();
80+
connection = null;
7981
}
82+
connection = new DBPort(serverAddress, null, getOptions(), 0);
8083
try {
8184
serverDescription = lookupServerDescription();
82-
} catch (IOException e) {
83-
// in case the connection has been reset since the last run, do one retry immediately before reporting that the server is
84-
// down
85-
count = 0;
86-
elapsedNanosSum = 0;
87-
if (connection != null) {
88-
connection.close();
89-
connection = null;
90-
}
91-
connection = new DBPort(serverAddress, null, getOptions(), 0);
92-
try {
93-
serverDescription = lookupServerDescription();
94-
} catch (IOException e1) {
95-
connection.close();
96-
connection = null;
97-
throw e1;
98-
}
85+
} catch (IOException e1) {
86+
connection.close();
87+
connection = null;
88+
throw e1;
9989
}
100-
} catch (Throwable t) {
101-
throwable = t;
102-
serverDescription = getUnconnectedServerDescription();
10390
}
91+
} catch (Throwable t) {
92+
throwable = t;
93+
serverDescription = getUnconnectedServerDescription();
94+
}
10495

105-
if (!isClosed) {
106-
try {
107-
// Note that the ServerDescription.equals method does not include the average ping time as part of the comparison,
108-
// so this will not spam the logs too hard.
109-
if (!currentServerDescription.equals(serverDescription)) {
110-
if (throwable != null) {
111-
LOGGER.log(Level.INFO, format("Exception in monitor thread while connecting to server %s", serverAddress),
112-
throwable);
113-
} else {
114-
LOGGER.info(format("Monitor thread successfully connected to server with description %s", serverDescription));
115-
}
96+
if (!isClosed) {
97+
try {
98+
// Note that the ServerDescription.equals method does not include the average ping time as part of the comparison,
99+
// so this will not spam the logs too hard.
100+
if (!currentServerDescription.equals(serverDescription)) {
101+
if (throwable != null) {
102+
LOGGER.log(Level.INFO, format("Exception in monitor thread while connecting to server %s", serverAddress),
103+
throwable);
104+
} else {
105+
LOGGER.info(format("Monitor thread successfully connected to server with description %s", serverDescription));
116106
}
117-
serverStateListener.stateChanged(new ChangeEvent<ServerDescription>(currentServerDescription, serverDescription));
118-
} catch (Throwable t) {
119-
LOGGER.log(Level.WARNING, "Exception in monitor thread during notification of server description state change", t);
120107
}
108+
serverStateListener.stateChanged(new ChangeEvent<ServerDescription>(currentServerDescription, serverDescription));
109+
} catch (Throwable t) {
110+
LOGGER.log(Level.WARNING, "Exception in monitor thread during notification of server description state change", t);
121111
}
122-
} finally {
123-
isRunning = false;
124112
}
125113
}
126114

0 commit comments

Comments
 (0)