104
104
import java .util .concurrent .atomic .AtomicLong ;
105
105
import java .util .concurrent .atomic .AtomicReference ;
106
106
import java .util .function .Consumer ;
107
+ import java .util .function .Supplier ;
107
108
import java .util .function .ToLongFunction ;
108
109
import javax .net .ssl .SSLEngine ;
109
110
import javax .net .ssl .SSLHandshakeException ;
@@ -178,7 +179,7 @@ public long applyAsLong(Object value) {
178
179
private final CredentialsProvider credentialsProvider ;
179
180
private final Runnable nettyClosing ;
180
181
private final int maxFrameSize ;
181
- private final boolean frameSizeCopped ;
182
+ private final boolean frameSizeCapped ;
182
183
private final EventLoopGroup eventLoopGroup ;
183
184
private final Map <String , String > clientProperties ;
184
185
private final String NETTY_HANDLER_FLUSH_CONSOLIDATION =
@@ -373,12 +374,18 @@ public void initChannel(SocketChannel ch) {
373
374
new TuneState (
374
375
parameters .requestedMaxFrameSize , (int ) parameters .requestedHeartbeat .getSeconds ());
375
376
this .clientProperties = clientProperties (parameters .clientProperties );
377
+ debug (() -> "exchanging peer properties" );
376
378
this .serverProperties = peerProperties ();
379
+ debug (() -> "peer properties exchanged" );
380
+ debug (() -> "starting SASL handshake" );
377
381
this .saslMechanisms = getSaslMechanisms ();
382
+ debug (() -> "SASL mechanisms supported by server ({})" , this .saslMechanisms );
383
+ debug (() -> "starting authentication" );
378
384
authenticate (this .credentialsProvider );
385
+ debug (() -> "authenticated" );
379
386
this .tuneState .await (Duration .ofSeconds (10 ));
380
387
this .maxFrameSize = this .tuneState .getMaxFrameSize ();
381
- this .frameSizeCopped = this .maxFrameSize () > 0 ;
388
+ this .frameSizeCapped = this .maxFrameSize () > 0 ;
382
389
LOGGER .debug (
383
390
"Connection tuned with max frame size {} and heartbeat {}" ,
384
391
this .maxFrameSize (),
@@ -418,6 +425,8 @@ public void initChannel(SocketChannel ch) {
418
425
started .set (true );
419
426
this .metricsCollector .openConnection ();
420
427
} catch (RuntimeException e ) {
428
+ LOGGER .debug (
429
+ "Error while opening connection {}: {}" , this .clientConnectionName , e .getMessage ());
421
430
this .closingSequence (null );
422
431
throw e ;
423
432
}
@@ -462,10 +471,14 @@ int maxFrameSize() {
462
471
return this .maxFrameSize ;
463
472
}
464
473
474
+ private int nextCorrelationId () {
475
+ return this .correlationSequence .getAndIncrement ();
476
+ }
477
+
465
478
private Map <String , String > peerProperties () {
466
479
int clientPropertiesSize = mapSize (this .clientProperties );
467
480
int length = 2 + 2 + 4 + clientPropertiesSize ;
468
- int correlationId = correlationSequence . incrementAndGet ();
481
+ int correlationId = nextCorrelationId ();
469
482
try {
470
483
ByteBuf bb = allocateNoCheck (length + 4 );
471
484
bb .writeInt (length );
@@ -474,6 +487,7 @@ private Map<String, String> peerProperties() {
474
487
bb .writeInt (correlationId );
475
488
writeMap (bb , this .clientProperties );
476
489
OutstandingRequest <Map <String , String >> request = outstandingRequest ();
490
+ LOGGER .debug ("Peer properties request has correlation ID {}" , correlationId );
477
491
outstandingRequests .put (correlationId , request );
478
492
channel .writeAndFlush (bb );
479
493
request .block ();
@@ -539,7 +553,7 @@ private SaslAuthenticateResponse sendSaslAuthenticate(
539
553
+ saslMechanism .getName ().length ()
540
554
+ 4
541
555
+ (challengeResponse == null ? 0 : challengeResponse .length );
542
- int correlationId = correlationSequence . incrementAndGet ();
556
+ int correlationId = nextCorrelationId ();
543
557
try {
544
558
ByteBuf bb = allocateNoCheck (length + 4 );
545
559
bb .writeInt (length );
@@ -569,7 +583,7 @@ private SaslAuthenticateResponse sendSaslAuthenticate(
569
583
570
584
private Map <String , String > open (String virtualHost ) {
571
585
int length = 2 + 2 + 4 + 2 + virtualHost .length ();
572
- int correlationId = correlationSequence . incrementAndGet ();
586
+ int correlationId = nextCorrelationId ();
573
587
try {
574
588
ByteBuf bb = allocate (length + 4 );
575
589
bb .writeInt (length );
@@ -610,7 +624,7 @@ void send(byte[] content) {
610
624
611
625
private void sendClose (short code , String reason ) {
612
626
int length = 2 + 2 + 4 + 2 + 2 + reason .length ();
613
- int correlationId = correlationSequence . incrementAndGet ();
627
+ int correlationId = nextCorrelationId ();
614
628
try {
615
629
ByteBuf bb = allocate (length + 4 );
616
630
bb .writeInt (length );
@@ -643,7 +657,7 @@ private void sendClose(short code, String reason) {
643
657
644
658
private List <String > getSaslMechanisms () {
645
659
int length = 2 + 2 + 4 ;
646
- int correlationId = correlationSequence . incrementAndGet ();
660
+ int correlationId = nextCorrelationId ();
647
661
try {
648
662
ByteBuf bb = allocateNoCheck (length + 4 );
649
663
bb .writeInt (length );
@@ -670,7 +684,7 @@ public Response create(String stream) {
670
684
671
685
public Response create (String stream , Map <String , String > arguments ) {
672
686
int length = 2 + 2 + 4 + 2 + stream .length () + mapSize (arguments );
673
- int correlationId = correlationSequence . incrementAndGet ();
687
+ int correlationId = nextCorrelationId ();
674
688
try {
675
689
ByteBuf bb = allocate (length + 4 );
676
690
bb .writeInt (length );
@@ -719,7 +733,7 @@ Response createSuperStream(
719
733
+ collectionSize (partitions )
720
734
+ collectionSize (bindingKeys )
721
735
+ mapSize (arguments );
722
- int correlationId = correlationSequence . incrementAndGet ();
736
+ int correlationId = nextCorrelationId ();
723
737
try {
724
738
ByteBuf bb = allocate (length + 4 );
725
739
bb .writeInt (length );
@@ -748,7 +762,7 @@ Response createSuperStream(
748
762
Response deleteSuperStream (String superStream ) {
749
763
this .superStreamManagementCommandVersionsCheck .run ();
750
764
int length = 2 + 2 + 4 + 2 + superStream .length ();
751
- int correlationId = correlationSequence . incrementAndGet ();
765
+ int correlationId = nextCorrelationId ();
752
766
try {
753
767
ByteBuf bb = allocate (length + 4 );
754
768
bb .writeInt (length );
@@ -808,7 +822,7 @@ private static ByteBuf writeMap(ByteBuf bb, Map<String, String> elements) {
808
822
}
809
823
810
824
ByteBuf allocate (ByteBufAllocator allocator , int capacity ) {
811
- if (frameSizeCopped && capacity > this .maxFrameSize ()) {
825
+ if (frameSizeCapped && capacity > this .maxFrameSize ()) {
812
826
throw new IllegalArgumentException (
813
827
"Cannot allocate "
814
828
+ capacity
@@ -832,7 +846,7 @@ private ByteBuf allocateNoCheck(int capacity) {
832
846
833
847
public Response delete (String stream ) {
834
848
int length = 2 + 2 + 4 + 2 + stream .length ();
835
- int correlationId = correlationSequence . incrementAndGet ();
849
+ int correlationId = nextCorrelationId ();
836
850
try {
837
851
ByteBuf bb = allocate (length + 4 );
838
852
bb .writeInt (length );
@@ -864,7 +878,7 @@ public Map<String, StreamMetadata> metadata(String... streams) {
864
878
throw new IllegalArgumentException ("At least one stream must be specified" );
865
879
}
866
880
int length = 2 + 2 + 4 + arraySize (streams ); // API code, version, correlation ID, array size
867
- int correlationId = correlationSequence . incrementAndGet ();
881
+ int correlationId = nextCorrelationId ();
868
882
try {
869
883
ByteBuf bb = allocate (length + 4 );
870
884
bb .writeInt (length );
@@ -897,7 +911,7 @@ public Response declarePublisher(byte publisherId, String publisherReference, St
897
911
"If specified, publisher reference must less than 256 characters" );
898
912
}
899
913
int length = 2 + 2 + 4 + 1 + 2 + publisherReferenceSize + 2 + stream .length ();
900
- int correlationId = correlationSequence . getAndIncrement ();
914
+ int correlationId = nextCorrelationId ();
901
915
try {
902
916
ByteBuf bb = allocate (length + 4 );
903
917
bb .writeInt (length );
@@ -928,7 +942,7 @@ public Response declarePublisher(byte publisherId, String publisherReference, St
928
942
929
943
public Response deletePublisher (byte publisherId ) {
930
944
int length = 2 + 2 + 4 + 1 ;
931
- int correlationId = correlationSequence . getAndIncrement ();
945
+ int correlationId = nextCorrelationId ();
932
946
try {
933
947
ByteBuf bb = allocate (length + 4 );
934
948
bb .writeInt (length );
@@ -1252,7 +1266,7 @@ public Response subscribe(
1252
1266
propertiesSize = mapSize (properties );
1253
1267
}
1254
1268
length += propertiesSize ;
1255
- int correlationId = correlationSequence . getAndIncrement ();
1269
+ int correlationId = nextCorrelationId ();
1256
1270
try {
1257
1271
ByteBuf bb = allocate (length + 4 );
1258
1272
bb .writeInt (length );
@@ -1320,7 +1334,7 @@ public QueryOffsetResponse queryOffset(String reference, String stream) {
1320
1334
}
1321
1335
1322
1336
int length = 2 + 2 + 4 + 2 + reference .length () + 2 + stream .length ();
1323
- int correlationId = correlationSequence . getAndIncrement ();
1337
+ int correlationId = nextCorrelationId ();
1324
1338
try {
1325
1339
ByteBuf bb = allocate (length + 4 );
1326
1340
bb .writeInt (length );
@@ -1361,7 +1375,7 @@ public long queryPublisherSequence(String publisherReference, String stream) {
1361
1375
}
1362
1376
1363
1377
int length = 2 + 2 + 4 + 2 + publisherReference .length () + 2 + stream .length ();
1364
- int correlationId = correlationSequence . getAndIncrement ();
1378
+ int correlationId = nextCorrelationId ();
1365
1379
try {
1366
1380
ByteBuf bb = allocate (length + 4 );
1367
1381
bb .writeInt (length );
@@ -1398,7 +1412,7 @@ public long queryPublisherSequence(String publisherReference, String stream) {
1398
1412
1399
1413
public Response unsubscribe (byte subscriptionId ) {
1400
1414
int length = 2 + 2 + 4 + 1 ;
1401
- int correlationId = correlationSequence . getAndIncrement ();
1415
+ int correlationId = nextCorrelationId ();
1402
1416
try {
1403
1417
ByteBuf bb = allocate (length + 4 );
1404
1418
bb .writeInt (length );
@@ -1445,6 +1459,8 @@ private void closeNetty() {
1445
1459
if (this .channel != null && this .channel .isOpen ()) {
1446
1460
LOGGER .debug ("Closing Netty channel" );
1447
1461
this .channel .close ().get (10 , TimeUnit .SECONDS );
1462
+ } else {
1463
+ LOGGER .debug ("No Netty channel to close" );
1448
1464
}
1449
1465
} catch (InterruptedException e ) {
1450
1466
LOGGER .info ("Channel closing has been interrupted" );
@@ -1530,7 +1546,7 @@ public List<String> route(String routingKey, String superStream) {
1530
1546
+ routingKey .length ()
1531
1547
+ 2
1532
1548
+ superStream .length (); // API code, version, correlation ID, 2 strings
1533
- int correlationId = correlationSequence . incrementAndGet ();
1549
+ int correlationId = nextCorrelationId ();
1534
1550
try {
1535
1551
ByteBuf bb = allocate (length + 4 );
1536
1552
bb .writeInt (length );
@@ -1565,7 +1581,7 @@ public List<String> partitions(String superStream) {
1565
1581
}
1566
1582
int length =
1567
1583
2 + 2 + 4 + 2 + superStream .length (); // API code, version, correlation ID, 1 string
1568
- int correlationId = correlationSequence . incrementAndGet ();
1584
+ int correlationId = nextCorrelationId ();
1569
1585
try {
1570
1586
ByteBuf bb = allocate (length + 4 );
1571
1587
bb .writeInt (length );
@@ -1593,7 +1609,7 @@ List<FrameHandlerInfo> exchangeCommandVersions() {
1593
1609
List <FrameHandlerInfo > commandVersions = ServerFrameHandler .commandVersions ();
1594
1610
int length = 2 + 2 + 4 + 4 ; // API code, version, correlation ID, array size
1595
1611
length += commandVersions .size () * (2 + 2 + 2 );
1596
- int correlationId = correlationSequence . incrementAndGet ();
1612
+ int correlationId = nextCorrelationId ();
1597
1613
try {
1598
1614
ByteBuf bb = allocate (length + 4 );
1599
1615
bb .writeInt (length );
@@ -1626,7 +1642,7 @@ StreamStatsResponse streamStats(String stream) {
1626
1642
throw new IllegalArgumentException ("stream must not be null" );
1627
1643
}
1628
1644
int length = 2 + 2 + 4 + 2 + stream .length (); // API code, version, correlation ID, 1 string
1629
- int correlationId = correlationSequence . incrementAndGet ();
1645
+ int correlationId = nextCorrelationId ();
1630
1646
try {
1631
1647
ByteBuf bb = allocate (length + 4 );
1632
1648
bb .writeInt (length );
@@ -2583,6 +2599,10 @@ public ClientParameters bootstrapCustomizer(Consumer<Bootstrap> bootstrapCustomi
2583
2599
return this ;
2584
2600
}
2585
2601
2602
+ Duration rpcTimeout () {
2603
+ return this .rpcTimeout ;
2604
+ }
2605
+
2586
2606
ClientParameters duplicate () {
2587
2607
ClientParameters duplicate = new ClientParameters ();
2588
2608
for (Field field : ClientParameters .class .getDeclaredFields ()) {
@@ -2926,4 +2946,10 @@ private <T> OutstandingRequest<T> outstandingRequest() {
2926
2946
public String toString () {
2927
2947
return "Client{connectionName='" + connectionName () + "'}" ;
2928
2948
}
2949
+
2950
+ private void debug (Supplier <String > format , Object ... args ) {
2951
+ if (LOGGER .isDebugEnabled ()) {
2952
+ LOGGER .debug ("Connection '" + this .clientConnectionName + "': " + format .get (), args );
2953
+ }
2954
+ }
2929
2955
}
0 commit comments