Skip to content

Commit

Permalink
JAVA-785: Improve logging for socket connect, read, and write. As par…
Browse files Browse the repository at this point in the history
…t 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.
  • Loading branch information
jyemin committed Mar 11, 2013
1 parent d63615e commit 76983d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
47 changes: 21 additions & 26 deletions src/main/com/mongodb/DBPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ public synchronized void ensureOpen()
_open();
}

boolean _open()
throws IOException {
void _open() throws IOException {

long sleepTime = 100;

Expand All @@ -212,11 +211,9 @@ boolean _open()
maxAutoConnectRetryTime = _options.maxAutoConnectRetryTime;
}

boolean successfullyConnected = false;
final long start = System.currentTimeMillis();
while ( true ){

IOException lastError = null;

do {
try {
_socket = _options.socketFactory.createSocket();
_socket.connect( _addr , _options.connectTimeout );
Expand All @@ -226,30 +223,28 @@ boolean _open()
_socket.setSoTimeout( _options.socketTimeout );
_in = new BufferedInputStream( _socket.getInputStream() );
_out = _socket.getOutputStream();
return true;
successfullyConnected = true;
}
catch ( IOException ioe ){
lastError = new IOException( "couldn't connect to [" + _addr + "] bc:" + ioe );
_logger.log( Level.INFO , "connect fail to : " + _addr , ioe );
catch ( IOException e ){
close();
}

if ( ! _options.autoConnectRetry || ( _pool != null && ! _pool._everWorked ) )
throw lastError;

long sleptSoFar = System.currentTimeMillis() - start;

if ( sleptSoFar >= maxAutoConnectRetryTime )
throw lastError;

if ( sleepTime + sleptSoFar > maxAutoConnectRetryTime )
sleepTime = maxAutoConnectRetryTime - sleptSoFar;
if (!_options.autoConnectRetry || (_pool != null && !_pool._everWorked))
throw e;

_logger.severe( "going to sleep and retry. total sleep time after = " + ( sleptSoFar + sleptSoFar ) + "ms this time:" + sleepTime + "ms" );
ThreadUtil.sleep( sleepTime );
sleepTime *= 2;

}
long waitSoFar = System.currentTimeMillis() - start;

if (waitSoFar >= maxAutoConnectRetryTime)
throw e;

if (sleepTime + waitSoFar > maxAutoConnectRetryTime)
sleepTime = maxAutoConnectRetryTime - waitSoFar;

_logger.log(Level.WARNING, "Exception connecting to " + serverAddress().getHost() + ": " + e +
". Total wait time so far is " + waitSoFar + " ms. Will retry after sleeping for " + sleepTime + " ms.");
ThreadUtil.sleep(sleepTime);
sleepTime *= 2;
}
} while (!successfullyConnected);
}

@Override
Expand Down
5 changes: 2 additions & 3 deletions src/main/com/mongodb/DBTCPConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public WriteResult say( DB db , OutMessage m , WriteConcern concern , ServerAddr
_error( ioe, false );

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

CommandResult res = new CommandResult(port.serverAddress());
res.put( "ok" , false );
Expand Down Expand Up @@ -250,8 +250,7 @@ private Response innerCall(final DB db, final DBCollection coll, final OutMessag
retry = retries > 0 && !coll._name.equals( "$cmd" )
&& !(ioe instanceof SocketTimeoutException) && _error( ioe, secondaryOk );
if ( !retry ){
throw new MongoException.Network( "can't call something : " + port.host() + "/" + db,
ioe );
throw new MongoException.Network("Read operation to server " + port.host() + " failed on database " + db , ioe );
}
}
catch ( RuntimeException re ){
Expand Down
20 changes: 20 additions & 0 deletions src/test/com/mongodb/DBPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -68,4 +69,23 @@ public void testAuthentication() throws IOException {
}
}

@Test
public void testOpenFailure() throws UnknownHostException {
final MongoOptions options = new MongoOptions();
options.autoConnectRetry = true;
options.maxAutoConnectRetryTime = 350;

final DBPortPool portPool = new DBPortPool(new ServerAddress("localhost", 50051), options);
portPool._everWorked = true;

DBPort port = new DBPort(new ServerAddress("localhost", 50051), portPool, options);
try {
port._open();
fail("Open should fail");
} catch (IOException e) {
// should get exception
}

}

}

0 comments on commit 76983d0

Please sign in to comment.