Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions store/src/main/java/org/apache/rocketmq/store/MappedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but no need to return this file to test code, you could structure the File object directly.

}

@Override
public String toString() {
return this.fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ public int deleteExpiredFileByTime(final long expiredTime,
} else {
break;
}
} else {
//avoid deleting files in the middle
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down