Skip to content

Commit fd2bd15

Browse files
author
Zhen
committed
Renamed some methods and classes to better describe their purpose
1 parent 14f1701 commit fd2bd15

File tree

11 files changed

+160
-137
lines changed

11 files changed

+160
-137
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ private ClusterComposition( long expirationTimestamp )
6565
this.routers.addAll( routers );
6666
}
6767

68-
public boolean isValid()
68+
public boolean hasWriters()
6969
{
7070
return !writers.isEmpty();
7171
}
72-
public boolean isIllegalResponse()
72+
public boolean hasRoutersAndReaders()
7373
{
7474
return routers.isEmpty() || readers.isEmpty();
7575
}
@@ -111,8 +111,7 @@ public static ClusterComposition parse( Record record, long now )
111111
return null;
112112
}
113113

114-
final ClusterComposition result;
115-
result = new ClusterComposition( expirationTimestamp( now, record ) );
114+
final ClusterComposition result = new ClusterComposition( expirationTimestamp( now, record ) );
116115
record.get( "servers" ).asList( new Function<Value,Void>()
117116
{
118117
@Override

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

Lines changed: 1 addition & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -18,94 +18,9 @@
1818
*/
1919
package org.neo4j.driver.internal.cluster;
2020

21-
import java.util.List;
22-
2321
import org.neo4j.driver.internal.spi.Connection;
24-
import org.neo4j.driver.internal.util.Clock;
25-
import org.neo4j.driver.v1.Logger;
26-
import org.neo4j.driver.v1.Record;
27-
import org.neo4j.driver.v1.exceptions.ClientException;
28-
import org.neo4j.driver.v1.exceptions.ProtocolException;
29-
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
30-
import org.neo4j.driver.v1.exceptions.value.ValueException;
31-
32-
import static java.lang.String.format;
3322

3423
public interface ClusterCompositionProvider
3524
{
36-
GetClusterCompositionResponse getClusterComposition( Connection connection );
37-
38-
class Default implements ClusterCompositionProvider
39-
{
40-
private String GET_SERVERS = "dbms.cluster.routing.getServers";
41-
private String PROTOCOL_ERROR_MESSAGE = "Failed to parse '" + GET_SERVERS + "' result received from server " +
42-
"due to ";
43-
44-
private final Clock clock;
45-
private final Logger log;
46-
private final GetServersRunner getServersRunner;
47-
48-
public Default( Clock clock, Logger log )
49-
{
50-
this( clock, log, new GetServersRunner() );
51-
}
52-
53-
Default( Clock clock, Logger log, GetServersRunner getServersRunner )
54-
{
55-
this.clock = clock;
56-
this.log = log;
57-
this.getServersRunner = getServersRunner;
58-
}
59-
60-
@Override
61-
public GetClusterCompositionResponse getClusterComposition( Connection connection )
62-
{
63-
List<Record> records = null;
64-
65-
// failed to invoke procedure
66-
try
67-
{
68-
records = getServersRunner.run( connection );
69-
}
70-
catch ( ClientException e )
71-
{
72-
return new GetClusterCompositionResponse.Failure( new ServiceUnavailableException( format(
73-
"Failed to call '%s' procedure on server. " +
74-
"Please make sure that there is a Neo4j 3.1+ causal cluster up running.", GET_SERVERS ), e ) );
75-
}
76-
77-
log.info( "Got getServers response: %s", records );
78-
long now = clock.millis();
79-
80-
// the record size is wrong
81-
if ( records.size() != 1 )
82-
{
83-
return new GetClusterCompositionResponse.Failure( new ProtocolException( format(
84-
"%srecords received '%s' is too few or too many.", PROTOCOL_ERROR_MESSAGE,
85-
records.size() ) ) );
86-
}
87-
88-
// failed to parse the record
89-
ClusterComposition cluster;
90-
try
91-
{
92-
cluster = ClusterComposition.parse( records.get( 0 ), now );
93-
}
94-
catch ( ValueException e )
95-
{
96-
return new GetClusterCompositionResponse.Failure( new ProtocolException( format(
97-
"%sunparsable record received.", PROTOCOL_ERROR_MESSAGE ), e ) );
98-
}
99-
100-
// the cluster result is not a legal reply
101-
if ( cluster.isIllegalResponse() )
102-
{
103-
return new GetClusterCompositionResponse.Failure( new ProtocolException( format(
104-
"%sno router or reader found in response.", PROTOCOL_ERROR_MESSAGE ) ) );
105-
}
106-
107-
// all good
108-
return new GetClusterCompositionResponse.Success( cluster );
109-
}
110-
}
25+
ClusterCompositionResponse getClusterComposition( Connection connection );
11126
}

driver/src/main/java/org/neo4j/driver/internal/cluster/GetClusterCompositionResponse.java renamed to driver/src/main/java/org/neo4j/driver/internal/cluster/ClusterCompositionResponse.java

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

21-
public interface GetClusterCompositionResponse
21+
public interface ClusterCompositionResponse
2222
{
2323
ClusterComposition clusterComposition();
2424

25-
class Failure implements GetClusterCompositionResponse
25+
class Failure implements ClusterCompositionResponse
2626
{
2727
private final RuntimeException error;
2828

@@ -38,7 +38,7 @@ public ClusterComposition clusterComposition()
3838
}
3939
}
4040

41-
class Success implements GetClusterCompositionResponse
41+
class Success implements ClusterCompositionResponse
4242
{
4343
private final ClusterComposition cluster;
4444

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.cluster;
20+
21+
import java.util.List;
22+
23+
import org.neo4j.driver.internal.spi.Connection;
24+
import org.neo4j.driver.internal.util.Clock;
25+
import org.neo4j.driver.v1.Logger;
26+
import org.neo4j.driver.v1.Record;
27+
import org.neo4j.driver.v1.exceptions.ClientException;
28+
import org.neo4j.driver.v1.exceptions.ProtocolException;
29+
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
30+
import org.neo4j.driver.v1.exceptions.value.ValueException;
31+
32+
import static java.lang.String.format;
33+
34+
public class GetServersProcedureClusterCompositionProvider implements ClusterCompositionProvider
35+
{
36+
private final String GET_SERVERS = "dbms.cluster.routing.getServers";
37+
private final String PROTOCOL_ERROR_MESSAGE = "Failed to parse '" + GET_SERVERS +
38+
"' result received from server due to ";
39+
40+
private final Clock clock;
41+
private final Logger log;
42+
private final GetServersProcedureRunner getServersRunner;
43+
44+
public GetServersProcedureClusterCompositionProvider( Clock clock, Logger log )
45+
{
46+
this( clock, log, new GetServersProcedureRunner() );
47+
}
48+
49+
GetServersProcedureClusterCompositionProvider( Clock clock, Logger log, GetServersProcedureRunner getServersRunner )
50+
{
51+
this.clock = clock;
52+
this.log = log;
53+
this.getServersRunner = getServersRunner;
54+
}
55+
56+
@Override
57+
public ClusterCompositionResponse getClusterComposition( Connection connection )
58+
{
59+
List<Record> records = null;
60+
61+
// failed to invoke procedure
62+
try
63+
{
64+
records = getServersRunner.run( connection );
65+
}
66+
catch ( ClientException e )
67+
{
68+
return new ClusterCompositionResponse.Failure( new ServiceUnavailableException( format(
69+
"Failed to call '%s' procedure on server. " +
70+
"Please make sure that there is a Neo4j 3.1+ causal cluster up running.", GET_SERVERS ), e ) );
71+
}
72+
73+
log.info( "Got getServers response: %s", records );
74+
long now = clock.millis();
75+
76+
// the record size is wrong
77+
if ( records.size() != 1 )
78+
{
79+
return new ClusterCompositionResponse.Failure( new ProtocolException( format(
80+
"%srecords received '%s' is too few or too many.", PROTOCOL_ERROR_MESSAGE,
81+
records.size() ) ) );
82+
}
83+
84+
// failed to parse the record
85+
ClusterComposition cluster;
86+
try
87+
{
88+
cluster = ClusterComposition.parse( records.get( 0 ), now );
89+
}
90+
catch ( ValueException e )
91+
{
92+
return new ClusterCompositionResponse.Failure( new ProtocolException( format(
93+
"%sunparsable record received.", PROTOCOL_ERROR_MESSAGE ), e ) );
94+
}
95+
96+
// the cluster result is not a legal reply
97+
if ( cluster.hasRoutersAndReaders() )
98+
{
99+
return new ClusterCompositionResponse.Failure( new ProtocolException( format(
100+
"%sno router or reader found in response.", PROTOCOL_ERROR_MESSAGE ) ) );
101+
}
102+
103+
// all good
104+
return new ClusterCompositionResponse.Success( cluster );
105+
}
106+
}

driver/src/main/java/org/neo4j/driver/internal/cluster/GetServersRunner.java renamed to driver/src/main/java/org/neo4j/driver/internal/cluster/GetServersProcedureRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
import org.neo4j.driver.v1.Record;
2727
import org.neo4j.driver.v1.Statement;
2828

29-
public class GetServersRunner
29+
public class GetServersProcedureRunner
3030
{
31-
private String CALL_GET_SERVERS = "CALL dbms.cluster.routing.getServers";
31+
private final String CALL_GET_SERVERS = "CALL dbms.cluster.routing.getServers";
3232

3333
public List<Record> run( Connection connection )
3434
{

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
@@ -47,7 +47,7 @@ public LoadBalancer(
4747
BoltServerAddress... routingAddresses ) throws ServiceUnavailableException
4848
{
4949
this( settings, clock, log, connections, new ClusterRoutingTable( clock, routingAddresses ),
50-
new ClusterCompositionProvider.Default( clock, log ) );
50+
new GetServersProcedureClusterCompositionProvider( clock, log ) );
5151
}
5252

5353
private LoadBalancer(

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.neo4j.driver.internal.spi.ConnectionPool;
2424
import org.neo4j.driver.internal.util.Clock;
2525
import org.neo4j.driver.v1.Logger;
26-
import org.neo4j.driver.v1.exceptions.ProtocolException;
2726
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
2827

2928
import static java.lang.String.format;
@@ -48,9 +47,9 @@ public Rediscovery( RoutingSettings settings, Clock clock, Logger logger, Cluste
4847
// Given the current routing table and connection pool, use the connection composition provider to fetch a new
4948
// cluster composition, which would be used to update the routing table and connection pool
5049
public ClusterComposition lookupRoutingTable( ConnectionPool connections, RoutingTable routingTable )
51-
throws InterruptedException, ServiceUnavailableException, ProtocolException
50+
throws InterruptedException
5251
{
53-
assertRouterIsNotEmpty( routingTable.routerSize() );
52+
assertHasRouters( routingTable );
5453
int failures = 0;
5554

5655
for ( long start = clock.millis(), delay = 0; ; delay = Math.max( settings.retryTimeoutDelay, delay * 2 ) )
@@ -62,15 +61,16 @@ public ClusterComposition lookupRoutingTable( ConnectionPool connections, Routin
6261
}
6362
start = clock.millis();
6463

65-
for ( int i = 0, size = routingTable.routerSize(); i < size; i++ )
64+
int size = routingTable.routerSize();
65+
for ( int i = 0; i < size; i++ )
6666
{
6767
BoltServerAddress address = routingTable.nextRouter();
6868
if ( address == null )
6969
{
7070
throw new ServiceUnavailableException( NO_ROUTERS_AVAILABLE );
7171
}
7272

73-
GetClusterCompositionResponse response = null;
73+
ClusterCompositionResponse response = null;
7474
try ( Connection connection = connections.acquire( address ) )
7575
{
7676
response = provider.getClusterComposition( connection );
@@ -81,13 +81,13 @@ public ClusterComposition lookupRoutingTable( ConnectionPool connections, Routin
8181
logger.error( format( "Failed to connect to routing server '%s'.", address ), e );
8282
routingTable.removeRouter( address );
8383

84-
assertRouterIsNotEmpty( routingTable.routerSize() );
84+
assertHasRouters( routingTable );
8585
continue;
8686
}
8787

8888
ClusterComposition cluster = response.clusterComposition();
8989
logger.info( "Got cluster composition %s", cluster );
90-
if ( cluster.isValid() )
90+
if ( cluster.hasWriters() )
9191
{
9292
return cluster;
9393
}
@@ -99,9 +99,9 @@ public ClusterComposition lookupRoutingTable( ConnectionPool connections, Routin
9999
}
100100
}
101101

102-
private void assertRouterIsNotEmpty( int size )
102+
private void assertHasRouters( RoutingTable table )
103103
{
104-
if ( size == 0 )
104+
if ( table.routerSize() == 0 )
105105
{
106106
throw new ServiceUnavailableException( NO_ROUTERS_AVAILABLE );
107107
}

driver/src/main/java/org/neo4j/driver/v1/exceptions/ProtocolException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
*/
2525
public class ProtocolException extends Neo4jException
2626
{
27-
private static String CODE = "Protocol violation: ";
27+
private static final String CODE = "Protocol violation: ";
28+
2829
public ProtocolException( String message )
2930
{
3031
super( CODE + message );

0 commit comments

Comments
 (0)