zhuzhurk commented on a change in pull request #12256: URL: https://github.com/apache/flink/pull/12256#discussion_r438858595
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/OneSlotPerExecutionSlotAllocatorTest.java ########## @@ -0,0 +1,305 @@ +/* + * 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.clusterframework.types.AllocationID; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.clusterframework.types.SlotProfile; +import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor; +import org.apache.flink.runtime.concurrent.FutureUtils; +import org.apache.flink.runtime.instance.SlotSharingGroupId; +import org.apache.flink.runtime.jobgraph.JobVertexID; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationConstraint; +import org.apache.flink.runtime.jobmanager.scheduler.CoLocationGroup; +import org.apache.flink.runtime.jobmaster.SlotRequestId; +import org.apache.flink.runtime.jobmaster.slotpool.BulkSlotProvider; +import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotRequest; +import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID; +import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation; +import org.apache.flink.runtime.taskmanager.TaskManagerLocation; +import org.apache.flink.util.TestLogger; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import static org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotTestUtils.createPhysicalSlot; +import static org.apache.flink.runtime.scheduler.ExecutionSlotAllocatorTestUtils.createSchedulingRequirements; +import static org.apache.flink.runtime.scheduler.ExecutionSlotAllocatorTestUtils.findSlotAssignmentByExecutionVertexId; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link OneSlotPerExecutionSlotAllocator}. + */ +public class OneSlotPerExecutionSlotAllocatorTest extends TestLogger { + + private TestingBulkSlotProvider slotProvider; + + @Before + public void setUp() throws Exception { + slotProvider = new TestingBulkSlotProvider(); + } + + @Test + public void testSucceededSlotAllocation() { + final ExecutionSlotAllocator executionSlotAllocator = createExecutionSlotAllocator(); + + final ExecutionVertexID executionVertexID = new ExecutionVertexID(new JobVertexID(), 0); + final List<ExecutionVertexSchedulingRequirements> schedulingRequirements = + createSchedulingRequirements(executionVertexID); + + final Collection<SlotExecutionVertexAssignment> slotExecutionVertexAssignments = + executionSlotAllocator.allocateSlotsFor(schedulingRequirements); + + assertThat(slotExecutionVertexAssignments, hasSize(1)); + + final SlotExecutionVertexAssignment slotAssignment = + findSlotAssignmentByExecutionVertexId(executionVertexID, slotExecutionVertexAssignments); + + assertThat(slotAssignment.getExecutionVertexId(), equalTo(executionVertexID)); + assertThat(slotAssignment.getLogicalSlotFuture().isDone(), is(true)); + assertThat(slotAssignment.getLogicalSlotFuture().isCompletedExceptionally(), is(false)); + } + + @Test + public void testFailedSlotAllocation() { + final OneSlotPerExecutionSlotAllocator executionSlotAllocator = createExecutionSlotAllocator(); + + final ExecutionVertexID executionVertexID = new ExecutionVertexID(new JobVertexID(), 0); + final List<ExecutionVertexSchedulingRequirements> schedulingRequirements = + createSchedulingRequirements(executionVertexID); + + slotProvider.forceFailingSlotAllocation(); + final Collection<SlotExecutionVertexAssignment> slotExecutionVertexAssignments = + executionSlotAllocator.allocateSlotsFor(schedulingRequirements); + + final SlotExecutionVertexAssignment slotAssignment = + findSlotAssignmentByExecutionVertexId(executionVertexID, slotExecutionVertexAssignments); + + assertThat(slotAssignment.getLogicalSlotFuture().isCompletedExceptionally(), is(true)); + assertThat(executionSlotAllocator.getPendingSlotAssignments().keySet(), hasSize(0)); + assertThat(slotProvider.getCancelledSlotRequestIds(), contains(slotAssignment.getSlotRequestId())); + } + + @Test + public void testInterBulkInputLocationPreferencesAreRespected() { + final ExecutionVertexID producerId = new ExecutionVertexID(new JobVertexID(), 0); + final ExecutionVertexID consumerId = new ExecutionVertexID(new JobVertexID(), 0); + + final TestingInputsLocationsRetriever inputsLocationsRetriever = new TestingInputsLocationsRetriever.Builder() + .connectConsumerToProducer(consumerId, producerId) + .build(); + + final ExecutionSlotAllocator executionSlotAllocator = createExecutionSlotAllocator( + new TestingStateLocationRetriever(), + inputsLocationsRetriever); + + inputsLocationsRetriever.markScheduled(producerId); + final List<ExecutionVertexSchedulingRequirements> schedulingRequirementsForProducer = + createSchedulingRequirements(producerId); + final Collection<SlotExecutionVertexAssignment> slotExecutionVertexAssignmentsForProducer = + executionSlotAllocator.allocateSlotsFor(schedulingRequirementsForProducer); + final SlotExecutionVertexAssignment producerSlotAssignment = + findSlotAssignmentByExecutionVertexId(producerId, slotExecutionVertexAssignmentsForProducer); + + assertThat(producerSlotAssignment.getLogicalSlotFuture().isDone(), is(true)); + + inputsLocationsRetriever.markScheduled(consumerId); + final List<ExecutionVertexSchedulingRequirements> schedulingRequirementsForConsumer = + createSchedulingRequirements(consumerId); + final Collection<SlotExecutionVertexAssignment> slotExecutionVertexAssignmentsForConsumer = + executionSlotAllocator.allocateSlotsFor(schedulingRequirementsForConsumer); + final SlotExecutionVertexAssignment consumerSlotAssignment = + findSlotAssignmentByExecutionVertexId(consumerId, slotExecutionVertexAssignmentsForConsumer); + + assertThat(consumerSlotAssignment.getLogicalSlotFuture().isDone(), is(false)); + + inputsLocationsRetriever.assignTaskManagerLocation(producerId); + + assertThat(consumerSlotAssignment.getLogicalSlotFuture().isDone(), is(true)); + } + + @Test + public void testIntraBulkInputLocationPreferencesDoNotBlockAllocation() { + final ExecutionVertexID producerId = new ExecutionVertexID(new JobVertexID(), 0); + final ExecutionVertexID consumerId = new ExecutionVertexID(new JobVertexID(), 0); + + final TestingInputsLocationsRetriever inputsLocationsRetriever = new TestingInputsLocationsRetriever.Builder() + .connectConsumerToProducer(consumerId, producerId) + .build(); + + final ExecutionSlotAllocator executionSlotAllocator = createExecutionSlotAllocator( + new TestingStateLocationRetriever(), + inputsLocationsRetriever); + + inputsLocationsRetriever.markScheduled(producerId); + inputsLocationsRetriever.markScheduled(consumerId); + + final List<ExecutionVertexSchedulingRequirements> schedulingRequirements = + createSchedulingRequirements(producerId, consumerId); + final Collection<SlotExecutionVertexAssignment> slotExecutionVertexAssignments = + executionSlotAllocator.allocateSlotsFor(schedulingRequirements); + + assertThat(slotExecutionVertexAssignments, hasSize(2)); + + final SlotExecutionVertexAssignment producerSlotAssignment = + findSlotAssignmentByExecutionVertexId(producerId, slotExecutionVertexAssignments); + final SlotExecutionVertexAssignment consumerSlotAssignment = + findSlotAssignmentByExecutionVertexId(consumerId, slotExecutionVertexAssignments); + + assertThat(producerSlotAssignment.getLogicalSlotFuture().isDone(), is(true)); + assertThat(consumerSlotAssignment.getLogicalSlotFuture().isDone(), is(true)); + } + + @Test + public void testCreatedSlotRequests() { + final ExecutionVertexID executionVertexId = new ExecutionVertexID(new JobVertexID(), 0); + final AllocationID allocationId = new AllocationID(); + final SlotSharingGroupId sharingGroupId = new SlotSharingGroupId(); + final ResourceProfile taskResourceProfile = ResourceProfile.fromResources(0.5, 250); + final ResourceProfile physicalSlotResourceProfile = ResourceProfile.fromResources(1.0, 300); + final TaskManagerLocation taskManagerLocation = new LocalTaskManagerLocation(); + + final TestingStateLocationRetriever stateLocationRetriever = new TestingStateLocationRetriever(); + stateLocationRetriever.setStateLocation(executionVertexId, taskManagerLocation); + + final ExecutionSlotAllocator executionSlotAllocator = createExecutionSlotAllocator( + stateLocationRetriever, + new TestingInputsLocationsRetriever.Builder().build()); + + final List<ExecutionVertexSchedulingRequirements> schedulingRequirements = Arrays.asList( Review comment: done. ---------------------------------------------------------------- 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]
