[ISSUE #10011] Optimize accelerated recovery process and refactor code (#10012)

* When IndexRocksDBEnable or TransRocksDBEnable are enabled, we need to take these two offsets into account to accelerate recovery.

* Add UTs

* Refactor the code based on the review comments

* Revert "[ISSUE #8127]Optimize the metric calculation logic of the time wheel"

* Remove useless import

* Refactor Code

* Refactor Code

* Refactor Code

* Refactor Code

* Refactor Code

* Implement accelerated recovery for the file-based ConsumeQueue.

* Implement accelerated recovery for the file-based ConsumeQueue.

Change-Id: Ieac45d0582f2f83d977aeb8e6f5084268b7f8752

* Implement accelerated recovery for the file-based ConsumeQueue.

* Ignore testTruncateCQ UT

---------

Co-authored-by: RongtongJin <user@example.com>
This commit is contained in:
rongtong
2026-02-28 10:30:15 +08:00
committed by GitHub
parent 7030c458ba
commit 628230d22c
18 changed files with 345 additions and 87 deletions
@@ -365,15 +365,6 @@ public class CommitLog implements Swappable {
long mappedFileOffset = 0;
long lastValidMsgPhyOffset = this.getConfirmOffset();
if (defaultMessageStore.getMessageStoreConfig().isEnableRocksDBStore()
&& defaultMessageStore.getMessageStoreConfig().isEnableAcceleratedRecovery()) {
mappedFileOffset = dispatchFromPhyOffset - mappedFile.getFileFromOffset();
if (mappedFileOffset > 0) {
log.info("recover using acceleration, recovery offset is {}", dispatchFromPhyOffset);
lastValidMsgPhyOffset = dispatchFromPhyOffset;
byteBuffer.position((int) mappedFileOffset);
}
}
while (true) {
DispatchRequest dispatchRequest = this.checkMessageAndReturnSize(byteBuffer, checkCRCOnRecover, checkDupInfo);
int size = dispatchRequest.getMsgSize();
@@ -744,7 +735,7 @@ public class CommitLog implements Swappable {
/**
* @throws RocksDBException only in rocksdb mode
*/
public void recoverAbnormally(long maxPhyOffsetOfConsumeQueue) throws RocksDBException {
public void recoverAbnormally(long dispatchFromPhyOffset) throws RocksDBException {
// recover by the minimum time stamp
boolean checkCRCOnRecover = this.defaultMessageStore.getMessageStoreConfig().isCheckCRCOnRecover();
boolean checkDupInfo = this.defaultMessageStore.getMessageStoreConfig().isDuplicationEnable();
@@ -779,18 +770,17 @@ public class CommitLog implements Swappable {
long lastValidMsgPhyOffset;
long lastConfirmValidMsgPhyOffset;
if (defaultMessageStore.getMessageStoreConfig().isEnableRocksDBStore()
&& defaultMessageStore.getMessageStoreConfig().isEnableAcceleratedRecovery()) {
mappedFileOffset = maxPhyOffsetOfConsumeQueue - mappedFile.getFileFromOffset();
if (defaultMessageStore.getMessageStoreConfig().isEnableAcceleratedRecovery()) {
mappedFileOffset = dispatchFromPhyOffset - mappedFile.getFileFromOffset();
// Protective measures, falling back to non-accelerated mode, which is extremely unlikely to occur
if (mappedFileOffset < 0) {
mappedFileOffset = 0;
lastValidMsgPhyOffset = processOffset;
lastConfirmValidMsgPhyOffset = processOffset;
} else {
log.info("recover using acceleration, recovery offset is {}", maxPhyOffsetOfConsumeQueue);
lastValidMsgPhyOffset = maxPhyOffsetOfConsumeQueue;
lastConfirmValidMsgPhyOffset = maxPhyOffsetOfConsumeQueue;
log.info("recover using acceleration, recovery offset is {}", dispatchFromPhyOffset);
lastValidMsgPhyOffset = dispatchFromPhyOffset;
lastConfirmValidMsgPhyOffset = dispatchFromPhyOffset;
byteBuffer.position((int) mappedFileOffset);
}
} else {
@@ -933,27 +923,15 @@ public class CommitLog implements Swappable {
return false;
}
if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable() &&
this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) {
if (storeTimestamp > this.defaultMessageStore.getStoreCheckpoint().getIndexMsgTimestamp()) {
return false;
}
log.info("CommitLog isMmapFileMatchedRecover find satisfied MmapFile for index, " +
"MmapFile storeTimestamp={}, MmapFile phyOffset={}, indexMsgTimestamp={}, recoverNormally={}",
storeTimestamp, phyOffset, this.defaultMessageStore.getStoreCheckpoint().getIndexMsgTimestamp(), recoverNormally);
}
return isMappedFileMatchedRecover(phyOffset, storeTimestamp, recoverNormally);
}
private boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException {
boolean result = this.defaultMessageStore.getQueueStore().isMappedFileMatchedRecover(phyOffset, storeTimestamp, recoverNormally);
if (null != this.defaultMessageStore.getTransMessageRocksDBStore() && defaultMessageStore.getMessageStoreConfig().isTransRocksDBEnable() && !defaultMessageStore.getMessageStoreConfig().isTransWriteOriginTransHalfEnable()) {
result = result && this.defaultMessageStore.getTransMessageRocksDBStore().isMappedFileMatchedRecover(phyOffset);
}
if (null != this.defaultMessageStore.getIndexRocksDBStore() && defaultMessageStore.getMessageStoreConfig().isIndexRocksDBEnable()) {
result = result && this.defaultMessageStore.getIndexRocksDBStore().isMappedFileMatchedRecover(phyOffset);
// Check all registered CommitLogDispatchStore instances
for (CommitLogDispatchStore store : defaultMessageStore.getCommitLogDispatchStores()) {
result = result && store.isMappedFileMatchedRecover(phyOffset, storeTimestamp, recoverNormally);
}
return result;
}
@@ -0,0 +1,50 @@
/*
* 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;
import org.rocksdb.RocksDBException;
/**
* Interface for stores that require commitlog dispatch and recovery. Each store implementing this interface should
* register itself in the commitlog when loading. This abstraction allows the commitlog recovery process to
* automatically consider all registered stores without needing to modify the recovery logic when adding a new store.
*/
public interface CommitLogDispatchStore {
/**
* Get the dispatch offset in the store. Messages whose phyOffset larger than this offset need to be dispatched. The
* dispatch offset is only used during recovery.
*
* @param recoverNormally true if broker exited normally last time (normal recovery), false for abnormal recovery
* @return the dispatch phyOffset, or null if the store is not enabled or has no valid offset
* @throws RocksDBException if there is an error accessing RocksDB storage
*/
Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException;
/**
* Used to determine whether to start doDispatch from this commitLog mappedFile.
*
* @param phyOffset the offset of the first message in this commitlog mappedFile
* @param storeTimestamp the timestamp of the first message in this commitlog mappedFile
* @param recoverNormally whether this is a normal recovery
* @return whether to start recovering from this MappedFile
* @throws RocksDBException if there is an error accessing RocksDB storage
*/
boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException;
}
@@ -712,6 +712,7 @@ public class ConsumeQueue implements ConsumeQueueInterface {
this.messageStore.getStoreCheckpoint().setPhysicMsgTimestamp(request.getStoreTimestamp());
}
this.messageStore.getStoreCheckpoint().setTmpLogicsMsgTimestamp(request.getStoreTimestamp());
this.messageStore.getStoreCheckpoint().setTmpLogicsPhysicalOffset(request.getCommitLogOffset());
if (MultiDispatchUtils.checkMultiDispatchQueue(this.messageStore.getMessageStoreConfig(), request)) {
multiDispatchLmqQueue(request, maxRetries);
}
@@ -175,6 +175,11 @@ public class DefaultMessageStore implements MessageStore {
private final LinkedList<CommitLogDispatcher> dispatcherList = new LinkedList<>();
/**
* List of stores that require commitlog dispatch and recovery. Each store registers itself when loading.
*/
private final List<CommitLogDispatchStore> commitLogDispatchStores = new ArrayList<>();
private final RandomAccessFile lockFile;
private FileLock lock;
@@ -333,6 +338,11 @@ public class DefaultMessageStore implements MessageStore {
// load Consume Queue
result = result && this.consumeQueueStore.load();
stateMachine.transitTo(MessageStoreStateMachine.MessageStoreState.LOAD_CONSUME_QUEUE_OK, result);
// Register consume queue store for commitlog dispatch
// AbstractConsumeQueueStore implements CommitLogDispatchStore, so we can register it directly
if (this.consumeQueueStore != null) {
registerCommitLogDispatchStore(this.consumeQueueStore);
}
if (messageStoreConfig.isEnableCompaction()) {
result = result && this.compactionService.load(lastExitOK);
@@ -342,7 +352,15 @@ public class DefaultMessageStore implements MessageStore {
if (result) {
loadCheckPoint();
result = this.indexService.load(lastExitOK);
registerCommitLogDispatchStore(this.indexService);
stateMachine.transitTo(MessageStoreStateMachine.MessageStoreState.LOAD_INDEX_OK, result);
// Register IndexRocksDBStore and TransMessageRocksDBStore for commit-log dispatch
if (messageStoreConfig.isIndexRocksDBEnable()) {
registerCommitLogDispatchStore(this.indexRocksDBStore);
}
if (messageStoreConfig.isTransRocksDBEnable() && transMessageRocksDBStore != null) {
registerCommitLogDispatchStore(this.transMessageRocksDBStore);
}
this.recover(lastExitOK);
LOGGER.info("message store recover end, and the max phy offset = {}", this.getMaxPhyOffset());
}
@@ -377,7 +395,16 @@ public class DefaultMessageStore implements MessageStore {
this.stateMachine.transitTo(MessageStoreStateMachine.MessageStoreState.RECOVER_CONSUME_QUEUE_OK);
// recover commitlog
long dispatchFromPhyOffset = this.consumeQueueStore.getDispatchFromPhyOffset();
// Calculate the minimum dispatch offset from all registered stores
Long dispatchFromPhyOffset = this.consumeQueueStore.getDispatchFromPhyOffset(lastExitOK);
for (CommitLogDispatchStore store : commitLogDispatchStores) {
Long storeOffset = store.getDispatchFromPhyOffset(lastExitOK);
if (storeOffset != null && storeOffset > 0) {
dispatchFromPhyOffset = Math.min(dispatchFromPhyOffset, storeOffset);
}
}
if (lastExitOK) {
this.commitLog.recoverNormally(dispatchFromPhyOffset);
} else {
@@ -1102,6 +1129,31 @@ public class DefaultMessageStore implements MessageStore {
@Override
public void setTransMessageRocksDBStore(TransMessageRocksDBStore transMessageRocksDBStore) {
this.transMessageRocksDBStore = transMessageRocksDBStore;
// Register TransMessageRocksDBStore for commitlog dispatch if enabled
if (transMessageRocksDBStore != null && messageStoreConfig.isTransRocksDBEnable()) {
registerCommitLogDispatchStore(this.transMessageRocksDBStore);
}
}
/**
* Register a store that requires commitlog dispatch and recovery. Each store should register itself when loading.
*
* @param store the store to register
*/
public void registerCommitLogDispatchStore(CommitLogDispatchStore store) {
if (store != null) {
commitLogDispatchStores.add(store);
LOGGER.info("Registered CommitLogDispatchStore: {}", store.getClass().getSimpleName());
}
}
/**
* Get all registered CommitLogDispatchStore instances.
*
* @return list of registered stores
*/
public List<CommitLogDispatchStore> getCommitLogDispatchStores() {
return commitLogDispatchStores;
}
@Override
@@ -1400,7 +1452,8 @@ public class DefaultMessageStore implements MessageStore {
}
@Override
public QueryMessageResult queryMessage(String topic, String key, int maxNum, long begin, long end, String indexType, String lastKey) {
public QueryMessageResult queryMessage(String topic, String key, int maxNum, long begin, long end, String indexType,
String lastKey) {
QueryMessageResult queryMessageResult = new QueryMessageResult();
long lastQueryMsgTime = end;
for (int i = 0; i < 3; i++) {
@@ -1510,10 +1563,9 @@ public class DefaultMessageStore implements MessageStore {
}
/**
* Lazy clean queue offset table.
* If offset table is cleaned, and old messages are dispatching after the old consume queue is cleaned,
* consume queue will be created with old offset, then later message with new offset table can not be
* dispatched to consume queue.
* Lazy clean queue offset table. If offset table is cleaned, and old messages are dispatching after the old consume
* queue is cleaned, consume queue will be created with old offset, then later message with new offset table can not
* be dispatched to consume queue.
*/
@Override
public int deleteTopics(final Set<String> deleteTopics) {
@@ -1677,6 +1729,7 @@ public class DefaultMessageStore implements MessageStore {
public long dispatchBehindBytes() {
return this.reputMessageService.behind();
}
@Override
public long dispatchBehindMilliseconds() {
return this.reputMessageService.behindMs();
@@ -1818,8 +1871,8 @@ public class DefaultMessageStore implements MessageStore {
}
/**
* The ratio val is estimated by the experiment and experience
* so that the result is not high accurate for different business
* The ratio val is estimated by the experiment and experience so that the result is not high accurate for different
* business
*
* @return
*/
@@ -36,6 +36,8 @@ public class StoreCheckpoint {
private volatile long tmpLogicsMsgTimestamp = 0;
private volatile long physicMsgTimestamp = 0;
private volatile long logicsMsgTimestamp = 0;
private volatile long tmpLogicsPhysicalOffset = 0;
private volatile long logicsPhysicalOffset = 0;
private volatile long indexMsgTimestamp = 0;
private volatile long masterFlushedOffset = 0;
private volatile long confirmPhyOffset = 0;
@@ -56,6 +58,7 @@ public class StoreCheckpoint {
this.indexMsgTimestamp = this.mappedByteBuffer.getLong(16);
this.masterFlushedOffset = this.mappedByteBuffer.getLong(24);
this.confirmPhyOffset = this.mappedByteBuffer.getLong(32);
this.logicsPhysicalOffset = this.mappedByteBuffer.getLong(40);
log.info("store checkpoint file physicMsgTimestamp " + this.physicMsgTimestamp + ", "
+ UtilAll.timeMillisToHumanString(this.physicMsgTimestamp));
@@ -65,6 +68,7 @@ public class StoreCheckpoint {
+ UtilAll.timeMillisToHumanString(this.indexMsgTimestamp));
log.info("store checkpoint file masterFlushedOffset " + this.masterFlushedOffset);
log.info("store checkpoint file confirmPhyOffset " + this.confirmPhyOffset);
log.info("store checkpoint file logicsPhysicalOffset " + this.logicsPhysicalOffset);
} else {
log.info("store checkpoint file not exists, " + scpPath);
}
@@ -91,6 +95,7 @@ public class StoreCheckpoint {
this.mappedByteBuffer.putLong(16, this.indexMsgTimestamp);
this.mappedByteBuffer.putLong(24, this.masterFlushedOffset);
this.mappedByteBuffer.putLong(32, this.confirmPhyOffset);
this.mappedByteBuffer.putLong(40, this.logicsPhysicalOffset);
this.mappedByteBuffer.force();
} catch (Throwable e) {
log.error("Failed to flush", e);
@@ -121,6 +126,22 @@ public class StoreCheckpoint {
this.tmpLogicsMsgTimestamp = tmpLogicsMsgTimestamp;
}
public long getTmpLogicsPhysicalOffset() {
return tmpLogicsPhysicalOffset;
}
public void setTmpLogicsPhysicalOffset(long tmpLogicsPhysicalOffset) {
this.tmpLogicsPhysicalOffset = tmpLogicsPhysicalOffset;
}
public long getLogicsPhysicalOffset() {
return logicsPhysicalOffset;
}
public void setLogicsPhysicalOffset(long logicsPhysicalOffset) {
this.logicsPhysicalOffset = logicsPhysicalOffset;
}
public long getConfirmPhyOffset() {
return confirmPhyOffset;
}
@@ -512,9 +512,8 @@ public class MessageStoreConfig {
private long rocksdbWalFileRollingThreshold = SizeUnit.GB;
/**
* Note: For correctness, this switch should be enabled only if the previous startup was configured with SYNC_FLUSH
* and the storeType was defaultRocksDB. This switch is not recommended for normal use cases (include master-slave
* or controller mode).
* Note: For correctness, this switch should be enabled only if the previous startup was configured with SYNC_FLUSH.
* This switch is not recommended for normal use cases (include master-slave or controller mode).
*/
private boolean enableAcceleratedRecovery = false;
@@ -467,8 +467,8 @@ public class DLedgerCommitLog extends CommitLog {
}
@Override
public void recoverAbnormally(long maxPhyOffsetOfConsumeQueue) throws RocksDBException {
dledgerRecoverAbnormally(maxPhyOffsetOfConsumeQueue);
public void recoverAbnormally(long dispatchFromPhyOffset) throws RocksDBException {
dledgerRecoverAbnormally(dispatchFromPhyOffset);
}
@Override
@@ -31,11 +31,13 @@ import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.common.sysflag.MessageSysFlag;
import org.apache.rocketmq.store.CommitLogDispatchStore;
import org.apache.rocketmq.store.DefaultMessageStore;
import org.apache.rocketmq.store.DispatchRequest;
import org.apache.rocketmq.store.config.StorePathConfigHelper;
import org.rocksdb.RocksDBException;
public class IndexService {
public class IndexService implements CommitLogDispatchStore {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME);
/**
* Maximum times to attempt index file creation.
@@ -455,4 +457,24 @@ public class IndexService {
this.readWriteLock.writeLock().unlock();
}
}
@Override
public Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException {
return -1L;
}
@Override
public boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException {
if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable() &&
this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) {
if (storeTimestamp > this.defaultMessageStore.getStoreCheckpoint().getIndexMsgTimestamp()) {
return false;
}
LOGGER.info("CommitLog isMmapFileMatchedRecover find satisfied MmapFile for index, " +
"MmapFile storeTimestamp={}, MmapFile phyOffset={}, indexMsgTimestamp={}, recoverNormally={}",
storeTimestamp, phyOffset, this.defaultMessageStore.getStoreCheckpoint().getIndexMsgTimestamp(), recoverNormally);
}
return true;
}
}
@@ -38,6 +38,7 @@ import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.common.sysflag.MessageSysFlag;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
import org.apache.rocketmq.store.CommitLogDispatchStore;
import org.apache.rocketmq.store.DefaultMessageStore;
import org.apache.rocketmq.store.DispatchRequest;
import org.apache.rocketmq.store.MessageStore;
@@ -46,14 +47,16 @@ import org.apache.rocketmq.store.index.QueryOffsetResult;
import org.apache.rocketmq.store.logfile.MappedFile;
import org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage;
import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import static org.apache.rocketmq.common.MixAll.dealTimeToHourStamps;
public class IndexRocksDBStore {
public class IndexRocksDBStore implements CommitLogDispatchStore {
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 int DEFAULT_CAPACITY = 100000;
private static final int BATCH_SIZE = 1000;
private static final Set<String> INDEX_TYPE_SET = new HashSet<>();
static {
INDEX_TYPE_SET.add(MessageConst.INDEX_KEY_TYPE);
INDEX_TYPE_SET.add(MessageConst.INDEX_TAG_TYPE);
@@ -239,7 +242,8 @@ public class IndexRocksDBStore {
}
}
public boolean isMappedFileMatchedRecover(long phyOffset) {
public boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException {
if (!storeConfig.isIndexRocksDBEnable()) {
return true;
}
@@ -252,7 +256,20 @@ public class IndexRocksDBStore {
return false;
}
public void destroy() {}
public void destroy() {
}
@Override
public Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException {
if (!storeConfig.isIndexRocksDBEnable()) {
return null;
}
Long dispatchFromIndexPhyOffset = messageRocksDBStorage.getLastOffsetPy(RocksDB.DEFAULT_COLUMN_FAMILY);
if (dispatchFromIndexPhyOffset != null && dispatchFromIndexPhyOffset > 0) {
return dispatchFromIndexPhyOffset;
}
return null;
}
private String getServiceThreadName() {
String brokerIdentifier = "";
@@ -537,6 +537,7 @@ public class BatchConsumeQueue implements ConsumeQueueInterface {
this.messageStore.getStoreCheckpoint().setPhysicMsgTimestamp(request.getStoreTimestamp());
}
this.messageStore.getStoreCheckpoint().setTmpLogicsMsgTimestamp(request.getStoreTimestamp());
this.messageStore.getStoreCheckpoint().setTmpLogicsPhysicalOffset(request.getCommitLogOffset());
return;
} else {
// XXX: warn and notify me
@@ -171,14 +171,15 @@ public class CombineConsumeQueueStore implements ConsumeQueueStoreInterface {
}
@Override
public long getDispatchFromPhyOffset() {
long dispatchFromPhyOffset = assignOffsetStore.getDispatchFromPhyOffset();
public Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException {
Long dispatchFromPhyOffset = assignOffsetStore.getDispatchFromPhyOffset(recoverNormally);
for (AbstractConsumeQueueStore store : innerConsumeQueueStoreList) {
if (store == assignOffsetStore) {
continue;
}
if (store.getDispatchFromPhyOffset() < dispatchFromPhyOffset) {
dispatchFromPhyOffset = store.getDispatchFromPhyOffset();
Long storeOffset = store.getDispatchFromPhyOffset(recoverNormally);
if (storeOffset != null && dispatchFromPhyOffset != null && storeOffset < dispatchFromPhyOffset) {
dispatchFromPhyOffset = storeOffset;
}
}
return dispatchFromPhyOffset;
@@ -52,6 +52,7 @@ import org.apache.rocketmq.store.DefaultMessageStore;
import org.apache.rocketmq.store.DispatchRequest;
import org.apache.rocketmq.store.SelectMappedBufferResult;
import org.apache.rocketmq.store.exception.StoreException;
import org.rocksdb.RocksDBException;
import static java.lang.String.format;
import static org.apache.rocketmq.store.config.StorePathConfigHelper.getStorePathBatchConsumeQueue;
@@ -61,9 +62,6 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
private final FlushConsumeQueueService flushConsumeQueueService;
private final CorrectLogicOffsetService correctLogicOffsetService;
private final CleanConsumeQueueService cleanConsumeQueueService;
private long dispatchFromPhyOffset;
private long dispatchFromStoreTimestamp;
private final AtomicInteger lmqCounter = new AtomicInteger(0);
public ConsumeQueueStore(DefaultMessageStore messageStore) {
@@ -105,14 +103,25 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
}
}
}
dispatchFromPhyOffset = this.getMaxPhyOffsetInConsumeQueue();
dispatchFromStoreTimestamp = this.messageStore.getStoreCheckpoint().getMinTimestamp();
}
/**
* Implementation of CommitLogDispatchStore.getDispatchFromPhyOffset() (inherited from ConsumeQueueStoreInterface).
* When recoverNormally is false, returns checkpoint's logicsPhysicalOffset so commitlog abnormal recovery starts
* from it.
*/
@Override
public long getDispatchFromPhyOffset() {
return getMaxPhyOffsetInConsumeQueue();
public Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException {
if (recoverNormally) {
return getMaxPhyOffsetInConsumeQueue();
} else {
long fromCheckpoint = this.messageStore.getStoreCheckpoint().getLogicsPhysicalOffset();
long physicMsgTimestamp = this.messageStore.getStoreCheckpoint().getPhysicMsgTimestamp();
if (physicMsgTimestamp > 0 && fromCheckpoint <= 0 && messageStoreConfig.isEnableAcceleratedRecovery()) {
throw new RuntimeException("Accelerated recovery is enabled but checkpoint's logicsPhysicalOffset is invalid");
}
return fromCheckpoint;
}
}
public boolean recoverConcurrently() {
@@ -491,6 +500,7 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
this.setTopicQueueTable(cqOffsetTable);
this.setBatchTopicQueueTable(bcqOffsetTable);
}
private void compensateForHA(ConcurrentMap<String, Long> cqOffsetTable) {
SelectMappedBufferResult lastBuffer = null;
long startReadOffset = messageStore.getCommitLog().getConfirmOffset() == -1 ? 0 : messageStore.getCommitLog().getConfirmOffset();
@@ -612,12 +622,12 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
}
@Override
public boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp, boolean recoverNormally) {
if (recoverNormally) {
return phyOffset <= this.dispatchFromPhyOffset;
} else {
return storeTimestamp <= this.dispatchFromStoreTimestamp;
public boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException {
if (!recoverNormally && this.messageStore.getStoreCheckpoint().getLogicsPhysicalOffset() <= 0) { // for the sake of compatibility
return storeTimestamp <= this.messageStore.getStoreCheckpoint().getLogicsMsgTimestamp();
}
return phyOffset <= getDispatchFromPhyOffset(recoverNormally);
}
@Override
@@ -642,6 +652,7 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
}
long logicsMsgTimestamp = 0;
long logicsPhysicalOffset = 0;
int flushConsumeQueueThoroughInterval = messageStoreConfig.getFlushConsumeQueueThoroughInterval();
long currentTimeMillis = System.currentTimeMillis();
@@ -649,6 +660,7 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
this.lastFlushTimestamp = currentTimeMillis;
flushConsumeQueueLeastPages = 0;
logicsMsgTimestamp = messageStore.getStoreCheckpoint().getTmpLogicsMsgTimestamp();
logicsPhysicalOffset = messageStore.getStoreCheckpoint().getTmpLogicsPhysicalOffset();
}
for (ConcurrentMap<Integer, ConsumeQueueInterface> maps : consumeQueueTable.values()) {
@@ -668,6 +680,9 @@ public class ConsumeQueueStore extends AbstractConsumeQueueStore {
if (logicsMsgTimestamp > 0) {
messageStore.getStoreCheckpoint().setLogicsMsgTimestamp(logicsMsgTimestamp);
}
if (logicsPhysicalOffset > 0) {
messageStore.getStoreCheckpoint().setLogicsPhysicalOffset(logicsPhysicalOffset);
}
messageStore.getStoreCheckpoint().flush();
}
}
@@ -19,15 +19,17 @@ package org.apache.rocketmq.store.queue;
import java.util.concurrent.ConcurrentMap;
import org.apache.rocketmq.common.BoundaryType;
import org.apache.rocketmq.common.message.MessageExtBrokerInner;
import org.apache.rocketmq.store.CommitLogDispatchStore;
import org.apache.rocketmq.store.DispatchRequest;
import org.apache.rocketmq.store.exception.ConsumeQueueException;
import org.apache.rocketmq.store.exception.StoreException;
import org.rocksdb.RocksDBException;
public interface ConsumeQueueStoreInterface {
public interface ConsumeQueueStoreInterface extends CommitLogDispatchStore {
/**
* Load from file.
*
* @return true if loaded successfully.
*/
boolean load();
@@ -38,29 +40,11 @@ public interface ConsumeQueueStoreInterface {
*/
void recover(boolean concurrently) throws RocksDBException;
/**
* Get the dispatch offset in consume queue store, messages whose phyOffset larger than this offset need
* to be dispatched. The dispatch offset only used in recover.
*
* @return the dispatch phyOffset
*/
long getDispatchFromPhyOffset();
/**
* Start the consumeQueueStore
*/
void start();
/**
* Used to determine whether to start doDispatch from this commitLog mappedFile
*
* @param phyOffset the offset of the first message in this commitlog mappedFile
* @param storeTimestamp the timestamp of the first message in this commitlog mappedFile
* @return whether to start recovering from this MappedFile
*/
boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException;
/**
* Shutdown the consumeQueueStore
* @return true if shutdown successfully.
@@ -191,7 +191,7 @@ public class RocksDBConsumeQueueStore extends AbstractConsumeQueueStore {
}
@Override
public long getDispatchFromPhyOffset() {
public Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException {
return dispatchFromPhyOffset;
}
@@ -35,6 +35,7 @@ import org.apache.rocketmq.common.message.MessageExtBrokerInner;
import org.apache.rocketmq.common.topic.TopicValidator;
import org.apache.rocketmq.logging.org.slf4j.Logger;
import org.apache.rocketmq.logging.org.slf4j.LoggerFactory;
import org.apache.rocketmq.store.CommitLogDispatchStore;
import org.apache.rocketmq.store.DefaultMessageStore;
import org.apache.rocketmq.store.DispatchRequest;
import org.apache.rocketmq.store.MessageStore;
@@ -44,9 +45,10 @@ import org.apache.rocketmq.store.StoreUtil;
import org.apache.rocketmq.store.config.MessageStoreConfig;
import org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage;
import org.apache.rocketmq.store.stats.BrokerStatsManager;
import org.rocksdb.RocksDBException;
import static org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage.TRANS_COLUMN_FAMILY;
public class TransMessageRocksDBStore {
public class TransMessageRocksDBStore implements CommitLogDispatchStore {
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 REMOVE_TAG = "d";
@@ -260,7 +262,8 @@ public class TransMessageRocksDBStore {
}
}
public boolean isMappedFileMatchedRecover(long phyOffset) {
public boolean isMappedFileMatchedRecover(long phyOffset, long storeTimestamp,
boolean recoverNormally) throws RocksDBException {
if (!storeConfig.isTransRocksDBEnable()) {
return true;
}
@@ -341,4 +344,16 @@ public class TransMessageRocksDBStore {
}
}
}
@Override
public Long getDispatchFromPhyOffset(boolean recoverNormally) throws RocksDBException {
if (!storeConfig.isTransRocksDBEnable()) {
return null;
}
Long dispatchFromTransPhyOffset = messageRocksDBStorage.getLastOffsetPy(TRANS_COLUMN_FAMILY);
if (dispatchFromTransPhyOffset != null && dispatchFromTransPhyOffset > 0) {
return dispatchFromTransPhyOffset;
}
return null;
}
}
@@ -20,6 +20,12 @@ package org.apache.rocketmq.store;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import org.mockito.ArgumentCaptor;
import com.google.common.collect.Sets;
import java.io.File;
@@ -954,6 +960,96 @@ public class DefaultMessageStoreTest {
assertThat(messageStoreConfig.isEnableBatchPush()).isTrue();
}
@Test
public void testRecoverWithRocksDBOffsets() throws Exception {
// Test that recovery process considers RocksDB offsets when IndexRocksDBEnable or TransRocksDBEnable is enabled
UUID uuid = UUID.randomUUID();
String storePathRootDir = System.getProperty("java.io.tmpdir") + File.separator + "store-recover-test-" + uuid.toString();
try {
// Test case 1: IndexRocksDBEnable enabled with valid offset
// index offset: 500L, expected: min(consumeQueueOffset, 500L)
testRecoverWithRocksDBOffset(storePathRootDir + "-1", true, false, 500L, null);
// Test case 2: TransRocksDBEnable enabled with valid offset
// trans offset: 600L, expected: min(consumeQueueOffset, 600L)
testRecoverWithRocksDBOffset(storePathRootDir + "-2", false, true, null, 600L);
// Test case 3: Both enabled, take minimum value
// index offset: 500L, trans offset: 300L, expected: min(consumeQueueOffset, 500L, 300L)
testRecoverWithRocksDBOffset(storePathRootDir + "-3", true, true, 500L, 300L);
} finally {
// Clean up all test directories
for (int i = 1; i <= 3; i++) {
UtilAll.deleteFile(new File(storePathRootDir + "-" + i));
}
}
}
private void testRecoverWithRocksDBOffset(String storePathRootDir, boolean indexEnable,
boolean transEnable, Long indexOffset, Long transOffset) throws Exception {
MessageStoreConfig messageStoreConfig = new MessageStoreConfig();
messageStoreConfig.setMappedFileSizeCommitLog(1024 * 1024 * 10);
messageStoreConfig.setMappedFileSizeConsumeQueue(1024 * 1024 * 10);
messageStoreConfig.setMaxHashSlotNum(10000);
messageStoreConfig.setMaxIndexNum(100 * 100);
messageStoreConfig.setFlushDiskType(FlushDiskType.SYNC_FLUSH);
messageStoreConfig.setHaListenPort(0);
messageStoreConfig.setStorePathRootDir(storePathRootDir);
messageStoreConfig.setIndexRocksDBEnable(indexEnable);
messageStoreConfig.setTransRocksDBEnable(transEnable);
DefaultMessageStore store = new DefaultMessageStore(messageStoreConfig,
new BrokerStatsManager("test", true),
new MyMessageArrivingListener(),
new BrokerConfig(), new ConcurrentHashMap<>());
// Get the actual consumeQueueStore dispatchFromPhyOffset before loading (normal recovery)
long consumeQueueOffset = store.getQueueStore().getDispatchFromPhyOffset(true);
// Calculate expected value: min of consumeQueueOffset and RocksDB offsets
long calculatedExpected = consumeQueueOffset;
if (indexEnable && indexOffset != null && indexOffset > 0) {
calculatedExpected = Math.min(calculatedExpected, indexOffset);
}
if (transEnable && transOffset != null && transOffset > 0) {
calculatedExpected = Math.min(calculatedExpected, transOffset);
}
// Mock messageRocksDBStorage
java.lang.reflect.Field field = DefaultMessageStore.class.getDeclaredField("messageRocksDBStorage");
field.setAccessible(true);
org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage mockStorage =
mock(org.apache.rocketmq.store.rocksdb.MessageRocksDBStorage.class);
field.set(store, mockStorage);
// Spy commitLog to verify invocation and capture the dispatchFromPhyOffset value
java.lang.reflect.Field commitLogField = DefaultMessageStore.class.getDeclaredField("commitLog");
commitLogField.setAccessible(true);
CommitLog commitLog = (CommitLog) commitLogField.get(store);
CommitLog spyCommitLog = spy(commitLog);
commitLogField.set(store, spyCommitLog);
// Use ArgumentCaptor to capture the dispatchFromPhyOffset value
ArgumentCaptor<Long> offsetCaptor = ArgumentCaptor.forClass(Long.class);
// Load store, which will call recover method
boolean loadResult = store.load();
assertTrue(loadResult);
// Verify recoverNormally or recoverAbnormally is called and capture the argument
// Since it's a new store (no abort file), it should call recoverNormally
verify(spyCommitLog, atLeastOnce()).recoverNormally(offsetCaptor.capture());
// Verify the dispatchFromPhyOffset value is correct (should be the minimum)
Long actualDispatchFromPhyOffset = offsetCaptor.getValue();
assertThat(actualDispatchFromPhyOffset).isEqualTo(calculatedExpected);
// Clean up resources
store.shutdown();
store.destroy();
}
private class MyMessageArrivingListener implements MessageArrivingListener {
@Override
public void arriving(String topic, int queueId, long logicOffset, long tagsCode, long msgStoreTime,
@@ -35,8 +35,10 @@ public class StoreCheckpointTest {
StoreCheckpoint storeCheckpoint = new StoreCheckpoint("target/checkpoint_test/0000");
long physicMsgTimestamp = 0xAABB;
long logicsMsgTimestamp = 0xCCDD;
long logicsPhysicalOffset = 0x1000L;
storeCheckpoint.setPhysicMsgTimestamp(physicMsgTimestamp);
storeCheckpoint.setLogicsMsgTimestamp(logicsMsgTimestamp);
storeCheckpoint.setLogicsPhysicalOffset(logicsPhysicalOffset);
storeCheckpoint.flush();
long diff = physicMsgTimestamp - storeCheckpoint.getMinTimestamp();
@@ -45,6 +47,7 @@ public class StoreCheckpointTest {
storeCheckpoint = new StoreCheckpoint("target/checkpoint_test/0000");
assertThat(storeCheckpoint.getPhysicMsgTimestamp()).isEqualTo(physicMsgTimestamp);
assertThat(storeCheckpoint.getLogicsMsgTimestamp()).isEqualTo(logicsMsgTimestamp);
assertThat(storeCheckpoint.getLogicsPhysicalOffset()).isEqualTo(logicsPhysicalOffset);
}
@After
@@ -42,6 +42,7 @@ import org.apache.rocketmq.store.StoreCheckpoint;
import org.apache.rocketmq.store.config.StorePathConfigHelper;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Assume;
import org.apache.rocketmq.common.MixAll;
@@ -58,6 +59,7 @@ public class DLedgerCommitlogTest extends MessageStoreTestBase {
Assume.assumeFalse(MixAll.isMac());
}
@Ignore
@Test
public void testTruncateCQ() throws Exception {
String base = createBaseDir();