wanglijie95 commented on code in PR #20039: URL: https://github.com/apache/flink/pull/20039#discussion_r905694491
########## flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/DefaultExecutionDeployerTest.java: ########## @@ -0,0 +1,388 @@ +/* + * 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; + +import org.apache.flink.api.common.time.Time; +import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor; +import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutorServiceAdapter; +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.ExecutionVertex; +import org.apache.flink.runtime.executiongraph.TestingDefaultExecutionGraphBuilder; +import org.apache.flink.runtime.io.network.partition.ResultPartitionID; +import org.apache.flink.runtime.io.network.partition.ResultPartitionType; +import org.apache.flink.runtime.io.network.partition.TestingJobMasterPartitionTracker; +import org.apache.flink.runtime.jobgraph.DistributionPattern; +import org.apache.flink.runtime.jobgraph.JobGraph; +import org.apache.flink.runtime.jobgraph.JobGraphTestUtils; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.shuffle.TestingShuffleMaster; +import org.apache.flink.runtime.testtasks.NoOpInvokable; +import org.apache.flink.util.ExecutorUtils; +import org.apache.flink.util.IterableUtils; +import org.apache.flink.util.TestLogger; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Fail.fail; + +/** Tests for {@link DefaultExecutionDeployer}. */ +public class DefaultExecutionDeployerTest extends TestLogger { + + private ScheduledExecutorService executor; + private ComponentMainThreadExecutor mainThreadExecutor; + private TestExecutionOperationsDecorator testExecutionOperations; + private ExecutionVertexVersioner executionVertexVersioner; + private TestExecutionSlotAllocator testExecutionSlotAllocator; + private TestingShuffleMaster shuffleMaster; + private TestingJobMasterPartitionTracker partitionTracker; + private Time partitionRegistrationTimeout; + + @BeforeEach + public void setUp() throws Exception { + executor = Executors.newSingleThreadScheduledExecutor(); + mainThreadExecutor = ComponentMainThreadExecutorServiceAdapter.forMainThread(); + testExecutionOperations = + new TestExecutionOperationsDecorator( + new ExecutionOperations() { + @Override + public void deploy(Execution execution) {} + + @Override + public CompletableFuture<?> cancel(Execution execution) { + return null; + } + + @Override + public void markFailed(Execution execution, Throwable cause) {} + }); + executionVertexVersioner = new ExecutionVertexVersioner(); + testExecutionSlotAllocator = new TestExecutionSlotAllocator(); + shuffleMaster = new TestingShuffleMaster(); + partitionTracker = new TestingJobMasterPartitionTracker(); + partitionRegistrationTimeout = Time.milliseconds(5000); + } + + @AfterEach + public void tearDown() throws Exception { + if (executor != null) { + ExecutorUtils.gracefulShutdown(5, TimeUnit.SECONDS, executor); + } + } + + @Test + public void testDeployTasks() throws Exception { + final JobGraph jobGraph = singleNonParallelJobVertexJobGraph(); + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + + deployTasks(executionDeployer, executionGraph); + + assertThat(testExecutionOperations.getDeployedExecutions()) + .contains(getAnyExecution(executionGraph).getAttemptId()); + } + + @Test + public void testDeployTasksOnlyIfAllSlotRequestsAreFulfilled() throws Exception { + final JobGraph jobGraph = singleJobVertexJobGraph(4); + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + + testExecutionSlotAllocator.disableAutoCompletePendingRequests(); + + deployTasks(executionDeployer, executionGraph); + + assertThat(testExecutionOperations.getDeployedExecutions()).hasSize(0); + + final ExecutionAttemptID attemptId = getAnyExecution(executionGraph).getAttemptId(); + testExecutionSlotAllocator.completePendingRequest(attemptId); + assertThat(testExecutionOperations.getDeployedExecutions()).hasSize(0); + + testExecutionSlotAllocator.completePendingRequests(); + assertThat(testExecutionOperations.getDeployedExecutions()).hasSize(4); + } + + @Test + public void testDeploymentFailures() throws Exception { + final JobGraph jobGraph = singleNonParallelJobVertexJobGraph(); + + testExecutionOperations.enableFailDeploy(); + + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + deployTasks(executionDeployer, executionGraph); + + assertThat(testExecutionOperations.getFailedExecutions()) + .contains(getAnyExecution(executionGraph).getAttemptId()); + } + + @Test + public void testSlotAllocationTimeout() throws Exception { + final JobGraph jobGraph = singleJobVertexJobGraph(2); + + testExecutionSlotAllocator.disableAutoCompletePendingRequests(); + + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + deployTasks(executionDeployer, executionGraph); + + assertThat(testExecutionSlotAllocator.getPendingRequests().keySet()).hasSize(2); + + final ExecutionAttemptID attemptId = getAnyExecution(executionGraph).getAttemptId(); + testExecutionSlotAllocator.timeoutPendingRequest(attemptId); + + assertThat(testExecutionOperations.getFailedExecutions()).contains(attemptId); + } + + @Test + public void testSkipDeploymentIfVertexVersionOutdated() throws Exception { + final JobGraph jobGraph = singleNonParallelJobVertexJobGraph(); + + testExecutionSlotAllocator.disableAutoCompletePendingRequests(); + + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + deployTasks(executionDeployer, executionGraph); + + final ExecutionAttemptID attemptId = getAnyExecution(executionGraph).getAttemptId(); + + executionVertexVersioner.recordModification(attemptId.getExecutionVertexId()); + testExecutionSlotAllocator.completePendingRequests(); + + assertThat(testExecutionOperations.getDeployedVertices()).hasSize(0); + } + + @Test + public void testReleaseSlotIfVertexVersionOutdated() throws Exception { + final JobGraph jobGraph = singleNonParallelJobVertexJobGraph(); + + testExecutionSlotAllocator.disableAutoCompletePendingRequests(); + + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + deployTasks(executionDeployer, executionGraph); + + final ExecutionAttemptID attemptId = getAnyExecution(executionGraph).getAttemptId(); + + executionVertexVersioner.recordModification(attemptId.getExecutionVertexId()); + testExecutionSlotAllocator.completePendingRequests(); + + assertThat(testExecutionSlotAllocator.getReturnedSlots()).hasSize(1); + } + + @Test + public void testDeployOnlyIfVertexIsCreated() throws Exception { + final JobGraph jobGraph = singleNonParallelJobVertexJobGraph(); + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + + // deploy once to transition the tasks out from CREATED state + deployTasks(executionDeployer, executionGraph); + + // The deploying of a non-CREATED vertex will result in IllegalStateException + try { + deployTasks(executionDeployer, executionGraph); + fail("IllegalStateException should happen"); + } catch (IllegalStateException e) { + // expected exception + } + } + + @Test + public void testDeploymentWaitForProducedPartitionRegistration() throws Exception { + shuffleMaster.setAutoCompleteRegistration(false); + + final List<ResultPartitionID> trackedPartitions = new ArrayList<>(); + partitionTracker.setStartTrackingPartitionsConsumer( + (resourceID, resultPartitionDeploymentDescriptor) -> + trackedPartitions.add( + resultPartitionDeploymentDescriptor + .getShuffleDescriptor() + .getResultPartitionID())); + + final JobGraph jobGraph = nonParallelSourceSinkJobGraph(); + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + + deployTasks(executionDeployer, executionGraph); + + assertThat(trackedPartitions).hasSize(0); + assertThat(testExecutionOperations.getDeployedExecutions()).hasSize(0); + + shuffleMaster.completeAllPendingRegistrations(); + assertThat(trackedPartitions).hasSize(1); + assertThat(testExecutionOperations.getDeployedExecutions()).hasSize(2); + } + + @Test + public void testFailedProducedPartitionRegistration() throws Exception { + shuffleMaster.setAutoCompleteRegistration(false); + + final JobGraph jobGraph = nonParallelSourceSinkJobGraph(); + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + + deployTasks(executionDeployer, executionGraph); + + assertThat(testExecutionOperations.getFailedExecutions()).hasSize(0); + + shuffleMaster.failAllPendingRegistrations(); + assertThat(testExecutionOperations.getFailedExecutions()).hasSize(1); + } + + @Test + public void testDirectExceptionOnProducedPartitionRegistration() throws Exception { + shuffleMaster.setThrowExceptionalOnRegistration(true); + + final JobGraph jobGraph = nonParallelSourceSinkJobGraph(); + final ExecutionGraph executionGraph = createExecutionGraph(jobGraph); + final ExecutionDeployer executionDeployer = createExecutionDeployer(executionGraph); + + deployTasks(executionDeployer, executionGraph); + + assertThat(testExecutionOperations.getFailedExecutions()).hasSize(1); + } + + @Test Review Comment: OK ########## flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/SlotSharingExecutionSlotAllocator.java: ########## @@ -94,6 +96,34 @@ class SlotSharingExecutionSlotAllocator implements ExecutionSlotAllocator { this.sharedSlots = new IdentityHashMap<>(); } + @Override + public List<ExecutionSlotAssignment> allocateSlotsFor( + List<ExecutionAttemptID> executionAttemptIds) { + + final Map<ExecutionVertexID, ExecutionAttemptID> vertexIdToExecutionId = new HashMap<>(); + executionAttemptIds.forEach( + executionId -> + vertexIdToExecutionId.put(executionId.getExecutionVertexId(), executionId)); + + checkState( + vertexIdToExecutionId.size() == executionAttemptIds.size(), + "SlotSharingExecutionSlotAllocator does not support one execution vertex to have multiple concurrent executions"); + + final List<ExecutionVertexID> vertexIds = Review Comment: OK -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org