29
29
import com .datastax .oss .driver .api .core .config .DefaultDriverOption ;
30
30
import com .datastax .oss .driver .api .core .config .DriverConfig ;
31
31
import com .datastax .oss .driver .api .core .config .DriverExecutionProfile ;
32
- import com .datastax .oss .driver .api .core .context .DriverContext ;
33
32
import com .datastax .oss .driver .api .core .metadata .EndPoint ;
34
33
import com .datastax .oss .driver .api .core .metadata .Node ;
35
34
import com .datastax .oss .driver .api .core .metadata .NodeShardingInfo ;
53
52
import io .netty .channel .ChannelInitializer ;
54
53
import io .netty .channel .ChannelOption ;
55
54
import io .netty .channel .ChannelPipeline ;
56
- import java .io .IOException ;
57
55
import java .net .InetSocketAddress ;
58
- import java .net .ServerSocket ;
59
56
import java .util .List ;
60
57
import java .util .Map ;
61
58
import java .util .Optional ;
62
59
import java .util .concurrent .CompletableFuture ;
63
60
import java .util .concurrent .CompletionStage ;
64
61
import java .util .concurrent .CopyOnWriteArrayList ;
65
62
import java .util .concurrent .atomic .AtomicBoolean ;
66
- import java .util .concurrent .atomic .AtomicInteger ;
67
63
import net .jcip .annotations .ThreadSafe ;
68
64
import org .slf4j .Logger ;
69
65
import org .slf4j .LoggerFactory ;
@@ -193,28 +189,43 @@ CompletionStage<DriverChannel> connect(
193
189
isNegotiating = true ;
194
190
}
195
191
192
+ PortIterator portIterator = null ;
193
+
194
+ if (shardId != null && shardingInfo != null ) {
195
+ int lowestPort =
196
+ context
197
+ .getConfig ()
198
+ .getDefaultProfile ()
199
+ .getInt (DefaultDriverOption .ADVANCED_SHARD_AWARENESS_PORT_LOW );
200
+ int highestPort =
201
+ context
202
+ .getConfig ()
203
+ .getDefaultProfile ()
204
+ .getInt (DefaultDriverOption .ADVANCED_SHARD_AWARENESS_PORT_HIGH );
205
+ portIterator =
206
+ new PortIterator (lowestPort , highestPort , shardingInfo .getShardsCount (), shardId );
207
+ }
208
+
196
209
connect (
197
210
endPoint ,
198
- shardingInfo ,
199
- shardId ,
200
211
options ,
201
212
nodeMetricUpdater ,
202
213
currentVersion ,
203
214
isNegotiating ,
204
215
attemptedVersions ,
216
+ portIterator ,
205
217
resultFuture );
206
218
return resultFuture ;
207
219
}
208
220
209
221
private void connect (
210
222
EndPoint endPoint ,
211
- NodeShardingInfo shardingInfo ,
212
- Integer shardId ,
213
223
DriverChannelOptions options ,
214
224
NodeMetricUpdater nodeMetricUpdater ,
215
225
ProtocolVersion currentVersion ,
216
226
boolean isNegotiating ,
217
227
List <ProtocolVersion > attemptedVersions ,
228
+ PortIterator portIterator ,
218
229
CompletableFuture <DriverChannel > resultFuture ) {
219
230
220
231
NettyOptions nettyOptions = context .getNettyOptions ();
@@ -230,26 +241,11 @@ private void connect(
230
241
nettyOptions .afterBootstrapInitialized (bootstrap );
231
242
232
243
ChannelFuture connectFuture ;
233
- if (shardId == null || shardingInfo == null ) {
234
- if (shardId != null ) {
235
- LOG .debug (
236
- "Requested connection to shard {} but shardingInfo is currently missing for Node at endpoint {}. Falling back to arbitrary local port." ,
237
- shardId ,
238
- endPoint );
239
- }
244
+ if (portIterator == null ) {
240
245
connectFuture = bootstrap .connect (endPoint .resolve ());
241
246
} else {
242
- int localPort =
243
- PortAllocator .getNextAvailablePort (shardingInfo .getShardsCount (), shardId , context );
244
- if (localPort == -1 ) {
245
- LOG .warn (
246
- "Could not find free port for shard {} at {}. Falling back to arbitrary local port." ,
247
- shardId ,
248
- endPoint );
249
- connectFuture = bootstrap .connect (endPoint .resolve ());
250
- } else {
251
- connectFuture = bootstrap .connect (endPoint .resolve (), new InetSocketAddress (localPort ));
252
- }
247
+ connectFuture =
248
+ bootstrap .connect (endPoint .resolve (), new InetSocketAddress (portIterator .get ()));
253
249
}
254
250
255
251
connectFuture .addListener (
@@ -299,19 +295,30 @@ private void connect(
299
295
downgraded .get ());
300
296
connect (
301
297
endPoint ,
302
- shardingInfo ,
303
- shardId ,
304
298
options ,
305
299
nodeMetricUpdater ,
306
300
downgraded .get (),
307
301
true ,
308
302
attemptedVersions ,
303
+ portIterator ,
309
304
resultFuture );
310
305
} else {
311
306
resultFuture .completeExceptionally (
312
307
UnsupportedProtocolVersionException .forNegotiation (
313
308
endPoint , attemptedVersions ));
314
309
}
310
+ } else if (portIterator != null
311
+ && isBindException (error )
312
+ && portIterator .bumpPortUp ()) {
313
+ connect (
314
+ endPoint ,
315
+ options ,
316
+ nodeMetricUpdater ,
317
+ currentVersion ,
318
+ true ,
319
+ attemptedVersions ,
320
+ portIterator ,
321
+ resultFuture );
315
322
} else {
316
323
// Note: might be completed already if the failure happened in initializer(), this is
317
324
// fine
@@ -321,6 +328,16 @@ private void connect(
321
328
});
322
329
}
323
330
331
+ private static boolean isBindException (Throwable error ) {
332
+ while (error != null ) {
333
+ if (error instanceof java .net .BindException ) {
334
+ return true ;
335
+ }
336
+ error = error .getCause ();
337
+ }
338
+ return false ;
339
+ }
340
+
324
341
@ VisibleForTesting
325
342
ChannelInitializer <Channel > initializer (
326
343
EndPoint endPoint ,
@@ -330,7 +347,7 @@ ChannelInitializer<Channel> initializer(
330
347
CompletableFuture <DriverChannel > resultFuture ) {
331
348
return new ChannelFactoryInitializer (
332
349
endPoint , protocolVersion , options , nodeMetricUpdater , resultFuture );
333
- };
350
+ }
334
351
335
352
class ChannelFactoryInitializer extends ChannelInitializer <Channel > {
336
353
@@ -440,87 +457,27 @@ protected void initChannel(Channel channel) {
440
457
}
441
458
}
442
459
443
- static class PortAllocator {
444
- private static final AtomicInteger lastPort = new AtomicInteger (-1 );
445
- private static final Logger LOG = LoggerFactory .getLogger (PortAllocator .class );
460
+ static class PortIterator {
461
+ private final int highestPort ;
462
+ private int currentPort ;
463
+ private final int shardCount ;
446
464
447
- public static int getNextAvailablePort (int shardCount , int shardId , DriverContext context ) {
448
- int lowPort =
449
- context
450
- .getConfig ()
451
- .getDefaultProfile ()
452
- .getInt (DefaultDriverOption .ADVANCED_SHARD_AWARENESS_PORT_LOW );
453
- int highPort =
454
- context
455
- .getConfig ()
456
- .getDefaultProfile ()
457
- .getInt (DefaultDriverOption .ADVANCED_SHARD_AWARENESS_PORT_HIGH );
458
- if (highPort - lowPort < shardCount ) {
459
- LOG .error (
460
- "There is not enough ports in range [{},{}] for {} shards. Update your configuration." ,
461
- lowPort ,
462
- highPort ,
463
- shardCount );
464
- }
465
- int lastPortValue , foundPort = -1 ;
466
- do {
467
- lastPortValue = lastPort .get ();
468
-
469
- // We will scan from lastPortValue
470
- // (or lowPort is there was no lastPort or lastPort is too low)
471
- int scanStart = lastPortValue == -1 ? lowPort : lastPortValue ;
472
- if (scanStart < lowPort ) {
473
- scanStart = lowPort ;
474
- }
475
-
476
- // Round it up to "% shardCount == shardId"
477
- scanStart += (shardCount - scanStart % shardCount ) + shardId ;
478
-
479
- // Scan from scanStart upwards to highPort.
480
- for (int port = scanStart ; port <= highPort ; port += shardCount ) {
481
- if (isTcpPortAvailable (port )) {
482
- foundPort = port ;
483
- break ;
484
- }
485
- }
486
-
487
- // If we started scanning from a high scanStart port
488
- // there might have been not enough ports left that are
489
- // smaller than highPort. Scan from the beginning
490
- // from the lowPort.
491
- if (foundPort == -1 ) {
492
- scanStart = lowPort + (shardCount - lowPort % shardCount ) + shardId ;
493
-
494
- for (int port = scanStart ; port <= highPort ; port += shardCount ) {
495
- if (isTcpPortAvailable (port )) {
496
- foundPort = port ;
497
- break ;
498
- }
499
- }
500
- }
501
-
502
- // No luck! All ports taken!
503
- if (foundPort == -1 ) {
504
- return -1 ;
505
- }
506
- } while (!lastPort .compareAndSet (lastPortValue , foundPort ));
465
+ PortIterator (int lowestPort , int highestPort , int shardCount , int shardId ) {
466
+ this .highestPort = highestPort ;
467
+ this .currentPort = lowestPort + (shardCount - lowestPort % shardCount ) + shardId ;
468
+ this .shardCount = shardCount ;
469
+ }
507
470
508
- return foundPort ;
471
+ public Integer get () {
472
+ return currentPort ;
509
473
}
510
474
511
- public static boolean isTcpPortAvailable (int port ) {
512
- try {
513
- ServerSocket serverSocket = new ServerSocket ();
514
- try {
515
- serverSocket .setReuseAddress (false );
516
- serverSocket .bind (new InetSocketAddress (port ), 1 );
517
- return true ;
518
- } finally {
519
- serverSocket .close ();
520
- }
521
- } catch (IOException ex ) {
475
+ public boolean bumpPortUp () {
476
+ if (this .currentPort + shardCount > highestPort ) {
522
477
return false ;
523
478
}
479
+ this .currentPort += shardCount ;
480
+ return true ;
524
481
}
525
482
}
526
483
}
0 commit comments