This is an automated email from the ASF dual-hosted git repository. sxnan pushed a commit to branch config-2.0 in repository https://gitbox.apache.org/repos/asf/flink.git
commit 80e80e1e6d6eb57cba124cdbfd0f9ee61fbc85e7 Author: sxnan <[email protected]> AuthorDate: Mon Sep 9 22:02:36 2024 +0800 [FLINK-34079][config] Refactor callers that use get/setDouble --- .../flink/configuration/ConfigurationUtils.java | 4 ++ .../ConfigurationConversionsTest.java | 48 ++++++++++++++-------- .../flink/configuration/ConfigurationTest.java | 5 ++- .../configuration/DelegatingConfigurationTest.java | 8 ++-- .../flink/runtime/operators/util/TaskConfig.java | 22 +++++----- .../flink/runtime/jobgraph/JobGraphTest.java | 3 +- 6 files changed, 56 insertions(+), 34 deletions(-) diff --git a/flink-core/src/main/java/org/apache/flink/configuration/ConfigurationUtils.java b/flink-core/src/main/java/org/apache/flink/configuration/ConfigurationUtils.java index 0ff67aec095..253970fb9bb 100755 --- a/flink-core/src/main/java/org/apache/flink/configuration/ConfigurationUtils.java +++ b/flink-core/src/main/java/org/apache/flink/configuration/ConfigurationUtils.java @@ -695,4 +695,8 @@ public class ConfigurationUtils { public static ConfigOption<Boolean> getBooleanConfigOption(String key) { return ConfigOptions.key(key).booleanType().noDefaultValue(); } + + public static ConfigOption<Double> getDoubleConfigOption(String key) { + return ConfigOptions.key(key).doubleType().noDefaultValue(); + } } diff --git a/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationConversionsTest.java b/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationConversionsTest.java index ce2daff3bc3..c7d5a2a9404 100644 --- a/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationConversionsTest.java +++ b/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationConversionsTest.java @@ -35,6 +35,7 @@ import java.util.Objects; import java.util.Optional; import static org.apache.flink.configuration.ConfigurationUtils.getBooleanConfigOption; +import static org.apache.flink.configuration.ConfigurationUtils.getDoubleConfigOption; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -62,10 +63,10 @@ class ConfigurationConversionsTest { pc.setLong("long", 15); pc.setLong("too_long", TOO_LONG); pc.setFloat("float", 2.1456775f); - pc.setDouble("double", Math.PI); - pc.setDouble("negative_double", -1.0); - pc.setDouble("zero", 0.0); - pc.setDouble("too_long_double", TOO_LONG_DOUBLE); + pc.set(getDoubleConfigOption("double"), Math.PI); + pc.set(getDoubleConfigOption("negative_double"), -1.0); + pc.set(getDoubleConfigOption("zero"), 0.0); + pc.set(getDoubleConfigOption("too_long_double"), TOO_LONG_DOUBLE); pc.setString("string", "42"); pc.setString("non_convertible_string", "bcdefg&&"); pc.set(getBooleanConfigOption("boolean"), true); @@ -78,7 +79,8 @@ class ConfigurationConversionsTest { TestSpec.whenAccessed(conf -> conf.getInteger("int", 0)).expect(5), TestSpec.whenAccessed(conf -> conf.getLong("int", 0)).expect(5L), TestSpec.whenAccessed(conf -> conf.getFloat("int", 0)).expect(5f), - TestSpec.whenAccessed(conf -> conf.getDouble("int", 0)).expect(5.0), + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("int"), 0.0)) + .expect(5.0), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("int"), true)) .expectException("Could not parse value '5' for key 'int'."), TestSpec.whenAccessed(conf -> conf.getString("int", "0")).expect("5"), @@ -89,7 +91,8 @@ class ConfigurationConversionsTest { TestSpec.whenAccessed(conf -> conf.getInteger("long", 0)).expect(15), TestSpec.whenAccessed(conf -> conf.getLong("long", 0)).expect(15L), TestSpec.whenAccessed(conf -> conf.getFloat("long", 0)).expect(15f), - TestSpec.whenAccessed(conf -> conf.getDouble("long", 0)).expect(15.0), + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("long"), 0.0)) + .expect(15.0), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("long"), true)) .expectException("Could not parse value '15' for key 'long'."), TestSpec.whenAccessed(conf -> conf.getString("long", "0")).expect("15"), @@ -104,7 +107,7 @@ class ConfigurationConversionsTest { TestSpec.whenAccessed(conf -> conf.getLong("too_long", 0)).expect(TOO_LONG), TestSpec.whenAccessed(conf -> conf.getFloat("too_long", 0)) .expect((float) TOO_LONG), - TestSpec.whenAccessed(conf -> conf.getDouble("too_long", 0)) + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("too_long"), 0.0)) .expect((double) TOO_LONG), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("too_long"), true)) .expectException("Could not parse value '2147483657' for key 'too_long'."), @@ -122,7 +125,7 @@ class ConfigurationConversionsTest { .expectException( "For input string: \"2.1456776\"", NumberFormatException.class), TestSpec.whenAccessed(conf -> conf.getFloat("float", 0)).expect(2.1456775f), - TestSpec.whenAccessed(conf -> conf.getDouble("float", 0)) + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("float"), 0.0)) .expect( new Condition<>( d -> Math.abs(d - 2.1456775) < 0.0000001, @@ -146,7 +149,8 @@ class ConfigurationConversionsTest { NumberFormatException.class), TestSpec.whenAccessed(conf -> conf.getFloat("double", 0)) .expect(new IsCloseTo(3.141592f, 0.000001f)), - TestSpec.whenAccessed(conf -> conf.getDouble("double", 0)).expect(Math.PI), + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("double"), 0.0)) + .expect(Math.PI), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("double"), true)) .expectException( "Could not parse value '3.141592653589793' for key 'double'."), @@ -163,7 +167,9 @@ class ConfigurationConversionsTest { .expectException("For input string: \"-1.0\"", NumberFormatException.class), TestSpec.whenAccessed(conf -> conf.getFloat("negative_double", 0)) .expect(new IsCloseTo(-1f, 0.000001f)), - TestSpec.whenAccessed(conf -> conf.getDouble("negative_double", 0)).expect(-1D), + TestSpec.whenAccessed( + conf -> conf.get(getDoubleConfigOption("negative_double"), 0.0)) + .expect(-1D), TestSpec.whenAccessed( conf -> conf.get(getBooleanConfigOption("negative_double"), true)) .expectException("Could not parse value '-1.0' for key 'negative_double'."), @@ -180,7 +186,8 @@ class ConfigurationConversionsTest { .expectException("For input string: \"0.0\"", NumberFormatException.class), TestSpec.whenAccessed(conf -> conf.getFloat("zero", 0)) .expect(new IsCloseTo(0f, 0.000001f)), - TestSpec.whenAccessed(conf -> conf.getDouble("zero", 0)).expect(0D), + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("zero"), 0.0)) + .expect(0D), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("zero"), true)) .expectException("Could not parse value '0.0' for key 'zero'."), TestSpec.whenAccessed(conf -> conf.getString("zero", "0")) @@ -201,7 +208,8 @@ class ConfigurationConversionsTest { TestSpec.whenAccessed(conf -> conf.getFloat("too_long_double", 0)) .expectException( "Configuration value 1.7976931348623157E308 overflows/underflows the float type."), - TestSpec.whenAccessed(conf -> conf.getDouble("too_long_double", 0)) + TestSpec.whenAccessed( + conf -> conf.get(getDoubleConfigOption("too_long_double"), 0.0)) .expect(TOO_LONG_DOUBLE), TestSpec.whenAccessed( conf -> conf.get(getBooleanConfigOption("too_long_double"), true)) @@ -217,7 +225,8 @@ class ConfigurationConversionsTest { TestSpec.whenAccessed(conf -> conf.getInteger("string", 0)).expect(42), TestSpec.whenAccessed(conf -> conf.getLong("string", 0)).expect(42L), TestSpec.whenAccessed(conf -> conf.getFloat("string", 0)).expect(42f), - TestSpec.whenAccessed(conf -> conf.getDouble("string", 0)).expect(42.0), + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("string"), 0.0)) + .expect(42.0), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("string"), true)) .expectException("Could not parse value '42' for key 'string'."), TestSpec.whenAccessed(conf -> conf.getString("string", "0")).expect("42"), @@ -235,9 +244,14 @@ class ConfigurationConversionsTest { TestSpec.whenAccessed(conf -> conf.getFloat("non_convertible_string", 0)) .expectException( "For input string: \"bcdefg&&\"", NumberFormatException.class), - TestSpec.whenAccessed(conf -> conf.getDouble("non_convertible_string", 0)) + TestSpec.whenAccessed( + conf -> + conf.get( + getDoubleConfigOption("non_convertible_string"), + 0.0)) .expectException( - "For input string: \"bcdefg&&\"", NumberFormatException.class), + "Could not parse value 'bcdefg&&' for key 'non_convertible_string'.", + IllegalArgumentException.class), TestSpec.whenAccessed( conf -> conf.get( @@ -258,8 +272,8 @@ class ConfigurationConversionsTest { .expectException("For input string: \"true\""), TestSpec.whenAccessed(conf -> conf.getFloat("boolean", 0)) .expectException("For input string: \"true\""), - TestSpec.whenAccessed(conf -> conf.getDouble("boolean", 0)) - .expectException("For input string: \"true\""), + TestSpec.whenAccessed(conf -> conf.get(getDoubleConfigOption("boolean"), 0.0)) + .expectException("Could not parse value 'true' for key 'boolean'."), TestSpec.whenAccessed(conf -> conf.get(getBooleanConfigOption("boolean"), false)) .expect(true), TestSpec.whenAccessed(conf -> conf.getString("boolean", "0")).expect("true"), diff --git a/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationTest.java b/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationTest.java index 931ce76fd50..6ec91f60a32 100644 --- a/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationTest.java +++ b/flink-core/src/test/java/org/apache/flink/configuration/ConfigurationTest.java @@ -38,6 +38,7 @@ import java.util.Map; import java.util.stream.Collectors; import static org.apache.flink.configuration.ConfigurationUtils.getBooleanConfigOption; +import static org.apache.flink.configuration.ConfigurationUtils.getDoubleConfigOption; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -87,7 +88,7 @@ class ConfigurationTest { orig.setInteger("mynumber", 100); orig.setLong("longvalue", 478236947162389746L); orig.setFloat("PI", 3.1415926f); - orig.setDouble("E", Math.E); + orig.set(getDoubleConfigOption("E"), Math.E); orig.set(getBooleanConfigOption("shouldbetrue"), true); orig.setBytes("bytes sequence", new byte[] {1, 2, 3, 4, 5}); @@ -96,7 +97,7 @@ class ConfigurationTest { assertThat(copy.getInteger("mynumber", 0)).isEqualTo(100); assertThat(copy.getLong("longvalue", 0L)).isEqualTo(478236947162389746L); assertThat(copy.getFloat("PI", 3.1415926f)).isCloseTo(3.1415926f, Offset.offset(0.0f)); - assertThat(copy.getDouble("E", 0.0)).isCloseTo(Math.E, Offset.offset(0.0)); + assertThat(copy.get(getDoubleConfigOption("E"), 0.0)).isCloseTo(Math.E, Offset.offset(0.0)); assertThat(copy.get(getBooleanConfigOption("shouldbetrue"), false)).isTrue(); assertThat(copy.getBytes("bytes sequence", null)).containsExactly(1, 2, 3, 4, 5); diff --git a/flink-core/src/test/java/org/apache/flink/configuration/DelegatingConfigurationTest.java b/flink-core/src/test/java/org/apache/flink/configuration/DelegatingConfigurationTest.java index 2618004cac8..f8edbc06e83 100644 --- a/flink-core/src/test/java/org/apache/flink/configuration/DelegatingConfigurationTest.java +++ b/flink-core/src/test/java/org/apache/flink/configuration/DelegatingConfigurationTest.java @@ -177,11 +177,11 @@ class DelegatingConfigurationTest { // Test for double ConfigOption<Double> doubleOption = ConfigOptions.key("double.key").doubleType().noDefaultValue(); - original.setDouble(doubleOption, 7d); - assertThat(delegatingConf.getDouble(doubleOption, 8d)).isEqualTo(8d); + original.set(doubleOption, 7d); assertThat(delegatingConf.get(doubleOption, 8d)).isEqualTo(8d); - delegatingConf.setDouble(doubleOption, 9f); - assertThat(delegatingConf.getDouble(doubleOption, 8d)).isEqualTo(9f); + assertThat(delegatingConf.get(doubleOption, 8d)).isEqualTo(8d); + delegatingConf.set(doubleOption, 9.0); + assertThat(delegatingConf.get(doubleOption, 8d)).isEqualTo(9f); assertThat(delegatingConf.get(doubleOption, 8d)).isEqualTo(9f); // Test for long diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/operators/util/TaskConfig.java b/flink-runtime/src/main/java/org/apache/flink/runtime/operators/util/TaskConfig.java index 785cbfbb1c8..537f40ec52d 100644 --- a/flink-runtime/src/main/java/org/apache/flink/runtime/operators/util/TaskConfig.java +++ b/flink-runtime/src/main/java/org/apache/flink/runtime/operators/util/TaskConfig.java @@ -50,6 +50,7 @@ import java.util.Collections; import java.util.List; import static org.apache.flink.configuration.ConfigurationUtils.getBooleanConfigOption; +import static org.apache.flink.configuration.ConfigurationUtils.getDoubleConfigOption; /** Configuration class which stores all relevant parameters required to set up the Pact tasks. */ public class TaskConfig implements Serializable { @@ -518,11 +519,11 @@ public class TaskConfig implements Serializable { } public void setRelativeInputMaterializationMemory(int inputNum, double relativeMemory) { - this.config.setDouble(INPUT_DAM_MEMORY_PREFIX + inputNum, relativeMemory); + this.config.set(getDoubleConfigOption(INPUT_DAM_MEMORY_PREFIX + inputNum), relativeMemory); } public double getRelativeInputMaterializationMemory(int inputNum) { - return this.config.getDouble(INPUT_DAM_MEMORY_PREFIX + inputNum, 0); + return this.config.get(getDoubleConfigOption(INPUT_DAM_MEMORY_PREFIX + inputNum), 0.0); } public void setBroadcastInputName(String name, int groupIndex) { @@ -682,19 +683,19 @@ public class TaskConfig implements Serializable { // -------------------------------------------------------------------------------------------- public void setRelativeMemoryDriver(double relativeMemorySize) { - this.config.setDouble(MEMORY_DRIVER, relativeMemorySize); + this.config.set(getDoubleConfigOption(MEMORY_DRIVER), relativeMemorySize); } public double getRelativeMemoryDriver() { - return this.config.getDouble(MEMORY_DRIVER, 0); + return this.config.get(getDoubleConfigOption(MEMORY_DRIVER), 0.0); } public void setRelativeMemoryInput(int inputNum, double relativeMemorySize) { - this.config.setDouble(MEMORY_INPUT_PREFIX + inputNum, relativeMemorySize); + this.config.set(getDoubleConfigOption(MEMORY_INPUT_PREFIX + inputNum), relativeMemorySize); } public double getRelativeMemoryInput(int inputNum) { - return this.config.getDouble(MEMORY_INPUT_PREFIX + inputNum, 0); + return this.config.get(getDoubleConfigOption(MEMORY_INPUT_PREFIX + inputNum), 0.0); } // -------------------------------------------------------------------------------------------- @@ -854,12 +855,12 @@ public class TaskConfig implements Serializable { if (relativeMemory < 0) { throw new IllegalArgumentException(); } - this.config.setDouble(ITERATION_HEAD_BACKCHANNEL_MEMORY, relativeMemory); + this.config.set(getDoubleConfigOption(ITERATION_HEAD_BACKCHANNEL_MEMORY), relativeMemory); } public double getRelativeBackChannelMemory() { double relativeBackChannelMemory = - this.config.getDouble(ITERATION_HEAD_BACKCHANNEL_MEMORY, 0); + this.config.get(getDoubleConfigOption(ITERATION_HEAD_BACKCHANNEL_MEMORY), 0.0); if (relativeBackChannelMemory <= 0) { throw new IllegalArgumentException(); } @@ -870,11 +871,12 @@ public class TaskConfig implements Serializable { if (relativeMemory < 0) { throw new IllegalArgumentException(); } - this.config.setDouble(ITERATION_HEAD_SOLUTION_SET_MEMORY, relativeMemory); + this.config.set(getDoubleConfigOption(ITERATION_HEAD_SOLUTION_SET_MEMORY), relativeMemory); } public double getRelativeSolutionSetMemory() { - double backChannelMemory = this.config.getDouble(ITERATION_HEAD_SOLUTION_SET_MEMORY, 0); + double backChannelMemory = + this.config.get(getDoubleConfigOption(ITERATION_HEAD_SOLUTION_SET_MEMORY), 0.0); if (backChannelMemory <= 0) { throw new IllegalArgumentException(); } diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/jobgraph/JobGraphTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/jobgraph/JobGraphTest.java index 58cd04f9987..19f3f11a53a 100644 --- a/flink-runtime/src/test/java/org/apache/flink/runtime/jobgraph/JobGraphTest.java +++ b/flink-runtime/src/test/java/org/apache/flink/runtime/jobgraph/JobGraphTest.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; +import static org.apache.flink.configuration.ConfigurationUtils.getDoubleConfigOption; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; @@ -57,7 +58,7 @@ public class JobGraphTest extends TestLogger { // add some configuration values { jg.getJobConfiguration().setString("some key", "some value"); - jg.getJobConfiguration().setDouble("Life of ", Math.PI); + jg.getJobConfiguration().set(getDoubleConfigOption("Life of "), Math.PI); } // add some vertices
