zentol commented on a change in pull request #14853: URL: https://github.com/apache/flink/pull/14853#discussion_r570095199
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/jobmaster/slotpool/DeclarativeSlotPoolServiceTest.java ########## @@ -0,0 +1,334 @@ +/* + * 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.JobID; +import org.apache.flink.api.common.time.Time; +import org.apache.flink.runtime.clusterframework.types.AllocationID; +import org.apache.flink.runtime.clusterframework.types.ResourceID; +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.instance.SimpleSlotContext; +import org.apache.flink.runtime.jobmaster.AllocatedSlotInfo; +import org.apache.flink.runtime.jobmaster.AllocatedSlotReport; +import org.apache.flink.runtime.jobmaster.JobMasterId; +import org.apache.flink.runtime.jobmaster.RpcTaskManagerGateway; +import org.apache.flink.runtime.jobmaster.SlotContext; +import org.apache.flink.runtime.jobmaster.SlotInfo; +import org.apache.flink.runtime.messages.Acknowledge; +import org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway; +import org.apache.flink.runtime.slots.ResourceRequirement; +import org.apache.flink.runtime.slots.ResourceRequirements; +import org.apache.flink.runtime.taskexecutor.TestingTaskExecutorGatewayBuilder; +import org.apache.flink.runtime.taskexecutor.slot.SlotOffer; +import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation; +import org.apache.flink.util.FlinkException; +import org.apache.flink.util.TestLogger; +import org.apache.flink.util.clock.SystemClock; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Test; + +import javax.annotation.Nonnull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicReference; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** Tests for the {@link DeclarativeSlotPoolService}. */ +public class DeclarativeSlotPoolServiceTest extends TestLogger { + + private static final JobID jobId = new JobID(); + private static final JobMasterId jobMasterId = JobMasterId.generate(); + private final ComponentMainThreadExecutor mainThreadExecutor = + ComponentMainThreadExecutorServiceAdapter.forMainThread(); + private static final String address = "localhost"; + + @Test + public void testUnknownTaskManagerRegistration() throws Exception { + try (DeclarativeSlotPoolService declarativeSlotPoolService = + createDeclarativeSlotPoolService()) { + final ResourceID unknownTaskManager = ResourceID.generate(); + + assertFalse( + declarativeSlotPoolService.isTaskManagerRegistered( + unknownTaskManager.getResourceID())); + } + } + + @Test + public void testKnownTaskManagerRegistration() throws Exception { + try (DeclarativeSlotPoolService declarativeSlotPoolService = + createDeclarativeSlotPoolService()) { + final ResourceID knownTaskManager = ResourceID.generate(); + declarativeSlotPoolService.registerTaskManager(knownTaskManager); + + assertTrue( + declarativeSlotPoolService.isTaskManagerRegistered( + knownTaskManager.getResourceID())); + } + } + + @Test + public void testReleaseTaskManager() throws Exception { + try (DeclarativeSlotPoolService declarativeSlotPoolService = + createDeclarativeSlotPoolService()) { + final ResourceID knownTaskManager = ResourceID.generate(); + declarativeSlotPoolService.registerTaskManager(knownTaskManager); + declarativeSlotPoolService.releaseTaskManager( + knownTaskManager, new FlinkException("Test cause")); + + assertFalse( + declarativeSlotPoolService.isTaskManagerRegistered( + knownTaskManager.getResourceID())); + } + } + + @Test + public void testSlotOfferingOfUnknownTaskManagerIsIgnored() throws Exception { + try (DeclarativeSlotPoolService declarativeSlotPoolService = + createDeclarativeSlotPoolService()) { + final Collection<SlotOffer> slotOffers = + Collections.singletonList( + new SlotOffer(new AllocationID(), 0, ResourceProfile.ANY)); + + final LocalTaskManagerLocation taskManagerLocation = new LocalTaskManagerLocation(); + + final Collection<SlotOffer> acceptedSlots = + declarativeSlotPoolService.offerSlots( + taskManagerLocation, + new RpcTaskManagerGateway( + new TestingTaskExecutorGatewayBuilder() + .createTestingTaskExecutorGateway(), + jobMasterId), + slotOffers); + + assertThat(acceptedSlots, is(empty())); + } + } + + @Test + public void testSlotOfferingOfKnownTaskManager() throws Exception { + final AtomicReference<Collection<? extends SlotOffer>> receivedSlotOffers = + new AtomicReference<>(); + try (DeclarativeSlotPoolService declarativeSlotPoolService = + createDeclarativeSlotPoolService( + new TestingDeclarativeSlotPoolFactory( + new TestingDeclarativeSlotPoolBuilder() + .setOfferSlotsFunction( + (slotOffers, + taskManagerLocation, + taskManagerGateway, + aLong) -> { + receivedSlotOffers.set(slotOffers); + return new ArrayList<>(slotOffers); + })))) { + final LocalTaskManagerLocation taskManagerLocation = new LocalTaskManagerLocation(); + + declarativeSlotPoolService.registerTaskManager(taskManagerLocation.getResourceID()); + + final Collection<SlotOffer> slotOffers = + Collections.singletonList( + new SlotOffer(new AllocationID(), 0, ResourceProfile.ANY)); + + declarativeSlotPoolService.offerSlots( + taskManagerLocation, + new RpcTaskManagerGateway( + new TestingTaskExecutorGatewayBuilder() + .createTestingTaskExecutorGateway(), + jobMasterId), + slotOffers); + + assertThat(receivedSlotOffers.get(), is(slotOffers)); + } + } + + @Test + public void testConnectToResourceManagerDeclaresRequiredResources() throws Exception { + final Collection<ResourceRequirement> requiredResources = + Arrays.asList( + ResourceRequirement.create(ResourceProfile.ANY, 2), Review comment: use `UNKNOWN` instead. `ANY` should only be used for the resource profile of a slot. This isn't really about the correctness of this test in particular but more about not spreading code that would in other places lead to subtle issues. ---------------------------------------------------------------- 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]
