@@ -28,6 +28,7 @@ class ReplicaSetStatus {
28
28
for ( ServerAddress addr : initial ){
29
29
_all .add ( new Node ( addr ) );
30
30
}
31
+ _nextResolveTime = System .currentTimeMillis () + inetAddrCacheMS ;
31
32
32
33
_updater = new Updater ();
33
34
_updater .start ();
@@ -114,6 +115,18 @@ class Node {
114
115
_names .add ( addr .toString () );
115
116
}
116
117
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
+
117
130
synchronized void update (){
118
131
update (null );
119
132
}
@@ -231,7 +244,7 @@ public String toString(){
231
244
232
245
final ServerAddress _addr ;
233
246
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
235
248
236
249
boolean _ok = false ;
237
250
long _lastCheck = 0 ;
@@ -254,6 +267,14 @@ public void run(){
254
267
try {
255
268
updateAll ();
256
269
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
+
257
278
// force check on master
258
279
// otherwise master change may go unnoticed for a while if no write concern
259
280
_mongo .getConnector ().checkMaster (true , false );
@@ -266,7 +287,6 @@ public void run(){
266
287
Thread .sleep ( updaterIntervalMS );
267
288
}
268
289
catch ( InterruptedException ie ){
269
- // TODO: maybe something smarter
270
290
}
271
291
272
292
}
@@ -375,15 +395,18 @@ void close(){
375
395
boolean _closed = false ;
376
396
377
397
final Random _random = new Random ();
398
+ long _nextResolveTime ;
378
399
379
400
static int updaterIntervalMS ;
380
401
static int slaveAcceptableLatencyMS ;
402
+ static int inetAddrCacheMS ;
381
403
382
404
static final MongoOptions _mongoOptions = new MongoOptions ();
383
405
384
406
static {
385
407
updaterIntervalMS = Integer .parseInt (System .getProperty ("com.mongodb.updaterIntervalMS" , "5000" ));
386
408
slaveAcceptableLatencyMS = Integer .parseInt (System .getProperty ("com.mongodb.slaveAcceptableLatencyMS" , "15" ));
409
+ inetAddrCacheMS = Integer .parseInt (System .getProperty ("com.mongodb.inetAddrCacheMS" , "300000" ));
387
410
_mongoOptions .connectTimeout = Integer .parseInt (System .getProperty ("com.mongodb.updaterConnectTimeoutMS" , "20000" ));
388
411
_mongoOptions .socketTimeout = Integer .parseInt (System .getProperty ("com.mongodb.updaterSocketTimeoutMS" , "20000" ));
389
412
}
0 commit comments