in ratis-server/src/main/java/org/apache/ratis/server/raftlog/segmented/SegmentedRaftLogWorker.java [695:737]
void execute() throws IOException {
freeSegmentedRaftLogOutputStream();
if (segments.getToDelete() != null && segments.getToDelete().length > 0) {
long minStart = segments.getToDelete()[0].getStartIndex();
for (SegmentFileInfo del : segments.getToDelete()) {
final File delFile = del.getFile(storage);
Preconditions.assertTrue(delFile.exists(),
"File %s to be deleted does not exist", delFile);
final Path deleted = FileUtils.deleteFile(delFile);
LOG.info("{}: Deleted RaftLog segment for {}: path={}", name, segments.getReason(), deleted);
minStart = Math.min(minStart, del.getStartIndex());
}
if (segments.getToTruncate() == null) {
lastWrittenIndex = minStart - 1;
}
}
if (segments.getToTruncate() != null) {
final File fileToTruncate = segments.getToTruncate().getFile(storage);
Preconditions.assertTrue(fileToTruncate.exists(),
"File %s to be truncated does not exist", fileToTruncate);
FileUtils.truncateFile(fileToTruncate, segments.getToTruncate().getTargetLength());
// rename the file
final File dstFile = segments.getToTruncate().getNewFile(storage);
Preconditions.assertTrue(!dstFile.exists(),
"Truncated file %s already exists ", dstFile);
FileUtils.move(fileToTruncate, dstFile);
LOG.info("{}: Truncated log file {} to length {} and moved it to {}", name,
fileToTruncate, segments.getToTruncate().getTargetLength(), dstFile);
// update lastWrittenIndex
lastWrittenIndex = segments.getToTruncate().getNewEndIndex();
}
if (stateMachineFuture != null) {
IOUtils.getFromFuture(stateMachineFuture, () -> this + "-truncateStateMachineData");
}
flushIndex.setUnconditionally(lastWrittenIndex, infoIndexChange);
safeCacheEvictIndex.setUnconditionally(lastWrittenIndex, infoIndexChange);
postUpdateFlushedIndex(0);
}