Skip to content

Treat one router as valid in routing table #356

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
Apr 12, 2017
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 @@ -60,7 +60,7 @@ private ClusterRoutingTable( Clock clock )
public boolean isStaleFor( AccessMode mode )
{
return expirationTimeout < clock.millis() ||
routers.size() <= MIN_ROUTERS ||
routers.size() < MIN_ROUTERS ||
mode == AccessMode.READ && readers.size() == 0 ||
mode == AccessMode.WRITE && writers.size() == 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,22 +963,7 @@ public void shouldAcceptRoutingTableWithoutWritersAndThenRediscover() throws Exc
// start another router which knows about writes, use same address as the initial router
router2 = StubServer.start( "acquire_endpoints.script", 9010 );

List<String> names = session.readTransaction( new TransactionWork<List<String>>()
{
@Override
public List<String> execute( Transaction tx )
{
List<Record> records = tx.run( "MATCH (n) RETURN n.name" ).list();
List<String> names = new ArrayList<>( records.size() );
for ( Record record : records )
{
names.add( record.get( 0 ).asString() );
}
return names;
}
} );

assertEquals( asList( "Bob", "Alice", "Tina" ), names );
assertEquals( asList( "Bob", "Alice", "Tina" ), readStrings( "MATCH (n) RETURN n.name", session ) );

StatementResult createResult = session.run( "CREATE (n {name:'Bob'})" );
assertFalse( createResult.hasNext() );
Expand All @@ -993,6 +978,35 @@ public List<String> execute( Transaction tx )
}
}

@Test
public void shouldTreatRoutingTableWithSingleRouterAsValid() throws Exception
{
StubServer router = StubServer.start( "discover_one_router.script", 9010 );
StubServer reader1 = StubServer.start( "read_server.script", 9003 );
StubServer reader2 = StubServer.start( "read_server.script", 9004 );

try ( Driver driver = GraphDatabase.driver( "bolt+routing://127.0.0.1:9010", config );
Session session = driver.session( AccessMode.READ ) )
{
// returned routing table contains only one router, this should be fine and we should be able to
// read multiple times without additional rediscovery

StatementResult readResult1 = session.run( "MATCH (n) RETURN n.name" );
assertEquals( "127.0.0.1:9003", readResult1.summary().server().address() );
assertEquals( 3, readResult1.list().size() );

StatementResult readResult2 = session.run( "MATCH (n) RETURN n.name" );
assertEquals( "127.0.0.1:9004", readResult2.summary().server().address() );
assertEquals( 3, readResult2.list().size() );
}
finally
{
assertEquals( 0, router.exitStatus() );
assertEquals( 0, reader1.exitStatus() );
assertEquals( 0, reader2.exitStatus() );
}
}

private static Driver newDriverWithSleeplessClock( String uriString )
{
DriverFactory driverFactory = new DriverFactoryWithClock( new SleeplessClock() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.neo4j.driver.internal.util.FakeClock;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -181,4 +182,19 @@ public void shouldPreserveOrderingOfReaders()
assertEquals( D, routingTable.readers().next() );
assertEquals( B, routingTable.readers().next() );
}

@Test
public void shouldTreatOneRouterAsValid()
{
ClusterRoutingTable routingTable = new ClusterRoutingTable( new FakeClock() );

List<BoltServerAddress> routers = singletonList( A );
List<BoltServerAddress> writers = asList( B, C );
List<BoltServerAddress> readers = asList( D, E );

routingTable.update( createClusterComposition( routers, writers, readers ) );

assertFalse( routingTable.isStaleFor( READ ) );
assertFalse( routingTable.isStaleFor( WRITE ) );
}
}
9 changes: 9 additions & 0 deletions driver/src/test/resources/discover_one_router.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
!: AUTO INIT
!: AUTO RESET
!: AUTO PULL_ALL

C: RUN "CALL dbms.cluster.routing.getServers" {}
PULL_ALL
S: SUCCESS {"fields": ["ttl", "servers"]}
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001","127.0.0.1:9002"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9003","127.0.0.1:9004"], "role": "READ"},{"addresses": ["127.0.0.1:9005"], "role": "ROUTE"}]]
SUCCESS {}