GJL commented on a change in pull request #8804: [FLINK-12883][WIP][runtime] 
Add elaborated partition release logic
URL: https://github.com/apache/flink/pull/8804#discussion_r295821637
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/executiongraph/failover/flip1/PipelinedRegionComputeUtil.java
 ##########
 @@ -0,0 +1,117 @@
+/*
+ * 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Utility for computing pipelined regions.
+ */
+public final class PipelinedRegionComputeUtil {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(PipelinedRegionComputeUtil.class);
+
+       public static Set<Set<FailoverVertex>> computePipelinedRegions(final 
FailoverTopology topology) {
+               // 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()) {
+                       return 
uniqueRegions(buildOneRegionForAllVertices(topology));
+               }
+
+               // 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 Map<FailoverVertex, Set<FailoverVertex>> vertexToRegion = 
new IdentityHashMap<>();
+
+               // iterate all the vertices which are topologically sorted
+               for (FailoverVertex vertex : topology.getFailoverVertices()) {
+                       Set<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 Set<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.");
+                                       }
+
+                                       // check if it is the same as the 
producer region, if so skip the merge
+                                       // this check can significantly reduce 
compute complexity in All-to-All PIPELINED edge case
+                                       if (currentRegion != producerRegion) {
+                                               // merge current region and 
producer region
+                                               // merge the smaller region 
into the larger one to reduce the cost
+                                               final Set<FailoverVertex> 
smallerSet;
+                                               final Set<FailoverVertex> 
largerSet;
+                                               if (currentRegion.size() < 
producerRegion.size()) {
+                                                       smallerSet = 
currentRegion;
+                                                       largerSet = 
producerRegion;
+                                               } else {
+                                                       smallerSet = 
producerRegion;
+                                                       largerSet = 
currentRegion;
+                                               }
+                                               for (FailoverVertex v : 
smallerSet) {
+                                                       vertexToRegion.put(v, 
largerSet);
+                                               }
+                                               largerSet.addAll(smallerSet);
+                                               currentRegion = largerSet;
+                                       }
+                               }
+                       }
+               }
+
+               return uniqueRegions(vertexToRegion);
+       }
+
+       private static Map<FailoverVertex, Set<FailoverVertex>> 
buildOneRegionForAllVertices(final FailoverTopology topology) {
+               LOG.warn("Cannot decompose the topology into individual 
failover regions due to use of " +
+                       "Co-Location constraints (iterations). Job will fail 
over as one holistic unit.");
+
+               final Map<FailoverVertex, Set<FailoverVertex>> vertexToRegion = 
new IdentityHashMap<>();
+
+               final Set<FailoverVertex> allVertices = new HashSet<>();
+               for (FailoverVertex vertex : topology.getFailoverVertices()) {
+                       allVertices.add(vertex);
+                       vertexToRegion.put(vertex, allVertices);
+               }
+               return vertexToRegion;
+       }
+
+       private static Set<Set<FailoverVertex>> uniqueRegions(final 
Map<FailoverVertex, Set<FailoverVertex>> vertexToRegion) {
+               // find out all the distinct regions
+               final IdentityHashMap<Set<FailoverVertex>, Object> 
distinctRegions = new IdentityHashMap<>();
 
 Review comment:
   Consider `Collections.newSetFromMap(new IdentityHashMap<>())`

----------------------------------------------------------------
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