Skip to content

Commit 892869f

Browse files
committed
[JAVA-266]: Need to update the IP resolved for hosts frequently enough to pick up dns changes
1 parent 40aec21 commit 892869f

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ public void close(){
441441
_myPort = null;
442442
}
443443

444+
void updatePortPool(ServerAddress addr) {
445+
// just remove from map, a new pool will be created lazily
446+
_portHolder._pools.remove(addr);
447+
}
448+
444449
public boolean isOpen(){
445450
return ! _closed;
446451
}

src/main/com/mongodb/ReplicaSetStatus.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/main/com/mongodb/ServerAddress.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ public String toString(){
205205

206206
final String _host;
207207
final int _port;
208-
final InetSocketAddress _addr;
209-
final InetAddress[] _all;
208+
InetSocketAddress _addr;
209+
InetAddress[] _all;
210210

211211
// --------
212212
// static helpers
@@ -236,5 +236,19 @@ public static String defaultHost(){
236236
public static int defaultPort(){
237237
return DBPort.PORT;
238238
}
239+
240+
/**
241+
* attempts to update the internal InetAddress by resolving the host name.
242+
* @return true if host resolved to a new IP that is different from old one, false otherwise
243+
* @throws UnknownHostException
244+
*/
245+
boolean updateInetAddr() throws UnknownHostException {
246+
InetSocketAddress oldaddr = _addr;
247+
_all = _getAddress( _host );
248+
_addr = new InetSocketAddress( _all[0] , _port );
249+
if (!_addr.equals(oldaddr))
250+
return true;
251+
return false;
252+
}
239253

240254
}

0 commit comments

Comments
 (0)