From dc66b3a3c615e28c09d5c29eef436eb1467255f5 Mon Sep 17 00:00:00 2001 From: frotmo <1823946536@qq.com> Date: Tue, 24 Nov 2026 22:59:44 +0800 Subject: [PATCH] [ISSUE #11603] Refuse slave truncation below confirmOffset in auto-switch HA A slave whose master restarted with a regressed commitlog resolved a consistent point below its own confirmOffset and truncated messages that were already acknowledged to producers. Refuse the truncation by default and log an error; enableTruncateBelowConfirmOffset is added as an explicit disaster-recovery override. Add unit tests for the refused case, the exact-confirm boundary, and the override. --- .../store/config/MessageStoreConfig.java | 15 ++ .../ha/autoswitch/AutoSwitchHAClient.java | 9 ++ .../ha/autoswitch/AutoSwitchHAClientTest.java | 130 ++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 store/src/test/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClientTest.java diff --git a/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java b/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java index 48122d64c7d..3fa9059ec80 100644 --- a/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java +++ b/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java @@ -396,6 +396,13 @@ public class MessageStoreConfig { @ImportantField private boolean allAckInSyncStateSet = false; + /** + * Whether to allow a slave to truncate its commitlog below the local confirmOffset when resolving a + * consistent point with the master in auto-switch HA mode. Enabling this may drop messages that were + * already confirmed to producers, so it should only be used for manual disaster recovery. + */ + private boolean enableTruncateBelowConfirmOffset = false; + /** * Dynamically adjust in-sync replicas to provide higher availability, the real time in-sync replicas * will smaller than inSyncReplicas config. @@ -1692,6 +1699,14 @@ public void setAllAckInSyncStateSet(boolean allAckInSyncStateSet) { this.allAckInSyncStateSet = allAckInSyncStateSet; } + public boolean isEnableTruncateBelowConfirmOffset() { + return enableTruncateBelowConfirmOffset; + } + + public void setEnableTruncateBelowConfirmOffset(boolean enableTruncateBelowConfirmOffset) { + this.enableTruncateBelowConfirmOffset = enableTruncateBelowConfirmOffset; + } + public boolean isEnableAutoInSyncReplicas() { return enableAutoInSyncReplicas; } diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java index 665d9aae3b0..04a554587a3 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java @@ -459,6 +459,15 @@ private boolean doTruncate(List masterEpochEntries, long masterEndOf LOGGER.error("Failed to find a consistent point between masterEpoch:{} and slaveEpoch:{}", masterEpochEntries, localEpochEntries); return false; } + final long confirmOffset = this.messageStore.getConfirmOffsetDirectly(); + if (truncateOffset < confirmOffset + && !this.messageStore.getMessageStoreConfig().isEnableTruncateBelowConfirmOffset()) { + LOGGER.error("Refuse to truncate slave log to {}, which is below the local confirmOffset {}. " + + "Truncating would drop messages already confirmed to producers. Set " + + "enableTruncateBelowConfirmOffset=true only for manual recovery. masterEpoch:{}, localEpoch:{}", + truncateOffset, confirmOffset, masterEpochEntries, localEpochEntries); + return false; + } if (!this.messageStore.truncateFiles(truncateOffset)) { LOGGER.error("Failed to truncate slave log to {}", truncateOffset); return false; diff --git a/store/src/test/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClientTest.java b/store/src/test/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClientTest.java new file mode 100644 index 00000000000..8925601edfe --- /dev/null +++ b/store/src/test/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClientTest.java @@ -0,0 +1,130 @@ +/* + * 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.ha.autoswitch; + +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.channels.SocketChannel; +import java.util.Collections; +import java.util.List; +import org.apache.rocketmq.common.BrokerConfig; +import org.apache.rocketmq.remoting.protocol.EpochEntry; +import org.apache.rocketmq.store.DefaultMessageStore; +import org.apache.rocketmq.store.config.MessageStoreConfig; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class AutoSwitchHAClientTest { + + @Rule + public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Test + public void testDoTruncateRefusesBelowConfirmOffset() throws Exception { + final ClientFixture fixture = createFixture(3280L, 3280L, false); + try { + assertFalse(invokeDoTruncate(fixture.client, + Collections.singletonList(new EpochEntry(1, 0)), 1580L)); + verify(fixture.messageStore, never()).truncateFiles(anyLong()); + verify(fixture.messageStore).getConfirmOffsetDirectly(); + } finally { + shutdown(fixture.client); + } + } + + @Test + public void testDoTruncateAllowsTruncateAtConfirmOffset() throws Exception { + final ClientFixture fixture = createFixture(3280L, 1580L, false); + try { + invokeDoTruncate(fixture.client, Collections.singletonList(new EpochEntry(1, 0)), 1580L); + verify(fixture.messageStore).truncateFiles(1580L); + } finally { + shutdown(fixture.client); + } + } + + @Test + public void testDoTruncateAllowsBelowConfirmOffsetWhenEnabled() throws Exception { + final ClientFixture fixture = createFixture(3280L, 3280L, true); + try { + invokeDoTruncate(fixture.client, Collections.singletonList(new EpochEntry(1, 0)), 1580L); + verify(fixture.messageStore).truncateFiles(1580L); + } finally { + shutdown(fixture.client); + } + } + + private ClientFixture createFixture(final long maxPhyOffset, final long confirmOffset, + final boolean enableTruncateBelowConfirmOffset) throws Exception { + final MessageStoreConfig messageStoreConfig = new MessageStoreConfig(); + messageStoreConfig.setEnableTruncateBelowConfirmOffset(enableTruncateBelowConfirmOffset); + + final DefaultMessageStore messageStore = mock(DefaultMessageStore.class); + when(messageStore.getMessageStoreConfig()).thenReturn(messageStoreConfig); + when(messageStore.getBrokerConfig()).thenReturn(new BrokerConfig()); + when(messageStore.getMaxPhyOffset()).thenReturn(maxPhyOffset); + when(messageStore.getConfirmOffsetDirectly()).thenReturn(confirmOffset); + when(messageStore.truncateFiles(anyLong())).thenReturn(true); + + final AutoSwitchHAService haService = mock(AutoSwitchHAService.class); + when(haService.getDefaultMessageStore()).thenReturn(messageStore); + + final String epochFilePath = new File(tempFolder.getRoot(), + "EpochFileCache-" + System.nanoTime()).getAbsolutePath(); + final EpochFileCache epochCache = new EpochFileCache(epochFilePath); + assertTrue(epochCache.appendEntry(new EpochEntry(1, 0))); + + final AutoSwitchHAClient client = new AutoSwitchHAClient(haService, messageStore, epochCache, 1L); + final Field socketChannelField = AutoSwitchHAClient.class.getDeclaredField("socketChannel"); + socketChannelField.setAccessible(true); + socketChannelField.set(client, mock(SocketChannel.class)); + + final ClientFixture fixture = new ClientFixture(); + fixture.client = client; + fixture.messageStore = messageStore; + return fixture; + } + + private void shutdown(final AutoSwitchHAClient client) throws Exception { + final Field socketChannelField = AutoSwitchHAClient.class.getDeclaredField("socketChannel"); + socketChannelField.setAccessible(true); + socketChannelField.set(client, null); + client.shutdown(); + } + + private boolean invokeDoTruncate(final AutoSwitchHAClient client, final List masterEpochEntries, + final long masterEndOffset) throws Exception { + final Method doTruncate = AutoSwitchHAClient.class.getDeclaredMethod("doTruncate", List.class, long.class); + doTruncate.setAccessible(true); + return (boolean) doTruncate.invoke(client, masterEpochEntries, masterEndOffset); + } + + private static class ClientFixture { + private AutoSwitchHAClient client; + private DefaultMessageStore messageStore; + } +} \ No newline at end of file