Skip to content

Commit 395d489

Browse files
application messages - remove not needed publish msg copies creation during processing
1 parent e8454dd commit 395d489

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

application/src/main/java/org/thingsboard/mqtt/broker/adaptor/ProtoConverter.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ public static String getClientId(QueueProtos.PublishMsgProto publishMsgProto) {
9595
return publishMsgProto != null ? publishMsgProto.getClientId() : null;
9696
}
9797

98-
public static PublishMsg convertToPublishMsg(QueueProtos.PublishMsgProto publishMsgProto) {
98+
public static PublishMsg convertToPublishMsg(QueueProtos.PublishMsgProto publishMsgProto, int packetId,
99+
int qos, boolean isDup) {
99100
return PublishMsg.builder()
100-
.packetId(publishMsgProto.getPacketId())
101101
.topicName(publishMsgProto.getTopicName())
102-
.qosLevel(publishMsgProto.getQos())
103102
.isRetained(publishMsgProto.getRetain())
104103
.payload(publishMsgProto.getPayload().toByteArray())
105104
.properties(createMqttProperties(publishMsgProto.getUserPropertiesList()))
105+
.packetId(packetId)
106+
.qosLevel(qos)
107+
.isDup(isDup)
106108
.build();
107109
}
108110

application/src/main/java/org/thingsboard/mqtt/broker/service/mqtt/persistence/application/ApplicationPersistenceProcessorImpl.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ public void startProcessingSharedSubscriptions(ClientSessionCtx clientSessionCtx
272272
applicationPubRelMsgCtx,
273273
clientSessionCtx,
274274
persistedMsgCtx,
275-
publishProtoMessages);
275+
publishProtoMessages,
276+
subscription);
276277
submitStrategy.init(messagesToDeliver);
277278

