zentol commented on a change in pull request #8316: [FLINK-12369] 
[coordination] Implement a region failover strategy regarding the next version 
FailoverStrategy interfaces
URL: https://github.com/apache/flink/pull/8316#discussion_r280752971
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/executiongraph/failover/flip1/RestartPipelinedRegionStrategy.java
 ##########
 @@ -0,0 +1,210 @@
+/*
+ * 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.executiongraph.failover.flip1;
+
+import org.apache.flink.annotation.VisibleForTesting;
+
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A failover strategy that proposes to restart involved regions when a vertex 
fails.
+ * A region is defined by this strategy as tasks that communicate via 
pipelined data exchange.
+ */
+public class RestartPipelinedRegionStrategy implements FailoverStrategy {
+
+       /** The log object used for debugging. */
+       private static final Logger LOG = 
LoggerFactory.getLogger(RestartPipelinedRegionStrategy.class);
+
+       /** The topology containing info about all the vertices and edges. */
+       private final FailoverTopology topology;
+
+       /** Maps execution vertex id to failover region. */
+       private final Map<ExecutionVertexID, FailoverRegion> regions;
+
+       /**
+        * Creates a new failover strategy to restart pipelined regions that 
works on the given topology.
+        *
+        * @param topology containing info about all the vertices and edges
+        */
+       public RestartPipelinedRegionStrategy(FailoverTopology topology) {
+               this.topology = checkNotNull(topology);
+               this.regions = new HashMap<>();
+
+               // build regions based on the given topology
+               LOG.info("Start building failover regions.");
+               buildFailoverRegions();
+       }
+
+       // 
------------------------------------------------------------------------
+       //  region building
+       // 
------------------------------------------------------------------------
+
+       private void buildFailoverRegions() {
+               // currently we let a job with co-location constraints fail as 
one region
+               // putting co-located vertices in the same region with each 
other can be a future improvement
+               if (topology.containsCoLocationConstraints()) {
+                       buildOneRegionForAllVertices();
+                       return;
+               }
+
+               // we use the map (list -> null) to imitate an IdentityHashSet 
(which does not exist)
+               // this helps to optimize the building performance as it uses 
reference equality
+               final IdentityHashMap<FailoverVertex, HashSet<FailoverVertex>> 
vertexToRegion = new IdentityHashMap<>();
+
+               // iterate all the vertices which are topologically sorted
+               for (FailoverVertex vertex : topology.getFailoverVertices()) {
+                       HashSet<FailoverVertex> currentRegion = null;
 
 Review comment:
   you could initialize this to `new HashSet<>(1)`, which would simplify the 
loop since `currentRegion`would never be null. We would in all cases go through 
the merge process.
   
   You could then move `vertexToRegion.put(vertex, currentRegion);` also to the 
top of the loop.
   
   The end result would be:
   ```
   // iterate all the vertices which are topologically sorted
   for (FailoverVertex vertex : topology.getFailoverVertices()) {
        HashSet<FailoverVertex> currentRegion = new HashSet<>(1);
        currentRegion.add(vertex);
        vertexToRegion.put(vertex, currentRegion);
   
        for (FailoverEdge inputEdge : vertex.getInputEdges()) {
                if (inputEdge.getResultPartitionType().isPipelined()) {
                        final FailoverVertex producerVertex = 
inputEdge.getSourceVertex();
                        final HashSet<FailoverVertex> producerRegion = 
vertexToRegion.get(producerVertex);
                        if (producerRegion == null) {
                                throw new IllegalStateException("Producer task 
" + producerVertex.getExecutionVertexName()
                                        + " failover region is null while 
calculating failover region for the consumer task "
                                        + vertex.getExecutionVertexName() + ". 
This should be a failover region building bug.");
                        }
   
                        if (currentRegion != producerRegion) {
                                // merge current region and producer region
                                for (FailoverVertex v : producerRegion) {
                                        vertexToRegion.put(v, currentRegion);
                                }
                                currentRegion.addAll(producerRegion);
                        }
                }
        }
   }
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to