You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@rocketmq.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2017/12/13 11:36:00 UTC

[jira] [Commented] (ROCKETMQ-321) Avoid deleting mapped files in the middle

    [ https://issues.apache.org/jira/browse/ROCKETMQ-321?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16289137#comment-16289137 ] 

ASF GitHub Bot commented on ROCKETMQ-321:
-----------------------------------------

vongosling closed pull request #198: [ROCKETMQ-321] Fix deleting files in the middle
URL: https://github.com/apache/rocketmq/pull/198
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 void munlock() {
         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 edf4c918..f2947f13 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 int deleteExpiredFileByTime(final long expiredTime,
                     } 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 203dfcd5..29580992 100644
--- a/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java
+++ b/store/src/test/java/org/apache/rocketmq/store/MappedFileQueueTest.java
@@ -20,9 +20,7 @@
 import java.io.File;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
-
 import org.apache.rocketmq.common.UtilAll;
-import org.apache.rocketmq.store.config.MessageStoreConfig;
 import org.junit.After;
 import org.junit.Test;
 
@@ -182,6 +180,33 @@ public void testDeleteExpiredFileByOffset() {
         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");


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Avoid deleting mapped files in the middle
> -----------------------------------------
>
>                 Key: ROCKETMQ-321
>                 URL: https://issues.apache.org/jira/browse/ROCKETMQ-321
>             Project: Apache RocketMQ
>          Issue Type: Bug
>    Affects Versions: 4.1.0-incubating
>            Reporter: dongeforever
>            Assignee: dongeforever
>             Fix For: 4.2.0
>
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)