xintongsong commented on a change in pull request #11615: [FLINK-16605] Add max limitation to the total number of slots URL: https://github.com/apache/flink/pull/11615#discussion_r407407657
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/resourcemanager/slotmanager/SlotManagerImplTest.java ########## @@ -1540,6 +1555,85 @@ public void testSpreadOutSlotAllocationStrategy() throws Exception { } } + /** + * Test that the slot manager respect the max limitation of the number of slots when allocate new resource. + */ + @Test + public void testRespectMaxLimitBeforeAllocateResource() throws Exception { + final int numberSlots = 1; + final int maxSlotNum = 1; + + final ResourceManagerId resourceManagerId = ResourceManagerId.generate(); + final JobID jobId = new JobID(); + + final AtomicInteger resourceRequests = new AtomicInteger(0); + ResourceActions resourceManagerActions = new TestingResourceActionsBuilder() + .setAllocateResourceFunction( + ignored -> { + resourceRequests.incrementAndGet(); + return true; + }) + .build(); + + final Configuration configuration = new Configuration(); + configuration.setInteger(ResourceManagerOptions.MAX_SLOT_NUM, maxSlotNum); + configuration.setInteger(TaskManagerOptions.NUM_TASK_SLOTS, numberSlots); + + try (SlotManagerImpl slotManager = createSlotManager(resourceManagerId, resourceManagerActions, configuration)) { + + assertTrue("The slot request should be accepted", slotManager.registerSlotRequest(createSlotRequest(jobId))); + assertThat(resourceRequests.get(), is(1)); + + // The second slot request should not try to allocate a new resource because of the max limitation. + assertTrue("The slot request should be accepted", slotManager.registerSlotRequest(createSlotRequest(jobId))); + assertThat(resourceRequests.get(), is(1)); + } + } + + /** + * Test that the slot manager release resource when the number of slots exceed max limit when new TaskExecutor registered. + */ + @Test + public void testReleaseResourceWhenExceedMaxLimit() throws Exception { + final int numberSlots = 1; + final int maxSlotNum = 1; + final ResourceManagerId resourceManagerId = ResourceManagerId.generate(); + + final CompletableFuture<InstanceID> releasedResourceFuture = new CompletableFuture<>(); + ResourceActions resourceManagerActions = new TestingResourceActionsBuilder() + .setReleaseResourceConsumer((instanceID, e) -> releasedResourceFuture.complete(instanceID)) + .build(); + + final Configuration configuration = new Configuration(); + configuration.setInteger(ResourceManagerOptions.MAX_SLOT_NUM, maxSlotNum); + configuration.setInteger(TaskManagerOptions.NUM_TASK_SLOTS, numberSlots); + + final TaskExecutorGateway taskExecutorGateway1 = new TestingTaskExecutorGatewayBuilder().createTestingTaskExecutorGateway(); + final TaskExecutorGateway taskExecutorGateway2 = new TestingTaskExecutorGatewayBuilder().createTestingTaskExecutorGateway(); + final ResourceID resourceId1 = ResourceID.generate(); + final ResourceID resourceId2 = ResourceID.generate(); + final TaskExecutorConnection taskManagerConnection1 = new TaskExecutorConnection(resourceId1, taskExecutorGateway1); + final TaskExecutorConnection taskManagerConnection2 = new TaskExecutorConnection(resourceId2, taskExecutorGateway2); + + final SlotID slotId1 = new SlotID(resourceId1, 0); + final SlotID slotId2 = new SlotID(resourceId1, 0); + final SlotStatus slotStatus1 = new SlotStatus(slotId1, ResourceProfile.UNKNOWN); + final SlotStatus slotStatus2 = new SlotStatus(slotId2, ResourceProfile.UNKNOWN); + final SlotReport slotReport1 = new SlotReport(Collections.singletonList(slotStatus1)); + final SlotReport slotReport2 = new SlotReport(Collections.singletonList(slotStatus2)); + + try (SlotManagerImpl slotManager = createSlotManager(resourceManagerId, resourceManagerActions, configuration)) { + slotManager.registerTaskManager(taskManagerConnection1, slotReport1); + slotManager.registerTaskManager(taskManagerConnection2, slotReport2); + + assertTrue("The number registered slots does not equal the expected number.", 1 == slotManager.getNumberRegisteredSlots()); Review comment: Better to use `assertThat(xxx, is(xxx))` ---------------------------------------------------------------- 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 With regards, Apache Git Services