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

zhangshenghang pushed a commit to branch fix/zeta-persist-restore-budget
in repository https://gitbox.apache.org/repos/asf/seatunnel.git

commit f689f46dd66b338a26a011430cba02b452faa570
Author: jast <[email protected]>
AuthorDate: Thu Nov 19 08:34:35 2026 +0800

    [Fix][Zeta] Persist the pipeline restore budget so a master failover cannot 
reset it
    
    pipelineRestoreNum and the restore causes were memory-only, so a master
    failover rebuilt every SubPlan with a fresh job.retry.times budget and no
    restore history: a crash-looping pipeline could restart indefinitely across
    failovers. Persist a small PipelineRestoreInfo record in the 
running-job-state
    IMap on every restore, read it back when SubPlan is reconstructed, and 
clean it
    up with the other job state keys.
---
 .../server/dag/physical/PipelineRestoreInfo.java   | 56 +++++++++++++
 .../engine/server/dag/physical/SubPlan.java        | 48 ++++++++++-
 .../seatunnel/engine/server/master/JobMaster.java  |  1 +
 .../engine/server/dag/physical/SubPlanTest.java    | 97 ++++++++++++++++++++++
 4 files changed, 201 insertions(+), 1 deletion(-)

diff --git 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/PipelineRestoreInfo.java
 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/PipelineRestoreInfo.java
new file mode 100644
index 0000000000..3d79329546
--- /dev/null
+++ 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/PipelineRestoreInfo.java
@@ -0,0 +1,56 @@
+/*
+ * 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.seatunnel.engine.server.dag.physical;
+
+import java.io.Serializable;
+import java.util.List;
+
+/**
+ * The persisted restore bookkeeping of one pipeline: how many restores it 
already consumed and why
+ * each of them happened.
+ *
+ * <p>SubPlan keeps this in memory only, but a master failover rebuilds every 
SubPlan from scratch.
+ * Without persisting it, the {@code job.retry.times} budget restarts at zero 
on every master
+ * switchover, so a crash-looping pipeline is restarted indefinitely, and the 
recorded restore
+ * causes are lost exactly when they are most needed. SubPlan therefore writes 
this record to the
+ * running-job-state IMap on every restore and reads it back when it is 
reconstructed.
+ *
+ * <p>The value is stored in an IMap of a Hazelcast cluster whose members run 
the same engine
+ * version, so plain Java serialization is sufficient here.
+ */
+public class PipelineRestoreInfo implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    private final int restoreCount;
+
+    private final List<String> restoreCauses;
+
+    public PipelineRestoreInfo(int restoreCount, List<String> restoreCauses) {
+        this.restoreCount = restoreCount;
+        this.restoreCauses = restoreCauses;
+    }
+
+    public int getRestoreCount() {
+        return restoreCount;
+    }
+
+    public List<String> getRestoreCauses() {
+        return restoreCauses;
+    }
+}
diff --git 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java
 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java
