Skip to content

Commit

Permalink
Remove server when failing on session acquisition
Browse files Browse the repository at this point in the history
We were only checking for connection failures on running and consuming the
results, however there might also be a connection failure on session acquisition.
By not properly removing the server on session-acquisition failures we end up
in a scenario where we constantly retry to connect to that endpoint.
  • Loading branch information
pontusmelke committed Oct 5, 2016
1 parent 68ff674 commit 2badccc
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
31 changes: 25 additions & 6 deletions driver/src/main/java/org/neo4j/driver/internal/RoutingDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public int compare( BoltServerAddress o1, BoltServerAddress o2 )
}
};
private static final int MIN_SERVERS = 1;
private static final int CONNECTION_RETRIES = 3;
private final ConnectionPool connections;
private final BiFunction<Connection,Logger,Session> sessionProvider;
private final Clock clock;
Expand Down Expand Up @@ -249,7 +250,7 @@ private boolean call( BoltServerAddress address, String procedureName, Consumer<
recorder.accept( records.next() );
}
}
catch ( ConnectionFailureException e )
catch ( Throwable e )
{
forget( address );
return false;
Expand Down Expand Up @@ -306,18 +307,36 @@ public void onWriteFailure( BoltServerAddress address )

private Connection acquireConnection( AccessMode role )
{
//Potentially rediscover servers if we are not happy with our current knowledge
checkServers();

ConcurrentRoundRobinSet<BoltServerAddress> servers;
switch ( role )
{
case READ:
return connections.acquire( readServers.hop() );
servers = readServers;
break;
case WRITE:
return connections.acquire( writeServers.hop() );
servers = writeServers;
break;
default:
throw new ClientException( role + " is not supported for creating new sessions" );
}

//Potentially rediscover servers if we are not happy with our current knowledge
checkServers();
int numberOfServers = servers.size();
for ( int i = 0; i < numberOfServers; i++ )
{
BoltServerAddress address = servers.hop();
try
{
return connections.acquire( address );
}
catch ( ConnectionFailureException e )
{
forget( address );
}
}

throw new ConnectionFailureException( "Failed to connect to any servers" );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.exceptions.ConnectionFailureException;
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
import org.neo4j.driver.v1.exceptions.SessionExpiredException;
import org.neo4j.driver.v1.util.Function;
import org.neo4j.driver.v1.util.StubServer;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.IsEqual.equalTo;
Expand Down Expand Up @@ -331,6 +333,37 @@ public void shouldForgetEndpointsOnFailure() throws IOException, InterruptedExce
assertThat( server.exitStatus(), equalTo( 0 ) );
}

@Test
public void shouldForgetEndpointsOnFailedSessionAcquisition() throws IOException, InterruptedException, StubServer.ForceKilled
{
// Given
StubServer server = StubServer.start( resource( "acquire_endpoints.script" ), 9001 );

//no read servers

URI uri = URI.create( "bolt+routing://127.0.0.1:9001" );
RoutingDriver driver = (RoutingDriver) GraphDatabase.driver( uri, config );
try
{
driver.session( AccessMode.READ );
fail();
}
catch ( ConnectionFailureException e )
{
//ignore
}

assertThat( driver.readServers(), empty() );
assertThat( driver.writeServers(), hasSize( 2 ) );
assertFalse( driver.connectionPool().hasAddress( address( 9005 ) ) );
assertFalse( driver.connectionPool().hasAddress( address( 9006 ) ) );
driver.close();

// Finally
assertThat( server.exitStatus(), equalTo( 0 ) );
}


@Test
public void shouldRediscoverIfNecessaryOnSessionAcquisition()
throws IOException, InterruptedException, StubServer.ForceKilled
Expand Down

0 comments on commit 2badccc

Please sign in to comment.