From 8cc4e0ccbd125248653a06489b824fd30bcf327e Mon Sep 17 00:00:00 2001 From: Andrei Tokar Date: Sat, 23 Mar 2024 15:13:10 -0400 Subject: [PATCH] fix for #3909 --- h2/src/main/org/h2/mvstore/FileStore.java | 2 +- .../main/org/h2/mvstore/RandomAccessStore.java | 18 ++++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/h2/src/main/org/h2/mvstore/FileStore.java b/h2/src/main/org/h2/mvstore/FileStore.java index 0194e7c928..949736503a 100644 --- a/h2/src/main/org/h2/mvstore/FileStore.java +++ b/h2/src/main/org/h2/mvstore/FileStore.java @@ -423,7 +423,7 @@ public final void setAutoCommitDelay(int millis) { stopBackgroundThread(millis >= 0); // start the background thread if needed if (millis > 0 && mvStore.isOpen()) { - int sleep = Math.max(1, millis / 5); + int sleep = Math.max(10, millis / 3); BackgroundWriterThread t = new BackgroundWriterThread(this, sleep, toString()); if (backgroundWriterThread.compareAndSet(null, t)) { t.start(); diff --git a/h2/src/main/org/h2/mvstore/RandomAccessStore.java b/h2/src/main/org/h2/mvstore/RandomAccessStore.java index 4f6a14a1f5..745e3b2af7 100644 --- a/h2/src/main/org/h2/mvstore/RandomAccessStore.java +++ b/h2/src/main/org/h2/mvstore/RandomAccessStore.java @@ -44,7 +44,6 @@ public abstract class RandomAccessStore extends FileStore private long reservedLow; private long reservedHigh; private boolean stopIdleHousekeeping; - private int restoreHousekeepingAtRate; public RandomAccessStore(Map config) { super(config); @@ -705,11 +704,13 @@ private void shrinkIfPossible(int minPercent) { protected void doHousekeeping(MVStore mvStore) throws InterruptedException { boolean idle = isIdle(); int rewritableChunksFillRate = getRewritableChunksFillRate(); - if (idle && stopIdleHousekeeping && rewritableChunksFillRate > restoreHousekeepingAtRate) { + if (idle && stopIdleHousekeeping) { return; } int autoCommitMemory = mvStore.getAutoCommitMemory(); - if (isFragmented() && getFillRate() < getAutoCompactFillRate()) { + int fileFillRate = getFillRate(); + long chunksTotalSize = size() * fileFillRate / 100; + if (isFragmented() && fileFillRate < getAutoCompactFillRate()) { mvStore.tryExecuteUnderStoreLock(() -> { int moveSize = 2 * autoCommitMemory; if (idle) { @@ -739,15 +740,8 @@ protected void doHousekeeping(MVStore mvStore) throws InterruptedException { stopIdleHousekeeping = false; if (idle) { int currentChunksFillRate = getChunksFillRate(); - stopIdleHousekeeping = currentChunksFillRate <= chunksFillRate; - if (stopIdleHousekeeping) { - // this rate can change with the time, even when database is idle, - // since chunks become older and may become eligible for re-writing - rewritableChunksFillRate = getRewritableChunksFillRate(); - restoreHousekeepingAtRate = rewritableChunksFillRate > currentChunksFillRate ? - (currentChunksFillRate + rewritableChunksFillRate) / 2 : - rewritableChunksFillRate - 2; - } + long currentTotalChunksSize = size() * getFillRate() / 100; + stopIdleHousekeeping = currentTotalChunksSize > chunksTotalSize || currentTotalChunksSize == chunksTotalSize && currentChunksFillRate <= chunksFillRate; } }