提交 390f7358 编写于 作者: Z Zhendong Liu 提交者: von gosling

[ROCKETMQ-321] Fix deleting files in the middle (#198)

上级 56b09832
...@@ -570,6 +570,11 @@ public class MappedFile extends ReferenceResource { ...@@ -570,6 +570,11 @@ public class MappedFile extends ReferenceResource {
log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime); log.info("munlock {} {} {} ret = {} time consuming = {}", address, this.fileName, this.fileSize, ret, System.currentTimeMillis() - beginTime);
} }
//testable
File getFile() {
return this.file;
}
@Override @Override
public String toString() { public String toString() {
return this.fileName; return this.fileName;
......
...@@ -367,6 +367,9 @@ public class MappedFileQueue { ...@@ -367,6 +367,9 @@ public class MappedFileQueue {
} else { } else {
break; break;
} }
} else {
//avoid deleting files in the middle
break;
} }
} }
} }
......
...@@ -202,6 +202,33 @@ public class MappedFileQueueTest { ...@@ -202,6 +202,33 @@ public class MappedFileQueueTest {
mappedFileQueue.destroy(); 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 @After
public void destory() { public void destory() {
File file = new File("target/unit_test_store"); File file = new File("target/unit_test_store");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册