zk-drizzle commented on code in PR #9787: URL: https://github.com/apache/rocketmq/pull/9787#discussion_r2488477641
########## store/src/main/java/org/apache/rocketmq/store/timer/rocksdb/TimerMessageRocksDBStore.java: ########## @@ -0,0 +1,619 @@ +/* + * 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.timer.rocksdb; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Function; +import com.conversantmedia.util.concurrent.DisruptorBlockingQueue; +import com.google.common.util.concurrent.RateLimiter; +import io.opentelemetry.api.common.Attributes; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.rocketmq.common.ServiceThread; +import org.apache.rocketmq.common.TopicFilterType; +import org.apache.rocketmq.common.constant.LoggerName; +import org.apache.rocketmq.common.message.MessageAccessor; +import org.apache.rocketmq.common.message.MessageClientIDSetter; +import org.apache.rocketmq.common.message.MessageConst; +import org.apache.rocketmq.common.message.MessageDecoder; +import org.apache.rocketmq.common.message.MessageExt; +import org.apache.rocketmq.common.message.MessageExtBrokerInner; +import org.apache.rocketmq.logging.org.slf4j.Logger; +import org.apache.rocketmq.logging.org.slf4j.LoggerFactory; +import org.apache.rocketmq.store.DefaultMessageStore; +import org.apache.rocketmq.store.MessageStore; +import org.apache.rocketmq.store.PutMessageResult; +import org.apache.rocketmq.store.StoreUtil; +import org.apache.rocketmq.store.config.MessageStoreConfig; +import org.apache.rocketmq.store.metrics.DefaultStoreMetricsConstant; +import org.apache.rocketmq.store.metrics.DefaultStoreMetricsManager; +import org.apache.rocketmq.store.metrics.StoreMetricsManager; +import org.apache.rocketmq.store.queue.ConsumeQueueInterface; +import org.apache.rocketmq.store.queue.CqUnit; +import org.apache.rocketmq.store.queue.ReferredIterator; +import org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage; +import org.apache.rocketmq.store.stats.BrokerStatsManager; +import org.apache.rocketmq.store.timer.TimerMetrics; +import org.apache.rocketmq.store.util.PerfCounter; +import static org.apache.rocketmq.common.message.MessageConst.PROPERTY_TIMER_ROLL_LABEL; +import static org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage.TIMER_COLUMN_FAMILY; +import static org.apache.rocketmq.store.timer.TimerMessageStore.TIMER_TOPIC; + +public class TimerMessageRocksDBStore { + 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 SCAN_SYS_TOPIC = "scanSysTopic"; + private static final String SCAN_SYS_TOPIC_MISS = "scanSysTopicMiss"; + private static final String OUT_BIZ_MESSAGE = "outBizMsg"; + private static final String ROLL_LABEL = "R"; + private static final int PUT_OK = 0, PUT_NEED_RETRY = 1, PUT_NO_RETRY = 2; + private static final int MAX_GET_MSG_TIMES = 3, MAX_PUT_MSG_TIMES = 3; + private static final int TIME_UP_CAPACITY = 100, ROLL_CAPACITY = 50; + private static final int INITIAL = 0, RUNNING = 1, SHUTDOWN = 2; + private volatile int state = INITIAL; + private static long expirationThresholdMillis = 999L; + private final AtomicLong readOffset = new AtomicLong(0);//timerSysTopic read offset + private final MessageStore messageStore; + private final TimerMetrics timerMetrics; + private final MessageStoreConfig storeConfig; + private final BrokerStatsManager brokerStatsManager; + private final MessageRocksDBStorage messageRocksDBStorage; + private Timeline timeline; + private TimerSysTopicScanService timerSysTopicScanService; + private TimerMessageReputService expiredMessageReputService; + private TimerMessageReputService rollMessageReputService; + protected long precisionMs; + private BlockingQueue<List<TimerRocksDBRecord>> expiredMessageQueue; + private BlockingQueue<List<TimerRocksDBRecord>> rollMessageQueue; + protected final PerfCounter.Ticks perfCounterTicks = new PerfCounter.Ticks(log); + private Function<MessageExtBrokerInner, PutMessageResult> escapeBridgeHook; + private ThreadLocal<ByteBuffer> bufferLocal = null; + + public TimerMessageRocksDBStore(final MessageStore messageStore, final TimerMetrics timerMetrics, + final BrokerStatsManager brokerStatsManager) { + this.messageStore = messageStore; + this.storeConfig = messageStore.getMessageStoreConfig(); + this.precisionMs = storeConfig.getTimerRocksDBPrecisionMs() < 100L ? 1000L : storeConfig.getTimerRocksDBPrecisionMs(); + expirationThresholdMillis = precisionMs - 1L; + this.messageRocksDBStorage = messageStore.getMessageRocksDBStorage(); + this.timerMetrics = timerMetrics; + this.brokerStatsManager = brokerStatsManager; + bufferLocal = ThreadLocal.withInitial(() -> ByteBuffer.allocate(storeConfig.getMaxMessageSize())); + } + + public synchronized boolean load() { + initService(); + boolean result = this.timerMetrics.load(); + log.info("TimerMessageRocksDBStore load result: {}", result); + return result; + } + + public synchronized void start() { + if (this.state == RUNNING) { + return; + } + long commitOffsetFile = null != this.messageStore.getTimerMessageStore() ? this.messageStore.getTimerMessageStore().getCommitQueueOffset() : 0L; + long commitOffsetRocksDB = messageRocksDBStorage.getCheckpointForTimer(TIMER_COLUMN_FAMILY, MessageRocksDBStorage.SYS_TOPIC_SCAN_OFFSET_CHECK_POINT); + long maxCommitOffset = Math.max(commitOffsetFile, commitOffsetRocksDB); + this.readOffset.set(maxCommitOffset); + this.expiredMessageReputService.start(); + this.rollMessageReputService.start(); + this.timeline.start(); + this.timerSysTopicScanService.start(); + this.state = RUNNING; + log.info("TimerMessageRocksDBStore start success, start commitOffsetFile: {}, commitOffsetRocksDB: {}, readOffset: {}", commitOffsetFile, commitOffsetRocksDB, this.readOffset.get()); + } + + public synchronized boolean restart() { + try { + this.storeConfig.setTimerStopEnqueue(true); + if (this.state == RUNNING && !this.storeConfig.isTimerRocksDBStopScan()) { + log.info("restart TimerMessageRocksDBStore has been running"); + return true; Review Comment: no problem -- 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]
