Skip to content

Commit cb8cc72

Browse files
committed
Refactor: Rename to ConcurrentRoundRobinSet
1 parent f30acd8 commit cb8cc72

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

driver/src/main/java/org/neo4j/driver/internal/ClusterDriver.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.neo4j.driver.internal.security.SecurityPlan;
2929
import org.neo4j.driver.internal.spi.Connection;
3030
import org.neo4j.driver.internal.spi.ConnectionPool;
31-
import org.neo4j.driver.internal.util.ConcurrentRingSet;
31+
import org.neo4j.driver.internal.util.ConcurrentRoundRobinSet;
3232
import org.neo4j.driver.internal.util.Consumer;
3333
import org.neo4j.driver.v1.AccessMode;
3434
import org.neo4j.driver.v1.Logging;
@@ -62,9 +62,9 @@ public int compare( BoltServerAddress o1, BoltServerAddress o2 )
6262

6363
protected final ConnectionPool connections;
6464

65-
private final ConcurrentRingSet<BoltServerAddress> routingServers = new ConcurrentRingSet<>(COMPARATOR);
66-
private final ConcurrentRingSet<BoltServerAddress> readServers = new ConcurrentRingSet<>(COMPARATOR);
67-
private final ConcurrentRingSet<BoltServerAddress> writeServers = new ConcurrentRingSet<>(COMPARATOR);
65+
private final ConcurrentRoundRobinSet<BoltServerAddress> routingServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
66+
private final ConcurrentRoundRobinSet<BoltServerAddress> readServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
67+
private final ConcurrentRoundRobinSet<BoltServerAddress> writeServers = new ConcurrentRoundRobinSet<>(COMPARATOR);
6868

