wanglijie95 commented on a change in pull request #18126: URL: https://github.com/apache/flink/pull/18126#discussion_r788574582
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/strategy/StagewiseSchedulingStrategy.java ########## @@ -0,0 +1,156 @@ +/* + * 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.strategy; + +import org.apache.flink.runtime.execution.ExecutionState; +import org.apache.flink.runtime.jobgraph.IntermediateResultPartitionID; +import org.apache.flink.runtime.scheduler.DeploymentOption; +import org.apache.flink.runtime.scheduler.ExecutionVertexDeploymentOption; +import org.apache.flink.runtime.scheduler.SchedulerOperations; +import org.apache.flink.runtime.scheduler.SchedulingTopologyListener; +import org.apache.flink.util.IterableUtils; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** + * {@link SchedulingStrategy} instance which schedules tasks in granularity of stage. Note that this + * strategy only supports ALL_EDGES_BLOCKING batch jobs. + */ +public class StagewiseSchedulingStrategy implements SchedulingStrategy, SchedulingTopologyListener { + + private final SchedulerOperations schedulerOperations; + + private final SchedulingTopology schedulingTopology; + + private final DeploymentOption deploymentOption = new DeploymentOption(false); + + private final Set<SchedulingExecutionVertex> pendingVertices = new HashSet<>(); + + public StagewiseSchedulingStrategy( + final SchedulerOperations schedulerOperations, + final SchedulingTopology schedulingTopology) { + + this.schedulerOperations = checkNotNull(schedulerOperations); + this.schedulingTopology = checkNotNull(schedulingTopology); + schedulingTopology.registerSchedulingTopologyListener(this); + } + + @Override + public void startScheduling() { + Set<SchedulingExecutionVertex> sourceVertices = + IterableUtils.toStream(schedulingTopology.getVertices()) + .filter(vertex -> vertex.getConsumedPartitionGroups().isEmpty()) + .collect(Collectors.toSet()); + + maybeScheduleVerticesOneByOne(sourceVertices); + } + + @Override + public void restartTasks(Set<ExecutionVertexID> verticesToRestart) { + maybeScheduleVerticesOneByOne( + SchedulingStrategyUtils.getVerticesFromIds(schedulingTopology, verticesToRestart)); + } + + @Override + public void onExecutionStateChange( + ExecutionVertexID executionVertexId, ExecutionState executionState) { + if (executionState == ExecutionState.FINISHED) { + SchedulingExecutionVertex executionVertex = + schedulingTopology.getVertex(executionVertexId); + + Set<SchedulingExecutionVertex> consumerVertices = + IterableUtils.toStream(executionVertex.getProducedResults()) + .flatMap(partition -> IterableUtils.toStream(partition.getConsumers())) + .collect(Collectors.toSet()); + + maybeScheduleVerticesOneByOne(consumerVertices); + } + } + + @Override + public void onPartitionConsumable(IntermediateResultPartitionID resultPartitionId) {} + + @Override + public void notifySchedulingTopologyUpdated( + SchedulingTopology schedulingTopology, List<ExecutionVertexID> newlyAddedVertices) { + checkState(schedulingTopology == this.schedulingTopology); + pendingVertices.addAll( + SchedulingStrategyUtils.getVerticesFromIds( + schedulingTopology, new HashSet<>(newlyAddedVertices))); + } + + private void maybeScheduleVerticesOneByOne(final Set<SchedulingExecutionVertex> vertices) { + + final Set<SchedulingExecutionVertex> allCandidates = new HashSet<>(vertices); + allCandidates.addAll( + pendingVertices.stream() + .filter(vertex -> vertex.getState() == ExecutionState.CREATED) + .collect(Collectors.toSet())); + + final Set<ExecutionVertexID> verticesToDeploy = + allCandidates.stream() + .filter(this::areVertexInputsAllConsumable) + .map( + vertex -> { + checkState(vertex.getState() == ExecutionState.CREATED); + return vertex.getId(); + }) + .collect(Collectors.toSet()); + + if (!verticesToDeploy.isEmpty()) { + final List<ExecutionVertexDeploymentOption> vertexDeploymentOptions = + SchedulingStrategyUtils + .createExecutionVertexDeploymentOptionsInTopologicalOrder( + schedulingTopology, verticesToDeploy, id -> deploymentOption); + + vertexDeploymentOptions.forEach( + option -> + schedulerOperations.allocateSlotsAndDeploy( + Collections.singletonList(option))); + + pendingVertices.clear(); Review comment: I will fix it. -- 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]