index 008d2314da..960d46fec0 100644
--- 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java
+++ 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/dag/physical/SubPlan.java
@@ -126,6 +126,13 @@ public class SubPlan {
 
     private AtomicInteger pipelineRestoreNum;
 
+    /**
+     * IMap key under which the {@link PipelineRestoreInfo} of this pipeline 
is persisted on every
+     * restore, so a master failover does not reset the {@code 
job.retry.times} budget nor lose the
+     * recorded restore causes.
+     */
+    private final String restoreInfoImapKey;
+
     private final Object restoreLock = new Object();
 
     private volatile PipelineStatus currPipelineStatus;
@@ -161,7 +168,8 @@ public class SubPlan {
         this.pipelineFuture = new CompletableFuture<>();
         this.physicalVertexList = physicalVertexList;
         this.coordinatorVertexList = coordinatorVertexList;
-        pipelineRestoreNum = new AtomicInteger();
+        this.restoreInfoImapKey =
+                "pipeline_restore_" + jobImmutableInformation.getJobId() + "_" 
+ pipelineId;
         pipelineMaxRestoreNum =
                 Integer.parseInt(
                         jobImmutableInformation
@@ -199,6 +207,18 @@ public class SubPlan {
 
         this.currPipelineStatus = (PipelineStatus) 
runningJobStateIMap.get(pipelineLocation);
 
+        // A master failover rebuilds every SubPlan from scratch with no 
process-local state.
+        // Restore the retry budget and the recorded causes of this pipeline, 
so job.retry.times
+        // keeps bounding the total number of restarts across failovers 
(#13323).
+        Object persistedRestoreInfo = 
runningJobStateIMap.get(restoreInfoImapKey);
+        if (persistedRestoreInfo instanceof PipelineRestoreInfo) {
+            PipelineRestoreInfo restoreInfo = (PipelineRestoreInfo) 
persistedRestoreInfo;
+            this.pipelineRestoreNum = new 
AtomicInteger(restoreInfo.getRestoreCount());
+            this.restoreCauses.addAll(restoreInfo.getRestoreCauses());
+        } else {
+            this.pipelineRestoreNum = new AtomicInteger();
+        }
+
         this.pipelineFullName =
                 String.format(
                         "Job %s (%s), Pipeline: [(%d/%d)]",
@@ -601,6 +621,26 @@ public class SubPlan {
         return new ArrayList<>(restoreCauses);
     }
 
+    /**
+     * Best-effort persistence of the restore bookkeeping, so a master 
failover does not reset the
+     * {@code job.retry.times} budget nor lose the restore causes. Failing to 
persist must not break
+     * the restore itself: the worst case is the previous behavior (#13323).
+     */
+    private void persistRestoreInfo() {
+        try {
+            runningJobStateIMap.put(
+                    restoreInfoImapKey,
+                    new PipelineRestoreInfo(pipelineRestoreNum.get(), 
getRestoreCauses()));
+        } catch (Exception e) {
+            log.warn(
+                    String.format(
+                            "Persist the restore info of %s failed, the 
restore budget may be "
+                                    + "reset by a master failover",
+                            pipelineFullName),
+                    e);
+        }
+    }
+
     /**
      * reset the pipeline and task state and init state future again
      *
@@ -613,6 +653,7 @@ public class SubPlan {
                 // Record why this run ended before anything clears it: 
reset() runs just below
                 // and initStateFuture() replaces errorByPhysicalVertex 
outright.
                 retainRestoreCause(pipelineRestoreNum.get(), 
errorByPhysicalVertex.get());
+                persistRestoreInfo();
                 log.info(
                         String.format(
                                 "Restore time %s, pipeline %s",
@@ -775,6 +816,11 @@ public class SubPlan {
         return pipelineRestoreNum.get();
     }
 
+    /** IMap key of the persisted {@link PipelineRestoreInfo} of this 
pipeline. */
+    public String getRestoreInfoImapKey() {
+        return restoreInfoImapKey;
+    }
+
     public void handleCheckpointError() {
         log.warn(
                 String.format(
diff --git 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/master/JobMaster.java
 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/master/JobMaster.java
index 836e017fc0..91112064df 100644
--- 
a/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/master/JobMaster.java
+++ 
b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/master/JobMaster.java
@@ -888,6 +888,7 @@ public class JobMaster {
                                 
stateKeys.add(checkpointCoordinator.getCheckpointStateImapKey());
                                 
stateKeys.add(checkpointCoordinator.getReadyToCloseImapKey());
                             }
+                            stateKeys.add(pipeline.getRestoreInfoImapKey());
                         });
         return new JobCleanupRecord(
                 initializationTimestamp,
diff --git 
a/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/dag/physical/SubPlanTest.java
 
b/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/dag/physical/SubPlanTest.java
index 41b72a0065..98a9b4eca5 100644
--- 
a/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/dag/physical/SubPlanTest.java
+++ 
b/seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/dag/physical/SubPlanTest.java
@@ -319,6 +319,103 @@ class SubPlanTest extends 
AbstractSeaTunnelServerTest<SubPlanTest> {
         }
     }
 
+    @Test
+    void shouldCarryTheRestoreBudgetAcrossReconstruction() throws Exception {
+        // A master failover rebuilds every SubPlan from the same IMaps with 
no process-local
+        // state: the retry budget and the restore causes must survive that 
reconstruction,
+        // otherwise job.retry.times restarts at zero on every failover 
(#13323).
+        CheckpointManager checkpointManager = mock(CheckpointManager.class);
+        PhysicalPlan physicalPlan = mock(PhysicalPlan.class);
+        JobMaster jobMaster = mock(JobMaster.class);
+        when(jobMaster.getCheckpointManager()).thenReturn(checkpointManager);
+        when(jobMaster.getPhysicalPlan()).thenReturn(physicalPlan);
+        doAnswer(
+                        invocation -> {
+                            ((SubPlan) 
invocation.getArgument(0)).initStateFuture();
+                            return null;
+                        })
+                .when(physicalPlan)
+                .addPipelineEndCallback(any(SubPlan.class));
+
+        PhysicalVertex vertex = mockPhysicalVertex(ExecutionState.FAILED);
+        when(vertex.initStateFuture())
+                .thenReturn(new PassiveCompletableFuture<>(new 
CompletableFuture<>()));
+
+        JobConfig jobConfig = new JobConfig();
+        jobConfig.setName("test");
+        
jobConfig.getEnvOptions().put(EnvCommonOptions.JOB_RETRY_INTERVAL_SECONDS.key(),
 0);
+        JobImmutableInformation jobImmutableInformation = 
mock(JobImmutableInformation.class);
+        when(jobImmutableInformation.getJobId()).thenReturn(1L);
+        when(jobImmutableInformation.getJobConfig()).thenReturn(jobConfig);
+        IMap<Object, Object> runningJobStateIMap =
+                nodeEngine
+                        .getHazelcastInstance()
+                        .getMap("sub-plan-restore-info-state-" + 
UUID.randomUUID());
+        IMap<Object, Long[]> runningJobStateTimestampsIMap =
+                nodeEngine
+                        .getHazelcastInstance()
+                        .getMap("sub-plan-restore-info-timestamps-" + 
UUID.randomUUID());
+
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+        try {
+            SubPlan first =
+                    new SubPlan(
+                            1,
+                            1,
+                            System.currentTimeMillis(),
+                            Collections.singletonList(vertex),
+                            Collections.emptyList(),
+                            jobImmutableInformation,
+                            executorService,
+                            runningJobStateIMap,
+                            runningJobStateTimestampsIMap,
+                            Collections.emptyMap());
+            first.setJobMaster(jobMaster);
+            first.setCurrPipelineStatus(PipelineStatus.FAILED);
+            setErrorByPhysicalVertex(first, "java.sql.SQLException: connection 
refused");
+            Assertions.assertTrue(
+                    invokePrepareRestorePipeline(first), 
"prepareRestorePipeline failed");
+
+            SubPlan rebuilt =
+                    new SubPlan(
+                            1,
+                            1,
+                            System.currentTimeMillis(),
+                            
Collections.singletonList(mockPhysicalVertex(ExecutionState.FAILED)),
+                            Collections.emptyList(),
+                            jobImmutableInformation,
+                            executorService,
+                            runningJobStateIMap,
+                            runningJobStateTimestampsIMap,
+                            Collections.emptyMap());
+
+            Assertions.assertEquals(1, rebuilt.getPipelineRestoreNum());
+            Assertions.assertEquals(1, rebuilt.getRestoreCauses().size());
+            Assertions.assertTrue(
+                    rebuilt.getRestoreCauses().get(0).contains("connection 
refused"),
+                    "unexpected restored cause: " + 
rebuilt.getRestoreCauses().get(0));
+        } finally {
+            executorService.shutdownNow();
+        }
+    }
+
+    @Test
+    void shouldStartANewPipelineWithoutAnyRestore() {
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+        try {
+            SubPlan subPlan =
+                    newSubPlan(
+                            executorService,
+                            
Collections.singletonList(mockPhysicalVertex(ExecutionState.FAILED)),
+                            Collections.emptyList());
+
+            Assertions.assertEquals(0, subPlan.getPipelineRestoreNum());
+            Assertions.assertTrue(subPlan.getRestoreCauses().isEmpty());
+        } finally {
+            executorService.shutdownNow();
+        }
+    }
+
     @Test
     void shouldCaptureTheRealErrorWhenARestoreIsPrepared() throws Exception {
         // The tests above call retainRestoreCause directly, which proves the 
retention works but

Reply via email to