Skip to content

Commit

Permalink
test & fix: truncate tail
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Nov 17, 2024
1 parent 0c76923 commit af95e52
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.dtprj.dongting.raft.impl.FileUtil;
import com.github.dtprj.dongting.raft.impl.RaftStatusImpl;
import com.github.dtprj.dongting.raft.impl.RaftUtil;
import com.github.dtprj.dongting.raft.server.ChecksumException;
import com.github.dtprj.dongting.raft.server.LogItem;
import com.github.dtprj.dongting.raft.server.RaftGroupConfigEx;
import com.github.dtprj.dongting.raft.sm.RaftCodecFactory;
Expand Down Expand Up @@ -139,10 +140,27 @@ public FrameCallResult execute(Void input) {
}

private FrameCallResult afterPosLoad(Long pos) {
if (logFiles.startPosOfFile(pos) == pos && index - 1 >= logFiles.getFirstIndex()) {
return Fiber.call(idxFiles.loadLogPos(index - 1), this::afterPosLoad2);
}
idxFiles.truncateTail(index);
logFiles.truncateTail(index, pos);
return Fiber.frameReturn();
}

private FrameCallResult afterPosLoad2(Long pos) {
return Fiber.call(logFiles.loadHeader(pos), h -> afterLoadHeader(h, pos));
}

private FrameCallResult afterLoadHeader(LogHeader h, long pos) {
if (h.crcMatch()) {
idxFiles.truncateTail(index);
logFiles.truncateTail(index, pos + h.totalLen);
return Fiber.frameReturn();
} else {
throw new ChecksumException("header checksum mismatch: " + pos);
}
}
};

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.ArrayList;
import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
Expand Down Expand Up @@ -208,4 +209,46 @@ private static Supplier<Boolean> fileDeleted(File dir, long startPos) {
private void plus1Hour() throws Exception {
doInFiber(() -> TestUtil.plus1Hour(raftStatus.getTs()));
}

@Test
void testTruncate() throws Exception {
int[] totalSizes = new int[]{800, 512, 256, 256, 512};
int[] bizHeaderLen = new int[]{10, 10, 10, 10, 10};
append(1, totalSizes, bizHeaderLen);
raftStatus.setCommitIndex(1);
doInFiber(new FiberFrame<>() {
@Override
public FrameCallResult execute(Void input) {
LogFile logFile = raftLog.logFiles.getLogFile(2048);
assertEquals(5, logFile.firstIndex);
return Fiber.call(raftLog.truncateTail(5), this::resume1);
}

private FrameCallResult resume1(Void unused) {
LogFile logFile = raftLog.logFiles.getLogFile(2048);
assertEquals(0, logFile.firstIndex);
logFile = raftLog.logFiles.getLogFile(1024);
assertEquals(2, logFile.firstIndex);
assertEquals(5, raftLog.logFiles.logAppender.nextPersistIndex);
assertEquals(2048, raftLog.logFiles.logAppender.nextPersistPos);
return Fiber.call(raftLog.truncateTail(3), this::resume2);
}

private FrameCallResult resume2(Void unused) {
LogFile logFile = raftLog.logFiles.getLogFile(1024);
assertEquals(2, logFile.firstIndex);
assertEquals(3, raftLog.logFiles.logAppender.nextPersistIndex);
assertEquals(1536, raftLog.logFiles.logAppender.nextPersistPos);
return Fiber.call(raftLog.truncateTail(2), this::resume3);
}

private FrameCallResult resume3(Void unused) {
LogFile logFile = raftLog.logFiles.getLogFile(1024);
assertEquals(0, logFile.firstIndex);
assertEquals(2, raftLog.logFiles.logAppender.nextPersistIndex);
assertEquals(800, raftLog.logFiles.logAppender.nextPersistPos);
return Fiber.frameReturn();
}
});
}
}

0 comments on commit af95e52

Please sign in to comment.