You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2026/09/16 05:23:14 UTC
(jackrabbit-oak) branch OAK-12400 created (now c5ef6d497f)
This is an automated email from the ASF dual-hosted git repository.
rishabhdaim pushed a change to branch OAK-12400
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
at c5ef6d497f OAK-12400 : online revision cleanup can delete referenced segments on a store not written in compaction order
This branch includes the following new commits:
new c5ef6d497f OAK-12400 : online revision cleanup can delete referenced segments on a store not written in compaction order
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(jackrabbit-oak) 01/01: OAK-12400 : online revision cleanup can delete referenced segments on a store not written in compaction order
Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
rishabhdaim pushed a commit to branch OAK-12400
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit c5ef6d497fcd8fb4b692e7b9f8c2a7f18fcf1a1d
Author: rishabhdaim <ri...@gmail.com>
AuthorDate: Wed Sep 16 10:52:53 2026 +0530
OAK-12400 : online revision cleanup can delete referenced segments on a store not written in compaction order
Add opt-in, order-independent generation-based detection of dangling future
segments in DefaultCleanupContext, controlled by the system property
oak.segment.cleanup.danglingByGeneration (default false, no behaviour change).
Co-Authored-By: Claude Opus 4.8 <no...@anthropic.com>
---
.../file/CleanupFirstCompactionStrategy.java | 4 +-
.../oak/segment/file/DefaultCleanupContext.java | 34 +++-
.../oak/segment/file/DefaultCleanupStrategy.java | 2 +-
.../segment/file/DefaultCleanupContextTest.java | 214 +++++++++++++++++++++
4 files changed, 250 insertions(+), 4 deletions(-)
diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/CleanupFirstCompactionStrategy.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/CleanupFirstCompactionStrategy.java
index 11c63ff9d1..960c3c641c 100644
--- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/CleanupFirstCompactionStrategy.java
+++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/CleanupFirstCompactionStrategy.java
@@ -114,7 +114,7 @@ class CleanupFirstCompactionStrategy implements CompactionStrategy {
return true;
}
return generation.getGeneration() < currentGeneration.getGeneration() && !generation.isCompacted();
- }, compactedRoot);
+ }, compactedRoot, currentGeneration);
case TAIL:
return new DefaultCleanupContext(context.getSegmentTracker(), generation -> {
if (generation == null) {
@@ -127,7 +127,7 @@ class CleanupFirstCompactionStrategy implements CompactionStrategy {
return !generation.isCompacted();
}
return generation.getGeneration() < currentGeneration.getGeneration() && !generation.isCompacted();
- }, compactedRoot);
+ }, compactedRoot, currentGeneration);
default:
throw new IllegalArgumentException("invalid garbage collection type");
}
diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContext.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContext.java
index d67601aaf3..0b110bf9c9 100644
--- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContext.java
+++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContext.java
@@ -19,6 +19,7 @@
package org.apache.jackrabbit.oak.segment.file;
+import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
import org.apache.jackrabbit.oak.segment.RecordId;
import org.apache.jackrabbit.oak.segment.SegmentId;
import org.apache.jackrabbit.oak.segment.SegmentTracker;
@@ -35,14 +36,35 @@ import java.util.stream.Collectors;
import static org.apache.jackrabbit.oak.segment.SegmentId.isDataSegmentId;
class DefaultCleanupContext implements CleanupContext {
+
+ /**
+ * When {@code true}, "dangling future segment" detection is order-independent: a compacted segment
+ * is treated as an unused future segment iff its generation is strictly newer than the head
+ * generation, instead of relying on the physical scan position of the last compacted root. The
+ * positional heuristic (below) assumes segments are laid out in compaction write order (compacted
+ * root written last), which does not hold for a segment store assembled out of compaction order
+ * (e.g. a promoted cold standby, whose segments are written via {@code FileStore.writeSegment} in
+ * sync-arrival order), where it can reclaim live, head-referenced compacted segments (OAK-12400).
+ * <p>
+ * Default {@code false} preserves the existing behaviour; set to {@code true} to opt in.
+ */
+ private static final String DANGLING_BY_GENERATION = "oak.segment.cleanup.danglingByGeneration";
+
private final @NotNull SegmentTracker segmentTracker;
private final @NotNull Predicate<GCGeneration> old;
private final @Nullable UUID rootSegmentUUID;
+ private final @NotNull GCGeneration headGeneration;
+ private final boolean danglingByGeneration;
private boolean aheadOfRoot;
- DefaultCleanupContext(@NotNull SegmentTracker tracker, @NotNull Predicate<GCGeneration> old, @NotNull String compactedRoot) {
+ DefaultCleanupContext(@NotNull SegmentTracker tracker, @NotNull Predicate<GCGeneration> old, @NotNull String compactedRoot,
+ @NotNull GCGeneration headGeneration) {
this.segmentTracker = tracker;
this.old = old;
+ this.headGeneration = headGeneration;
+ this.danglingByGeneration = SystemPropertySupplier
+ .create(DANGLING_BY_GENERATION, Boolean.FALSE)
+ .get();
RecordId rootId = RecordId.fromString(tracker, compactedRoot);
if (rootId.equals(RecordId.NULL)) {
@@ -76,8 +98,18 @@ class DefaultCleanupContext implements CleanupContext {
* they are persisted after the last compacted root. This context relies on the cleanup algorithm to mark
* TAR entries in reverse order and will consider each compacted segment to be reclaimable until the root
* has been encountered, i.e. as long as {@code aheadOfRoot} is true.
+ * <p>
+ * The reverse-order/position assumption only holds for a store written by compaction (the compacted root
+ * is written last). It is violated by a store assembled out of compaction order (e.g. a promoted cold
+ * standby), where live head-referenced compacted segments can be encountered before the root and get
+ * wrongly reclaimed (OAK-12400). When {@link #DANGLING_BY_GENERATION} is enabled, detection is instead
+ * generation-based: a compacted segment is an unused future segment iff its generation is strictly newer
+ * than the head generation, which is independent of physical segment order.
*/
private boolean isDanglingFutureSegment(UUID id, GCGeneration generation) {
+ if (danglingByGeneration) {
+ return generation.isCompacted() && generation.compareWith(headGeneration) > 0;
+ }
return (aheadOfRoot &= !id.equals(rootSegmentUUID)) && generation.isCompacted();
}
diff --git a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupStrategy.java b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupStrategy.java
index e694c8f76e..ba20807d36 100644
--- a/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupStrategy.java
+++ b/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupStrategy.java
@@ -77,7 +77,7 @@ class DefaultCleanupStrategy implements CleanupStrategy {
private static CleanupContext newCleanupContext(Context context) {
return new DefaultCleanupContext(context.getSegmentTracker(), context.getReclaimer(),
- context.getCompactedRootId());
+ context.getCompactedRootId(), getGcGeneration(context));
}
private static String toFileNames(@NotNull List<String> files) {
diff --git a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContextTest.java b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContextTest.java
new file mode 100644
index 0000000000..8c19973b42
--- /dev/null
+++ b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/DefaultCleanupContextTest.java
@@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.segment.file;
+
+import java.io.IOException;
+import java.util.UUID;
+import java.util.function.Predicate;
+
+import org.apache.jackrabbit.oak.segment.RecordId;
+import org.apache.jackrabbit.oak.segment.SegmentId;
+import org.apache.jackrabbit.oak.segment.SegmentTracker;
+import org.apache.jackrabbit.oak.segment.memory.MemoryStore;
+import org.apache.jackrabbit.oak.segment.spi.persistence.GCGeneration;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests for {@link org.apache.jackrabbit.oak.segment.file.DefaultCleanupContext}, covering the
+ * OAK-12400 scenario: the positional "dangling future segment" heuristic can reclaim live,
+ * head-referenced compacted segments on a store assembled out of compaction order (e.g. a promoted
+ * cold standby). The order-independent, generation-based detection is opt-in via the system property
+ * {@code oak.segment.cleanup.danglingByGeneration} (default {@code false} - existing behaviour).
+ */
+public class DefaultCleanupContextTest {
+
+ private static final String DANGLING_BY_GENERATION = "oak.segment.cleanup.danglingByGeneration";
+
+ /** The current head generation (live commits on top of the last compaction). */
+ private static final GCGeneration HEAD_GENERATION = GCGeneration.newGCGeneration(26, 11, false);
+
+ /** The compacted base generation referenced by the head; must be retained. */
+ private static final GCGeneration COMPACTED_HEAD_GENERATION = GCGeneration.newGCGeneration(26, 11, true);
+
+ /** A genuinely unused future generation (aborted/uncommitted full compaction target). */
+ private static final GCGeneration FUTURE_COMPACTED_GENERATION = GCGeneration.newGCGeneration(27, 12, true);
+
+ /** A genuinely unused future generation from a TAIL compaction (generation+1, full unchanged). */
+ private static final GCGeneration FUTURE_TAIL_COMPACTED_GENERATION = GCGeneration.newGCGeneration(27, 11, true);
+
+ /** A transient live write newer than the head but not compacted. */
+ private static final GCGeneration FUTURE_NON_COMPACTED_GENERATION = GCGeneration.newGCGeneration(27, 11, false);
+
+ /** An older compacted generation (below the head). */
+ private static final GCGeneration OLD_COMPACTED_GENERATION = GCGeneration.newGCGeneration(25, 10, true);
+
+ private static final String NULL_ROOT = RecordId.NULL.toString10();
+
+ private SegmentTracker tracker;
+ private String compactedRoot;
+ private UUID rootSegment;
+ private UUID liveCompactedDataSegment;
+
+ @Before
+ public void setUp() throws IOException {
+ MemoryStore store = new MemoryStore();
+ tracker = new SegmentTracker((msb, lsb) -> new SegmentId(store, msb, lsb));
+
+ // A non-null gc.log root arms the positional heuristic (aheadOfRoot = true); its segment is
+ // the sentinel that flips aheadOfRoot to false once encountered in the reverse scan.
+ SegmentId root = tracker.newDataSegmentId();
+ rootSegment = root.asUUID();
+ compactedRoot = new RecordId(root, 0).toString10();
+
+ // A distinct compacted data segment. On a promoted standby this stands for a live,
+ // head-referenced compacted segment that the reverse scan reaches BEFORE the root sentinel.
+ liveCompactedDataSegment = tracker.newDataSegmentId().asUUID();
+ }
+
+ @After
+ public void tearDown() {
+ System.clearProperty(DANGLING_BY_GENERATION);
+ }
+
+ private DefaultCleanupContext newCleanupContext(Predicate<GCGeneration> old) {
+ return newCleanupContext(old, compactedRoot);
+ }
+
+ private DefaultCleanupContext newCleanupContext(Predicate<GCGeneration> old, String root) {
+ return new DefaultCleanupContext(tracker, old, root, HEAD_GENERATION);
+ }
+
+ // ---- Default (legacy positional heuristic): oak.segment.cleanup.danglingByGeneration=false ----
+
+ /**
+ * Reproduces OAK-12400: with the default positional heuristic, a live compacted segment
+ * encountered before the gc.log root sentinel is (wrongly) reclaimable - the mechanism that
+ * deletes referenced segments on a store not written in compaction order.
+ */
+ @Test
+ public void reclaimsLiveCompactedSegmentBeforeRootByDefault() {
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertTrue(context.shouldReclaim(liveCompactedDataSegment, COMPACTED_HEAD_GENERATION, false));
+ }
+
+ /**
+ * Positional heuristic is correct on a compaction-ordered store: once the gc.log root sentinel is
+ * encountered (reverse scan), subsequent compacted segments behind it are retained.
+ */
+ @Test
+ public void retainsSegmentAfterRootEncounteredByDefault() {
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertFalse(context.shouldReclaim(rootSegment, COMPACTED_HEAD_GENERATION, false));
+ Assert.assertFalse(context.shouldReclaim(liveCompactedDataSegment, COMPACTED_HEAD_GENERATION, false));
+ }
+
+ /** With no compacted root in gc.log the positional heuristic is disarmed. */
+ @Test
+ public void disarmedWhenNoCompactedRootByDefault() {
+ DefaultCleanupContext context = newCleanupContext(generation -> false, NULL_ROOT);
+ Assert.assertFalse(context.shouldReclaim(liveCompactedDataSegment, COMPACTED_HEAD_GENERATION, false));
+ }
+
+ /** The positional heuristic never reclaims a non-compacted segment. */
+ @Test
+ public void ignoresNonCompactedSegmentByDefault() {
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertFalse(context.shouldReclaim(liveCompactedDataSegment, FUTURE_NON_COMPACTED_GENERATION, false));
+ }
+
+ /** Ordinary generational reclamation of old data segments is unaffected. */
+ @Test
+ public void stillReclaimsOldDataSegmentsByDefault() {
+ DefaultCleanupContext context = newCleanupContext(generation -> true);
+ Assert.assertTrue(context.shouldReclaim(liveCompactedDataSegment, OLD_COMPACTED_GENERATION, false));
+ }
+
+ /** Reference-based reclamation of unreferenced bulk segments is unaffected. */
+ @Test
+ public void stillReclaimsUnreferencedBulkSegmentsByDefault() {
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ UUID unreferencedBulk = tracker.newBulkSegmentId().asUUID();
+ Assert.assertTrue(context.shouldReclaim(unreferencedBulk, OLD_COMPACTED_GENERATION, false));
+ }
+
+ // ---- Opt-in generational detection: oak.segment.cleanup.danglingByGeneration=true ----
+
+ /** The fix: a live compacted segment at the head generation is retained regardless of scan position. */
+ @Test
+ public void retainsLiveCompactedHeadSegmentWhenEnabled() {
+ System.setProperty(DANGLING_BY_GENERATION, "true");
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertFalse(context.shouldReclaim(liveCompactedDataSegment, COMPACTED_HEAD_GENERATION, false));
+ }
+
+ /** A genuine future compacted leftover (full compaction target, newer generation) is reclaimed. */
+ @Test
+ public void reclaimsGenuineFutureCompactedSegmentWhenEnabled() {
+ System.setProperty(DANGLING_BY_GENERATION, "true");
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertTrue(context.shouldReclaim(liveCompactedDataSegment, FUTURE_COMPACTED_GENERATION, false));
+ }
+
+ /** A genuine future compacted leftover from a TAIL compaction is reclaimed. */
+ @Test
+ public void reclaimsFutureTailCompactedSegmentWhenEnabled() {
+ System.setProperty(DANGLING_BY_GENERATION, "true");
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertTrue(context.shouldReclaim(liveCompactedDataSegment, FUTURE_TAIL_COMPACTED_GENERATION, false));
+ }
+
+ /** A non-compacted segment newer than the head (transient write) is not a dangling future segment. */
+ @Test
+ public void retainsNonCompactedFutureSegmentWhenEnabled() {
+ System.setProperty(DANGLING_BY_GENERATION, "true");
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertFalse(context.shouldReclaim(liveCompactedDataSegment, FUTURE_NON_COMPACTED_GENERATION, false));
+ }
+
+ /** An older compacted segment is not a dangling future segment; ordinary reclamation handles it. */
+ @Test
+ public void retainsOlderCompactedSegmentWhenEnabledAndNotOld() {
+ System.setProperty(DANGLING_BY_GENERATION, "true");
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ Assert.assertFalse(context.shouldReclaim(liveCompactedDataSegment, OLD_COMPACTED_GENERATION, false));
+ }
+
+ /** Generational detection does not disable ordinary generational reclamation of old data segments. */
+ @Test
+ public void stillReclaimsOldDataSegmentsWhenEnabled() {
+ System.setProperty(DANGLING_BY_GENERATION, "true");
+ DefaultCleanupContext context = newCleanupContext(generation -> true);
+ Assert.assertTrue(context.shouldReclaim(liveCompactedDataSegment, OLD_COMPACTED_GENERATION, false));
+ }
+
+ // ---- shouldFollow: reference propagation is only for bulk segments ----
+
+ @Test
+ public void followsOnlyBulkSegmentReferences() {
+ DefaultCleanupContext context = newCleanupContext(generation -> false);
+ UUID from = tracker.newDataSegmentId().asUUID();
+ UUID dataTarget = tracker.newDataSegmentId().asUUID();
+ UUID bulkTarget = tracker.newBulkSegmentId().asUUID();
+ Assert.assertFalse(context.shouldFollow(from, dataTarget));
+ Assert.assertTrue(context.shouldFollow(from, bulkTarget));
+ }
+}