RongtongJin commented on code in PR #9787: URL: https://github.com/apache/rocketmq/pull/9787#discussion_r2476501796
########## store/src/main/java/org/apache/rocketmq/store/rocksdb/MessageRocksDBStorage.java: ########## @@ -0,0 +1,652 @@ +/* + * 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.rocksdb; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.common.MixAll; +import org.apache.rocketmq.common.UtilAll; +import org.apache.rocketmq.common.config.AbstractRocksDBStorage; +import org.apache.rocketmq.common.constant.LoggerName; +import org.apache.rocketmq.logging.org.slf4j.Logger; +import org.apache.rocketmq.logging.org.slf4j.LoggerFactory; +import org.apache.rocketmq.store.config.MessageStoreConfig; +import org.apache.rocketmq.store.index.rocksdb.IndexRocksDBRecord; +import org.apache.rocketmq.store.timer.rocksdb.TimerRocksDBRecord; +import org.apache.rocketmq.store.transaction.TransRocksDBRecord; +import org.rocksdb.ColumnFamilyDescriptor; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.ColumnFamilyOptions; +import org.rocksdb.ReadOptions; +import org.rocksdb.RocksDB; +import org.rocksdb.RocksIterator; +import org.rocksdb.Slice; +import org.rocksdb.WriteBatch; +import static org.apache.rocketmq.common.MixAll.dealTimeToHourStamps; +import static org.apache.rocketmq.common.MixAll.getHours; +import static org.apache.rocketmq.common.MixAll.isHourTime; +import static org.apache.rocketmq.store.index.rocksdb.IndexRocksDBRecord.KEY_SPLIT; +import static org.apache.rocketmq.store.index.rocksdb.IndexRocksDBRecord.KEY_SPLIT_BYTES; +import static org.apache.rocketmq.store.timer.rocksdb.TimerRocksDBRecord.TIMER_ROCKSDB_DELETE; +import static org.apache.rocketmq.store.timer.rocksdb.TimerRocksDBRecord.TIMER_ROCKSDB_PUT; +import static org.apache.rocketmq.store.timer.rocksdb.TimerRocksDBRecord.TIMER_ROCKSDB_UPDATE; + +public class MessageRocksDBStorage extends AbstractRocksDBStorage { + private static final Logger log = LoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME); + private static final Logger logError = LoggerFactory.getLogger(LoggerName.STORE_ERROR_LOGGER_NAME); + private static final String ROCKSDB_MESSAGE_DIRECTORY = "rocksdbstore"; + + public static final byte[] TIMER_COLUMN_FAMILY = "timer".getBytes(StandardCharsets.UTF_8); + public static final byte[] TRANS_COLUMN_FAMILY = "trans".getBytes(StandardCharsets.UTF_8); + private static final byte[] LAST_OFFSET_PY = "lastOffsetPy".getBytes(StandardCharsets.UTF_8); + private static final byte[] LAST_STORE_TIMESTAMP = "lastStoreTimeStamp".getBytes(StandardCharsets.UTF_8); + private static final byte[] END_SUFFIX_BYTES = new byte[512]; + static { + Arrays.fill(END_SUFFIX_BYTES, (byte) 0xFF); + } + private static final Set<byte[]> COMMON_CHECK_POINT_KEY_SET_FOR_TIMER = new HashSet<>(); + public static final byte[] SYS_TOPIC_SCAN_OFFSET_CHECK_POINT = "sys_topic_scan_offset_checkpoint".getBytes(StandardCharsets.UTF_8); + public static final byte[] TIMELINE_CHECK_POINT = "timeline_checkpoint".getBytes(StandardCharsets.UTF_8); + static { + COMMON_CHECK_POINT_KEY_SET_FOR_TIMER.add(SYS_TOPIC_SCAN_OFFSET_CHECK_POINT); + COMMON_CHECK_POINT_KEY_SET_FOR_TIMER.add(TIMELINE_CHECK_POINT); + } + private static final byte[] DELETE_VAL_FLAG = new byte[] {(byte)0xFF}; + private static final int LAST_OFFSET_PY_LENGTH = LAST_OFFSET_PY.length; + + private volatile ColumnFamilyHandle timerCFHandle; + private volatile ColumnFamilyHandle transCFHandle; + + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + private static final Cache<byte[], byte[]> DELETE_KEY_CACHE_FOR_TIMER = CacheBuilder.newBuilder() + .maximumSize(10000) + .expireAfterWrite(60, TimeUnit.MINUTES) + .build(); + + public MessageRocksDBStorage(MessageStoreConfig messageStoreConfig) { + super(Paths.get(messageStoreConfig.getStorePathRootDir(), ROCKSDB_MESSAGE_DIRECTORY).toString()); + this.start(); + } + + @Override + protected boolean postLoad() { + try { + UtilAll.ensureDirOK(this.dbPath); + initOptions(); + ColumnFamilyOptions indexCFOptions = RocksDBOptionsFactory.createIndexCFOptions(); + ColumnFamilyOptions timerCFOptions = RocksDBOptionsFactory.createTimerCFOptions(); + ColumnFamilyOptions transCFOptions = RocksDBOptionsFactory.createTransCFOptions(); + this.cfOptions.add(indexCFOptions); + this.cfOptions.add(timerCFOptions); + this.cfOptions.add(transCFOptions); + + List<ColumnFamilyDescriptor> cfDescriptors = new ArrayList<>(); + cfDescriptors.add(new ColumnFamilyDescriptor(RocksDB.DEFAULT_COLUMN_FAMILY, indexCFOptions)); + cfDescriptors.add(new ColumnFamilyDescriptor(TIMER_COLUMN_FAMILY, timerCFOptions)); + cfDescriptors.add(new ColumnFamilyDescriptor(TRANS_COLUMN_FAMILY, transCFOptions)); + this.open(cfDescriptors); + this.defaultCFHandle = cfHandles.get(0); + this.timerCFHandle = cfHandles.get(1); + this.transCFHandle = cfHandles.get(2); + scheduler.scheduleAtFixedRate(() -> { + try { + db.flush(flushOptions, timerCFHandle); + log.info("MessageRocksDBStorage flush timer wal success"); + } catch (Exception e) { + logError.error("MessageRocksDBStorage flush timer wal failed, error: {}", e.getMessage()); Review Comment: Directly printing the exception is preferable, as printing e.getMessage() may lose important stack trace information, hindering troubleshooting. ########## pom.xml: ########## @@ -88,7 +88,7 @@ </issueManagement> <properties> - <revision>5.3.4-SNAPSHOT</revision> + <revision>5.3.6-rocksdb-SNAPSHOT</revision> Review Comment: The version number should not be changed here. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
