31
31
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
32
32
33
33
@ ThreadSafe
34
+ @ SuppressWarnings ("deprecation" )
34
35
class ServerStateNotifier implements Runnable {
35
36
36
37
private static final Logger LOGGER = Loggers .getLogger ("cluster" );
@@ -43,7 +44,7 @@ class ServerStateNotifier implements Runnable {
43
44
private long elapsedNanosSum ;
44
45
private volatile ServerDescription serverDescription ;
45
46
private volatile boolean isClosed ;
46
- DBPort connection ;
47
+ private DBPort connection ;
47
48
48
49
ServerStateNotifier (final ServerAddress serverAddress , final ChangeListener <ServerDescription > serverStateListener ,
49
50
final SocketSettings socketSettings , final Mongo mongo ) {
@@ -68,21 +69,20 @@ public synchronized void run() {
68
69
connection = new DBPort (serverAddress , null , getOptions (), 0 );
69
70
}
70
71
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 ();
79
73
} 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 ) {
81
83
connection .close ();
82
84
connection = null ;
83
- count = 0 ;
84
- elapsedNanosSum = 0 ;
85
- throw e ;
85
+ throw e1 ;
86
86
}
87
87
}
88
88
} catch (Throwable t ) {
@@ -98,8 +98,7 @@ public synchronized void run() {
98
98
if (throwable != null ) {
99
99
LOGGER .log (Level .INFO , format ("Exception in monitor thread while connecting to server %s" , serverAddress ),
100
100
throwable );
101
- }
102
- else {
101
+ } else {
103
102
LOGGER .info (format ("Monitor thread successfully connected to server with description %s" , serverDescription ));
104
103
}
105
104
}
@@ -126,6 +125,17 @@ private MongoOptions getOptions() {
126
125
return options ;
127
126
}
128
127
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
+
129
139
@ SuppressWarnings ("unchecked" )
130
140
private ServerDescription createDescription (final CommandResult commandResult , final CommandResult buildInfoResult ,
131
141
final long averagePingTimeNanos ) {
@@ -159,8 +169,7 @@ private static ServerVersion getVersion(final CommandResult buildInfoResult) {
159
169
private Set <String > listToSet (final List <String > list ) {
160
170
if (list == null || list .isEmpty ()) {
161
171
return Collections .emptySet ();
162
- }
163
- else {
172
+ } else {
164
173
return new HashSet <String >(list );
165
174
}
166
175
}
0 commit comments