From 57affe936468f09bd01a8d15176d0543d38a671e Mon Sep 17 00:00:00 2001 From: Andrei Tokar Date: Tue, 30 Jul 2024 19:31:35 -0400 Subject: [PATCH 1/3] prevent immediate re-write of the last saved chunk --- h2/src/main/org/h2/mvstore/FileStore.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/h2/src/main/org/h2/mvstore/FileStore.java b/h2/src/main/org/h2/mvstore/FileStore.java index f7d16d1415..89c681289e 100644 --- a/h2/src/main/org/h2/mvstore/FileStore.java +++ b/h2/src/main/org/h2/mvstore/FileStore.java @@ -693,7 +693,9 @@ private boolean isSeasonedChunk(C chunk, long time) { } private boolean isRewritable(C chunk, long time) { - return chunk.isRewritable() && isSeasonedChunk(chunk, time); + return chunk.isRewritable() && isSeasonedChunk(chunk, time) + // to prevent last saved chunk from being re-written as it may cause "endless" re-write loop + && chunk.version < getMvStore().getCurrentVersion() - 1; } /** From dcc8c0560e0afa7d6832613a6e89b6a66dd56c77 Mon Sep 17 00:00:00 2001 From: Andrei Tokar Date: Tue, 30 Jul 2024 19:33:26 -0400 Subject: [PATCH 2/3] MVStoreTool.dump() does not show chunk's position, because it's not part of the chunk header anymore --- h2/src/main/org/h2/mvstore/MVStoreTool.java | 1 + 1 file changed, 1 insertion(+) diff --git a/h2/src/main/org/h2/mvstore/MVStoreTool.java b/h2/src/main/org/h2/mvstore/MVStoreTool.java index a6d09f0032..747ac40be5 100644 --- a/h2/src/main/org/h2/mvstore/MVStoreTool.java +++ b/h2/src/main/org/h2/mvstore/MVStoreTool.java @@ -153,6 +153,7 @@ public static void dump(String fileName, Writer writer, boolean details) { Chunk c; try { c = new SFChunk(Chunk.readChunkHeader(buffer)); + c.block = pos / blockSize; } catch (MVStoreException e) { // Chunks are not always contiguous (due to chunk compaction/move/drop and space re-use) // Blocks following a chunk can therefore contain something else than a valid chunk header From 693590dae3b0abefef8e771ce397a4204842256e Mon Sep 17 00:00:00 2001 From: Andrei Tokar Date: Tue, 30 Jul 2024 19:50:13 -0400 Subject: [PATCH 3/3] do not free file space prematurely --- h2/src/main/org/h2/mvstore/RandomAccessStore.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/h2/src/main/org/h2/mvstore/RandomAccessStore.java b/h2/src/main/org/h2/mvstore/RandomAccessStore.java index 755e6d5e19..ca046d8cce 100644 --- a/h2/src/main/org/h2/mvstore/RandomAccessStore.java +++ b/h2/src/main/org/h2/mvstore/RandomAccessStore.java @@ -657,11 +657,11 @@ private boolean moveChunk(SFChunk chunk, long reservedAreaLow, long reservedArea assert reservedAreaHigh > 0 || block <= chunk.block : block + " " + chunk; ByteBuffer readBuff = readFully(chunk, start, length); writeFully(null, pos, readBuff); - free(start, length); - // can not set chunk's new block/len until it's fully written at new location, + // can not set chunk's new block until it's fully written at new location, // because concurrent reader can pick it up prematurely, chunk.block = block; chunk.next = 0; + free(start, length); saveChunkMetadataChanges(chunk); return true; }