Skip to content

Commit 76983d0

Browse files
committed
JAVA-785: Improve logging for socket connect, read, and write. As part of this "can't say/call something is gone for good and replaced with something more understandable.
Also fixed the retry log message to properly report elapsed time, and changed it from severe to warning. As part of this, restructured the code to make it more intelligible.
1 parent d63615e commit 76983d0

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

src/main/com/mongodb/DBPort.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ public synchronized void ensureOpen()
202202
_open();
203203
}
204204

205-
boolean _open()
206-
throws IOException {
205+
void _open() throws IOException {
207206

208207
long sleepTime = 100;
209208

@@ -212,11 +211,9 @@ boolean _open()
212211
maxAutoConnectRetryTime = _options.maxAutoConnectRetryTime;
213212
}
214213

214+
boolean successfullyConnected = false;
215215
final long start = System.currentTimeMillis();
216-
while ( true ){
217-
218-
IOException lastError = null;
219-
216+
do {
220217
try {
221218
_socket = _options.socketFactory.createSocket();
222219
_socket.connect( _addr , _options.connectTimeout );
@@ -226,30 +223,28 @@ boolean _open()
226223
_socket.setSoTimeout( _options.socketTimeout );
227224
_in = new BufferedInputStream( _socket.getInputStream() );
228225
_out = _socket.getOutputStream();
229-
return true;
226+
successfullyConnected = true;
230227
}
231-
catch ( IOException ioe ){
232-
lastError = new IOException( "couldn't connect to [" + _addr + "] bc:" + ioe );
233-
_logger.log( Level.INFO , "connect fail to : " + _addr , ioe );
228+
catch ( IOException e ){
234229
close();
235-
}
236-
237-
if ( ! _options.autoConnectRetry || ( _pool != null && ! _pool._everWorked ) )
238-
throw lastError;
239-
240-
long sleptSoFar = System.currentTimeMillis() - start;
241230

242-
if ( sleptSoFar >= maxAutoConnectRetryTime )
243-
throw lastError;
244-
245-
if ( sleepTime + sleptSoFar > maxAutoConnectRetryTime )
246-
sleepTime = maxAutoConnectRetryTime - sleptSoFar;
231+
if (!_options.autoConnectRetry || (_pool != null && !_pool._everWorked))
232+
throw e;
247233

248-
_logger.severe( "going to sleep and retry. total sleep time after = " + ( sleptSoFar + sleptSoFar ) + "ms this time:" + sleepTime + "ms" );
249-
ThreadUtil.sleep( sleepTime );
250-
sleepTime *= 2;
251-
252-
}
234+
long waitSoFar = System.currentTimeMillis() - start;
235+
236+
if (waitSoFar >= maxAutoConnectRetryTime)
237+
throw e;
238+
239+
if (sleepTime + waitSoFar > maxAutoConnectRetryTime)
240+
sleepTime = maxAutoConnectRetryTime - waitSoFar;
241+
242+
_logger.log(Level.WARNING, "Exception connecting to " + serverAddress().getHost() + ": " + e +
243+
". Total wait time so far is " + waitSoFar + " ms. Will retry after sleeping for " + sleepTime + " ms.");
244+
ThreadUtil.sleep(sleepTime);
245+
sleepTime *= 2;
246+
}
247+
} while (!successfullyConnected);
253248
}
254249

255250
@Override

src/main/com/mongodb/DBTCPConnector.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
150150
_error( ioe, false );
151151

152152
if ( concern.raiseNetworkErrors() )
153-
throw new MongoException.Network( "can't say something" , ioe );
153+
throw new MongoException.Network("Write operation to server " + port.host() + " failed on database " + db , ioe );
154154

155155
CommandResult res = new CommandResult(port.serverAddress());
156156
res.put( "ok" , false );
@@ -250,8 +250,7 @@ private Response innerCall(final DB db, final DBCollection coll, final OutMessag
250250
retry = retries > 0 && !coll._name.equals( "$cmd" )
251251
&& !(ioe instanceof SocketTimeoutException) && _error( ioe, secondaryOk );
252252
if ( !retry ){
253-
throw new MongoException.Network( "can't call something : " + port.host() + "/" + db,
254-
ioe );
253+
throw new MongoException.Network("Read operation to server " + port.host() + " failed on database " + db , ioe );
255254
}
256255
}
257256
catch ( RuntimeException re ){

src/test/com/mongodb/DBPortTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.testng.annotations.Test;
2222

2323
import java.io.IOException;
24+
import java.net.UnknownHostException;
2425
import java.util.HashSet;
2526
import java.util.Set;
2627

@@ -68,4 +69,23 @@ public void testAuthentication() throws IOException {
6869
}
6970
}
7071

72+
@Test
73+
public void testOpenFailure() throws UnknownHostException {
74+
final MongoOptions options = new MongoOptions();
75+
options.autoConnectRetry = true;
76+
options.maxAutoConnectRetryTime = 350;
77+
78+
final DBPortPool portPool = new DBPortPool(new ServerAddress("localhost", 50051), options);
79+
portPool._everWorked = true;
80+
81+
DBPort port = new DBPort(new ServerAddress("localhost", 50051), portPool, options);
82+
try {
83+
port._open();
84+
fail("Open should fail");
85+
} catch (IOException e) {
86+
// should get exception
87+
}
88+
89+
}
90+
7191
}

0 commit comments

Comments
 (0)