6969
public ClusterDriver( BoltServerAddress seedAddress, ConnectionSettings connectionSettings,
7070
SecurityPlan securityPlan,
@@ -99,7 +99,7 @@ private void getServers()
9999
boolean success = false;
100100
while ( !routingServers.isEmpty() && !success )
101101
{
102-
address = routingServers.next();
102+
address = routingServers.hop();
103103
success = call( address, GET_SERVERS, new Consumer<Record>()
104104
{
105105
@Override
@@ -224,9 +224,9 @@ private Connection acquireConnection( AccessMode mode )
224224
switch ( mode )
225225
{
226226
case READ:
227-
return connections.acquire( readServers.next() );
227+
return connections.acquire( readServers.hop() );
228228
case WRITE:
229-
return connections.acquire( writeServers.next() );
229+
return connections.acquire( writeServers.hop() );
230230
default:
231231
throw new ClientException( mode + " is not supported for creating new sessions" );
232232
}

driver/src/main/java/org/neo4j/driver/internal/net/pooling/SocketConnectionPool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void purge( BoltServerAddress address )
156156
@Override
157157
public boolean hasAddress( BoltServerAddress address )
158158
{
159-
return pools.contains( address );
159+
return pools.containsKey( address );
160160
}
161161

162162
@Override

driver/src/main/java/org/neo4j/driver/internal/util/ConcurrentRingSet.java renamed to driver/src/main/java/org/neo4j/driver/internal/util/ConcurrentRoundRobinSet.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@
2525
import java.util.concurrent.ConcurrentSkipListSet;
2626

2727
/**
28-
*
29-
* @param <T>
28+
* A set that exposes a method {@link #hop()} that cycles through the members of the set.
29+
* @param <T> the type of elements in the set
3030
*/
31-
public class ConcurrentRingSet<T> implements Set<T>
31+
public class ConcurrentRoundRobinSet<T> implements Set<T>
3232
{
3333
private final ConcurrentSkipListSet<T> set;
3434
private T current;
3535

36-
public ConcurrentRingSet()
36+
public ConcurrentRoundRobinSet()
3737
{
3838
set = new ConcurrentSkipListSet<>();
3939
}
4040

41-
public ConcurrentRingSet( Comparator<T> comparator )
41+
public ConcurrentRoundRobinSet( Comparator<T> comparator )
4242
{
4343
set = new ConcurrentSkipListSet<>( comparator );
4444
}
4545

46-
public T next()
46+
public T hop()
4747
{
4848
if ( current == null )
4949
{

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848
import static org.hamcrest.Matchers.hasItem;
4949
import static org.hamcrest.Matchers.hasSize;
50-
import static org.hamcrest.Matchers.notNullValue;
5150
import static org.hamcrest.core.IsEqual.equalTo;
5251
import static org.hamcrest.core.IsNot.not;
5352
import static org.junit.Assert.assertThat;
@@ -375,23 +374,25 @@ public void shouldFailOnNonDiscoverableServer() throws IOException, InterruptedE
375374
public void shouldHandleLeaderSwitchWhenWriting()
376375
throws IOException, InterruptedException, StubServer.ForceKilled
377376
{
378-
//Expect
379-
exception.expect( SessionExpiredException.class );
380-
exception.expectMessage( "Server at 127.0.0.1:9006 no longer accepts writes" );
381-
382377
// Given
383378
StubServer server = StubServer.start( resource( "acquire_endpoints.script" ), 9001 );
384379

385380
//START a write server that doesn't accept writes
386381
StubServer.start( resource( "not_able_to_write_server.script" ), 9006 );
387382
URI uri = URI.create( "bolt+routing://127.0.0.1:9001" );
388383
ClusterDriver driver = (ClusterDriver) GraphDatabase.driver( uri, config );
384+
boolean failed = false;
389385
try ( Session session = driver.session( AccessMode.WRITE ) )
390386
{
391387
assertThat(driver.writeServers(), hasItem( new BoltServerAddress( "127.0.0.1", 9006 ) ));
392388
session.run( "CREATE ()" ).consume();
393389
}
394-
390+
catch (SessionExpiredException e)
391+
{
392+
failed = true;
393+
assertThat(e.getMessage(), equalTo( "Server at 127.0.0.1:9006 no longer accepts writes" ));
394+
}
395+
assertTrue( failed );
395396
assertThat( driver.writeServers(), not( hasItem( new BoltServerAddress( "127.0.0.1", 9006 ) ) ) );
396397
assertTrue( driver.connectionPool().hasAddress( new BoltServerAddress( "127.0.0.1", 9006 ) ) );
397398

driver/src/test/java/org/neo4j/driver/internal/util/ConcurrentRingSetTest.java renamed to driver/src/test/java/org/neo4j/driver/internal/util/ConcurrentRoundRobinSetTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,30 @@
2727
import static org.hamcrest.CoreMatchers.equalTo;
2828
import static org.hamcrest.MatcherAssert.assertThat;
2929

30-
public class ConcurrentRingSetTest
30+
public class ConcurrentRoundRobinSetTest
3131
{
3232

3333
@Test
3434
public void shouldBeAbleToIterateIndefinitely()
3535
{
3636
// Given
37-
ConcurrentRingSet<Integer> integers = new ConcurrentRingSet<>();
37+
ConcurrentRoundRobinSet<Integer> integers = new ConcurrentRoundRobinSet<>();
3838

3939
// When
4040
integers.addAll( asList( 0, 1, 2, 3, 4 ) );
4141

4242
// Then
4343
for ( int i = 0; i < 100; i++ )
4444
{
45-
assertThat(integers.next(), equalTo( i % 5));
45+
assertThat( integers.hop(), equalTo( i % 5 ) );
4646
}
4747
}
4848

4949
@Test
5050
public void shouldBeAbleToUseCustomComparator()
5151
{
5252
// Given
53-
ConcurrentRingSet<Integer> integers = new ConcurrentRingSet<>( new Comparator<Integer>()
53+
ConcurrentRoundRobinSet<Integer> integers = new ConcurrentRoundRobinSet<>( new Comparator<Integer>()
5454
{
5555
@Override
5656
public int compare( Integer o1, Integer o2 )
@@ -63,13 +63,13 @@ public int compare( Integer o1, Integer o2 )
6363
integers.addAll( asList( 0, 1, 2, 3, 4 ) );
6464

6565
// Then
66-
assertThat(integers.next(), equalTo( 4));
67-
assertThat(integers.next(), equalTo( 3));
68-
assertThat(integers.next(), equalTo( 2));
69-
assertThat(integers.next(), equalTo( 1));
70-
assertThat(integers.next(), equalTo( 0));
71-
assertThat(integers.next(), equalTo( 4));
72-
assertThat(integers.next(), equalTo( 3));
66+
assertThat( integers.hop(), equalTo( 4 ) );
67+
assertThat( integers.hop(), equalTo( 3 ) );
68+
assertThat( integers.hop(), equalTo( 2 ) );
69+
assertThat( integers.hop(), equalTo( 1 ) );
70+
assertThat( integers.hop(), equalTo( 0 ) );
71+
assertThat( integers.hop(), equalTo( 4 ) );
72+
assertThat( integers.hop(), equalTo( 3 ) );
7373
//....
7474
}
7575
}

driver/src/test/resources/not_able_to_write_server.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ C: PULL_ALL
88
S: FAILURE {"code": "Neo.ClientError.General.ForbiddenOnFollower", "message": "blabla"}
99
S: IGNORED
1010
C: ACK_FAILURE
11-
S: SUCCESS {}
11+
S: SUCCESS {}

0 commit comments

Comments
 (0)