Skip to content

Commit

Permalink
Avoid unnecessary force write. (apache#3847)
Browse files Browse the repository at this point in the history
* Avoid unnecessary force write.

* code clean.

* fix style
  • Loading branch information
horizonzy authored and Anup Ghatage committed Jul 12, 2024
1 parent 8b65a36 commit fa80202
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public static class ForceWriteRequest {
private boolean shouldClose;
private long lastFlushedPosition;
private long logId;
private boolean flushed;

public int process() {
closeFileIfNecessary();
Expand All @@ -381,13 +382,20 @@ public int process() {
return forceWriteWaiters.size();
}

private void flushFileToDisk() throws IOException {
if (!flushed) {
logFile.forceWrite(false);
flushed = true;
}
}

public void closeFileIfNecessary() {
// Close if shouldClose is set
if (shouldClose) {
// We should guard against exceptions so its
// safe to call in catch blocks
try {
logFile.forceWrite(false);
flushFileToDisk();
logFile.close();
// Call close only once
shouldClose = false;
Expand Down Expand Up @@ -532,7 +540,7 @@ public void run() {
private void syncJournal(ForceWriteRequest lastRequest) throws IOException {
long fsyncStartTime = MathUtils.nowInNano();
try {
lastRequest.logFile.forceWrite(false);
lastRequest.flushFileToDisk();
journalStats.getJournalSyncStats().registerSuccessfulEvent(MathUtils.elapsedNanos(fsyncStartTime),
TimeUnit.NANOSECONDS);
lastLogMark.setCurLogMark(lastRequest.logId, lastRequest.lastFlushedPosition);
Expand Down

0 comments on commit fa80202

Please sign in to comment.