tillrohrmann commented on a change in pull request #13964: URL: https://github.com/apache/flink/pull/13964#discussion_r540154657
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/jobmaster/slotpool/SlotPool.java ########## @@ -151,32 +151,18 @@ void start( Collection<SlotInfo> getAllocatedSlotsInformation(); /** - * Allocates the available slot with the given allocation id under the given request id. This method returns - * {@code null} if no slot with the given allocation id is available. + * Allocates the available slot with the given allocation id under the given request id for the given requirement profile. + * The slot must be able to fulfill the requirement profile, otherwise an exception will be thrown. Review comment: What exception will be thrown? ########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/slotpool/DeclarativeSlotPoolBridgeResourceDeclarationTest.java ########## @@ -0,0 +1,154 @@ +/* + * 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.jobmaster.slotpool; + +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.concurrent.ComponentMainThreadExecutor; +import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutorServiceAdapter; +import org.apache.flink.runtime.jobmaster.JobMasterId; +import org.apache.flink.runtime.jobmaster.SlotRequestId; +import org.apache.flink.util.TestLogger; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collections; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeoutException; + +import static org.apache.flink.runtime.jobmaster.slotpool.DeclarativeSlotPoolBridgeTest.createAllocatedSlot; +import static org.apache.flink.runtime.jobmaster.slotpool.DeclarativeSlotPoolBridgeTest.createDeclarativeSlotPoolBridge; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * Tests for the {@link DeclarativeSlotPoolBridge}. + */ +public class DeclarativeSlotPoolBridgeResourceDeclarationTest extends TestLogger { + + private static final JobMasterId jobMasterId = JobMasterId.generate(); + private final ComponentMainThreadExecutor mainThreadExecutor = ComponentMainThreadExecutorServiceAdapter.forMainThread(); + + private RequirementListener requirementListener; + private DeclarativeSlotPoolBridge declarativeSlotPoolBridge; + + @Before + public void setup() throws Exception { + requirementListener = new RequirementListener(); + + final TestingDeclarativeSlotPoolBuilder slotPoolBuilder = TestingDeclarativeSlotPool.builder() + .setIncreaseResourceRequirementsByConsumer(requirementListener::increaseRequirements) + .setDecreaseResourceRequirementsByConsumer(requirementListener::decreaseRequirements) + .setReserveFreeSlotFunction((allocationId, resourceProfile) -> createAllocatedSlot(allocationId)) + .setFreeReservedSlotFunction((allocationID, throwable, aLong) -> ResourceCounter.withResource(ResourceProfile.UNKNOWN, 1)) + .setReleaseSlotFunction((allocationID, e) -> ResourceCounter.withResource(ResourceProfile.UNKNOWN, 1)); + + final DeclarativeSlotPoolBridgeTest.TestingDeclarativeSlotPoolFactory declarativeSlotPoolFactory = new DeclarativeSlotPoolBridgeTest.TestingDeclarativeSlotPoolFactory(slotPoolBuilder); + declarativeSlotPoolBridge = createDeclarativeSlotPoolBridge(declarativeSlotPoolFactory); + declarativeSlotPoolBridge.start(jobMasterId, "localhost", mainThreadExecutor); + } + + @After + public void teardown() throws Exception { + if (declarativeSlotPoolBridge != null) { + declarativeSlotPoolBridge.close(); + } + } + + @Test + public void testRequirementsIncreasedOnNewAllocation() { + // requesting the allocation of a new slot should increase the requirements + declarativeSlotPoolBridge.requestNewAllocatedSlot(new SlotRequestId(), ResourceProfile.UNKNOWN, Time.minutes(5)); + assertThat(requirementListener.getRequirements().getResourceCount(ResourceProfile.UNKNOWN), is(1)); + } + + @Test + public void testRequirementsDecreasedOnAllocationTimeout() { + // requesting the allocation of a new slot increases the requirements + final CompletableFuture<PhysicalSlot> allocationFuture = declarativeSlotPoolBridge.requestNewAllocatedSlot(new SlotRequestId(), ResourceProfile.UNKNOWN, Time.minutes(5)); + // when the allocation fails the requirements should be reduced (it is the users responsibility to retry) + allocationFuture.completeExceptionally(new TimeoutException()); Review comment: I think the fact that failing this future triggers the cleanup is an implementation detail which can change in the future. I would suggest to use a very small timeout so that the request fails fast. ---------------------------------------------------------------- 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