gaoyunhaii commented on a change in pull request #16655:
URL: https://github.com/apache/flink/pull/16655#discussion_r683650309



##########
File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/VertexFinishedStateChecker.java
##########
@@ -0,0 +1,216 @@
+/*
+ * 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.flink.runtime.checkpoint;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.runtime.OperatorIDPair;
+import org.apache.flink.runtime.executiongraph.ExecutionJobVertex;
+import org.apache.flink.runtime.executiongraph.IntermediateResult;
+import org.apache.flink.runtime.jobgraph.DistributionPattern;
+import org.apache.flink.runtime.jobgraph.JobEdge;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * This class encapsulates the operation that checks if there are illegal 
modification to the
+ * JobGraph when restoring from a checkpoint with partly or fully finished 
operator states.
+ *
+ * <p>As a whole, it ensures
+ *
+ * <ol>
+ *   <li>All the operators inside a JobVertex have the same finished state.
+ *   <li>The predecessors of a fully finished vertex must also be fully 
finished.
+ *   <li>The processors of a partly finished vertex
+ *       <ul>
+ *         <li>If connected via ALL_TO_ALL edge, the predecessor must be fully 
finished.
+ *         <li>If connected via POINTWISE edge, the predecessor must be partly 
finished or fully
+ *             finished.
+ *       </ul>
+ * </ol>
+ */
+public class VertexFinishedStateChecker {
+
+    private final Set<ExecutionJobVertex> vertices;
+
+    private final Map<OperatorID, OperatorState> operatorStates;
+
+    public VertexFinishedStateChecker(
+            Set<ExecutionJobVertex> vertices, Map<OperatorID, OperatorState> 
operatorStates) {
+        this.vertices = vertices;
+        this.operatorStates = operatorStates;
+    }
+
+    public void validateOperatorsFinishedState() {
+        VerticesFinishedStatusCache verticesFinishedCache =
+                new VerticesFinishedStatusCache(operatorStates);
+        for (ExecutionJobVertex vertex : vertices) {
+            VertexFinishedState vertexFinishedState = 
verticesFinishedCache.getOrUpdate(vertex);
+
+            if (vertexFinishedState == VertexFinishedState.FULLY_FINISHED) {
+                checkProcessorsOfFullyFinishedVertex(vertex, 
verticesFinishedCache);
+            } else if (vertexFinishedState == 
VertexFinishedState.PARTLY_FINISHED) {
+                checkProcessorsOfPartlyFinishedVertex(vertex, 
verticesFinishedCache);
+            }
+        }
+    }
+
+    private void checkProcessorsOfFullyFinishedVertex(
+            ExecutionJobVertex vertex, VerticesFinishedStatusCache 
verticesFinishedStatusCache) {
+        boolean allPredecessorsFinished =
+                vertex.getInputs().stream()
+                        .map(IntermediateResult::getProducer)
+                        .allMatch(
+                                jobVertex ->
+                                        
verticesFinishedStatusCache.getOrUpdate(jobVertex)
+                                                == 
VertexFinishedState.FULLY_FINISHED);
+
+        if (!allPredecessorsFinished) {
+            throw new FlinkRuntimeException(
+                    "Illegal JobGraph modification. Cannot run a program with 
fully finished"
+                            + " vertices predeceased with the ones not fully 
finished. Task vertex "
+                            + vertex.getName()
+                            + "("
+                            + vertex.getJobVertexId()
+                            + ")"
+                            + " has a predecessor not fully finished");
+        }
+    }
+
+    private void checkProcessorsOfPartlyFinishedVertex(
+            ExecutionJobVertex vertex, VerticesFinishedStatusCache 
verticesFinishedStatusCache) {
+        // Computes the distribution pattern from the predecessors. If there 
are multiple edges,
+        // ALL_TO_ALL edges would have a higher priority.
+        Map<JobVertexID, DistributionPattern> predecessorDistribution = new 
HashMap<>();
+        for (JobEdge jobEdge : vertex.getJobVertex().getInputs()) {
+            predecessorDistribution.compute(
+                    jobEdge.getSource().getProducer().getID(),
+                    (k, v) ->
+                            v == DistributionPattern.ALL_TO_ALL

Review comment:
       I updated the comments above these lines to be more specific~




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to