You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/10/07 13:23:42 UTC

[GitHub] [rocketmq-mqtt] pingww commented on a diff in pull request #131: [ISSUE #70] Retained message based on raft state machine

pingww commented on code in PR #131:
URL: https://github.com/apache/rocketmq-mqtt/pull/131#discussion_r990081045


##########
mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/protocol/mqtt/handler/MqttSubscribeHandler.java:
##########
@@ -128,4 +159,63 @@ private MqttSubAckMessage getResponse(MqttSubscribeMessage mqttSubscribeMessage)
         return mqttSubAckMessage;
     }
 
+
+    private void sendRetainMessage(ChannelHandlerContext ctx, Set<Subscription> subscriptions) throws InterruptedException, RemotingException, org.apache.rocketmq.remoting.exception.RemotingException {
+
+        String clientId = ChannelInfo.getClientId(ctx.channel());
+        Session session = sessionLoop.getSession(ChannelInfo.getId(ctx.channel()));
+        Set<Subscription> preciseTopics = new HashSet<>();
+        Set<Subscription> wildcardTopics = new HashSet<>();
+
+        for (Subscription subscription : subscriptions) {
+            if (!TopicUtils.isWildCard(subscription.getTopicFilter())) {
+                preciseTopics.add(subscription);
+            } else {
+                wildcardTopics.add(subscription);
+            }
+        }
+
+        for (Subscription subscription : preciseTopics) {
+            CompletableFuture<Message> retainedMessage = retainedPersistManager.getRetainedMessage(subscription.getTopicFilter());
+            retainedMessage.whenComplete((msg, throwable) -> {
+                if (msg == null) {
+                    return;
+                }
+                _sendMessage(session, clientId, subscription, msg);
+            });
+        }
+
+        for (Subscription subscription : wildcardTopics) {
+
+            CompletableFuture<ArrayList<Message>> future = retainedPersistManager.getMsgsFromTrie(subscription);
+            future.whenComplete((msgsList, throwable) -> {
+                for (Message msg : msgsList) {
+                    if (msg == null) {
+                        return;
+                    }
+                    _sendMessage(session, clientId, subscription, msg);
+                }
+            });
+
+        }
+    }
+
+    private void _sendMessage(Session session, String clientId, Subscription subscription, Message message) {
+
+        String payLoad = new String(message.getPayload());
+        if (payLoad.equals(MessageUtil.EMPTYSTRING) && message.isEmpty()) {
+            return;
+        }
+
+        int mqttId = mqttMsgId.nextId(clientId);
+        int qos = min(subscription.getQos(), message.qos());
+        if (qos == 0) {
+            pushAction.write(session, message, mqttId, 0, subscription);
+            pushAction.rollNextByAck(session, mqttId);
+        } else {
+            retryDriver.mountPublish(mqttId, message, subscription.getQos(), ChannelInfo.getId(session.getChannel()), subscription);

Review Comment:
   New subscribers are all subscribed from the maximum site, RETAIN messages cannot be retried, if wildcard subscriptions are not in the queue. Whether a retry test has been performed?



##########
mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/protocol/mqtt/handler/MqttSubscribeHandler.java:
##########
@@ -128,4 +159,63 @@ private MqttSubAckMessage getResponse(MqttSubscribeMessage mqttSubscribeMessage)
         return mqttSubAckMessage;
     }
 
+
+    private void sendRetainMessage(ChannelHandlerContext ctx, Set<Subscription> subscriptions) throws InterruptedException, RemotingException, org.apache.rocketmq.remoting.exception.RemotingException {
+
+        String clientId = ChannelInfo.getClientId(ctx.channel());
+        Session session = sessionLoop.getSession(ChannelInfo.getId(ctx.channel()));
+        Set<Subscription> preciseTopics = new HashSet<>();
+        Set<Subscription> wildcardTopics = new HashSet<>();
+
+        for (Subscription subscription : subscriptions) {
+            if (!TopicUtils.isWildCard(subscription.getTopicFilter())) {
+                preciseTopics.add(subscription);
+            } else {
+                wildcardTopics.add(subscription);
+            }
+        }
+
+        for (Subscription subscription : preciseTopics) {
+            CompletableFuture<Message> retainedMessage = retainedPersistManager.getRetainedMessage(subscription.getTopicFilter());
+            retainedMessage.whenComplete((msg, throwable) -> {
+                if (msg == null) {
+                    return;
+                }
+                _sendMessage(session, clientId, subscription, msg);
+            });
+        }
+
+        for (Subscription subscription : wildcardTopics) {
+
+            CompletableFuture<ArrayList<Message>> future = retainedPersistManager.getMsgsFromTrie(subscription);
+            future.whenComplete((msgsList, throwable) -> {
+                for (Message msg : msgsList) {
+                    if (msg == null) {
+                        return;
+                    }
+                    _sendMessage(session, clientId, subscription, msg);
+                }
+            });
+
+        }
+    }
+
+    private void _sendMessage(Session session, String clientId, Subscription subscription, Message message) {

Review Comment:
   The send logic should converge to the push module



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org