@@ -67,6 +67,8 @@ class PostOffice {
67
67
68
68
private static final String WILL_PUBLISHER = "will_publisher" ;
69
69
private static final String INTERNAL_PUBLISHER = "internal_publisher" ;
70
+ public static final String BT_ROUTE_TARGET = "Route to target session" ;
71
+ public static final String BT_PUB_IN = "PUB in" ;
70
72
71
73
/**
72
74
* Maps the failed packetID per clientId (id client source, id_packet) -> [id client target]
@@ -621,14 +623,14 @@ CompletableFuture<Void> receivedPublishQos0(MQTTConnection connection, String us
621
623
final Topic topic = new Topic (msg .variableHeader ().topicName ());
622
624
if (!authorizator .canWrite (topic , username , clientID )) {
623
625
LOG .error ("client is not authorized to publish on topic: {}" , topic );
624
- ReferenceCountUtil .release (msg );
626
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, auth failed" );
625
627
return CompletableFuture .completedFuture (null );
626
628
}
627
629
628
630
if (isPayloadFormatToValidate (msg )) {
629
631
if (!validatePayloadAsUTF8 (msg )) {
630
632
LOG .warn ("Received not valid UTF-8 payload when payload format indicator was enabled (QoS0)" );
631
- ReferenceCountUtil .release (msg );
633
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, invalid format" );
632
634
connection .brokerDisconnect (MqttReasonCodes .Disconnect .PAYLOAD_FORMAT_INVALID );
633
635
connection .disconnectSession ();
634
636
connection .dropConnection ();
@@ -639,7 +641,7 @@ CompletableFuture<Void> receivedPublishQos0(MQTTConnection connection, String us
639
641
final RoutingResults publishResult = publish2Subscribers (clientID , messageExpiry , msg );
640
642
if (publishResult .isAllFailed ()) {
641
643
LOG .info ("No one publish was successfully enqueued to session loops" );
642
- ReferenceCountUtil .release (msg );
644
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, can't forward to next session loop" );
643
645
return CompletableFuture .completedFuture (null );
644
646
}
645
647
@@ -650,7 +652,7 @@ CompletableFuture<Void> receivedPublishQos0(MQTTConnection connection, String us
650
652
}
651
653
652
654
interceptor .notifyTopicPublished (msg , clientID , username );
653
- ReferenceCountUtil .release (msg );
655
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok" );
654
656
});
655
657
}
656
658
@@ -662,13 +664,13 @@ RoutingResults receivedPublishQos1(MQTTConnection connection, String username, i
662
664
if (!topic .isValid ()) {
663
665
LOG .warn ("Invalid topic format, force close the connection" );
664
666
connection .dropConnection ();
665
- ReferenceCountUtil .release (msg );
667
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, qos1 invalid topic" );
666
668
return RoutingResults .preroutingError ();
667
669
}
668
670
final String clientId = connection .getClientId ();
669
671
if (!authorizator .canWrite (topic , username , clientId )) {
670
672
LOG .error ("MQTT client: {} is not authorized to publish on topic: {}" , clientId , topic );
671
- ReferenceCountUtil .release (msg );
673
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, qos1 auth failed" );
672
674
return RoutingResults .preroutingError ();
673
675
}
674
676
@@ -677,15 +679,15 @@ RoutingResults receivedPublishQos1(MQTTConnection connection, String username, i
677
679
LOG .warn ("Received not valid UTF-8 payload when payload format indicator was enabled (QoS1)" );
678
680
connection .sendPubAck (messageID , MqttReasonCodes .PubAck .PAYLOAD_FORMAT_INVALID );
679
681
680
- ReferenceCountUtil .release (msg );
682
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, qos1 invalid format" );
681
683
return RoutingResults .preroutingError ();
682
684
}
683
685
}
684
686
685
687
if (isContentTypeToValidate (msg )) {
686
688
if (!validateContentTypeAsUTF8 (msg )) {
687
689
LOG .warn ("Received not valid UTF-8 content type (QoS1)" );
688
- ReferenceCountUtil .release (msg );
690
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, qos1 invalid content type" );
689
691
connection .brokerDisconnect (MqttReasonCodes .Disconnect .PROTOCOL_ERROR );
690
692
connection .disconnectSession ();
691
693
connection .dropConnection ();
@@ -713,7 +715,7 @@ RoutingResults receivedPublishQos1(MQTTConnection connection, String username, i
713
715
// some session event loop enqueue raised a problem
714
716
failedPublishes .insertAll (messageID , clientId , routes .failedRoutings );
715
717
}
716
- ReferenceCountUtil .release (msg );
718
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, qos1" );
717
719
718
720
// cleanup success resends from the failed publishes cache
719
721
failedPublishes .removeAll (messageID , clientId , routes .successedRoutings );
@@ -871,12 +873,18 @@ private RoutingResults publish2Subscribers(String publisherClientId,
871
873
LOG .trace ("No matching subscriptions for topic: {}" , topic );
872
874
return new RoutingResults (Collections .emptyList (), Collections .emptyList (), CompletableFuture .completedFuture (null ));
873
875
}
876
+ // sanity check
877
+ if (subscriptionCount > sessionLoops .getEventLoopCount ()) {
878
+ LOG .error ("Cardinality of subscription batches ({}) is bigger then the available session loops {}" ,
879
+ subscriptionCount , sessionLoops .getEventLoopCount ());
880
+ return new RoutingResults (Collections .emptyList (), Collections .emptyList (), CompletableFuture .completedFuture (null ));
881
+ }
874
882
875
- msg .retain (subscriptionCount );
883
+ Utils .retain (msg , subscriptionCount , BT_ROUTE_TARGET );
876
884
877
885
List <RouteResult > publishResults = collector .routeBatchedPublishes ((batch ) -> {
878
886
publishToSession (topic , batch , publishingQos , retainPublish , messageExpiry , msg );
879
- msg .release ();
887
+ Utils .release (msg , BT_ROUTE_TARGET );
880
888
});
881
889
882
890
final CompletableFuture [] publishFutures = publishResults .stream ()
@@ -890,7 +898,7 @@ private RoutingResults publish2Subscribers(String publisherClientId,
890
898
Collection <String > subscibersIds = collector .subscriberIdsByEventLoop (rr .clientId );
891
899
if (rr .status == RouteResult .Status .FAIL ) {
892
900
failedRoutings .addAll (subscibersIds );
893
- msg .release ();
901
+ Utils .release (msg , BT_ROUTE_TARGET + "- failed routing" );
894
902
} else {
895
903
successedRoutings .addAll (subscibersIds );
896
904
}
@@ -970,7 +978,7 @@ RoutingResults receivedPublishQos2(MQTTConnection connection, MqttPublishMessage
970
978
final String clientId = connection .getClientId ();
971
979
if (!authorizator .canWrite (topic , username , clientId )) {
972
980
LOG .error ("MQTT client is not authorized to publish on topic: {}" , topic );
973
- ReferenceCountUtil .release (msg );
981
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, phase 2 qos2 auth failed" );
974
982
// WARN this is a special case failed is empty, but this result is to be considered as error.
975
983
return RoutingResults .preroutingError ();
976
984
}
@@ -981,7 +989,7 @@ RoutingResults receivedPublishQos2(MQTTConnection connection, MqttPublishMessage
981
989
LOG .warn ("Received not valid UTF-8 payload when payload format indicator was enabled (QoS2)" );
982
990
connection .sendPubRec (messageID , MqttReasonCodes .PubRec .PAYLOAD_FORMAT_INVALID );
983
991
984
- ReferenceCountUtil .release (msg );
992
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, phase 2 qos2 invalid format" );
985
993
return RoutingResults .preroutingError ();
986
994
}
987
995
}
@@ -1002,7 +1010,7 @@ RoutingResults receivedPublishQos2(MQTTConnection connection, MqttPublishMessage
1002
1010
// some session event loop enqueue raised a problem
1003
1011
failedPublishes .insertAll (messageID , clientId , publishRoutings .failedRoutings );
1004
1012
}
1005
- ReferenceCountUtil .release (msg );
1013
+ Utils .release (msg , PostOffice . BT_PUB_IN + " - ok, phase 2 qos2" );
1006
1014
1007
1015
// cleanup success resends from the failed publishes cache
1008
1016
failedPublishes .removeAll (messageID , clientId , publishRoutings .successedRoutings );
0 commit comments