zentol commented on a change in pull request #14921: URL: https://github.com/apache/flink/pull/14921#discussion_r577558648
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/declarative/DeclarativeSchedulerTest.java ########## @@ -0,0 +1,326 @@ +/* + * 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.declarative; + +import org.apache.flink.api.common.JobStatus; +import org.apache.flink.api.common.time.Time; +import org.apache.flink.runtime.clusterframework.types.ResourceProfile; +import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor; +import org.apache.flink.runtime.concurrent.ManuallyTriggeredComponentMainThreadExecutor; +import org.apache.flink.runtime.execution.SuppressRestartsException; +import org.apache.flink.runtime.executiongraph.ArchivedExecutionGraph; +import org.apache.flink.runtime.executiongraph.ExecutionGraph; +import org.apache.flink.runtime.executiongraph.failover.flip1.NoRestartBackoffTimeStrategy; +import org.apache.flink.runtime.executiongraph.failover.flip1.TestRestartBackoffTimeStrategy; +import org.apache.flink.runtime.jobgraph.JobGraph; +import org.apache.flink.runtime.jobgraph.JobType; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.jobgraph.tasks.AbstractInvokable; +import org.apache.flink.runtime.jobmaster.slotpool.DefaultAllocatedSlotPool; +import org.apache.flink.runtime.jobmaster.slotpool.DefaultDeclarativeSlotPool; +import org.apache.flink.runtime.jobmaster.slotpool.ResourceCounter; +import org.apache.flink.runtime.rest.handler.legacy.utils.ArchivedExecutionGraphBuilder; +import org.apache.flink.runtime.slots.ResourceRequirement; +import org.apache.flink.util.TestLogger; + +import org.junit.Test; +import org.slf4j.Logger; + +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import static org.apache.flink.runtime.jobmaster.slotpool.DefaultDeclarativeSlotPoolTest.createSlotOffersForResourceRequirements; +import static org.apache.flink.runtime.jobmaster.slotpool.DefaultDeclarativeSlotPoolTest.offerSlots; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +/** Tests for the {@link DeclarativeScheduler}. */ +public class DeclarativeSchedulerTest extends TestLogger { + + private static final int PARALLELISM = 4; + private static final JobVertex JOB_VERTEX; + + static { + JOB_VERTEX = new JobVertex("v1"); + JOB_VERTEX.setParallelism(PARALLELISM); + JOB_VERTEX.setInvokableClass(AbstractInvokable.class); + } + + private final ComponentMainThreadExecutor mainThreadExecutor = + new ManuallyTriggeredComponentMainThreadExecutor(Thread.currentThread()); + + @Test + public void testInitialState() throws Exception { + final DeclarativeScheduler scheduler = + new DeclarativeSchedulerBuilder(getJobGraph(), mainThreadExecutor).build(); + + assertThat(scheduler.getState(), instanceOf(Created.class)); + } + + @Test + public void testIsState() throws Exception { + final DeclarativeScheduler scheduler = + new DeclarativeSchedulerBuilder(getJobGraph(), mainThreadExecutor).build(); + + final State state = scheduler.getState(); + + assertThat(scheduler.isState(state), is(true)); + assertThat(scheduler.isState(new DummyState()), is(false)); + } + + @Test + public void testRunIfState() throws Exception { + final DeclarativeScheduler scheduler = + new DeclarativeSchedulerBuilder(getJobGraph(), mainThreadExecutor).build(); + + AtomicBoolean ran = new AtomicBoolean(false); + scheduler.runIfState(scheduler.getState(), () -> ran.set(true)); + assertThat(ran.get(), is(true)); + } + + @Test + public void testRunIfStateWithStateMismatch() throws Exception { + final DeclarativeScheduler scheduler = + new DeclarativeSchedulerBuilder(getJobGraph(), mainThreadExecutor).build(); + + AtomicBoolean ran = new AtomicBoolean(false); + scheduler.runIfState(new DummyState(), () -> ran.set(true)); + assertThat(ran.get(), is(false)); + } + + @Test + public void testHasEnoughResources() throws Exception { + final JobGraph jobGraph = getJobGraph(); + + final DefaultDeclarativeSlotPool declarativeSlotPool = + new DefaultDeclarativeSlotPool( + jobGraph.getJobID(), + new DefaultAllocatedSlotPool(), + ignored -> {}, + Time.minutes(10), + Time.minutes(10)); + + final DeclarativeScheduler scheduler = + new DeclarativeSchedulerBuilder(jobGraph, mainThreadExecutor) + .setDeclarativeSlotPool(declarativeSlotPool) + .build(); + + scheduler.startScheduling(); + + final ResourceCounter resourceRequirement = + ResourceCounter.withResource(ResourceProfile.UNKNOWN, 1); + + assertThat(scheduler.hasEnoughResources(resourceRequirement), is(false)); + + offerSlots( + declarativeSlotPool, createSlotOffersForResourceRequirements(resourceRequirement)); + + assertThat(scheduler.hasEnoughResources(resourceRequirement), is(true)); Review comment: > I think we should also test that non matched resources decrease UNKNOWN resources. ~~Isn't that slot pool behavior?~~ ---------------------------------------------------------------- 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