Skip to content

Commit 53dc05b

Browse files
authored
Merge pull request #288 from lutovich/1.1-maxRoutingFailures-off-by-one
Fix off-by-one error in max routing failures
2 parents b48d6c7 + 99eb1f9 commit 53dc05b

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

driver/src/main/java/org/neo4j/driver/internal/cluster/LoadBalancer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private ClusterComposition lookupRoutingTable() throws InterruptedException, Ser
199199
return cluster;
200200
}
201201
}
202-
if ( ++failures > settings.maxRoutingFailures )
202+
if ( ++failures >= settings.maxRoutingFailures )
203203
{
204204
throw new ServiceUnavailableException( NO_ROUTERS_AVAILABLE );
205205
}

driver/src/test/java/org/neo4j/driver/internal/EventHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21+
import org.hamcrest.Matcher;
22+
2123
import java.io.PrintStream;
2224
import java.io.PrintWriter;
2325
import java.lang.reflect.Constructor;
@@ -32,8 +34,6 @@
3234
import java.util.concurrent.ConcurrentMap;
3335
import java.util.concurrent.CopyOnWriteArrayList;
3436

35-
import org.hamcrest.Matcher;
36-
3737
import org.neo4j.driver.internal.util.MatcherFactory;
3838
import org.neo4j.driver.v1.EventLogger;
3939

@@ -110,7 +110,7 @@ public final void assertCount( Matcher<? extends Event> matcher, Matcher<Integer
110110
{
111111
synchronized ( events )
112112
{
113-
assertThat( events, (Matcher) count( matcher, count ) );
113+
assertThat( "Unexpected count " + count, events, (Matcher) count( matcher, count ) );
114114
}
115115
}
116116

driver/src/test/java/org/neo4j/driver/internal/cluster/LoadBalancerTest.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
*/
1919
package org.neo4j.driver.internal.cluster;
2020

21-
import java.util.ArrayList;
22-
import java.util.List;
23-
2421
import org.hamcrest.Matcher;
2522
import org.junit.Rule;
2623
import org.junit.Test;
2724
import org.junit.rules.TestRule;
2825
import org.junit.runner.Description;
2926
import org.junit.runners.model.Statement;
3027

28+
import java.util.ArrayList;
29+
import java.util.List;
30+
3131
import org.neo4j.driver.internal.EventHandler;
3232
import org.neo4j.driver.internal.net.BoltServerAddress;
3333
import org.neo4j.driver.internal.spi.Connection;
@@ -40,9 +40,11 @@
4040

4141
import static org.hamcrest.Matchers.any;
4242
import static org.hamcrest.Matchers.equalTo;
43+
import static org.hamcrest.Matchers.instanceOf;
4344
import static org.hamcrest.Matchers.sameInstance;
4445
import static org.junit.Assert.assertEquals;
4546
import static org.junit.Assert.assertNotEquals;
47+
import static org.junit.Assert.assertThat;
4648
import static org.junit.Assert.fail;
4749
import static org.neo4j.driver.internal.cluster.ClusterTopology.Role.READ;
4850
import static org.neo4j.driver.internal.cluster.ClusterTopology.Role.ROUTE;
@@ -90,9 +92,15 @@ public void evaluate() throws Throwable
9092
private final ClusterTopology cluster = new ClusterTopology( events, clock );
9193

9294
private LoadBalancer seedLoadBalancer( String host, int port ) throws Exception
95+
{
96+
RoutingSettings defaultSettings = new RoutingSettings( MAX_ROUTING_FAILURES, RETRY_TIMEOUT_DELAY );
97+
return seedLoadBalancer( host, port, defaultSettings );
98+
}
99+
100+
private LoadBalancer seedLoadBalancer( String host, int port, RoutingSettings settings ) throws Exception
93101
{
94102
return new LoadBalancer(
95-
new RoutingSettings( MAX_ROUTING_FAILURES, RETRY_TIMEOUT_DELAY ),
103+
settings,
96104
clock,
97105
log,
98106
connections,
@@ -351,6 +359,38 @@ public void connectionFailure( BoltServerAddress address )
351359
any( ClusterComposition.class ) ) ) );
352360
}
353361

362+
@Test
363+
public void shouldTryConfiguredMaxRoutingFailures() throws Exception
364+
{
365+
// given
366+
int port = 1337;
367+
int maxRoutingFailures = 7;
368+
RoutingSettings settings = new RoutingSettings( maxRoutingFailures, 10 );
369+
370+
coreClusterOn( 20, "one", port, "two" );
371+
connections.up( "one", port );
372+
373+
LoadBalancer routing = seedLoadBalancer( "one", port, settings );
374+
375+
// when
376+
connections.down( "one", port );
377+
clock.progress( 25_000 ); // will cause TTL timeout
378+
379+
try
380+
{
381+
routing.acquireWriteConnection();
382+
fail( "Exception expected" );
383+
}
384+
catch ( Exception e )
385+
{
386+
assertThat( e, instanceOf( ServiceUnavailableException.class ) );
387+
}
388+
389+
// then
390+
events.assertCount( connectionFailure( "one", port ), equalTo( maxRoutingFailures ) );
391+
events.assertCount( connectionFailure( "two", port ), equalTo( maxRoutingFailures ) );
392+
}
393+
354394
@Test
355395
public void shouldFailIfEnoughConnectionAttemptsFail() throws Exception
356396
{
@@ -570,7 +610,6 @@ public void sleep( long timestamp, long millis )
570610

571611
// then
572612
assertEquals( new BoltServerAddress( "two", 1337 ), connection.boltServerAddress() );
573-
events.printEvents( System.out );
574613
}
575614

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

0 commit comments

Comments
 (0)