You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by pn...@apache.org on 2022/11/08 13:20:21 UTC
[flink] 03/03: [FLINK-29888][streaming] Validate CheckpointConfig#storage manualy
This is an automated email from the ASF dual-hosted git repository.
pnowojski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 488db2569147d1bb2fbefb95f351239f930dd0d0
Author: Piotr Nowojski <pi...@gmail.com>
AuthorDate: Mon Nov 7 13:24:43 2022 +0100
[FLINK-29888][streaming] Validate CheckpointConfig#storage manualy
Unfortunately, CheckpointConfig#setCheckpointStorage is not backed by a
Configuration, but it also has to be validated. For this validation we
are implementing a one off manual check
---
.../client/program/StreamContextEnvironment.java | 16 +++
.../program/StreamContextEnvironmentTest.java | 109 +++++++++++++++++----
.../dispatcher/ConfigurationNotAllowedMessage.java | 5 +
.../storage/ExternalizedSnapshotLocation.java | 19 ++++
.../state/storage/FileSystemCheckpointStorage.java | 20 ++++
5 files changed, 150 insertions(+), 19 deletions(-)
diff --git a/flink-clients/src/main/java/org/apache/flink/client/program/StreamContextEnvironment.java b/flink-clients/src/main/java/org/apache/flink/client/program/StreamContextEnvironment.java
index 15f7022086a..efe5ada5912 100644
--- a/flink-clients/src/main/java/org/apache/flink/client/program/StreamContextEnvironment.java
+++ b/flink-clients/src/main/java/org/apache/flink/client/program/StreamContextEnvironment.java
@@ -22,6 +22,7 @@ import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.JobExecutionResult;
import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.configuration.CheckpointingOptions;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.core.execution.DetachedJobExecutionResult;
@@ -47,6 +48,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
+import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -294,6 +296,20 @@ public class StreamContextEnvironment extends StreamExecutionEnvironment {
checkpointCfg.toConfiguration(),
checkpointCfg.getClass().getSimpleName(),
errors);
+
+ /**
+ * Unfortunately, {@link CheckpointConfig#setCheckpointStorage} is not backed by a {@link
+ * Configuration}, but it also has to be validated. For this validation we are implementing
+ * a one off manual check.
+ */
+ if (!programConfigWildcards.contains(CheckpointingOptions.CHECKPOINTS_DIRECTORY.key())
+ && !Objects.equals(
+ checkpointCfg.getCheckpointStorage(),
+ expectedCheckpointConfig.getCheckpointStorage())) {
+ errors.add(
+ ConfigurationNotAllowedMessage.ofConfigurationObjectSetterUsed(
+ checkpointCfg.getClass().getSimpleName(), "setCheckpointStorage"));
+ }
}
private void checkExecutionConfig(Configuration clusterConfigMap, List<String> errors) {
diff --git a/flink-clients/src/test/java/org/apache/flink/client/program/StreamContextEnvironmentTest.java b/flink-clients/src/test/java/org/apache/flink/client/program/StreamContextEnvironmentTest.java
index f9432e048be..0d1b29c6358 100644
--- a/flink-clients/src/test/java/org/apache/flink/client/program/StreamContextEnvironmentTest.java
+++ b/flink-clients/src/test/java/org/apache/flink/client/program/StreamContextEnvironmentTest.java
@@ -20,6 +20,7 @@ package org.apache.flink.client.program;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.RuntimeExecutionMode;
+import org.apache.flink.configuration.CheckpointingOptions;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.DeploymentOptions;
import org.apache.flink.configuration.ExecutionOptions;
@@ -27,6 +28,7 @@ import org.apache.flink.configuration.PipelineOptions;
import org.apache.flink.core.execution.PipelineExecutorFactory;
import org.apache.flink.core.execution.PipelineExecutorServiceLoader;
import org.apache.flink.runtime.jobgraph.SavepointConfigOptions;
+import org.apache.flink.runtime.state.storage.JobManagerCheckpointStorage;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
@@ -37,8 +39,8 @@ import org.apache.flink.util.function.ThrowingConsumer;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
-import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
@@ -64,17 +66,8 @@ class StreamContextEnvironmentTest {
programConfig.set(ExecutionOptions.RUNTIME_MODE, RuntimeExecutionMode.BATCH);
programConfig.set(ExecutionOptions.SORT_INPUTS, true);
- final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final StreamContextEnvironment environment =
- new StreamContextEnvironment(
- new MockExecutorServiceLoader(),
- clusterConfig,
- clusterConfig,
- classLoader,
- true,
- true,
- false,
- new ArrayList<>());
+ constructStreamContextEnvironment(clusterConfig, Collections.emptyList());
// Change the CheckpointConfig
environment.enableCheckpointing(500, CheckpointingMode.EXACTLY_ONCE);
@@ -97,6 +90,77 @@ class StreamContextEnvironmentTest {
PipelineOptions.MAX_PARALLELISM.key());
}
+ @ParameterizedTest
+ @MethodSource("provideExecutors")
+ void testDisallowCheckpointStorage(
+ ThrowingConsumer<StreamExecutionEnvironment, Exception> executor) {
+ final Configuration clusterConfig = new Configuration();
+ clusterConfig.set(DeploymentOptions.PROGRAM_CONFIG_ENABLED, false);
+ clusterConfig.set(DeploymentOptions.TARGET, "local");
+ clusterConfig.set(CheckpointingOptions.CHECKPOINTS_DIRECTORY, "file:///flink/checkpoints");
+
+ final StreamContextEnvironment environment =
+ constructStreamContextEnvironment(clusterConfig, Collections.emptyList());
+
+ String disallowedPath = "file:///flink/disallowed/modification";
+ // Change the CheckpointConfig
+ environment.getCheckpointConfig().setCheckpointStorage(disallowedPath);
+
+ environment.fromCollection(Collections.singleton(1)).addSink(new DiscardingSink<>());
+ assertThatThrownBy(() -> executor.accept(environment))
+ .isInstanceOf(MutatedConfigurationException.class)
+ .hasMessageContainingAll(
+ CheckpointConfig.class.getSimpleName(), "setCheckpointStorage");
+
+ environment.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());
+
+ environment.fromCollection(Collections.singleton(1)).addSink(new DiscardingSink<>());
+ assertThatThrownBy(() -> executor.accept(environment))
+ .isInstanceOf(MutatedConfigurationException.class)
+ .hasMessageContainingAll(
+ CheckpointConfig.class.getSimpleName(), "setCheckpointStorage");
+ }
+
+ @ParameterizedTest
+ @MethodSource("provideExecutors")
+ void testAllowCheckpointStorage(
+ ThrowingConsumer<StreamExecutionEnvironment, Exception> executor) {
+ final Configuration clusterConfig = new Configuration();
+ clusterConfig.set(DeploymentOptions.PROGRAM_CONFIG_ENABLED, false);
+ clusterConfig.set(DeploymentOptions.TARGET, "local");
+ clusterConfig.set(CheckpointingOptions.CHECKPOINTS_DIRECTORY, "file:///flink/checkpoints");
+
+ final StreamContextEnvironment environment =
+ constructStreamContextEnvironment(
+ clusterConfig,
+ Arrays.asList(CheckpointingOptions.CHECKPOINTS_DIRECTORY.key()));
+
+ String allowedPath = "file:///flink/allowed/modification";
+ // Change the CheckpointConfig
+ environment.getCheckpointConfig().setCheckpointStorage(allowedPath);
+
+ environment.fromCollection(Collections.singleton(1)).addSink(new DiscardingSink<>());
+ assertThatThrownBy(() -> executor.accept(environment))
+ .isInstanceOf(ExecutorReachedException.class);
+ }
+
+ @ParameterizedTest
+ @MethodSource("provideExecutors")
+ void testNotModifiedCheckpointStorage(
+ ThrowingConsumer<StreamExecutionEnvironment, Exception> executor) {
+ final Configuration clusterConfig = new Configuration();
+ clusterConfig.set(DeploymentOptions.PROGRAM_CONFIG_ENABLED, false);
+ clusterConfig.set(DeploymentOptions.TARGET, "local");
+ clusterConfig.set(CheckpointingOptions.CHECKPOINTS_DIRECTORY, "file:///flink/checkpoints");
+
+ final StreamContextEnvironment environment =
+ constructStreamContextEnvironment(clusterConfig, Collections.emptyList());
+
+ environment.fromCollection(Collections.singleton(1)).addSink(new DiscardingSink<>());
+ assertThatThrownBy(() -> executor.accept(environment))
+ .isInstanceOf(ExecutorReachedException.class);
+ }
+
@ParameterizedTest
@MethodSource("provideExecutors")
void testAllowProgramConfigurationWildcards(
@@ -114,16 +178,9 @@ class StreamContextEnvironmentTest {
PipelineOptions.GLOBAL_JOB_PARAMETERS,
Collections.singletonMap("my-other-param", "my-other-value"));
- final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final StreamContextEnvironment environment =
- new StreamContextEnvironment(
- new MockExecutorServiceLoader(),
- clusterConfig,
+ constructStreamContextEnvironment(
clusterConfig,
- classLoader,
- true,
- true,
- false,
Arrays.asList(
PipelineOptions.GLOBAL_JOB_PARAMETERS.key(),
PipelineOptions.MAX_PARALLELISM.key()));
@@ -139,6 +196,20 @@ class StreamContextEnvironmentTest {
.containsOnlyKeys("my-other-param");
}
+ private static StreamContextEnvironment constructStreamContextEnvironment(
+ Configuration clusterConfig, Collection<String> programConfigWildcards) {
+ final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ return new StreamContextEnvironment(
+ new MockExecutorServiceLoader(),
+ clusterConfig,
+ clusterConfig,
+ classLoader,
+ true,
+ true,
+ false,
+ programConfigWildcards);
+ }
+
private static List<ThrowingConsumer<StreamExecutionEnvironment, Exception>>
provideExecutors() {
return Arrays.asList(
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/ConfigurationNotAllowedMessage.java b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/ConfigurationNotAllowedMessage.java
index 23299bc059e..0d5e36a16f7 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/ConfigurationNotAllowedMessage.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/dispatcher/ConfigurationNotAllowedMessage.java
@@ -66,4 +66,9 @@ public class ConfigurationNotAllowedMessage {
"Configuration %s:%s was removed from the configuration object %s.",
configKey, configValue, configurationObject);
}
+
+ public static String ofConfigurationObjectSetterUsed(
+ String configurationObject, String setter) {
+ return String.format("Setter %s#%s has been used", configurationObject, setter);
+ }
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/ExternalizedSnapshotLocation.java b/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/ExternalizedSnapshotLocation.java
index 1dcabd78dec..ddb1c1fc097 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/ExternalizedSnapshotLocation.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/ExternalizedSnapshotLocation.java
@@ -28,6 +28,7 @@ import org.apache.flink.core.fs.Path;
import javax.annotation.Nullable;
import java.io.Serializable;
+import java.util.Objects;
import java.util.Optional;
/**
@@ -154,4 +155,22 @@ class ExternalizedSnapshotLocation implements Serializable {
}
});
}
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(baseCheckpointPath, baseSavepointPath);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) {
+ return true;
+ }
+ if (other == null || getClass() != other.getClass()) {
+ return false;
+ }
+ ExternalizedSnapshotLocation that = (ExternalizedSnapshotLocation) other;
+ return Objects.equals(baseCheckpointPath, that.baseCheckpointPath)
+ && Objects.equals(baseSavepointPath, that.baseSavepointPath);
+ }
}
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/FileSystemCheckpointStorage.java b/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/FileSystemCheckpointStorage.java
index 4f0e0246d42..486744c11cb 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/FileSystemCheckpointStorage.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/state/storage/FileSystemCheckpointStorage.java
@@ -40,6 +40,7 @@ import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URI;
+import java.util.Objects;
import static org.apache.flink.configuration.CheckpointingOptions.FS_SMALL_FILE_THRESHOLD;
import static org.apache.flink.util.Preconditions.checkArgument;
@@ -371,4 +372,23 @@ public class FileSystemCheckpointStorage
? writeBufferSize
: CheckpointingOptions.FS_WRITE_BUFFER_SIZE.defaultValue();
}
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(location, fileStateThreshold, writeBufferSize);
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (this == other) {
+ return true;
+ }
+ if (other == null || getClass() != other.getClass()) {
+ return false;
+ }
+ FileSystemCheckpointStorage that = (FileSystemCheckpointStorage) other;
+ return Objects.equals(location, that.location)
+ && Objects.equals(fileStateThreshold, that.fileStateThreshold)
+ && Objects.equals(writeBufferSize, that.writeBufferSize);
+ }
}