akalash commented on code in PR #28999:
URL: https://github.com/apache/flink/pull/28999#discussion_r4721446823


##########
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/adaptive/MockRestartingContext.java:
##########


Review Comment:
   Fixed in cf71403 and 1c94416. The validator now checks the target 
parallelism passed to `goToWaitingForResources` and that the execution graph is 
not null.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/adaptive/AdaptiveScheduler.java:
##########
@@ -1269,16 +1285,21 @@ public ArchivedExecutionGraph getArchivedExecutionGraph(
     }
 
     @Override
-    public void goToWaitingForResources(@Nullable ExecutionGraph 
previousExecutionGraph) {
+    public void goToWaitingForResources(
+            @Nullable ExecutionGraph previousExecutionGraph,
+            @Nullable VertexParallelism restartWithParallelism) {

Review Comment:
   Fixed in cf71403. `goToWaitingForResources` now takes 
`targetVertexParallelism`; `restartWithParallelism` remains only where the 
restart context is actually meant (`Restarting`, `goToRestarting`).



##########
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/adaptive/AdaptiveSchedulerRescaleRestartTimingTest.java:
##########
@@ -0,0 +1,193 @@
+/*
+ * 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.adaptive;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.JobManagerOptions;
+import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
+import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobmaster.slotpool.DefaultAllocatedSlotPool;
+import org.apache.flink.runtime.jobmaster.slotpool.DefaultDeclarativeSlotPool;
+import 
org.apache.flink.runtime.scheduler.adaptive.AdaptiveSchedulerTest.SubmissionBufferingTaskManagerGateway;
+import org.apache.flink.runtime.scheduler.adaptive.allocator.VertexParallelism;
+import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
+import org.apache.flink.runtime.testutils.CommonTestUtils;
+import org.apache.flink.runtime.util.ResourceCounter;
+
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.Collections;
+
+import static 
org.apache.flink.runtime.jobgraph.JobGraphTestUtils.streamingJobGraph;
+import static 
org.apache.flink.runtime.jobmaster.slotpool.SlotPoolTestUtils.createSlotOffersForResourceRequirements;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Integration test proving that {@link WaitingForResources}, driven by the 
real {@link
+ * DefaultStateTransitionManager} (not a no-op test double), correctly gates a 
rescale-triggered
+ * restart on genuinely free slots reaching the pre-restart target, and falls 
back once the
+ * rescale resource-stabilization timeout elapses.
+ *
+ * <p>Kept in its own file, following the precedent set by {@link
+ * AdaptiveSchedulerFreeSlotVertexParallelismTest} and other companion test 
files extracted out of
+ * {@link AdaptiveSchedulerTest}.
+ */
+class AdaptiveSchedulerRescaleRestartTimingTest extends 
AdaptiveSchedulerTestBase {
+
+    private static final int RETRY_INTERVAL_MILLIS = 20;
+    private static final int RETRY_ATTEMPTS = 250;
+
+    @Test
+    void 
testWaitingForResourcesDoesNotTransitionUntilFreeSlotsReachRescaleTarget()
+            throws Exception {
+        final JobGraph jobGraph = createJobGraph();
+        final DefaultDeclarativeSlotPool declarativeSlotPool =
+                createDeclarativeSlotPool(jobGraph.getJobID(), 
singleThreadMainThreadExecutor);
+
+        // long enough that the stabilization timeout cannot fire during this 
test.
+        scheduler =
+                prepareScheduler(jobGraph, declarativeSlotPool, 
Duration.ofSeconds(10)).build();
+
+        final SubmissionBufferingTaskManagerGateway taskManagerGateway =
+                new SubmissionBufferingTaskManagerGateway(2);
+
+        // go straight to the restart-triggered WaitingForResources from the 
initial Created
+        // state, the same way Restarting#goToSubsequentState does - never 
through the plain
+        // submission path (startScheduling()), which would race a second, 
unrelated
+        // WaitingForResources transition using the submission timeout config.
+        final VertexParallelism restartTarget = vertexParallelism(2);
+        runInMainThread(
+                () ->
+                        scheduler.goToWaitingForResources(
+                                new StateTrackingMockExecutionGraph(), 
restartTarget));
+
+        
assertThat(scheduler.getState()).isInstanceOf(WaitingForResources.class);
+
+        offerSlots(declarativeSlotPool, taskManagerGateway, 1);
+
+        // only 1 of the 2 targeted slots is free: must not have shortcut to
+        // CreatingExecutionGraph yet, even though 1 slot is already 
"sufficient" to run the job
+        // at a lower parallelism.
+        Thread.sleep(300);
+        
assertThat(scheduler.getState()).isInstanceOf(WaitingForResources.class);
+
+        offerSlots(declarativeSlotPool, taskManagerGateway, 1);
+
+        CommonTestUtils.waitUntilCondition(
+                () -> scheduler.getState() instanceof CreatingExecutionGraph,
+                RETRY_INTERVAL_MILLIS,
+                RETRY_ATTEMPTS);
+    }
+
+    @Test
+    void 
testWaitingForResourcesFallsBackAfterRescaleResourceStabilizationTimeoutElapses()
+            throws Exception {
+        final JobGraph jobGraph = createJobGraph();
+        final DefaultDeclarativeSlotPool declarativeSlotPool =
+                createDeclarativeSlotPool(jobGraph.getJobID(), 
singleThreadMainThreadExecutor);
+
+        scheduler =
+                prepareScheduler(jobGraph, declarativeSlotPool, 
Duration.ofMillis(300)).build();

Review Comment:
   Fixed in cf71403. The 300 ms is now explained in a comment: short enough to 
keep the test fast, but longer than the `offerSlots()` call so only the timeout 
can trigger the fallback.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/adaptive/MockRestartingContext.java:
##########
@@ -44,31 +45,37 @@ class MockRestartingContext extends 
MockStateWithExecutionGraphContext
     private final StateValidator<ExecutingTest.CancellingArguments> 
cancellingStateValidator =
             new StateValidator<>("Cancelling");
 
-    private final StateValidator<ExecutionGraph> 
waitingForResourcesStateValidator =
+    private final StateValidator<WaitingForResourcesArguments> 
waitingForResourcesStateValidator =
             new StateValidator<>("WaitingForResources");
 
     private final StateValidator<ExecutionGraph> 
creatingExecutionGraphStateValidator =
             new StateValidator<>("CreatingExecutionGraph");
 
-    @Nullable private VertexParallelism availableVertexParallelism;
+    @Nullable private VertexParallelism freeSlotVertexParallelism;
 
     private boolean hasDesiredResources = false;
 
     public void 
setExpectCancelling(Consumer<ExecutingTest.CancellingArguments> asserter) {
         cancellingStateValidator.expectInput(asserter);
     }
 
-    public void setExpectWaitingForResources() {
-        waitingForResourcesStateValidator.expectInput(assertNonNull());
+    public void setExpectWaitingForResources(
+            @Nullable VertexParallelism expectedTargetVertexParallelism) {
+        waitingForResourcesStateValidator.expectInput(
+                arguments -> {
+                    assertNonNull().accept(arguments);

Review Comment:
   Fixed in 1c94416. The validator asserts that `arguments.getExecutionGraph()` 
is not null again.



##########
flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/adaptive/WaitingForResources.java:
##########
@@ -143,6 +162,29 @@ public ScheduledFuture<?> scheduleOperation(Runnable 
callback, Duration delay) {
         return context.runIfState(this, callback, delay);
     }
 
+    private boolean isFreeSlotVertexParallelismAtLeast(VertexParallelism 
target) {

Review Comment:
   Fixed in 1c94416. Renamed to `hasFreeSlotsFor`.



##########
flink-runtime/src/test/java/org/apache/flink/runtime/scheduler/adaptive/AdaptiveSchedulerRescaleRestartTimingTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.adaptive;
+
+import org.apache.flink.api.common.JobID;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.JobManagerOptions;
+import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
+import org.apache.flink.runtime.concurrent.ComponentMainThreadExecutor;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobmaster.slotpool.DefaultAllocatedSlotPool;
+import org.apache.flink.runtime.jobmaster.slotpool.DefaultDeclarativeSlotPool;
+import 
org.apache.flink.runtime.scheduler.adaptive.AdaptiveSchedulerTest.SubmissionBufferingTaskManagerGateway;
+import org.apache.flink.runtime.scheduler.adaptive.allocator.VertexParallelism;
+import org.apache.flink.runtime.taskmanager.LocalTaskManagerLocation;
+import org.apache.flink.runtime.testutils.CommonTestUtils;
+import org.apache.flink.runtime.util.ResourceCounter;
+
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.Collections;
+
+import static 
org.apache.flink.runtime.jobgraph.JobGraphTestUtils.streamingJobGraph;
+import static 
org.apache.flink.runtime.jobmaster.slotpool.SlotPoolTestUtils.createSlotOffersForResourceRequirements;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Integration test proving that {@link WaitingForResources}, driven by the 
real {@link
+ * DefaultStateTransitionManager} (not a no-op test double), correctly gates a 
rescale-triggered
+ * restart on genuinely free slots reaching the pre-restart target, and falls 
back once the
+ * rescale resource-stabilization timeout elapses.
+ *
+ * <p>Kept in its own file, following the precedent set by {@link
+ * AdaptiveSchedulerFreeSlotVertexParallelismTest} and other companion test 
files extracted out of
+ * {@link AdaptiveSchedulerTest}.
+ */
+class AdaptiveSchedulerRescaleRestartTimingTest extends 
AdaptiveSchedulerTestBase {
+
+    private static final int RETRY_INTERVAL_MILLIS = 20;
+    private static final int RETRY_ATTEMPTS = 250;
+
+    @Test
+    void 
testWaitingForResourcesDoesNotTransitionUntilFreeSlotsReachRescaleTarget()
+            throws Exception {
+        final JobGraph jobGraph = createJobGraph();
+        final DefaultDeclarativeSlotPool declarativeSlotPool =
+                createDeclarativeSlotPool(jobGraph.getJobID(), 
singleThreadMainThreadExecutor);
+
+        // long enough that the stabilization timeout cannot fire during this 
test.
+        scheduler =
+                prepareScheduler(jobGraph, declarativeSlotPool, 
Duration.ofSeconds(10)).build();
+
+        final SubmissionBufferingTaskManagerGateway taskManagerGateway =
+                new SubmissionBufferingTaskManagerGateway(2);
+
+        // go straight to the restart-triggered WaitingForResources from the 
initial Created
+        // state, the same way Restarting#goToSubsequentState does - never 
through the plain
+        // submission path (startScheduling()), which would race a second, 
unrelated
+        // WaitingForResources transition using the submission timeout config.
+        final VertexParallelism restartTarget = vertexParallelism(2);
+        runInMainThread(
+                () ->
+                        scheduler.goToWaitingForResources(
+                                new StateTrackingMockExecutionGraph(), 
restartTarget));
+
+        
assertThat(scheduler.getState()).isInstanceOf(WaitingForResources.class);
+
+        offerSlots(declarativeSlotPool, taskManagerGateway, 1);
+
+        // only 1 of the 2 targeted slots is free: must not have shortcut to
+        // CreatingExecutionGraph, even though 1 slot is already "sufficient" 
to run the job at a
+        // lower parallelism. No sleep is needed here: offerSlots() runs 
synchronously on the main
+        // thread executor, and no stabilization work gets scheduled while 
desired resources
+        // (gated on the restart target) aren't met, so the state is already 
final by the time it
+        // returns.
+        
assertThat(scheduler.getState()).isInstanceOf(WaitingForResources.class);
+
+        offerSlots(declarativeSlotPool, taskManagerGateway, 1);
+
+        CommonTestUtils.waitUntilCondition(
+                () -> scheduler.getState() instanceof CreatingExecutionGraph,
+                RETRY_INTERVAL_MILLIS,
+                RETRY_ATTEMPTS);
+    }
+
+    @Test
+    void 
testWaitingForResourcesFallsBackAfterRescaleResourceStabilizationTimeoutElapses()
+            throws Exception {
+        final JobGraph jobGraph = createJobGraph();
+        final DefaultDeclarativeSlotPool declarativeSlotPool =
+                createDeclarativeSlotPool(jobGraph.getJobID(), 
singleThreadMainThreadExecutor);
+
+        // short enough to keep the test fast, but comfortably longer than the 
offerSlots() call
+        // below so the fallback can only be triggered by the timeout, not by 
a race with it.
+        final Duration rescaleResourceStabilizationTimeout = 
Duration.ofMillis(300);
+        scheduler =
+                prepareScheduler(jobGraph, declarativeSlotPool, 
rescaleResourceStabilizationTimeout)
+                        .build();
+
+        final int requiredParallelism = 2;
+        final SubmissionBufferingTaskManagerGateway taskManagerGateway =
+                new SubmissionBufferingTaskManagerGateway(requiredParallelism 
- 1);
+
+        // go straight to the restart-triggered WaitingForResources from the 
initial Created
+        // state, as in the test above - never through the plain submission 
path.
+        final VertexParallelism restartTarget = 
vertexParallelism(requiredParallelism);
+        runInMainThread(
+                () ->
+                        scheduler.goToWaitingForResources(
+                                new StateTrackingMockExecutionGraph(), 
restartTarget));
+
+        
assertThat(scheduler.getState()).isInstanceOf(WaitingForResources.class);
+
+        // only 1 of the 2 targeted slots is ever offered.
+        offerSlots(declarativeSlotPool, taskManagerGateway, 
requiredParallelism - 1);
+
+        // the target is never reached, but the stabilization timeout must 
still force the
+        // transition once it elapses, rather than waiting forever.
+        CommonTestUtils.waitUntilCondition(
+                () -> scheduler.getState() instanceof CreatingExecutionGraph,

Review Comment:
   Fixed in 1c94416. Both tests now wait for `!(scheduler.getState() instanceof 
WaitingForResources)`; the first test had the same race, and this is how the 
"Elapses" test failed on CI ("Exhausted retry attempts").



##########
flink-runtime/src/main/java/org/apache/flink/runtime/scheduler/adaptive/Restarting.java:
##########
@@ -218,7 +226,7 @@ public Restarting getState() {
                     operatorCoordinatorHandler,
                     log,
                     backoffTime,
-                    restartWithParallelism,
+                    targetVertexParallelism,

Review Comment:
   Fixed in 1c94416. `Restarting.Factory` now uses `restartWithParallelism` as 
well, so it is consistent with `Restarting` and `StateTransitions`.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to