18
18
*/
19
19
package org .neo4j .driver .internal ;
20
20
21
- import java .util .Collections ;
22
- import java .util .Comparator ;
23
- import java .util .HashSet ;
24
21
import java .util .List ;
25
22
import java .util .Set ;
26
23
29
26
import org .neo4j .driver .internal .spi .Connection ;
30
27
import org .neo4j .driver .internal .spi .ConnectionPool ;
31
28
import org .neo4j .driver .internal .util .Clock ;
32
- import org .neo4j .driver .internal .util .ConcurrentRoundRobinSet ;
33
29
import org .neo4j .driver .v1 .AccessMode ;
34
- import org .neo4j .driver .v1 .Logger ;
35
30
import org .neo4j .driver .v1 .Logging ;
36
31
import org .neo4j .driver .v1 .Record ;
37
32
import org .neo4j .driver .v1 .Session ;
@@ -48,20 +43,7 @@ public class RoutingDriver extends BaseDriver
48
43
{
49
44
private static final String GET_SERVERS = "dbms.cluster.routing.getServers" ;
50
45
private static final long MAX_TTL = Long .MAX_VALUE / 1000L ;
51
- private final static Comparator <BoltServerAddress > COMPARATOR = new Comparator <BoltServerAddress >()
52
- {
53
- @ Override
54
- public int compare ( BoltServerAddress o1 , BoltServerAddress o2 )
55
- {
56
- int compare = o1 .host ().compareTo ( o2 .host () );
57
- if ( compare == 0 )
58
- {
59
- compare = Integer .compare ( o1 .port (), o2 .port () );
60
- }
61
46
62
- return compare ;
63
- }
64
- };
65
47
private final ConnectionPool connections ;
66
48
private final Function <Connection ,Session > sessionProvider ;
67
49
private final Clock clock ;
@@ -197,117 +179,6 @@ List<BoltServerAddress> addresses()
197
179
}
198
180
}
199
181
200
- private static class ClusterView
201
- {
202
- private static final int MIN_ROUTERS = 1 ;
203
-
204
- private final ConcurrentRoundRobinSet <BoltServerAddress > routingServers =
205
- new ConcurrentRoundRobinSet <>( COMPARATOR );
206
- private final ConcurrentRoundRobinSet <BoltServerAddress > readServers =
207
- new ConcurrentRoundRobinSet <>( COMPARATOR );
208
- private final ConcurrentRoundRobinSet <BoltServerAddress > writeServers =
209
- new ConcurrentRoundRobinSet <>( COMPARATOR );
210
- private final Clock clock ;
211
- private final long expires ;
212
- private final Logger log ;
213
-
214
- private ClusterView ( long expires , Clock clock , Logger log )
215
- {
216
- this .expires = expires ;
217
- this .clock = clock ;
218
- this .log = log ;
219
- }
220
-
221
- public void addRouter ( BoltServerAddress router )
222
- {
223
- this .routingServers .add ( router );
224
- }
225
-
226
- public boolean isStale ()
227
- {
228
- return expires < clock .millis () ||
229
- routingServers .size () <= MIN_ROUTERS ||
230
- readServers .isEmpty () ||
231
- writeServers .isEmpty ();
232
- }
233
-
234
- Set <BoltServerAddress > all ()
235
- {
236
- HashSet <BoltServerAddress > all =
237
- new HashSet <>( routingServers .size () + readServers .size () + writeServers .size () );
238
- all .addAll ( routingServers );
239
- all .addAll ( readServers );
240
- all .addAll ( writeServers );
241
- return all ;
242
- }
243
-
244
- public int numberOfRouters ()
245
- {
246
- return routingServers .size ();
247
- }
248
-
249
- public BoltServerAddress nextRouter ()
250
- {
251
- return routingServers .hop ();
252
- }
253
-
254
- public BoltServerAddress nextReader ()
255
- {
256
- return readServers .hop ();
257
- }
258
-
259
- public BoltServerAddress nextWriter ()
260
- {
261
- return writeServers .hop ();
262
- }
263
-
264
- public void addReaders ( List <BoltServerAddress > addresses )
265
- {
266
- readServers .addAll ( addresses );
267
- }
268
-
269
- public void addWriters ( List <BoltServerAddress > addresses )
270
- {
271
- writeServers .addAll ( addresses );
272
- }
273
-
274
- public void addRouters ( List <BoltServerAddress > addresses )
275
- {
276
- routingServers .addAll ( addresses );
277
- }
278
-
279
- public void remove ( BoltServerAddress address )
280
- {
281
- if ( routingServers .remove ( address ) )
282
- {
283
- log .debug ( "Removing %s from routers" , address .toString () );
284
- }
285
- if ( readServers .remove ( address ) )
286
- {
287
- log .debug ( "Removing %s from readers" , address .toString () );
288
- }
289
- if ( writeServers .remove ( address ) )
290
- {
291
- log .debug ( "Removing %s from writers" , address .toString () );
292
- }
293
- }
294
-
295
- public boolean removeWriter ( BoltServerAddress address )
296
- {
297
- return writeServers .remove ( address );
298
- }
299
-
300
- public int numberOfReaders ()
301
- {
302
- return readServers .size ();
303
- }
304
-
305
- public int numberOfWriters ()
306
- {
307
- return writeServers .size ();
308
- }
309
- }
310
-
311
182
private List <ServerInfo > servers ( Record record )
312
183
{
313
184
return record .get ( "servers" ).asList ( new Function <Value ,ServerInfo >()
@@ -371,7 +242,8 @@ public Session session()
371
242
@ Override
372
243
public Session session ( final AccessMode mode )
373
244
{
374
- return new RoutingNetworkSession ( mode , acquireConnection ( mode ),
245
+ Connection connection = acquireConnection ( mode );
246
+ return new RoutingNetworkSession ( new NetworkSession ( connection ), mode , connection .address (),
375
247
new RoutingErrorHandler ()
376
248
{
377
249
@ Override
@@ -458,19 +330,19 @@ public void close()
458
330
//For testing
459
331
public Set <BoltServerAddress > routingServers ()
460
332
{
461
- return Collections . unmodifiableSet ( clusterView .routingServers );
333
+ return clusterView .routingServers ( );
462
334
}
463
335
464
336
//For testing
465
337
public Set <BoltServerAddress > readServers ()
466
338
{
467
- return Collections . unmodifiableSet ( clusterView .readServers );
339
+ return clusterView .readServers ( );
468
340
}
469
341
470
342
//For testing
471
343
public Set <BoltServerAddress > writeServers ()
472
344
{
473
- return Collections . unmodifiableSet ( clusterView .writeServers );
345
+ return clusterView .writeServers ( );
474
346
}
475
347
476
348
//For testing
0 commit comments