1515 */
1616package com .mongodb ;
1717
18- import java .io .IOException ;
1918import java .util .ArrayList ;
2019import java .util .List ;
2120import java .util .logging .Level ;
2221import java .util .logging .Logger ;
2322
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+
2427/**
2528 * Base class for classes that manage connections to mongo instances as background tasks.
2629 */
@@ -171,68 +174,53 @@ public BackgroundUpdater(final String name) {
171174 }
172175
173176 static abstract class UpdatableNode {
177+
178+ enum ConnectionState {
179+ Connecting , Connected , Unconnected
180+ }
181+
174182 UpdatableNode (final ServerAddress addr , Mongo mongo , MongoOptions mongoOptions ) {
175183 this ._addr = addr ;
176184 this ._mongo = mongo ;
177185 this ._mongoOptions = mongoOptions ;
178186 this ._port = new DBPort (addr , null , mongoOptions );
179187 }
180188
189+ public boolean isOk () {
190+ return _connectionState == Connected ;
191+ }
192+
181193 public CommandResult update () {
182- CommandResult res = null ;
183194 try {
184195 long start = System .nanoTime ();
185- res = _port .runCommand (_mongo .getDB ("admin" ), isMasterCmd );
196+ CommandResult res = _port .runCommand (_mongo .getDB ("admin" ), isMasterCmd );
197+
186198 long end = System .nanoTime ();
187199 float newPingMS = (end - start ) / 1000000F ;
188- if (! successfullyContacted )
200+ if (_connectionState != Connected ) {
189201 _pingTimeMS = newPingMS ;
190- else
202+ }
203+ else {
191204 _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" );
199205 }
200206
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 );
205208
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 );
215212 }
216213
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 );
228215
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 );
231221 }
232- _ok = false ;
222+ return null ;
233223 }
234-
235- return res ;
236224 }
237225
238226 protected abstract Logger getLogger ();
@@ -243,10 +231,9 @@ public CommandResult update() {
243231
244232 DBPort _port ; // we have our own port so we can set different socket options and don't have to worry about the pool
245233
246- boolean successfullyContacted = false ;
247- boolean _ok = false ;
248234 float _pingTimeMS = 0 ;
249235 int _maxBsonObjectSize ;
236+ ConnectionState _connectionState = Connecting ;
250237 }
251238
252239}
0 commit comments