6591812 commented on code in PR #20072:
URL: https://github.com/apache/flink/pull/20072#discussion_r907396767


##########
flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/slowtaskdetector/ExecutionTimeBasedSlowTaskDetector.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * 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.scheduler.slowtaskdetector;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.SlowTaskDetectorOptions;
+import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor;
+import org.apache.flink.runtime.execution.ExecutionState;
+import org.apache.flink.runtime.executiongraph.Execution;
+import org.apache.flink.runtime.executiongraph.ExecutionAttemptID;
+import org.apache.flink.runtime.executiongraph.ExecutionGraph;
+import org.apache.flink.runtime.executiongraph.ExecutionJobVertex;
+import org.apache.flink.runtime.executiongraph.ExecutionVertex;
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.apache.flink.util.IterableUtils;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/** The slow task detector which detects slow tasks based on their execution 
time. */
+public class ExecutionTimeBasedSlowTaskDetector implements SlowTaskDetector {
+
+    private final long checkIntervalMillis;
+
+    private final long baselineLowerBoundMillis;
+
+    private final double baselineRatio;
+
+    private final double baselineMultiplier;
+
+    private ScheduledFuture<?> scheduledDetectionFuture;
+
+    public ExecutionTimeBasedSlowTaskDetector(Configuration configuration) {
+        this.checkIntervalMillis =
+                
configuration.get(SlowTaskDetectorOptions.CHECK_INTERVAL).toMillis();
+        this.baselineLowerBoundMillis =
+                configuration
+                        
.get(SlowTaskDetectorOptions.EXECUTION_TIME_BASELINE_LOWER_BOUND)
+                        .toMillis();
+        this.baselineRatio =
+                
configuration.getDouble(SlowTaskDetectorOptions.EXECUTION_TIME_BASELINE_RATIO);
+        this.baselineMultiplier =
+                
configuration.getDouble(SlowTaskDetectorOptions.EXECUTION_TIME_BASELINE_MULTIPLIER);
+    }
+
+    @Override
+    public void start(
+            final ExecutionGraph executionGraph,
+            final SlowTaskDetectorListener listener,
+            final ComponentMainThreadExecutor mainThreadExecutor) {
+        scheduleTask(executionGraph, listener, mainThreadExecutor);
+    }
+
+    /** Schedule periodical slow task detection. */
+    private void scheduleTask(
+            final ExecutionGraph executionGraph,
+            final SlowTaskDetectorListener listener,
+            final ComponentMainThreadExecutor mainThreadExecutor) {
+        this.scheduledDetectionFuture =
+                mainThreadExecutor.schedule(
+                        () -> {
+                            
listener.notifySlowTasks(findSlowTasks(executionGraph));
+                            scheduleTask(executionGraph, listener, 
mainThreadExecutor);
+                        },
+                        checkIntervalMillis,
+                        TimeUnit.MILLISECONDS);
+    }
+
+    /**
+     * Given that the parallelism is N and the ratio is R, define T as the 
median of the first N*R
+     * finished tasks' execution time. The baseline will be T*M, where M is 
the multiplier. A task
+     * will be identified as slow if its execution time is longer than the 
baseline.
+     */
+    @VisibleForTesting
+    Map<ExecutionVertexID, Collection<ExecutionAttemptID>> findSlowTasks(
+            final ExecutionGraph executionGraph) {
+        final long currentTimeMillis = System.currentTimeMillis();
+
+        final Map<ExecutionVertexID, Collection<ExecutionAttemptID>> slowTasks 
= new HashMap<>();
+
+        final List<ExecutionJobVertex> jobVerticesToCheck = 
getJobVerticesToCheck(executionGraph);
+
+        for (ExecutionJobVertex ejv : jobVerticesToCheck) {
+            final long baseline = getBaseline(ejv, currentTimeMillis);
+
+            for (ExecutionVertex ev : ejv.getTaskVertices()) {
+                if (ev.getExecutionState().isTerminal()) {
+                    continue;
+                }
+
+                final List<ExecutionAttemptID> slowExecutions =
+                        findExecutionsExceedingBaseline(
+                                ev.getCurrentExecutionAttempts(), baseline, 
currentTimeMillis);
+
+                if (!slowExecutions.isEmpty()) {
+                    slowTasks.put(ev.getID(), slowExecutions);
+                }
+            }
+        }
+
+        return slowTasks;
+    }
+
+    private List<ExecutionJobVertex> getJobVerticesToCheck(final 
ExecutionGraph executionGraph) {
+        return 
IterableUtils.toStream(executionGraph.getVerticesTopologically())
+                .filter(ejv -> ejv.isInitialized())
+                .filter(ejv -> ejv.getAggregateState() != 
ExecutionState.FINISHED)

Review Comment:
   isTerminal cannot be used to judge here.
   Because an ExecutionVertex in this ExecutionJobVertex maybe is failover now.



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