278279
if (isDebugEnabled) {
@@ -286,7 +287,7 @@ public void startProcessingSharedSubscriptions(ClientSessionCtx clientSessionCtx
286287
int totalPubRelMsgs = ctx.getPubRelPendingMsgMap().size();
287288
cachePackProcessingCtx(clientId, subscription, ctx);
288289

289-
process(submitStrategy, clientSessionCtx, clientId, subscription);
290+
process(submitStrategy, clientSessionCtx, clientId);
290291

291292
if (isJobActive(job)) {
292293
ctx.await(packProcessingTimeout, TimeUnit.MILLISECONDS);
@@ -330,12 +331,14 @@ public void startProcessingSharedSubscriptions(ClientSessionCtx clientSessionCtx
330331
private List<PersistedMsg> getMessagesToDeliver(ApplicationPubRelMsgCtx applicationPubRelMsgCtx,
331332
ClientSessionCtx clientSessionCtx,
332333
ApplicationPersistedMsgCtx persistedMsgCtx,
333-
List<TbProtoQueueMsg<PublishMsgProto>> publishProtoMessages) {
334+
List<TbProtoQueueMsg<PublishMsgProto>> publishProtoMessages,
335+
TopicSharedSubscription subscription) {
334336
List<PersistedPubRelMsg> pubRelMessagesToDeliver = applicationPubRelMsgCtx.toSortedPubRelMessagesToDeliver();
335337
List<PersistedPublishMsg> publishMessagesToDeliver = toPublishMessagesToDeliver(
336338
clientSessionCtx,
337339
persistedMsgCtx,
338-
publishProtoMessages
340+
publishProtoMessages,
341+
subscription
339342
);
340343

341344
return collectMessagesToDeliver(pubRelMessagesToDeliver, publishMessagesToDeliver);
@@ -654,7 +657,8 @@ private void processPersistedMessages(TbQueueControlledOffsetConsumer<TbProtoQue
654657
applicationPubRelMsgCtx,
655658
clientSessionCtx,
656659
persistedMsgCtx,
657-
publishProtoMessages);
660+
publishProtoMessages,
661+
null);
658662
submitStrategy.init(messagesToDeliver);
659663

660664
applicationPubRelMsgCtx = new ApplicationPubRelMsgCtx(Sets.newConcurrentHashSet());
@@ -664,7 +668,7 @@ private void processPersistedMessages(TbQueueControlledOffsetConsumer<TbProtoQue
664668
int totalPubRelMsgs = ctx.getPubRelPendingMsgMap().size();
665669
packProcessingCtxMap.put(clientId, ctx);
666670

667-
process(submitStrategy, clientSessionCtx, clientId, null);
671+
process(submitStrategy, clientSessionCtx, clientId);
668672

669673
if (isClientConnected(sessionId, clientState)) {
670674
ctx.await(packProcessingTimeout, TimeUnit.MILLISECONDS);
@@ -695,17 +699,15 @@ private void processPersistedMessages(TbQueueControlledOffsetConsumer<TbProtoQue
695699
}
696700
}
697701

698-
private void process(ApplicationSubmitStrategy submitStrategy, ClientSessionCtx clientSessionCtx,
699-
String clientId, TopicSharedSubscription topicSharedSubscription) {
702+
private void process(ApplicationSubmitStrategy submitStrategy, ClientSessionCtx clientSessionCtx, String clientId) {
700703
if (isDebugEnabled) {
701704
log.debug("[{}] Start sending the pack of messages from processing ctx: {}", clientId, submitStrategy.getOrderedMessages());
702705
}
703706
submitStrategy.process(msg -> {
704707
switch (msg.getPacketType()) {
705708
case PUBLISH:
706709
PublishMsg publishMsg = ((PersistedPublishMsg) msg).getPublishMsg();
707-
int minQoSValue = getMinQoSValue(topicSharedSubscription, publishMsg);
708-
publishMsgDeliveryService.sendPublishMsgToClientWithoutFlush(clientSessionCtx, publishMsg.toBuilder().qosLevel(minQoSValue).build());
710+
publishMsgDeliveryService.sendPublishMsgToClientWithoutFlush(clientSessionCtx, publishMsg);
709711
break;
710712
case PUBREL:
711713
publishMsgDeliveryService.sendPubRelMsgToClientWithoutFlush(clientSessionCtx, msg.getPacketId());
@@ -716,9 +718,8 @@ private void process(ApplicationSubmitStrategy submitStrategy, ClientSessionCtx
716718
clientSessionCtx.getChannel().flush();
717719
}
718720

719-
private int getMinQoSValue(TopicSharedSubscription subscription, PublishMsg publishMsg) {
720-
return subscription == null ? publishMsg.getQosLevel() :
721-
Math.min(subscription.getQos(), publishMsg.getQosLevel());
721+
private int getMinQoSValue(TopicSharedSubscription subscription, int publishMsgQos) {
722+
return subscription == null ? publishMsgQos : Math.min(subscription.getQos(), publishMsgQos);
722723
}
723724

724725
private List<PersistedMsg> collectMessagesToDeliver(List<PersistedPubRelMsg> pubRelMessagesToDeliver,
@@ -743,23 +744,22 @@ private ApplicationPubRelMsgCtx persistedMsgCtxToPubRelMsgCtx(ApplicationPersist
743744

744745
private List<PersistedPublishMsg> toPublishMessagesToDeliver(ClientSessionCtx clientSessionCtx,
745746
ApplicationPersistedMsgCtx persistedMsgCtx,
746-
List<TbProtoQueueMsg<PublishMsgProto>> publishProtoMessages) {
747+
List<TbProtoQueueMsg<PublishMsgProto>> publishProtoMessages,
748+
TopicSharedSubscription subscription) {
747749
List<PersistedPublishMsg> result = new ArrayList<>(publishProtoMessages.size());
748750
for (TbProtoQueueMsg<PublishMsgProto> msg : publishProtoMessages) {
749751
var msgPacketId = persistedMsgCtx.getMsgPacketId(msg.getOffset());
750752
int packetId = msgPacketId != null ? msgPacketId : clientSessionCtx.getMsgIdSeq().nextMsgId();
751753
boolean isDup = msgPacketId != null;
752-
PublishMsg publishMsg = toPubMsg(msg.getValue(), packetId, isDup);
754+
int minQoSValue = getMinQoSValue(subscription, msg.getValue().getQos());
755+
PublishMsg publishMsg = toPubMsg(msg.getValue(), packetId, minQoSValue, isDup);
753756
result.add(new PersistedPublishMsg(publishMsg, msg.getOffset()));
754757
}
755758
return result;
756759
}
757760

758-
private PublishMsg toPubMsg(PublishMsgProto persistedMsgProto, int packetId, boolean isDup) {
759-
return ProtoConverter.convertToPublishMsg(persistedMsgProto).toBuilder()
760-
.packetId(packetId)
761-
.isDup(isDup)
762-
.build();
761+
private PublishMsg toPubMsg(PublishMsgProto persistedMsgProto, int packetId, int qos, boolean isDup) {
762+
return ProtoConverter.convertToPublishMsg(persistedMsgProto, packetId, qos, isDup);
763763
}
764764

765765
private boolean isClientConnected(UUID sessionId, ClientActorStateInfo clientState) {

0 commit comments

Comments
 (0)