diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/CheckpointFile.java b/common/src/main/java/org/apache/rocketmq/common/utils/CheckpointFile.java index 1cb85d05eee..91d48768809 100644 --- a/common/src/main/java/org/apache/rocketmq/common/utils/CheckpointFile.java +++ b/common/src/main/java/org/apache/rocketmq/common/utils/CheckpointFile.java @@ -24,7 +24,6 @@ import java.nio.file.Files; import java.util.ArrayList; import java.util.List; -import org.apache.commons.collections.CollectionUtils; import org.apache.rocketmq.common.MixAll; import org.apache.rocketmq.common.UtilAll; @@ -75,9 +74,6 @@ public String getBackFilePath() { * Write entries to file */ public void write(final List entries) throws IOException { - if (entries.isEmpty()) { - return; - } synchronized (this) { StringBuilder entryContent = new StringBuilder(); for (T entry : entries) { @@ -143,11 +139,10 @@ private List read(String filePath) throws IOException { */ public List read() throws IOException { try { - List result = this.read(this.filePath); - if (CollectionUtils.isEmpty(result)) { - result = this.read(this.getBackFilePath()); + if (!new File(this.filePath).exists()) { + return this.read(this.getBackFilePath()); } - return result; + return this.read(this.filePath); } catch (IOException e) { return this.read(this.getBackFilePath()); } diff --git a/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/CheckpointFileTest.java b/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/CheckpointFileTest.java index 658f59e4806..f9dae612027 100644 --- a/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/CheckpointFileTest.java +++ b/remoting/src/test/java/org/apache/rocketmq/remoting/protocol/CheckpointFileTest.java @@ -84,6 +84,14 @@ public void testNormalWriteAndRead() throws IOException { Assert.assertEquals(entryList, listFromFile); } + @Test + public void testEmptyCheckpointOverridesOldState() throws IOException { + this.checkpoint.write(entryList); + this.checkpoint.write(new ArrayList<>()); + + Assert.assertTrue(checkpoint.read().isEmpty()); + } + @Test public void testAbNormalWriteAndRead() throws IOException { this.checkpoint.write(entryList);