18
18
*/
19
19
package org .neo4j .driver .internal .cluster ;
20
20
21
+ import java .util .Collections ;
21
22
import java .util .HashSet ;
22
23
import java .util .Set ;
23
24
@@ -42,6 +43,8 @@ public class Rediscovery
42
43
private final ClusterCompositionProvider provider ;
43
44
private final HostNameResolver hostNameResolver ;
44
45
46
+ private boolean useInitialRouter ;
47
+
45
48
public Rediscovery ( BoltServerAddress initialRouter , RoutingSettings settings , Clock clock , Logger logger ,
46
49
ClusterCompositionProvider provider , HostNameResolver hostNameResolver )
47
50
{
@@ -53,9 +56,15 @@ public Rediscovery( BoltServerAddress initialRouter, RoutingSettings settings, C
53
56
this .hostNameResolver = hostNameResolver ;
54
57
}
55
58
56
- // Given the current routing table and connection pool, use the connection composition provider to fetch a new
57
- // cluster composition, which would be used to update the routing table and connection pool
58
- public ClusterComposition lookupClusterComposition ( ConnectionPool connections , RoutingTable routingTable )
59
+ /**
60
+ * Given the current routing table and connection pool, use the connection composition provider to fetch a new
61
+ * cluster composition, which would be used to update the routing table and connection pool.
62
+ *
63
+ * @param routingTable current routing table.
64
+ * @param connections connection pool.
65
+ * @return new cluster composition.
66
+ */
67
+ public ClusterComposition lookupClusterComposition ( RoutingTable routingTable , ConnectionPool connections )
59
68
{
60
69
int failures = 0 ;
61
70
@@ -65,7 +74,7 @@ public ClusterComposition lookupClusterComposition( ConnectionPool connections,
65
74
sleep ( waitTime );
66
75
start = clock .millis ();
67
76
68
- ClusterComposition composition = lookupClusterCompositionOnKnownRouters ( connections , routingTable );
77
+ ClusterComposition composition = lookup ( routingTable , connections );
69
78
if ( composition != null )
70
79
{
71
80
return composition ;
@@ -78,11 +87,56 @@ public ClusterComposition lookupClusterComposition( ConnectionPool connections,
78
87
}
79
88
}
80
89
81
- private ClusterComposition lookupClusterCompositionOnKnownRouters ( ConnectionPool connections ,
82
- RoutingTable routingTable )
90
+ private ClusterComposition lookup ( RoutingTable routingTable , ConnectionPool connections )
91
+ {
92
+ ClusterComposition composition ;
93
+
94
+ if ( useInitialRouter )
95
+ {
96
+ composition = lookupOnInitialRouterThenOnKnownRouters ( routingTable , connections );
97
+ useInitialRouter = false ;
98
+ }
99
+ else
100
+ {
101
+ composition = lookupOnKnownRoutersThenOnInitialRouter ( routingTable , connections );
102
+ }
103
+
104
+ if ( composition != null && !composition .hasWriters () )
105
+ {
106
+ useInitialRouter = true ;
107
+ }
108
+
109
+ return composition ;
110
+ }
111
+
112
+ private ClusterComposition lookupOnKnownRoutersThenOnInitialRouter ( RoutingTable routingTable ,
113
+ ConnectionPool connections )
114
+ {
115
+ Set <BoltServerAddress > seenServers = new HashSet <>();
116
+ ClusterComposition composition = lookupOnKnownRouters ( routingTable , connections , seenServers );
117
+ if ( composition == null )
118
+ {
119
+ return lookupOnInitialRouter ( routingTable , connections , seenServers );
120
+ }
121
+ return composition ;
122
+ }
123
+
124
+ private ClusterComposition lookupOnInitialRouterThenOnKnownRouters ( RoutingTable routingTable ,
125
+ ConnectionPool connections )
126
+ {
127
+ Set <BoltServerAddress > seenServers = Collections .emptySet ();
128
+ ClusterComposition composition = lookupOnInitialRouter ( routingTable , connections , seenServers );
129
+ if ( composition == null )
130
+ {
131
+ return lookupOnKnownRouters ( routingTable , connections , new HashSet <BoltServerAddress >() );
132
+ }
133
+ return composition ;
134
+ }
135
+
136
+ private ClusterComposition lookupOnKnownRouters ( RoutingTable routingTable , ConnectionPool connections ,
137
+ Set <BoltServerAddress > seenServers )
83
138
{
84
139
int size = routingTable .routerSize ();
85
- Set <BoltServerAddress > triedServers = new HashSet <>();
86
140
for ( int i = 0 ; i < size ; i ++ )
87
141
{
88
142
BoltServerAddress address = routingTable .nextRouter ();
@@ -91,22 +145,28 @@ private ClusterComposition lookupClusterCompositionOnKnownRouters( ConnectionPoo
91
145
break ;
92
146
}
93
147
94
- ClusterComposition composition = lookupClusterCompositionOnRouter ( address , connections , routingTable );
148
+ ClusterComposition composition = lookupOnRouter ( address , routingTable , connections );
95
149
if ( composition != null )
96
150
{
97
151
return composition ;
98
152
}
99
153
else
100
154
{
101
- triedServers .add ( address );
155
+ seenServers .add ( address );
102
156
}
103
157
}
104
158
159
+ return null ;
160
+ }
161
+
162
+ private ClusterComposition lookupOnInitialRouter ( RoutingTable routingTable ,
163
+ ConnectionPool connections , Set <BoltServerAddress > triedServers )
164
+ {
105
165
Set <BoltServerAddress > ips = hostNameResolver .resolve ( initialRouter );
106
166
ips .removeAll ( triedServers );
107
167
for ( BoltServerAddress address : ips )
108
168
{
109
- ClusterComposition composition = lookupClusterCompositionOnRouter ( address , connections , routingTable );
169
+ ClusterComposition composition = lookupOnRouter ( address , routingTable , connections );
110
170
if ( composition != null )
111
171
{
112
172
return composition ;
@@ -116,8 +176,8 @@ private ClusterComposition lookupClusterCompositionOnKnownRouters( ConnectionPoo
116
176
return null ;
117
177
}
118
178
119
- private ClusterComposition lookupClusterCompositionOnRouter ( BoltServerAddress routerAddress ,
120
- ConnectionPool connections , RoutingTable routingTable )
179
+ private ClusterComposition lookupOnRouter ( BoltServerAddress routerAddress , RoutingTable routingTable ,
180
+ ConnectionPool connections )
121
181
{
122
182
ClusterCompositionResponse response ;
123
183
try ( Connection connection = connections .acquire ( routerAddress ) )
@@ -139,11 +199,7 @@ private ClusterComposition lookupClusterCompositionOnRouter( BoltServerAddress r
139
199
140
200
ClusterComposition cluster = response .clusterComposition ();
141
201
logger .info ( "Got cluster composition %s" , cluster );
142
- if ( cluster .hasWriters () )
143
- {
144
- return cluster ;
145
- }
146
- return null ;
202
+ return cluster ;
147
203
}
148
204
149
205
private void sleep ( long millis )
0 commit comments