lindong28 commented on code in PR #22670: URL: https://github.com/apache/flink/pull/22670#discussion_r1245079165
########## flink-tests/src/test/java/org/apache/flink/test/checkpointing/ImmediateCheckpointingAfterAllTasksFinishedITCase.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.test.checkpointing; + +import org.apache.flink.api.common.restartstrategy.RestartStrategies; +import org.apache.flink.api.common.typeinfo.Types; +import org.apache.flink.api.connector.sink2.Sink; +import org.apache.flink.api.connector.sink2.SinkWriter; +import org.apache.flink.core.execution.SavepointFormatType; +import org.apache.flink.runtime.jobgraph.JobGraph; +import org.apache.flink.runtime.jobgraph.JobVertex; +import org.apache.flink.runtime.jobgraph.SavepointRestoreSettings; +import org.apache.flink.runtime.minicluster.MiniCluster; +import org.apache.flink.runtime.minicluster.MiniClusterConfiguration; +import org.apache.flink.runtime.state.StateSnapshotContext; +import org.apache.flink.runtime.testutils.CommonTestUtils; +import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; +import org.apache.flink.streaming.api.functions.source.RichSourceFunction; +import org.apache.flink.streaming.api.graph.StreamGraph; +import org.apache.flink.streaming.api.operators.AbstractStreamOperator; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.test.util.AbstractTestBase; + +import org.junit.Assert; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +/** + * Tests an immediate checkpoint should be triggered right after all tasks reached the end of data. + */ +public class ImmediateCheckpointingAfterAllTasksFinishedITCase extends AbstractTestBase { + private static final int SMALL_SOURCE_NUM_RECORDS = 20; + private static final int BIG_SOURCE_NUM_RECORDS = 100; + + private StreamExecutionEnvironment env; + + @TempDir private java.nio.file.Path tmpDir; + + @BeforeEach + public void setUp() { + env = StreamExecutionEnvironment.getExecutionEnvironment(); + env.setRestartStrategy(RestartStrategies.noRestart()); + env.enableCheckpointing(Long.MAX_VALUE - 1); + } + + @ParameterizedTest + @ValueSource(ints = {1, 4}) + public void testImmediateCheckpointing(int parallelism) throws Exception { + env.setParallelism(parallelism); + StreamGraph streamGraph = getStreamGraph(env); + env.execute(streamGraph); + } + + @ParameterizedTest + @ValueSource(ints = {1, 4}) + public void testRestoreAfterSomeTasksFinished(int parallelism) throws Exception { + env.setParallelism(parallelism); + + final MiniClusterConfiguration cfg = + new MiniClusterConfiguration.Builder() + .withRandomPorts() + .setNumTaskManagers(1) + .setNumSlotsPerTaskManager(4) + .build(); + try (MiniCluster miniCluster = new MiniCluster(cfg)) { + miniCluster.start(); + + env.enableCheckpointing(100); + JobGraph jobGraph = getStreamGraph(env).getJobGraph(); + miniCluster.submitJob(jobGraph).get(); + + CommonTestUtils.waitForSubtasksToFinish( + miniCluster, + jobGraph.getJobID(), + findVertexByName(jobGraph, "Source: smallSource").getID(), + false); + + String savepointPath = + miniCluster + .triggerSavepoint( Review Comment: While it is likely that we can eventually catch error given that this test runs every day, it is still preferred to avoid having flaky test if possible. Also note that It is preferred not to use sleep in CI. Though it might seem OK to sleep for 1 sec in this test, the overall CI time can be several times higher than necessary if every test does this. Overall, I feel the benefit of reducing test execution time and making the test less flaky is probably worth the one-time development cost. Note that many existing tests use synchronization primitives (e.g. CountDownLatch) instead of sleep for cases like this. Maybe you can checkout the code https://github.com/apache/flink/blob/master/flink-tests/src/test/java/org/apache/flink/test/checkpointing/RescalingITCase.java#L322 for example. -- 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]
