Skip to content

Commit

Permalink
[FLINK-19520][configuration] Add randomization of checkpoint config i…
Browse files Browse the repository at this point in the history
…n ITCases.
  • Loading branch information
Arvid Heise authored and AHeise committed Feb 10, 2021
1 parent 31346e8 commit 4fd2cec
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 47 deletions.
26 changes: 0 additions & 26 deletions flink-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -485,32 +485,6 @@ under the License.
<reuseForks>false</reuseForks>
</configuration>
</plugin>
<plugin>
<!-- Description: https://github.com/git-commit-id/git-commit-id-maven-plugin
Used to show the git ref when starting the jobManager. -->
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<executions>
<execution>
<id>get-the-git-infos</id>
<phase>validate</phase>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
<skipPoms>false</skipPoms>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
<gitDescribe>
<!-- Don't generate the describe property -->
<!-- It is useless due to the way Flink does branches and tags -->
<skip>true</skip>
</gitDescribe>
</configuration>
</plugin>
<!-- Add version to jar http://stackoverflow.com/questions/2712970/how-to-get-maven-artifact-version-at-runtime
-->
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.flink.runtime.util;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.configuration.GlobalConfiguration;
import org.apache.flink.util.OperatingSystem;

Expand All @@ -41,6 +42,8 @@
* startup options, or the JVM version.
*/
public class EnvironmentInformation {
@VisibleForTesting public static final String UNKNOWN_COMMIT_ID = "DecafC0ffeeD0d0F00d";
@VisibleForTesting public static final String UNKNOWN_COMMIT_ID_ABBREV = "DeadD0d0";

private static final Logger LOG = LoggerFactory.getLogger(EnvironmentInformation.class);

Expand Down Expand Up @@ -113,8 +116,6 @@ public static RevisionInformation getRevisionInformation() {
private static final class Versions {
private static final Instant DEFAULT_TIME_INSTANT = Instant.EPOCH;
private static final String DEFAULT_TIME_STRING = "1970-01-01T00:00:00+0000";
private static final String UNKNOWN_COMMIT_ID = "DecafC0ffeeD0d0F00d";
private static final String UNKNOWN_COMMIT_ID_ABBREV = "DeadD0d0";
private String projectVersion = UNKNOWN;
private String scalaVersion = UNKNOWN;
private Instant gitBuildTime = DEFAULT_TIME_INSTANT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void failed(Throwable e, Description description) {
}
};

@Rule public final TestRule nameProvider = new TestNameProvider();

private static String exceptionToString(Throwable t) {
if (t == null) {
return "(null)";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.util;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import javax.annotation.Nullable;

/**
* A rule that provides the current test name per thread. Currently, the test name is available for
* all tests that extend {@link TestLogger}.
*/
public class TestNameProvider implements TestRule {
private static ThreadLocal<String> testName = new ThreadLocal<>();

@Nullable
public static String getCurrentTestName() {
return testName.get();
}

@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
testName.set(description.getDisplayName());
try {
base.evaluate();
} finally {
testName.set(null);
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* 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.streaming.util;

import org.apache.flink.annotation.Internal;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.util.EnvironmentInformation;

import net.jcip.annotations.NotThreadSafe;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Optional;
import java.util.Random;
import java.util.function.Function;

/**
* Initializes the {@link Configuration} for particular {@link ConfigOption}s with random values if
* unset.
*
* <p>With the same seed, the same values are always selected if the {@link #select(Configuration,
* ConfigOption, Object[])} invocation happens in the same order. A different seed should select
* different values.
*
* <p>The seed is calculated from a global seed (~unique per build) and a seed specific to test
* cases. Thus, two different builds will mostly result in different values for the same test case.
* Similarly, two test cases in the same build will have different randomized values.
*
* <p>The seed can be set with the maven/system property test.randomization.seed and is set by
* default to commit id. If the seed is empty, {@link EnvironmentInformation} and as a last fallback
* git command is used to retrieve the commit id.
*/
@Internal
@NotThreadSafe
class PseudoRandomValueSelector {
private static final Logger LOG = LoggerFactory.getLogger(PseudoRandomValueSelector.class);

private final Function<Integer, Integer> randomValueSupplier;

private static final long GLOBAL_SEED = (long) getGlobalSeed().hashCode() << 32;

private PseudoRandomValueSelector(Function<Integer, Integer> randomValueSupplier) {
this.randomValueSupplier = randomValueSupplier;
}

public <T> void select(Configuration configuration, ConfigOption<T> option, T... alternatives) {
if (configuration.contains(option)) {
return;
}
final int choice = randomValueSupplier.apply(alternatives.length);
T value = alternatives[choice];
LOG.info("Randomly selected {} for {}", value, option.key());
configuration.set(option, value);
}

public static PseudoRandomValueSelector create(Object entryPointSeed) {
final long combinedSeed = GLOBAL_SEED | entryPointSeed.hashCode();
final Random random = new Random(combinedSeed);
return new PseudoRandomValueSelector(random::nextInt);
}

private static String getGlobalSeed() {
// manual seed or set by maven
final String seed = System.getProperty("test.randomization.seed");
if (seed != null) {
return seed;
}

// Read with git command (if installed)
final Optional<String> gitCommitId = getGitCommitId();
if (gitCommitId.isPresent()) {
return gitCommitId.get();
}

// try EnvironmentInformation, which is set in the maven process
final String commitId = EnvironmentInformation.getGitCommitId();
if (!commitId.equals(EnvironmentInformation.UNKNOWN_COMMIT_ID)) {
return commitId;
}

LOG.warn(
"Test randomization was enabled but neither test.randomization.seed was configured nor could the commit hash be retrieved from git or the EnvironmentInformation. Please set the test.randomization.seed property manually to make the build reproducible.");
// return any constant
return "";
}

@VisibleForTesting
static Optional<String> getGitCommitId() {
try {
Process process = new ProcessBuilder("git", "rev-parse", "HEAD").start();
try (InputStream input = process.getInputStream()) {
final String commit = IOUtils.toString(input, Charset.defaultCharset()).trim();
if (commit.matches("[a-f0-9]{40}")) {
return Optional.of(commit);
}
LOG.debug("Cannot parse {}", commit);
}
} catch (IOException e) {
LOG.debug("Could not invoke git", e);
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@

package org.apache.flink.streaming.util;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.minicluster.MiniCluster;
import org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironmentFactory;
import org.apache.flink.test.util.MiniClusterPipelineExecutorServiceLoader;
import org.apache.flink.util.TestNameProvider;

import java.net.URL;
import java.util.Collection;
import java.util.Collections;

/** A {@link StreamExecutionEnvironment} that executes its jobs on {@link MiniCluster}. */
public class TestStreamEnvironment extends StreamExecutionEnvironment {
private static final boolean RANDOMIZE_CHECKPOINTING_CONFIG =
Boolean.parseBoolean(System.getProperty("checkpointing.randomization", "false"));

public TestStreamEnvironment(
MiniCluster miniCluster,
Expand Down Expand Up @@ -69,13 +74,30 @@ public static void setAsContext(
TestStreamEnvironment env =
new TestStreamEnvironment(
miniCluster, parallelism, jarFiles, classpaths);
randomize(conf);
env.configure(conf, env.getUserClassloader());
return env;
};

initializeContextEnvironment(factory);
}

/**
* Randomizes configuration on test case level even if mini cluster is used in a class rule.
*
* <p>Note that only unset properties are randomized.
*
* @param conf the configuration to randomize
*/
private static void randomize(Configuration conf) {
if (RANDOMIZE_CHECKPOINTING_CONFIG) {
final String testName = TestNameProvider.getCurrentTestName();
final PseudoRandomValueSelector valueSelector =
PseudoRandomValueSelector.create(testName != null ? testName : "unknown");
valueSelector.select(conf, ExecutionCheckpointingOptions.ENABLE_UNALIGNED, true, false);
}
}

/**
* Sets the streaming context environment to a TestStreamEnvironment that runs its programs on
* the given cluster with the given default parallelism.
Expand Down
Loading

0 comments on commit 4fd2cec

Please sign in to comment.