You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2026/10/10 22:34:03 UTC

(pulsar) branch master updated: [fix][broker] Prune sealed scalable-topic segments ancestors first (#26842)

This is an automated email from the ASF dual-hosted git repository.

codelipenghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new e5b2ce87c629 [fix][broker] Prune sealed scalable-topic segments ancestors first (#26842)
e5b2ce87c629 is described below

commit e5b2ce87c629b51ca23b19357d20d1407d18bc18
Author: Matteo Merli <mm...@apache.org>
AuthorDate: Sat Oct 10 15:33:55 2026 -0700

    [fix][broker] Prune sealed scalable-topic segments ancestors first (#26842)
---
 .../service/scalable/ScalableTopicController.java  | 18 ++++----
 .../broker/service/scalable/SegmentLayout.java     |  6 +--
 .../scalable/ScalableTopicControllerTest.java      | 49 ++++++++++++++++++++++
 3 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/ScalableTopicController.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/ScalableTopicController.java
index 04bbd14be519..f8f3042f1286 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/ScalableTopicController.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/ScalableTopicController.java
@@ -1330,8 +1330,11 @@ public class ScalableTopicController {
                     long now = clock.millis();
                     List<SegmentInfo> candidates = new ArrayList<>();
                     for (SegmentInfo seg : layout.getAllSegments().values()) {
+                        // Ancestors first: a segment whose parent is still in the DAG stays until
+                        // the parent is pruned (see pruneEligibleAsync).
                         if (seg.isSealed() && seg.sealedAtMs() > 0
-                                && (now - seg.sealedAtMs()) >= retentionMs) {
+                                && (now - seg.sealedAtMs()) >= retentionMs
+                                && layout.getParents(seg.segmentId()).isEmpty()) {
                             candidates.add(seg);
                         }
                     }
@@ -1356,12 +1359,13 @@ public class ScalableTopicController {
      * {@code isSegmentDrained} reports {@code false} — the segment is treated as
      * "still in use" and never pruned while a CHECKPOINT subscription is registered.
      *
-     * <p><b>Parent-vs-child ordering.</b> Sealed segments form a DAG; pruning is allowed
-     * in any order because the active leaves always cover the full hash range, and the
-     * managed-ledger storage of each segment is independent. {@link SegmentLayout#pruneSegment}
-     * rewrites the parent/child edges, so consumers using the post-prune layout see the
-     * pruned segment as "no longer present" — equivalent to "drained" for parent-drain
-     * ordering.
+     * <p><b>Parent-vs-child ordering.</b> {@link SegmentLayout#pruneSegment} drops the
+     * pruned segment from its children's parent lists, so consumers using the post-prune
+     * layout see it as "no longer present" — equivalent to "drained" for parent-drain
+     * ordering. That only holds if none of its own ancestors are still present: otherwise
+     * its children would lose their link to an ancestor that may still hold older messages
+     * of their keys, and be read before it. So only segments with no parent left in the
+     * DAG are candidates: ancestors are pruned first, one generation per tick.
      */
     private CompletableFuture<Void> pruneEligibleAsync(List<SegmentInfo> candidates) {
         return resources.listSubscriptionsAsync(topicName)
diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/SegmentLayout.java b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/SegmentLayout.java
index 6f62047314e9..fc3a9ce1a462 100644
--- a/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/SegmentLayout.java
+++ b/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/scalable/SegmentLayout.java
@@ -291,9 +291,9 @@ public class SegmentLayout {
     }
 
     /**
-     * Prune an expired segment from the DAG. The segment must be sealed and have no
-     * children that are still in the DAG (i.e., children have already been pruned or
-     * the segment is a leaf that was sealed).
+     * Prune an expired segment from the DAG. The segment must be sealed. It is dropped from
+     * its children's parent lists without linking them to its own parents, so callers prune
+     * a segment only once none of its parents are left in the DAG (ancestors first).
      *
      * @param segmentId the segment to prune
      * @return a new SegmentLayout with the segment removed
diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/scalable/ScalableTopicControllerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/scalable/ScalableTopicControllerTest.java
index 028894e03d27..261d58cc37cd 100644
--- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/scalable/ScalableTopicControllerTest.java
+++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/scalable/ScalableTopicControllerTest.java
@@ -36,6 +36,7 @@ import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertThrows;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
+import java.time.Duration;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -46,6 +47,7 @@ import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 import org.apache.pulsar.broker.PulsarService;
 import org.apache.pulsar.broker.namespace.NamespaceService;
 import org.apache.pulsar.broker.resources.ConsumerRegistration;
@@ -67,6 +69,7 @@ import org.apache.pulsar.common.policies.data.ScalableSubscriptionType;
 import org.apache.pulsar.common.policies.data.ScalableTopicStats;
 import org.apache.pulsar.common.policies.data.SegmentTopicStats;
 import org.apache.pulsar.common.policies.data.stats.TopicStatsImpl;
+import org.apache.pulsar.common.scalable.SegmentInfo;
 import org.apache.pulsar.common.scalable.SegmentTopicName;
 import org.apache.pulsar.common.util.FutureUtil;
 import org.apache.pulsar.metadata.api.MetadataStoreConfig;
@@ -890,6 +893,52 @@ public class ScalableTopicControllerTest {
                 "negative retention must keep sealed segments forever");
     }
 
+    /**
+     * Ancestors first: a sealed segment that every subscription has drained stays in the DAG while
+     * its parent is still there. Pruning it would drop it from its children's parent lists, and a
+     * consumer could then read its children before the parent's remaining messages.
+     */
+    @Test
+    public void testGcTickPrunesAncestorsFirst() throws Exception {
+        installGcMocks(/* nsRetentionMinutes */ 1);
+        long startMs = 1_700_000_000_000L;
+        AdjustableClock clock = new AdjustableClock(startMs);
+        if (controller != null) {
+            controller.close().join();
+        }
+        controller = newControllerWithClock(topicName, clock, Duration.ofHours(1));
+        controller.initialize().get();
+        resources.createSubscriptionAsync(topicName, "sub-a", SubscriptionType.STREAM).get();
+
+        // Split 0, then split one of its children: that child is now a sealed intermediate segment.
+        controller.splitSegment(0).get();
+        long intermediate = controller.getLayout().get().getAllSegments().get(0L).childIds().get(1);
+        controller.splitSegment(intermediate).get();
+        SegmentInfo root = controller.getLayout().get().getAllSegments().get(0L);
+        String rootTopic = SegmentTopicName.fromParent(topicName, root.hashRange(), 0L).toString();
+        // sub-a still has backlog on segment 0 and has drained everything else.
+        long[] rootBacklog = {5};
+        when(scalableTopics.getSegmentSubscriptionBacklogAsync(anyString(), eq("sub-a")))
+                .thenAnswer(invocation -> CompletableFuture.completedFuture(
+                        rootTopic.equals(invocation.getArgument(0)) ? rootBacklog[0] : 0L));
+
+        clock.set(startMs + TimeUnit.MINUTES.toMillis(1) + 1_000L);
+        controller.runGcTickAsync().get();
+        Map<Long, SegmentInfo> segments = controller.getLayout().get().getAllSegments();
+        assertTrue(segments.containsKey(0L), "segment 0 still has backlog");
+        assertTrue(segments.containsKey(intermediate),
+                "a drained segment must not be pruned while its parent is still in the DAG");
+
+        // Once segment 0 drains it is pruned, and its child goes on the next tick.
+        rootBacklog[0] = 0;
+        controller.runGcTickAsync().get();
+        segments = controller.getLayout().get().getAllSegments();
+        assertFalse(segments.containsKey(0L));
+        assertTrue(segments.containsKey(intermediate), "pruned one generation per tick");
+        controller.runGcTickAsync().get();
+        assertFalse(controller.getLayout().get().getAllSegments().containsKey(intermediate));
+    }
+
     /** Settable {@link java.time.Clock} for the GC tick tests. */
     private static final class AdjustableClock extends java.time.Clock {
         private volatile long nowMs;