15
15
*/
16
16
package com .mongodb ;
17
17
18
- import java .io .IOException ;
19
18
import java .util .ArrayList ;
20
19
import java .util .List ;
21
20
import java .util .logging .Level ;
22
21
import java .util .logging .Logger ;
23
22
23
+ import static com .mongodb .ConnectionStatus .UpdatableNode .ConnectionState .Connected ;
24
+ import static com .mongodb .ConnectionStatus .UpdatableNode .ConnectionState .Connecting ;
25
+ import static com .mongodb .ConnectionStatus .UpdatableNode .ConnectionState .Unconnected ;
26
+
24
27
/**
25
28
* Base class for classes that manage connections to mongo instances as background tasks.
26
29
*/
@@ -171,68 +174,53 @@ public BackgroundUpdater(final String name) {
171
174
}
172
175
173
176
static abstract class UpdatableNode {
177
+
178
+ enum ConnectionState {
179
+ Connecting , Connected , Unconnected
180
+ }
181
+
174
182
UpdatableNode (final ServerAddress addr , Mongo mongo , MongoOptions mongoOptions ) {
175
183
this ._addr = addr ;
176
184
this ._mongo = mongo ;
177
185
this ._mongoOptions = mongoOptions ;
178
186
this ._port = new DBPort (addr , null , mongoOptions );
179
187
}
180
188
189
+ public boolean isOk () {
190
+ return _connectionState == Connected ;
191
+ }
192
+
181
193
public CommandResult update () {
182
- CommandResult res = null ;
183
194
try {
184
195
long start = System .nanoTime ();
185
- res = _port .runCommand (_mongo .getDB ("admin" ), isMasterCmd );
196
+ CommandResult res = _port .runCommand (_mongo .getDB ("admin" ), isMasterCmd );
197
+
186
198
long end = System .nanoTime ();
187
199
float newPingMS = (end - start ) / 1000000F ;
188
- if (! successfullyContacted )
200
+ if (_connectionState != Connected ) {
189
201
_pingTimeMS = newPingMS ;
190
- else
202
+ }
203
+ else {
191
204
_pingTimeMS = _pingTimeMS + ((newPingMS - _pingTimeMS ) / latencySmoothFactor );
192
-
193
- getLogger ().log (Level .FINE , "Latency to " + _addr + " actual=" + newPingMS + " smoothed=" + _pingTimeMS );
194
-
195
- successfullyContacted = true ;
196
-
197
- if (res == null ) {
198
- throw new MongoInternalException ("Invalid null value returned from isMaster" );
199
205
}
200
206
201
- if (!_ok ) {
202
- getLogger ().log (Level .INFO , "Server seen up: " + _addr );
203
- }
204
- _ok = true ;
207
+ _maxBsonObjectSize = res .getInt ("maxBsonObjectSize" , Bytes .MAX_OBJECT_SIZE );
205
208
206
- // max size was added in 1.8
207
- if (res .containsField ("maxBsonObjectSize" )) {
208
- _maxBsonObjectSize = (Integer ) res .get ("maxBsonObjectSize" );
209
- } else {
210
- _maxBsonObjectSize = Bytes .MAX_OBJECT_SIZE ;
211
- }
212
- } catch (Exception e ) {
213
- if (!((_ok ) ? true : (Math .random () > 0.1 ))) {
214
- return res ;
209
+ if (_connectionState != Connected ) {
210
+ _connectionState = Connected ;
211
+ getLogger ().log (Level .INFO , "Server seen up: " + _addr );
215
212
}
216
213
217
- final StringBuilder logError = (new StringBuilder ("Server seen down: " )).append (_addr );
218
-
219
- if (e instanceof IOException ) {
220
-
221
- logError .append (" - " ).append (IOException .class .getName ());
222
-
223
- if (e .getMessage () != null ) {
224
- logError .append (" - message: " ).append (e .getMessage ());
225
- }
226
-
227
- getLogger ().log (Level .WARNING , logError .toString ());
214
+ getLogger ().log (Level .FINE , "Latency to " + _addr + " actual=" + newPingMS + " smoothed=" + _pingTimeMS );
228
215
229
- } else {
230
- getLogger ().log (Level .WARNING , logError .toString (), e );
216
+ return res ;
217
+ } catch (Exception e ) {
218
+ if (_connectionState != Unconnected ) {
219
+ _connectionState = Unconnected ;
220
+ getLogger ().log (Level .WARNING , String .format ("Server seen down: %s" , _addr ), e );
231
221
}
232
- _ok = false ;
222
+ return null ;
233
223
}
234
-
235
- return res ;
236
224
}
237
225
238
226
protected abstract Logger getLogger ();
@@ -243,10 +231,9 @@ public CommandResult update() {
243
231
244
232
DBPort _port ; // we have our own port so we can set different socket options and don't have to worry about the pool
245
233
246
- boolean successfullyContacted = false ;
247
- boolean _ok = false ;
248
234
float _pingTimeMS = 0 ;
249
235
int _maxBsonObjectSize ;
236
+ ConnectionState _connectionState = Connecting ;
250
237
}
251
238
252
239
}
0 commit comments