Improve compact check of BufferedLinearRegionFile

This commit is contained in:
2026-07-18 18:30:52 +08:00
parent d5733936ac
commit 9af4757cdb
@@ -269,13 +269,13 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
public void syncIfNeeded() throws IOException { public void syncIfNeeded() throws IOException {
try { try {
this.syncToMasterFile(); this.syncToMasterFile(false, false);
} finally { } finally {
BEING_SYNCED_HANDLE.setVolatile(this, false); // mark as not being synced BEING_SYNCED_HANDLE.setVolatile(this, false); // mark as not being synced
} }
} }
private void syncToMasterFile() throws IOException { private void syncToMasterFile(boolean forceSync, boolean forceCompact) throws IOException {
// serialized against close: the swap channel cannot go away under a running sync // serialized against close: the swap channel cannot go away under a running sync
synchronized (this.syncLock) { synchronized (this.syncLock) {
// skip if closed already // skip if closed already
@@ -285,12 +285,12 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
// fast skip when there is nothing to sync; writers flip the flag back // fast skip when there is nothing to sync; writers flip the flag back
// via markAsToSync() which triggers the next round // via markAsToSync() which triggers the next round
if (!SYNCED_HANDLE.compareAndSet(this, false, true)) { if (!SYNCED_HANDLE.compareAndSet(this, false, true) && !forceSync) {
return; return;
} }
try { try {
this.masterFileParser.sync(this.masterFilePath); this.masterFileParser.sync(this.masterFilePath, forceCompact);
} catch (Throwable e) { } catch (Throwable e) {
// set back // set back
SYNCED_HANDLE.setVolatile(this, false); SYNCED_HANDLE.setVolatile(this, false);
@@ -403,7 +403,7 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
} }
if (initiallySyncRequired) { if (initiallySyncRequired) {
this.syncToMasterFile(); this.syncToMasterFile(false, false);
} }
} }
@@ -427,7 +427,7 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
// concurrent flusher sync is still running when we tear down below. // concurrent flusher sync is still running when we tear down below.
// if this throws we deliberately stay open: the flusher can retry the sync // if this throws we deliberately stay open: the flusher can retry the sync
// later, and the not-yet-synced swap data is not dropped on the floor // later, and the not-yet-synced swap data is not dropped on the floor
this.syncToMasterFile(); this.syncToMasterFile(true, true);
IOException failure = null; IOException failure = null;
@@ -974,13 +974,13 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
} }
// must be called under syncLock (see syncToMasterFile) // must be called under syncLock (see syncToMasterFile)
public void sync(@NotNull Path mainFile) throws IOException { public void sync(@NotNull Path mainFile, boolean forceCompact) throws IOException {
this.masterFileLock.writeLock().lock(); this.masterFileLock.writeLock().lock();
try { try {
// full rewrite on the first sync after open, and afterwards whenever the // full rewrite on the first sync after open, and afterwards whenever the
// appended garbage passed the auto-compact threshold: writes a tmp file, // appended garbage passed the auto-compact threshold: writes a tmp file,
// then atomically replaces the master file with it // then atomically replaces the master file with it
if (this.appendChannel == null || this.shouldCompactMasterFile()) { if (this.appendChannel == null || this.shouldCompactMasterFile() || forceCompact) {
this.rewriteFully(mainFile); this.rewriteFully(mainFile);
} else { } else {
// WAL-style otherwise: only append the dirty buckets // WAL-style otherwise: only append the dirty buckets
@@ -1589,7 +1589,7 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
// old parsed, remove the original file, and we will recreate it as we sync // old parsed, remove the original file, and we will recreate it as we sync
if (oldParsed) { if (oldParsed) {
// immediately do sync operation // immediately do sync operation
BufferedLinearRegionFile.this.syncToMasterFile(); BufferedLinearRegionFile.this.syncToMasterFile(true, true);
return; return;
} }