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/04/21 06:20:06 UTC

[GitHub] [rocketmq] humkum commented on a diff in pull request #4118: [RIP 30] Support Compaction topic

humkum commented on code in PR #4118:
URL: https://github.com/apache/rocketmq/pull/4118#discussion_r854819135


##########
store/src/main/java/org/apache/rocketmq/store/kv/CompactionStore.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.store.kv;
+
+import org.apache.rocketmq.common.ThreadFactoryImpl;
+import org.apache.rocketmq.common.constant.LoggerName;
+import org.apache.rocketmq.logging.InternalLogger;
+import org.apache.rocketmq.logging.InternalLoggerFactory;
+import org.apache.rocketmq.store.GetMessageResult;
+import org.apache.rocketmq.store.MessageStore;
+import org.apache.rocketmq.store.SelectMappedBufferResult;
+import org.apache.rocketmq.store.config.MessageStoreConfig;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+public class CompactionStore {
+
+    public static final String COMPACTION_DIR = "compaction";
+    public static final String COMPACTION_LOG_DIR = "compactionLog";
+    public static final String COMPACTION_CQ_DIR = "compactionCq";
+
+    private final String compactionPath;
+    private final String compactionLogPath;
+    private final String compactionCqPath;
+    private final MessageStore defaultMessageStore;
+    private final CompactionPositionMgr positionMgr;
+    private final ConcurrentHashMap<String, CompactionLog> compactionLogTable;
+    private final ScheduledExecutorService compactionSchedule;
+    private final int compactionInterval;
+    private final int compactionThreadNum;
+    private final int offsetMapSize;
+
+    private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME);
+
+    public CompactionStore(MessageStore defaultMessageStore) {
+        this.defaultMessageStore = defaultMessageStore;
+        this.compactionLogTable = new ConcurrentHashMap<>();
+        MessageStoreConfig config = defaultMessageStore.getMessageStoreConfig();
+        String storeRootPath = config.getStorePathRootDir();
+        this.compactionPath = Paths.get(storeRootPath, COMPACTION_DIR).toString();
+        this.compactionLogPath = Paths.get(compactionPath, COMPACTION_LOG_DIR).toString();
+        this.compactionCqPath = Paths.get(compactionPath, COMPACTION_CQ_DIR).toString();
+        this.positionMgr = new CompactionPositionMgr(compactionPath);
+        if (config.getCompactionThreadNum() <= 0) {
+            this.compactionThreadNum = Runtime.getRuntime().availableProcessors();
+        } else {
+            this.compactionThreadNum = config.getCompactionThreadNum();
+        }
+        this.compactionSchedule = Executors.newScheduledThreadPool(this.compactionThreadNum,
+            new ThreadFactoryImpl("compactionSchedule_"));
+        this.offsetMapSize = config.getMaxOffsetMapSize() / compactionThreadNum;
+
+        this.compactionInterval = defaultMessageStore.getMessageStoreConfig().getCompactionScheduleInternal();
+    }
+
+    public void load() {
+        File logRoot = new File(compactionLogPath);
+        File[] fileTopicList = logRoot.listFiles();
+        if (fileTopicList != null) {
+            for (File fileTopic : fileTopicList) {
+                if (!fileTopic.isDirectory()) {
+                    continue;
+                }
+
+                File[] fileQueueIdList = fileTopic.listFiles();
+                if (fileQueueIdList != null) {
+                    for (File fileQueueId : fileQueueIdList) {
+                        if (!fileQueueId.isDirectory()) {
+                            continue;
+                        }
+                        try {
+                            String topic = fileTopic.getName();
+                            int queueId = Integer.parseInt(fileQueueId.getName());
+
+                            if (Files.isDirectory(Paths.get(compactionCqPath, topic, String.valueOf(queueId)))) {
+                                CompactionLog log = new CompactionLog(defaultMessageStore, topic, queueId,
+                                    offsetMapSize, positionMgr, compactionLogPath, compactionCqPath);
+                                compactionLogTable.put(topic + "_" + queueId, log);
+                                compactionSchedule.scheduleWithFixedDelay(log::doCompaction, compactionInterval, compactionInterval, TimeUnit.SECONDS);

Review Comment:
   Different compactionLog files are distinguished by topic and queueId, and do compaction in every compactionLog. Does this mean messages with the same keys must send to the same queue?



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