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/08/03 07:36:50 UTC

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

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


##########
meta/src/main/java/org/apache/rocketmq/mqtt/meta/raft/processor/RetainedMsgStateProcess.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.mqtt.meta.raft.processor;
+
+import com.alibaba.fastjson.JSON;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotReader;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter;
+import com.google.protobuf.ByteString;
+import org.apache.rocketmq.mqtt.common.model.Message;
+import org.apache.rocketmq.mqtt.common.model.Trie;
+import org.apache.rocketmq.mqtt.common.model.consistency.ReadRequest;
+import org.apache.rocketmq.mqtt.common.model.consistency.Response;
+import org.apache.rocketmq.mqtt.common.model.consistency.WriteRequest;
+import org.apache.rocketmq.mqtt.common.util.TopicUtils;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.SnapshotOperation;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.impl.CounterSnapshotOperation;
+
+import java.util.HashMap;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+
+public class RetainedMsgStateProcess extends StateProcessor {
+
+    private final AtomicLong value = new AtomicLong(0);
+
+    private final HashMap<String, String> retainedMsgMap = new HashMap<>();  //key:topic value:retained msg
+
+    private final HashMap<String, Trie<String, String>> retainedMsgTopicTrie = new HashMap<>();  //key:firstTopic value:retained topic Trie
+
+    protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+    private static final int LIMIT_RETAINED_MESSAGE_COUNT = 100;

Review Comment:
   LIMIT_RETAINED_MESSAGE_COUNT should be config in service.conf



##########
meta/src/main/java/org/apache/rocketmq/mqtt/meta/raft/processor/RetainedMsgStateProcess.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.mqtt.meta.raft.processor;
+
+import com.alibaba.fastjson.JSON;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotReader;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter;
+import com.google.protobuf.ByteString;
+import org.apache.rocketmq.mqtt.common.model.Message;
+import org.apache.rocketmq.mqtt.common.model.Trie;
+import org.apache.rocketmq.mqtt.common.model.consistency.ReadRequest;
+import org.apache.rocketmq.mqtt.common.model.consistency.Response;
+import org.apache.rocketmq.mqtt.common.model.consistency.WriteRequest;
+import org.apache.rocketmq.mqtt.common.util.TopicUtils;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.SnapshotOperation;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.impl.CounterSnapshotOperation;
+
+import java.util.HashMap;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+
+public class RetainedMsgStateProcess extends StateProcessor {
+
+    private final AtomicLong value = new AtomicLong(0);
+
+    private final HashMap<String, String> retainedMsgMap = new HashMap<>();  //key:topic value:retained msg

Review Comment:
   Does the state machine need to care about message content?  Or use byte array storage directly, reducing serialization and deserialization 
   



##########
meta/src/main/java/org/apache/rocketmq/mqtt/meta/raft/processor/Constants.java:
##########
@@ -20,5 +20,7 @@
 public class Constants {
     public static final String COUNTER = "counter";
 
+    public static final String RETAINEDMSG = "retainedmsg";

Review Comment:
   retainedmsg -> retainedMsg



##########
meta/src/main/java/org/apache/rocketmq/mqtt/meta/raft/processor/RetainedMsgStateProcess.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.mqtt.meta.raft.processor;
+
+import com.alibaba.fastjson.JSON;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotReader;
+import com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter;
+import com.google.protobuf.ByteString;
+import org.apache.rocketmq.mqtt.common.model.Message;
+import org.apache.rocketmq.mqtt.common.model.Trie;
+import org.apache.rocketmq.mqtt.common.model.consistency.ReadRequest;
+import org.apache.rocketmq.mqtt.common.model.consistency.Response;
+import org.apache.rocketmq.mqtt.common.model.consistency.WriteRequest;
+import org.apache.rocketmq.mqtt.common.util.TopicUtils;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.SnapshotOperation;
+import org.apache.rocketmq.mqtt.meta.raft.snapshot.impl.CounterSnapshotOperation;
+
+import java.util.HashMap;
+
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiConsumer;
+
+public class RetainedMsgStateProcess extends StateProcessor {
+
+    private final AtomicLong value = new AtomicLong(0);
+
+    private final HashMap<String, String> retainedMsgMap = new HashMap<>();  //key:topic value:retained msg
+
+    private final HashMap<String, Trie<String, String>> retainedMsgTopicTrie = new HashMap<>();  //key:firstTopic value:retained topic Trie
+
+    protected final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+    private static final int LIMIT_RETAINED_MESSAGE_COUNT = 100;
+    private SnapshotOperation snapshotOperation;
+
+
+    @Override
+    public Response onReadRequest(ReadRequest request) {
+        try {
+            String topic = request.getExtDataMap().get("topic");
+            String flag = request.getExtDataMap().get("flag");

Review Comment:
   flag can be replace by type in ReadRequest



-- 
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