Skip to content

Commit

Permalink
[ISSUE #7684] Fix iterator.remove() bug (#7682)
Browse files Browse the repository at this point in the history
* bugfix: CopyOnWriteArray#listIterator do not support remove action when iterating

* add testcase

---------

Co-authored-by: mipengcheng3 <[email protected]>
  • Loading branch information
EvanMi and mipengcheng3 authored Dec 20, 2023
1 parent fbfc066 commit f0a3e93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ public boolean resetOffset(long offset) {
}

ListIterator<MappedFile> iterator = this.mappedFiles.listIterator(mappedFiles.size());
List<MappedFile> toRemoves = new ArrayList<>();

while (iterator.hasPrevious()) {
mappedFileLast = iterator.previous();
Expand All @@ -416,9 +417,14 @@ public boolean resetOffset(long offset) {
mappedFileLast.setCommittedPosition(where);
break;
} else {
iterator.remove();
toRemoves.add(mappedFileLast);
}
}

if (!toRemoves.isEmpty()) {
this.mappedFiles.removeAll(toRemoves);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,21 @@ public void testMappedFile_Rename() throws IOException, InterruptedException {
TimeUnit.SECONDS.sleep(3);
}

@Test
public void testReset() {
final String fixedMsg = "0123456789abcdef";
MappedFileQueue mappedFileQueue =
new MappedFileQueue(storePath + File.separator + "a/", 64, null);
for (int i = 0; i < 8; i++) {
MappedFile mappedFile = mappedFileQueue.getLastMappedFile(0);
assertThat(mappedFile).isNotNull();
assertThat(mappedFile.appendMessage(fixedMsg.getBytes())).isTrue();
}
assertThat(mappedFileQueue.getMappedFiles().size()).isEqualTo(2);
assertThat(mappedFileQueue.resetOffset(0)).isTrue();
assertThat(mappedFileQueue.getMappedFiles().size()).isEqualTo(1);
}

@After
public void destroy() {
File file = new File(storePath);
Expand Down

0 comments on commit f0a3e93

Please sign in to comment.