You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by mb...@apache.org on 2022/10/27 19:08:15 UTC
[flink] 24/30: [backport][FLINK-29571] Support Map ConfigOptions
This is an automated email from the ASF dual-hosted git repository.
mbalassi pushed a commit to branch release-1.16.0.1-acs
in repository https://gitbox.apache.org/repos/asf/flink.git
commit f9c42d636a7eed3840548c897c51ff7f0186eb1e
Author: Maximilian Michels <mx...@apache.org>
AuthorDate: Mon Oct 10 16:52:03 2022 +0200
[backport][FLINK-29571] Support Map<String, T> ConfigOptions
---
.../apache/flink/configuration/ConfigOption.java | 37 ++++++++----
.../apache/flink/configuration/ConfigOptions.java | 67 +++++++++++++++++-----
.../apache/flink/configuration/Configuration.java | 2 +
.../flink/configuration/ConfigurationUtils.java | 40 ++++++++-----
.../configuration/DelegatingConfiguration.java | 2 +-
.../flink/configuration/ConfigurationTest.java | 55 ++++++++++++++++++
6 files changed, 162 insertions(+), 41 deletions(-)
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/ConfigOption.java b/flink-core/src/main/java/org/apache/flink/configuration/ConfigOption.java
index 6afdcee8469..e297b98217c 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/ConfigOption.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/ConfigOption.java
@@ -64,13 +64,20 @@ public class ConfigOption<T> {
*
* <ul>
* <li>typeClass == atomic class (e.g. {@code Integer.class}) -> {@code ConfigOption<Integer>}
- * <li>typeClass == {@code Map.class} -> {@code ConfigOption<Map<String, String>>}
- * <li>typeClass == atomic class and isList == true for {@code ConfigOption<List<Integer>>}
+ * <li>typeClass == atomic class and type == LIST for {@code ConfigOption<List<Integer>>}
+ * <li>typeClass == atomic class and type == MAP for {@code ConfigOption<Map<String,
+ * Integer>>}
* </ul>
*/
private final Class<?> clazz;
- private final boolean isList;
+ enum Type {
+ VALUE,
+ LIST,
+ MAP
+ }
+
+ private final Type valueType;
// ------------------------------------------------------------------------
@@ -78,8 +85,16 @@ public class ConfigOption<T> {
return clazz;
}
+ public Type getType() {
+ return valueType;
+ }
+
boolean isList() {
- return isList;
+ return valueType == Type.LIST;
+ }
+
+ boolean isMap() {
+ return valueType == Type.MAP;
}
/**
@@ -89,8 +104,8 @@ public class ConfigOption<T> {
* @param clazz describes type of the ConfigOption, see description of the clazz field
* @param description Description for that option
* @param defaultValue The default value for this option
- * @param isList tells if the ConfigOption describes a list option, see description of the clazz
- * field
+ * @param valueType tells if the ConfigOption describes a list option, see description of the
+ * clazz field
* @param fallbackKeys The list of fallback keys, in the order to be checked
*/
ConfigOption(
@@ -98,14 +113,14 @@ public class ConfigOption<T> {
Class<?> clazz,
Description description,
T defaultValue,
- boolean isList,
+ Type valueType,
FallbackKey... fallbackKeys) {
this.key = checkNotNull(key);
this.description = description;
this.defaultValue = defaultValue;
this.fallbackKeys = fallbackKeys == null || fallbackKeys.length == 0 ? EMPTY : fallbackKeys;
this.clazz = checkNotNull(clazz);
- this.isList = isList;
+ this.valueType = valueType;
}
// ------------------------------------------------------------------------
@@ -131,7 +146,7 @@ public class ConfigOption<T> {
final FallbackKey[] mergedAlternativeKeys =
Stream.concat(newFallbackKeys, currentAlternativeKeys).toArray(FallbackKey[]::new);
return new ConfigOption<>(
- key, clazz, description, defaultValue, isList, mergedAlternativeKeys);
+ key, clazz, description, defaultValue, valueType, mergedAlternativeKeys);
}
/**
@@ -156,7 +171,7 @@ public class ConfigOption<T> {
Stream.concat(currentAlternativeKeys, newDeprecatedKeys)
.toArray(FallbackKey[]::new);
return new ConfigOption<>(
- key, clazz, description, defaultValue, isList, mergedAlternativeKeys);
+ key, clazz, description, defaultValue, valueType, mergedAlternativeKeys);
}
/**
@@ -178,7 +193,7 @@ public class ConfigOption<T> {
* @return A new config option, with given description.
*/
public ConfigOption<T> withDescription(final Description description) {
- return new ConfigOption<>(key, clazz, description, defaultValue, isList, fallbackKeys);
+ return new ConfigOption<>(key, clazz, description, defaultValue, valueType, fallbackKeys);
}
// ------------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/ConfigOptions.java b/flink-core/src/main/java/org/apache/flink/configuration/ConfigOptions.java
index ee371a07f5b..565ad07dfef 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/ConfigOptions.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/ConfigOptions.java
@@ -26,6 +26,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import static org.apache.flink.configuration.ConfigOption.Type.LIST;
+import static org.apache.flink.configuration.ConfigOption.Type.MAP;
+import static org.apache.flink.configuration.ConfigOption.Type.VALUE;
import static org.apache.flink.util.Preconditions.checkNotNull;
/**
@@ -87,13 +90,6 @@ public class ConfigOptions {
* ConfigOptions#key(String)}.
*/
public static final class OptionBuilder {
- /**
- * Workaround to reuse the {@link TypedConfigOptionBuilder} for a {@link Map Map<String,
- * String>}.
- */
- @SuppressWarnings("unchecked")
- private static final Class<Map<String, String>> PROPERTIES_MAP_CLASS =
- (Class<Map<String, String>>) (Class<?>) Map.class;
/** The key for the config option. */
private final String key;
@@ -160,8 +156,16 @@ public class ConfigOptions {
* Defines that the value of the option should be a set of properties, which can be
* represented as {@code Map<String, String>}.
*/
- public TypedConfigOptionBuilder<Map<String, String>> mapType() {
- return new TypedConfigOptionBuilder<>(key, PROPERTIES_MAP_CLASS);
+ public MapConfigOptionBuilder<String> mapType() {
+ return mapType(String.class);
+ }
+
+ /**
+ * Defines that the value of the option should be a set of properties, which can be
+ * represented as {@code Map<String, T>}.
+ */
+ public <T> MapConfigOptionBuilder<T> mapType(Class<T> valueClazz) {
+ return new MapConfigOptionBuilder<>(key, valueClazz);
}
/**
@@ -180,7 +184,7 @@ public class ConfigOptions {
public <T> ConfigOption<T> defaultValue(T value) {
checkNotNull(value);
return new ConfigOption<>(
- key, value.getClass(), ConfigOption.EMPTY_DESCRIPTION, value, false);
+ key, value.getClass(), ConfigOption.EMPTY_DESCRIPTION, value, VALUE);
}
/**
@@ -194,7 +198,7 @@ public class ConfigOptions {
@Deprecated
public ConfigOption<String> noDefaultValue() {
return new ConfigOption<>(
- key, String.class, ConfigOption.EMPTY_DESCRIPTION, null, false);
+ key, String.class, ConfigOption.EMPTY_DESCRIPTION, null, VALUE);
}
}
@@ -217,6 +221,14 @@ public class ConfigOptions {
return new ListConfigOptionBuilder<>(key, clazz);
}
+ /**
+ * Defines that the option's type should be a map with values of the previously defined
+ * atomic type.
+ */
+ public MapConfigOptionBuilder<T> asMap() {
+ return new MapConfigOptionBuilder<>(key, clazz);
+ }
+
/**
* Creates a ConfigOption with the given default value.
*
@@ -224,7 +236,7 @@ public class ConfigOptions {
* @return The config option with the default value.
*/
public ConfigOption<T> defaultValue(T value) {
- return new ConfigOption<>(key, clazz, ConfigOption.EMPTY_DESCRIPTION, value, false);
+ return new ConfigOption<>(key, clazz, ConfigOption.EMPTY_DESCRIPTION, value, VALUE);
}
/**
@@ -234,7 +246,7 @@ public class ConfigOptions {
*/
public ConfigOption<T> noDefaultValue() {
return new ConfigOption<>(
- key, clazz, Description.builder().text("").build(), null, false);
+ key, clazz, Description.builder().text("").build(), null, VALUE);
}
}
@@ -261,7 +273,7 @@ public class ConfigOptions {
@SafeVarargs
public final ConfigOption<List<E>> defaultValues(E... values) {
return new ConfigOption<>(
- key, clazz, ConfigOption.EMPTY_DESCRIPTION, Arrays.asList(values), true);
+ key, clazz, ConfigOption.EMPTY_DESCRIPTION, Arrays.asList(values), LIST);
}
/**
@@ -270,7 +282,32 @@ public class ConfigOptions {
* @return The config option without a default value.
*/
public ConfigOption<List<E>> noDefaultValue() {
- return new ConfigOption<>(key, clazz, ConfigOption.EMPTY_DESCRIPTION, null, true);
+ return new ConfigOption<>(key, clazz, ConfigOption.EMPTY_DESCRIPTION, null, LIST);
+ }
+ }
+
+ /** Builder for map type {@link ConfigOption} with a value type V. */
+ public static class MapConfigOptionBuilder<V> {
+ private final String key;
+ private final Class<V> clazz;
+
+ MapConfigOptionBuilder(String key, Class<V> clazz) {
+ this.key = key;
+ this.clazz = clazz;
+ }
+
+ /** Defines that the option's type should be a list of previously defined atomic type. */
+ @SuppressWarnings("rawtypes")
+ public ListConfigOptionBuilder<Map<String, V>> asList() {
+ return (ListConfigOptionBuilder) new ListConfigOptionBuilder<>(key, Map.class);
+ }
+
+ public final ConfigOption<Map<String, V>> defaultValue(Map<String, V> defaultMap) {
+ return new ConfigOption<>(key, clazz, ConfigOption.EMPTY_DESCRIPTION, defaultMap, MAP);
+ }
+
+ public ConfigOption<Map<String, V>> noDefaultValue() {
+ return new ConfigOption<>(key, clazz, ConfigOption.EMPTY_DESCRIPTION, null, MAP);
}
}
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/Configuration.java b/flink-core/src/main/java/org/apache/flink/configuration/Configuration.java
index d2032c606c4..8751982fbad 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/Configuration.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/Configuration.java
@@ -712,6 +712,8 @@ public class Configuration extends ExecutionConfig.GlobalJobParameters
try {
if (option.isList()) {
return rawValue.map(v -> ConfigurationUtils.convertToList(v, clazz));
+ } else if (option.isMap()) {
+ return rawValue.map(v -> ConfigurationUtils.convertToMap(v, clazz));
} else {
return rawValue.map(v -> ConfigurationUtils.convertValue(v, clazz));
}
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 7975f34f72e..e750c794431 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
@@ -332,7 +332,7 @@ public class ConfigurationUtils {
} else if (clazz == MemorySize.class) {
return (T) convertToMemorySize(rawValue);
} else if (clazz == Map.class) {
- return (T) convertToProperties(rawValue);
+ return convertToMap(rawValue, String.class);
}
throw new IllegalArgumentException("Unsupported type: " + clazz);
@@ -351,22 +351,34 @@ public class ConfigurationUtils {
}
@SuppressWarnings("unchecked")
- static Map<String, String> convertToProperties(Object o) {
+ static <T> T convertToMap(Object o, Class<?> valueClazz) {
if (o instanceof Map) {
- return (Map<String, String>) o;
+ if (valueClazz == String.class) {
+ // We don't have to convert the values
+ return (T) o;
+ }
+ @SuppressWarnings("rawtypes")
+ Map<String, T> map = (Map<String, T>) o;
+ Map<String, T> newMap = new HashMap<>(map.size());
+ map.forEach((k, v) -> newMap.put(k, convertValue(v, valueClazz)));
+ return (T) newMap;
} else {
List<String> listOfRawProperties =
StructuredOptionsSplitter.splitEscaped(o.toString(), ',');
- return listOfRawProperties.stream()
- .map(s -> StructuredOptionsSplitter.splitEscaped(s, ':'))
- .peek(
- pair -> {
- if (pair.size() != 2) {
- throw new IllegalArgumentException(
- "Could not parse pair in the map " + pair);
- }
- })
- .collect(Collectors.toMap(a -> a.get(0), a -> a.get(1)));
+ return (T)
+ listOfRawProperties.stream()
+ .map(s -> StructuredOptionsSplitter.splitEscaped(s, ':'))
+ .peek(
+ pair -> {
+ if (pair.size() != 2) {
+ throw new IllegalArgumentException(
+ "Could not parse pair in the map " + pair);
+ }
+ })
+ .collect(
+ Collectors.toMap(
+ a -> a.get(0),
+ a -> convertValue(a.get(1), valueClazz)));
}
}
@@ -534,7 +546,7 @@ public class ConfigurationUtils {
* </pre>
*/
public static boolean canBePrefixMap(ConfigOption<?> configOption) {
- return configOption.getClazz() == Map.class && !configOption.isList();
+ return configOption.getClazz() == Map.class || configOption.isMap();
}
/** Filter condition for prefix map keys. */
diff --git a/flink-core/src/main/java/org/apache/flink/configuration/DelegatingConfiguration.java b/flink-core/src/main/java/org/apache/flink/configuration/DelegatingConfiguration.java
index f1c480dc247..3e96ec9321f 100644
--- a/flink-core/src/main/java/org/apache/flink/configuration/DelegatingConfiguration.java
+++ b/flink-core/src/main/java/org/apache/flink/configuration/DelegatingConfiguration.java
@@ -402,7 +402,7 @@ public final class DelegatingConfiguration extends Configuration {
option.getClazz(),
option.description(),
option.defaultValue(),
- option.isList(),
+ option.getType(),
deprecated);
}
}
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 cd8ca26ae5e..c5318bc83f7 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
@@ -21,6 +21,9 @@ package org.apache.flink.configuration;
import org.apache.flink.util.InstantiationUtil;
import org.apache.flink.util.TestLogger;
+import org.apache.flink.shaded.guava30.com.google.common.collect.ImmutableMap;
+import org.apache.flink.shaded.guava30.com.google.common.collect.Lists;
+
import org.junit.Test;
import java.time.Duration;
@@ -54,14 +57,24 @@ public class ConfigurationTest extends TestLogger {
private static final ConfigOption<Map<String, String>> MAP_OPTION =
ConfigOptions.key("test-map-key").mapType().noDefaultValue();
+ private static final ConfigOption<Map<String, Integer>> MAP_OPTION_INTEGER_TYPE =
+ ConfigOptions.key("test-map-key").mapType(Integer.class).noDefaultValue();
+
+ private static final ConfigOption<List<Map<String, String>>> MAP_OPTION_LIST =
+ ConfigOptions.key("test-map-list-key").mapType().asList().noDefaultValue();
+
private static final ConfigOption<Duration> DURATION_OPTION =
ConfigOptions.key("test-duration-key").durationType().noDefaultValue();
private static final Map<String, String> PROPERTIES_MAP = new HashMap<>();
+ private static final Map<String, Integer> PROPERTIES_MAP2 = new HashMap<>();
+
static {
PROPERTIES_MAP.put("prop1", "value1");
PROPERTIES_MAP.put("prop2", "12");
+ PROPERTIES_MAP2.put("prop1", 1);
+ PROPERTIES_MAP2.put("prop2", 2);
}
private static final String MAP_PROPERTY_1 = MAP_OPTION.key() + ".prop1";
@@ -435,6 +448,48 @@ public class ConfigurationTest extends TestLogger {
assertFalse(cfg.containsKey(MAP_PROPERTY_2));
}
+ @Test
+ public void testMapWithIntegerType() {
+ final Configuration cfg = new Configuration();
+ Map<String, Integer> map = ImmutableMap.of("one", 1, "two", 2);
+ cfg.set(MAP_OPTION_INTEGER_TYPE, map);
+
+ assertEquals(map, cfg.get(MAP_OPTION_INTEGER_TYPE));
+ }
+
+ @Test
+ public void testMapWithIntegerTypeParsing() {
+ final Configuration cfg = new Configuration();
+ cfg.setString(MAP_OPTION_INTEGER_TYPE.key() + ".prop1", "1");
+ cfg.setString(MAP_OPTION_INTEGER_TYPE.key() + ".prop2", "2");
+
+ Map<String, Integer> stringIntegerMap = cfg.get(MAP_OPTION_INTEGER_TYPE);
+ assertEquals((Integer) 1, stringIntegerMap.get("prop1"));
+ }
+
+ @Test
+ public void testMapList() {
+ final Configuration cfg = new Configuration();
+ Map<String, String> first = ImmutableMap.of("test1", "first");
+ Map<String, String> second = ImmutableMap.of("test2", "second");
+ cfg.set(MAP_OPTION_LIST, Lists.newArrayList(first, second));
+
+ List<Map<String, String>> stringIntegerMap = cfg.get(MAP_OPTION_LIST);
+ assertEquals(first, stringIntegerMap.get(0));
+ assertEquals(second, stringIntegerMap.get(1));
+ }
+
+ @Test
+ public void testMapListParsing() {
+ final Configuration cfg = new Configuration();
+ Map<String, String> first = ImmutableMap.of("test1", "one");
+ Map<String, String> second = ImmutableMap.of("test2", "two", "test3", "three");
+ cfg.setString(MAP_OPTION_LIST.key(), "test1:one;" + "test2:two,test3:three");
+
+ List<Map<String, String>> listMap = cfg.get(MAP_OPTION_LIST);
+ assertEquals(Lists.newArrayList(first, second), listMap);
+ }
+
// --------------------------------------------------------------------------------------------
// Test classes
// --------------------------------------------------------------------------------------------