Skip to content

Fix off-by-one error in max routing failures #288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private ClusterComposition lookupRoutingTable() throws InterruptedException, Ser
return cluster;
}
}
if ( ++failures > settings.maxRoutingFailures )
if ( ++failures >= settings.maxRoutingFailures )
{
throw new ServiceUnavailableException( NO_ROUTERS_AVAILABLE );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.driver.internal;

import org.hamcrest.Matcher;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
Expand All @@ -32,8 +34,6 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.hamcrest.Matcher;

import org.neo4j.driver.internal.util.MatcherFactory;
import org.neo4j.driver.v1.EventLogger;

Expand Down Expand Up @@ -110,7 +110,7 @@ public final void assertCount( Matcher<? extends Event> matcher, Matcher<Integer
{
synchronized ( events )
{
assertThat( events, (Matcher) count( matcher, count ) );
assertThat( "Unexpected count " + count, events, (Matcher) count( matcher, count ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
package org.neo4j.driver.internal.cluster;

import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.driver.internal.EventHandler;
import org.neo4j.driver.internal.net.BoltServerAddress;
import org.neo4j.driver.internal.spi.Connection;
Expand All @@ -40,9 +40,11 @@

import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.neo4j.driver.internal.cluster.ClusterTopology.Role.READ;
import static org.neo4j.driver.internal.cluster.ClusterTopology.Role.ROUTE;
Expand Down Expand Up @@ -90,9 +92,15 @@ public void evaluate() throws Throwable
private final ClusterTopology cluster = new ClusterTopology( events, clock );

private LoadBalancer seedLoadBalancer( String host, int port ) throws Exception
{
RoutingSettings defaultSettings = new RoutingSettings( MAX_ROUTING_FAILURES, RETRY_TIMEOUT_DELAY );
return seedLoadBalancer( host, port, defaultSettings );
}

private LoadBalancer seedLoadBalancer( String host, int port, RoutingSettings settings ) throws Exception
{
return new LoadBalancer(
new RoutingSettings( MAX_ROUTING_FAILURES, RETRY_TIMEOUT_DELAY ),
settings,
clock,
log,
connections,
Expand Down Expand Up @@ -351,6 +359,38 @@ public void connectionFailure( BoltServerAddress address )
any( ClusterComposition.class ) ) ) );
}

@Test
public void shouldTryConfiguredMaxRoutingFailures() throws Exception
{
// given
int port = 1337;
int maxRoutingFailures = 7;
RoutingSettings settings = new RoutingSettings( maxRoutingFailures, 10 );

coreClusterOn( 20, "one", port, "two" );
connections.up( "one", port );

LoadBalancer routing = seedLoadBalancer( "one", port, settings );

// when
connections.down( "one", port );
clock.progress( 25_000 ); // will cause TTL timeout

try
{
routing.acquireWriteConnection();
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( ServiceUnavailableException.class ) );
}

// then
events.assertCount( connectionFailure( "one", port ), equalTo( maxRoutingFailures ) );
events.assertCount( connectionFailure( "two", port ), equalTo( maxRoutingFailures ) );
}

@Test
public void shouldFailIfEnoughConnectionAttemptsFail() throws Exception
{
Expand Down Expand Up @@ -570,7 +610,6 @@ public void sleep( long timestamp, long millis )

// then
assertEquals( new BoltServerAddress( "two", 1337 ), connection.boltServerAddress() );
events.printEvents( System.out );
}

private void coreClusterOn( int ttlSeconds, String leader, int port, String... others )
Expand Down