zhuzhurk commented on a change in pull request #13071:
URL: https://github.com/apache/flink/pull/13071#discussion_r469426685



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/SlotSharingExecutionSlotAllocatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.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.executiongraph.utils.SimpleAckingTaskManagerGateway;
+import org.apache.flink.runtime.instance.SimpleSlotContext;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmaster.LogicalSlot;
+import org.apache.flink.runtime.jobmaster.SlotRequestId;
+import org.apache.flink.runtime.jobmaster.TestingPayload;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlot;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotProvider;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotRequest;
+import 
org.apache.flink.runtime.scheduler.SharedSlotProfileRetriever.SharedSlotProfileRetrieverFactory;
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.Preconditions;
+
+import org.junit.Test;
+import org.powermock.core.IdentityHashSet;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.function.BiConsumer;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Test suite for {@link SlotSharingExecutionSlotAllocator}.
+ */
+public class SlotSharingExecutionSlotAllocatorTest {
+       @Test
+       public void testSlotProfileRequestAskedBulkAndGroup() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+
+               context.allocateSlotsFor(0, 2);
+
+               Set<ExecutionVertexID> ids = new HashSet<>(context.getReqIds(0, 
2));
+               
assertThat(context.getSlotProfileRetrieverFactory().getAskedBulks(), 
containsInAnyOrder(ids));
+               
assertThat(context.getSlotProfileRetrieverFactory().getAskedGroups(), 
containsInAnyOrder(executionSlotSharingGroup));
+       }
+
+       @Test
+       public void testSlotRequestCompletionAfterProfileCompletion() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).completeSlotProfileFutureManually().build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(0, 2);
+
+               executionVertexAssignments.forEach(assignment -> 
assertThat(assignment.getLogicalSlotFuture().isDone(), is(false)));
+               
context.getSlotProfileRetrieverFactory().completeSlotProfileFutureFor(executionSlotSharingGroup);
+               executionVertexAssignments.forEach(assignment -> 
assertThat(assignment.getLogicalSlotFuture().isDone(), is(true)));
+       }
+
+       @Test
+       public void testSlotRequestProfile() {
+               ResourceProfile physicalsSlotResourceProfile = 
ResourceProfile.fromResources(3, 5);
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+               
context.getSlotProfileRetrieverFactory().addGroupResourceProfile(executionSlotSharingGroup,
 physicalsSlotResourceProfile);
+
+               context.allocateSlotsFor(0, 2);
+
+               Optional<PhysicalSlotRequest> slotRequest = 
context.getSlotProvider().getRequests().values().stream().findFirst();
+               assertThat(slotRequest.isPresent(), is(true));
+               slotRequest.ifPresent(r -> 
assertThat(r.getSlotProfile().getPhysicalSlotResourceProfile(), 
is(physicalsSlotResourceProfile)));
+       }
+
+       @Test
+       public void testNewAllocatePhysicalSlotForSharedSlot() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2, 2).build();
+
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(0, 4);
+               Collection<ExecutionVertexID> assignIds = 
getAssignIds(executionVertexAssignments);
+
+               assertThat(assignIds, containsInAnyOrder(context.getReqIds(0, 
4).toArray()));
+               assertThat(context.getSlotProvider().getRequests().keySet(), 
hasSize(2));
+       }
+
+       @Test
+       public void testAllocateLogicalSlotFromAvailableSharedSlot() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+
+               context.allocateSlotsFor(0, 1);
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(1, 2);
+               Collection<ExecutionVertexID> assignIds = 
getAssignIds(executionVertexAssignments);
+
+               // execution 0 from the first allocateSlotsFor call and 
execution 1 from the second allocateSlotsFor call
+               // share a slot, therefore only one physical slot allocation 
should happen
+               assertThat(assignIds, containsInAnyOrder(context.getReqIds(1, 
2).toArray()));
+               assertThat(context.getSlotProvider().getRequests().keySet(), 
hasSize(1));
+       }
+
+       @Test
+       public void testDuplicateAllocationDoesNotRecreateLogicalSlotFuture() 
throws ExecutionException, InterruptedException {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(1).build();
+
+               SlotExecutionVertexAssignment assignment1 = 
context.allocateSlotsFor(0, 1).get(0);
+               SlotExecutionVertexAssignment assignment2 = 
context.allocateSlotsFor(0, 1).get(0);
+
+               assertThat(assignment1.getLogicalSlotFuture().get() == 
assignment2.getLogicalSlotFuture().get(), is(true));
+       }
+
+       @Test
+       public void 
testFailedPhysicalSlotRequestFailsLogicalSlotFuturesAndRemovesSharedSlot() {
+               AllocationContext context = AllocationContext
+                       .newBuilder()
+                       .setGroups(1)
+                       .completePhysicalSlotFutureManually()
+                       .build();
+               CompletableFuture<LogicalSlot> logicalSlotFuture = 
context.allocateSlotsFor(0, 1).get(0).getLogicalSlotFuture();
+               SlotRequestId slotRequestId = 
context.getSlotProvider().getRequests().values().stream().findFirst().get().getSlotRequestId();
+
+               assertThat(logicalSlotFuture.isDone(), is(false));
+               
context.getSlotProvider().failPhysicalSlotFutureFor(slotRequestId, new 
Throwable());
+               assertThat(logicalSlotFuture.isCompletedExceptionally(), 
is(true));
+
+               // next allocation allocates new shared slot
+               context.allocateSlotsFor(0, 1);
+               assertThat(context.getSlotProvider().getRequests().keySet(), 
hasSize(2));
+       }
+
+       @Test
+       public void testSlotWillBeOccupiedIndefinitelyFalse() throws 
ExecutionException, InterruptedException {
+               testSlotWillBeOccupiedIndefinitely(false);
+       }
+
+       @Test
+       public void testSlotWillBeOccupiedIndefinitelyTrue() throws 
ExecutionException, InterruptedException {
+               testSlotWillBeOccupiedIndefinitely(true);
+       }
+
+       private static void testSlotWillBeOccupiedIndefinitely(boolean 
slotWillBeOccupiedIndefinitely) throws ExecutionException, InterruptedException 
{
+               AllocationContext context = AllocationContext
+                       .newBuilder()
+                       .setGroups(1)
+                       
.setSlotWillBeOccupiedIndefinitely(slotWillBeOccupiedIndefinitely)
+                       .build();
+               context.allocateSlotsFor(0, 1);
+
+               PhysicalSlotRequest slotRequest = 
context.getSlotProvider().getRequests().values().stream().findFirst().get();
+               assertThat(slotRequest.willSlotBeOccupiedIndefinitely(), 
is(slotWillBeOccupiedIndefinitely));
+
+               TestingPhysicalSlot physicalSlot = 
context.getSlotProvider().getResponses().get(slotRequest.getSlotRequestId()).get();
+               assertThat(physicalSlot.getPayload(), notNullValue());
+               
assertThat(physicalSlot.getPayload().willOccupySlotIndefinitely(), 
is(slotWillBeOccupiedIndefinitely));
+       }
+
+       @Test
+       public void testReturningLogicalSlotsRemovesSharedSlot() {
+               testLogicalSlotRequestCancellation(
+                       false,
+                       (context, assignment) -> {
+                               try {
+                                       
assignment.getLogicalSlotFuture().get().releaseSlot(null);
+                               } catch (InterruptedException | 
ExecutionException e) {
+                                       throw new 
FlinkRuntimeException("Unexpected", e);
+                               }
+                       });
+       }
+
+       @Test
+       public void 
testLogicalSlotCancelsPhysicalSlotRequestAndRemovesSharedSlot() {
+               testLogicalSlotRequestCancellation(
+                       true,
+                       (context, assignment) -> {
+                               
context.getAllocator().cancel(assignment.getExecutionVertexId());
+                               try {
+                                       assignment.getLogicalSlotFuture().get();
+                                       fail("THe logical future must finish 
with the cancellation exception");
+                               } catch (InterruptedException | 
ExecutionException e) {
+                                       assertThat(e.getCause(), 
instanceOf(CancellationException.class));
+                               }
+                       });
+       }
+
+       private static void testLogicalSlotRequestCancellation(
+                       boolean completePhysicalSlotFutureManually,
+                       BiConsumer<AllocationContext, 
SlotExecutionVertexAssignment> cancelAction) {
+               //if (completePhysicalSlotRequest) {

Review comment:
       legacy comment?

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/SlotSharingExecutionSlotAllocatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.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.executiongraph.utils.SimpleAckingTaskManagerGateway;
+import org.apache.flink.runtime.instance.SimpleSlotContext;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmaster.LogicalSlot;
+import org.apache.flink.runtime.jobmaster.SlotRequestId;
+import org.apache.flink.runtime.jobmaster.TestingPayload;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlot;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotProvider;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotRequest;
+import 
org.apache.flink.runtime.scheduler.SharedSlotProfileRetriever.SharedSlotProfileRetrieverFactory;
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.Preconditions;
+
+import org.junit.Test;
+import org.powermock.core.IdentityHashSet;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.function.BiConsumer;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Test suite for {@link SlotSharingExecutionSlotAllocator}.
+ */
+public class SlotSharingExecutionSlotAllocatorTest {
+       @Test
+       public void testSlotProfileRequestAskedBulkAndGroup() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+
+               context.allocateSlotsFor(0, 2);
+
+               Set<ExecutionVertexID> ids = new HashSet<>(context.getReqIds(0, 
2));
+               
assertThat(context.getSlotProfileRetrieverFactory().getAskedBulks(), 
containsInAnyOrder(ids));
+               
assertThat(context.getSlotProfileRetrieverFactory().getAskedGroups(), 
containsInAnyOrder(executionSlotSharingGroup));
+       }
+
+       @Test
+       public void testSlotRequestCompletionAfterProfileCompletion() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).completeSlotProfileFutureManually().build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(0, 2);
+
+               executionVertexAssignments.forEach(assignment -> 
assertThat(assignment.getLogicalSlotFuture().isDone(), is(false)));
+               
context.getSlotProfileRetrieverFactory().completeSlotProfileFutureFor(executionSlotSharingGroup);
+               executionVertexAssignments.forEach(assignment -> 
assertThat(assignment.getLogicalSlotFuture().isDone(), is(true)));
+       }
+
+       @Test
+       public void testSlotRequestProfile() {
+               ResourceProfile physicalsSlotResourceProfile = 
ResourceProfile.fromResources(3, 5);
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+               
context.getSlotProfileRetrieverFactory().addGroupResourceProfile(executionSlotSharingGroup,
 physicalsSlotResourceProfile);
+
+               context.allocateSlotsFor(0, 2);
+
+               Optional<PhysicalSlotRequest> slotRequest = 
context.getSlotProvider().getRequests().values().stream().findFirst();
+               assertThat(slotRequest.isPresent(), is(true));
+               slotRequest.ifPresent(r -> 
assertThat(r.getSlotProfile().getPhysicalSlotResourceProfile(), 
is(physicalsSlotResourceProfile)));
+       }
+
+       @Test
+       public void testNewAllocatePhysicalSlotForSharedSlot() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2, 2).build();
+
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(0, 4);
+               Collection<ExecutionVertexID> assignIds = 
getAssignIds(executionVertexAssignments);
+
+               assertThat(assignIds, containsInAnyOrder(context.getReqIds(0, 
4).toArray()));
+               assertThat(context.getSlotProvider().getRequests().keySet(), 
hasSize(2));
+       }
+
+       @Test
+       public void testAllocateLogicalSlotFromAvailableSharedSlot() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+
+               context.allocateSlotsFor(0, 1);
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(1, 2);
+               Collection<ExecutionVertexID> assignIds = 
getAssignIds(executionVertexAssignments);
+
+               // execution 0 from the first allocateSlotsFor call and 
execution 1 from the second allocateSlotsFor call
+               // share a slot, therefore only one physical slot allocation 
should happen
+               assertThat(assignIds, containsInAnyOrder(context.getReqIds(1, 
2).toArray()));
+               assertThat(context.getSlotProvider().getRequests().keySet(), 
hasSize(1));
+       }
+
+       @Test
+       public void testDuplicateAllocationDoesNotRecreateLogicalSlotFuture() 
throws ExecutionException, InterruptedException {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(1).build();
+
+               SlotExecutionVertexAssignment assignment1 = 
context.allocateSlotsFor(0, 1).get(0);
+               SlotExecutionVertexAssignment assignment2 = 
context.allocateSlotsFor(0, 1).get(0);
+
+               assertThat(assignment1.getLogicalSlotFuture().get() == 
assignment2.getLogicalSlotFuture().get(), is(true));
+       }
+
+       @Test
+       public void 
testFailedPhysicalSlotRequestFailsLogicalSlotFuturesAndRemovesSharedSlot() {
+               AllocationContext context = AllocationContext
+                       .newBuilder()
+                       .setGroups(1)
+                       .completePhysicalSlotFutureManually()
+                       .build();
+               CompletableFuture<LogicalSlot> logicalSlotFuture = 
context.allocateSlotsFor(0, 1).get(0).getLogicalSlotFuture();
+               SlotRequestId slotRequestId = 
context.getSlotProvider().getRequests().values().stream().findFirst().get().getSlotRequestId();
+
+               assertThat(logicalSlotFuture.isDone(), is(false));
+               
context.getSlotProvider().failPhysicalSlotFutureFor(slotRequestId, new 
Throwable());
+               assertThat(logicalSlotFuture.isCompletedExceptionally(), 
is(true));
+
+               // next allocation allocates new shared slot
+               context.allocateSlotsFor(0, 1);
+               assertThat(context.getSlotProvider().getRequests().keySet(), 
hasSize(2));
+       }
+
+       @Test
+       public void testSlotWillBeOccupiedIndefinitelyFalse() throws 
ExecutionException, InterruptedException {
+               testSlotWillBeOccupiedIndefinitely(false);
+       }
+
+       @Test
+       public void testSlotWillBeOccupiedIndefinitelyTrue() throws 
ExecutionException, InterruptedException {
+               testSlotWillBeOccupiedIndefinitely(true);
+       }
+
+       private static void testSlotWillBeOccupiedIndefinitely(boolean 
slotWillBeOccupiedIndefinitely) throws ExecutionException, InterruptedException 
{
+               AllocationContext context = AllocationContext
+                       .newBuilder()
+                       .setGroups(1)
+                       
.setSlotWillBeOccupiedIndefinitely(slotWillBeOccupiedIndefinitely)
+                       .build();
+               context.allocateSlotsFor(0, 1);
+
+               PhysicalSlotRequest slotRequest = 
context.getSlotProvider().getRequests().values().stream().findFirst().get();
+               assertThat(slotRequest.willSlotBeOccupiedIndefinitely(), 
is(slotWillBeOccupiedIndefinitely));
+
+               TestingPhysicalSlot physicalSlot = 
context.getSlotProvider().getResponses().get(slotRequest.getSlotRequestId()).get();
+               assertThat(physicalSlot.getPayload(), notNullValue());
+               
assertThat(physicalSlot.getPayload().willOccupySlotIndefinitely(), 
is(slotWillBeOccupiedIndefinitely));
+       }
+
+       @Test
+       public void testReturningLogicalSlotsRemovesSharedSlot() {
+               testLogicalSlotRequestCancellation(
+                       false,
+                       (context, assignment) -> {
+                               try {
+                                       
assignment.getLogicalSlotFuture().get().releaseSlot(null);
+                               } catch (InterruptedException | 
ExecutionException e) {
+                                       throw new 
FlinkRuntimeException("Unexpected", e);
+                               }
+                       });
+       }
+
+       @Test
+       public void 
testLogicalSlotCancelsPhysicalSlotRequestAndRemovesSharedSlot() {
+               testLogicalSlotRequestCancellation(
+                       true,
+                       (context, assignment) -> {
+                               
context.getAllocator().cancel(assignment.getExecutionVertexId());
+                               try {
+                                       assignment.getLogicalSlotFuture().get();
+                                       fail("THe logical future must finish 
with the cancellation exception");

Review comment:
       typo: `THe` -> `The `

##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/SlotSharingExecutionSlotAllocatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.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.executiongraph.utils.SimpleAckingTaskManagerGateway;
+import org.apache.flink.runtime.instance.SimpleSlotContext;
+import org.apache.flink.runtime.jobgraph.JobVertexID;
+import org.apache.flink.runtime.jobmaster.LogicalSlot;
+import org.apache.flink.runtime.jobmaster.SlotRequestId;
+import org.apache.flink.runtime.jobmaster.TestingPayload;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlot;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotProvider;
+import org.apache.flink.runtime.jobmaster.slotpool.PhysicalSlotRequest;
+import 
org.apache.flink.runtime.scheduler.SharedSlotProfileRetriever.SharedSlotProfileRetrieverFactory;
+import org.apache.flink.runtime.scheduler.strategy.ExecutionVertexID;
+import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
+import org.apache.flink.util.FlinkRuntimeException;
+import org.apache.flink.util.Preconditions;
+
+import org.junit.Test;
+import org.powermock.core.IdentityHashSet;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.function.BiConsumer;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * Test suite for {@link SlotSharingExecutionSlotAllocator}.
+ */
+public class SlotSharingExecutionSlotAllocatorTest {
+       @Test
+       public void testSlotProfileRequestAskedBulkAndGroup() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+
+               context.allocateSlotsFor(0, 2);
+
+               Set<ExecutionVertexID> ids = new HashSet<>(context.getReqIds(0, 
2));
+               
assertThat(context.getSlotProfileRetrieverFactory().getAskedBulks(), 
containsInAnyOrder(ids));
+               
assertThat(context.getSlotProfileRetrieverFactory().getAskedGroups(), 
containsInAnyOrder(executionSlotSharingGroup));
+       }
+
+       @Test
+       public void testSlotRequestCompletionAfterProfileCompletion() {
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).completeSlotProfileFutureManually().build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+
+               List<SlotExecutionVertexAssignment> executionVertexAssignments 
= context.allocateSlotsFor(0, 2);
+
+               executionVertexAssignments.forEach(assignment -> 
assertThat(assignment.getLogicalSlotFuture().isDone(), is(false)));
+               
context.getSlotProfileRetrieverFactory().completeSlotProfileFutureFor(executionSlotSharingGroup);
+               executionVertexAssignments.forEach(assignment -> 
assertThat(assignment.getLogicalSlotFuture().isDone(), is(true)));
+       }
+
+       @Test
+       public void testSlotRequestProfile() {
+               ResourceProfile physicalsSlotResourceProfile = 
ResourceProfile.fromResources(3, 5);
+               AllocationContext context = 
AllocationContext.newBuilder().setGroups(2).build();
+               ExecutionSlotSharingGroup executionSlotSharingGroup =
+                       
context.getSlotSharingStrategy().getExecutionSlotSharingGroup(context.getReqIds(0,
 1).get(0));
+               
context.getSlotProfileRetrieverFactory().addGroupResourceProfile(executionSlotSharingGroup,
 physicalsSlotResourceProfile);
+
+               context.allocateSlotsFor(0, 2);
+
+               Optional<PhysicalSlotRequest> slotRequest = 
context.getSlotProvider().getRequests().values().stream().findFirst();
+               assertThat(slotRequest.isPresent(), is(true));
+               slotRequest.ifPresent(r -> 
assertThat(r.getSlotProfile().getPhysicalSlotResourceProfile(), 
is(physicalsSlotResourceProfile)));
+       }
+
+       @Test
+       public void testNewAllocatePhysicalSlotForSharedSlot() {

Review comment:
       do you mean `testAllocatePhysicalSlotForNewSharedSlot `?




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to