@@ -28,6 +28,7 @@ class ReplicaSetStatus {
2828 for ( ServerAddress addr : initial ){
2929 _all .add ( new Node ( addr ) );
3030 }
31+ _nextResolveTime = System .currentTimeMillis () + inetAddrCacheMS ;
3132
3233 _updater = new Updater ();
3334 _updater .start ();
@@ -114,6 +115,18 @@ class Node {
114115 _names .add ( addr .toString () );
115116 }
116117
118+ private void updateAddr () {
119+ try {
120+ if (_addr .updateInetAddr ()) {
121+ // address changed, need to use new ports
122+ _port = new DBPort (_addr , null , _mongoOptions );
123+ _mongo .getConnector ().updatePortPool (_addr );
124+ }
125+ } catch (UnknownHostException ex ) {
126+ _logger .log (Level .WARNING , null , ex );
127+ }
128+ }
129+
117130 synchronized void update (){
118131 update (null );
119132 }
@@ -231,7 +244,7 @@ public String toString(){
231244
232245 final ServerAddress _addr ;
233246 final Set <String > _names = Collections .synchronizedSet ( new HashSet <String >() );
234- final DBPort _port ; // we have our own port so we can set different socket options and don't have to owrry about the pool
247+ DBPort _port ; // we have our own port so we can set different socket options and don't have to owrry about the pool
235248
236249 boolean _ok = false ;
237250 long _lastCheck = 0 ;
@@ -254,6 +267,14 @@ public void run(){
254267 try {
255268 updateAll ();
256269
270+ long now = System .currentTimeMillis ();
271+ if (inetAddrCacheMS > 0 && _nextResolveTime < now ) {
272+ _nextResolveTime = now + inetAddrCacheMS ;
273+ for (Node node : _all ) {
274+ node .updateAddr ();
275+ }
276+ }
277+
257278 // force check on master
258279 // otherwise master change may go unnoticed for a while if no write concern
259280 _mongo .getConnector ().checkMaster (true , false );
@@ -266,7 +287,6 @@ public void run(){
266287 Thread .sleep ( updaterIntervalMS );
267288 }
268289 catch ( InterruptedException ie ){
269- // TODO: maybe something smarter
270290 }
271291
272292 }
@@ -375,15 +395,18 @@ void close(){
375395 boolean _closed = false ;
376396
377397 final Random _random = new Random ();
398+ long _nextResolveTime ;
378399
379400 static int updaterIntervalMS ;
380401 static int slaveAcceptableLatencyMS ;
402+ static int inetAddrCacheMS ;
381403
382404 static final MongoOptions _mongoOptions = new MongoOptions ();
383405
384406 static {
385407 updaterIntervalMS = Integer .parseInt (System .getProperty ("com.mongodb.updaterIntervalMS" , "5000" ));
386408 slaveAcceptableLatencyMS = Integer .parseInt (System .getProperty ("com.mongodb.slaveAcceptableLatencyMS" , "15" ));
409+ inetAddrCacheMS = Integer .parseInt (System .getProperty ("com.mongodb.inetAddrCacheMS" , "300000" ));
387410 _mongoOptions .connectTimeout = Integer .parseInt (System .getProperty ("com.mongodb.updaterConnectTimeoutMS" , "20000" ));
388411 _mongoOptions .socketTimeout = Integer .parseInt (System .getProperty ("com.mongodb.updaterSocketTimeoutMS" , "20000" ));
389412 }
0 commit comments