From 390f73582b234e7081ad4b16a93326ab0e128df0 Mon Sep 17 00:00:00 2001 From: Zhendong Liu Date: Wed, 13 Dec 2017 19:35:21 +0800 Subject: [PATCH] [ROCKETMQ-321] Fix deleting files in the middle (#198) --- .../org/apache/rocketmq/store/MappedFile.java | 5 ++++ .../rocketmq/store/MappedFileQueue.java | 3 +++ .../rocketmq/store/MappedFileQueueTest.java | 27 +++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/store/src/main/java/org/apache/rocketmq/store/MappedFile.java b/store/src/main/java/org/apache/rocketmq/store/MappedFile.java index 492ac5f2..0a43d47f 100644 --- a/store/src/main/java/org/apache/rocketmq/store/MappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/MappedFile.java @@ -570,6 +570,11 @@ public class MappedFile extends ReferenceResource { log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); } + //testable + File getFile() { + return this.file; + } + @Override public String toString() { return this.fileName; diff --git a/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java b/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java index 0ee1b9f9..9eb3b3ab 100644 --- a/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java +++ b/store/src/main/java/org/apache/rocketmq/store/MappedFileQueue.java @@ -367,6 +367,9 @@ public class MappedFileQueue { } else { break; } + } else { + //avoid deleting files in the middle + break; } } } diff --git a/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java b/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java index dd7b229d..92f1876b 100644 --- a/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java +++ b/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java @@ -202,6 +202,33 @@ public class MappedFileQueueTest { mappedFileQueue.destroy(); } + @Test + public void testDeleteExpiredFileByTime() throws Exception { + MappedFileQueue mappedFileQueue = + new MappedFileQueue("target/unit_test_store/f/", 1024, null); + + for (int i = 0; i < 100; i++) { + MappedFile mappedFile = mappedFileQueue.getLastMappedFile(0); + assertThat(mappedFile).isNotNull(); + byte[] bytes = new byte[512]; + assertThat(mappedFile.appendMessage(bytes)).isTrue(); + } + + assertThat(mappedFileQueue.getMappedFiles().size()).isEqualTo(50); + long expiredTime = 100 * 1000; + for (int i = 0; i < mappedFileQueue.getMappedFiles().size(); i++) { + MappedFile mappedFile = mappedFileQueue.getMappedFiles().get(i); + if (i < 5) { + mappedFile.getFile().setLastModified(System.currentTimeMillis() - expiredTime * 2); + } + if (i > 20) { + mappedFile.getFile().setLastModified(System.currentTimeMillis() - expiredTime * 2); + } + } + mappedFileQueue.deleteExpiredFileByTime(expiredTime, 0, 0, false); + assertThat(mappedFileQueue.getMappedFiles().size()).isEqualTo(45); + } + @After public void destory() { File file = new File("target/unit_test_store"); -- GitLab