This is an automated email from the ASF dual-hosted git repository.
adoroszlai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/master by this push:
new 04840e04381 HDDS-17907. Reject duplicated keys in each `@Config` class
(#12141)
04840e04381 is described below
commit 04840e04381223bcbfb161c3dac39a6ad2fca2f4
Author: Nati <[email protected]>
AuthorDate: Fri Nov 27 18:22:13 2026 +0800
HDDS-17907. Reject duplicated keys in each `@Config` class (#12141)
---
.../hadoop/hdds/scm/TestOzoneClientConfig.java | 5 --
.../hadoop/hdds/conf/ConfigFileGenerator.java | 68 +++++++++++++++-------
.../hdds/conf/ConfigurationReflectionUtil.java | 11 ----
.../hadoop/hdds/conf/TestConfigFileGenerator.java | 58 ++++++++++++++++++
.../hdds/conf/TestConfigurationReflectionUtil.java | 22 -------
5 files changed, 106 insertions(+), 58 deletions(-)
diff --git
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestOzoneClientConfig.java
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestOzoneClientConfig.java
index 63a539c7803..cc5d5ceb5b9 100644
---
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestOzoneClientConfig.java
+++
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestOzoneClientConfig.java
@@ -136,11 +136,6 @@ void testDatastreamPutBlockWithoutRaftConfigParsing() {
assertTrue(subject.isDatastreamPutBlockWithoutRaftEnabled());
}
- @Test
- void testNoDuplicatedKeys() {
-
ConfigurationReflectionUtil.assertNoDuplicatedKeys(OzoneClientConfig.class);
- }
-
@Test
void testNettyWarmUpEnabledByDefault() {
assertTrue(new
OzoneConfiguration().getObject(OzoneClientConfig.class).isNettyWarmUpEnabled());
diff --git
a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigFileGenerator.java
b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigFileGenerator.java
index 7596eebccf1..5477ba4070c 100644
---
a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigFileGenerator.java
+++
b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigFileGenerator.java
@@ -26,6 +26,8 @@
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
+import java.util.HashMap;
+import java.util.Map;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
@@ -37,6 +39,7 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
@@ -127,29 +130,54 @@ private static boolean isTestOutput(Filer filer) throws
IOException {
return dir != null && TEST_OUTPUT_DIR.equals(dir.getName());
}
- private void writeConfigAnnotations(ConfigGroup configGroup,
- ConfigFileAppender appender,
- TypeElement typeElement) {
- for (Element element : typeElement.getEnclosedElements()) {
- if (element.getKind() == ElementKind.FIELD) {
- if (element.getAnnotation(Config.class) != null) {
-
- Config configAnnotation = element.getAnnotation(Config.class);
- String prefix = configGroup.prefix() + ".";
- String key = configAnnotation.key();
- if (!key.startsWith(prefix)) {
- processingEnv.getMessager().printMessage(Kind.ERROR,
- prefix + " is not a prefix of " + key,
- typeElement);
- }
-
- appender.addConfig(key,
- configAnnotation.defaultValue(),
- configAnnotation.description(),
- configAnnotation.tags());
+ private void writeConfigAnnotations(ConfigGroup configGroup,
ConfigFileAppender appender, TypeElement typeElement) {
+ // Collect the key of every @Config field in the class and its superclass
chain and fail the
+ // compilation on a duplicated key, including keys inherited from @Config
superclasses.
+ Map<String, String> keyToField = new HashMap<>();
+ for (TypeElement clazz = typeElement; clazz != null; clazz =
superclassOf(clazz)) {
+ for (Element element : clazz.getEnclosedElements()) {
+ if (element.getKind() != ElementKind.FIELD ||
element.getAnnotation(Config.class) == null) {
+ continue;
}
+ Config configAnnotation = element.getAnnotation(Config.class);
+ String key = configAnnotation.key();
+ String field = clazz.getSimpleName() + "#" + element.getSimpleName();
+ String previousField = keyToField.put(key, field);
+ if (previousField != null) {
+ processingEnv.getMessager().printMessage(Kind.ERROR,
+ "Duplicated config key '" + key + "': " + previousField + " and
" + field, element);
+ }
+
+ if (clazz != typeElement) {
+ // Fields of superclasses are validated here but written by their
own @ConfigGroup class,
+ // keeping the generated file unchanged.
+ continue;
+ }
+
+ String prefix = configGroup.prefix() + ".";
+ if (!key.startsWith(prefix)) {
+ processingEnv.getMessager().printMessage(Kind.ERROR, prefix + " is
not a prefix of " + key, typeElement);
+ }
+
+ appender.addConfig(key, configAnnotation.defaultValue(),
configAnnotation.description(),
+ configAnnotation.tags());
}
+ }
+ }
+ private TypeElement superclassOf(TypeElement typeElement) {
+ TypeMirror superclass = typeElement.getSuperclass();
+ switch (superclass.getKind()) {
+ case NONE:
+ case ERROR:
+ return null;
+ default:
+ Element element = processingEnv.getTypeUtils().asElement(superclass);
+ if (element instanceof TypeElement
+ && !"java.lang.Object".equals(((TypeElement)
element).getQualifiedName().toString())) {
+ return (TypeElement) element;
+ }
+ return null;
}
}
diff --git
a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
index be4799a6d61..d6c0ba7bbb1 100644
---
a/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
+++
b/hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java
@@ -264,17 +264,6 @@ public static Optional<Config> getConfigByKey(Class<?>
configClass, String key)
return Optional.empty();
}
- public static void assertNoDuplicatedKeys(Class<?> configClass) {
- for (Field field : configClass.getDeclaredFields()) {
- final Config config = field.getAnnotation(Config.class);
- if (config == null) {
- continue;
- }
-
- getConfigByKey(configClass, config.key()); // getConfigByKey throws
IllegalStateException for duplicated
- }
- }
-
private static void checkNotFinal(
Class<?> configurationClass, Field field) {
diff --git
a/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigFileGenerator.java
b/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigFileGenerator.java
index 157e809f445..628fda690db 100644
---
a/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigFileGenerator.java
+++
b/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigFileGenerator.java
@@ -21,9 +21,21 @@
import java.io.File;
import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URI;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.List;
import java.util.Scanner;
+import javax.tools.DiagnosticCollector;
+import javax.tools.JavaCompiler;
+import javax.tools.JavaFileObject;
+import javax.tools.SimpleJavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import javax.tools.ToolProvider;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
/**
* Test the ConfigFileGenerator.
@@ -34,6 +46,9 @@
*/
public class TestConfigFileGenerator {
+ @TempDir
+ private Path tempDir;
+
@Test
public void testGeneratedXml() throws FileNotFoundException {
String generatedXml =
@@ -49,4 +64,47 @@ public void testGeneratedXml() throws FileNotFoundException {
assertThat(generatedXml)
.contains("<tag>MANAGEMENT</tag>");
}
+
+ @Test
+ public void testDuplicatedKeyFailsCompilation() throws IOException {
+ JavaFileObject source = new SimpleJavaFileObject(
+ URI.create("string:///DuplicatedKeys.java"),
JavaFileObject.Kind.SOURCE) {
+ @Override
+ public CharSequence getCharContent(boolean ignoreEncodingErrors) {
+ return "package org.apache.hadoop.hdds.conf;\n"
+ + "@ConfigGroup(prefix = \"ozone.test.config\")\n"
+ + "class DuplicatedKeys {\n"
+ + " @ConfigGroup(prefix = \"ozone.test.config.nested\")\n"
+ + " static class Nested {\n"
+ + " @Config(key = \"ozone.test.config.nested.same\",
defaultValue = \"1\",\n"
+ + " description = \"first\", tags = {})\n"
+ + " int first;\n"
+ + " @Config(key = \"ozone.test.config.nested.same\",
defaultValue = \"2\",\n"
+ + " description = \"second\", tags = {})\n"
+ + " int second;\n"
+ + " }\n"
+ + "}\n";
+ }
+ };
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<>();
+ List<String> options = Arrays.asList(
+ "-proc:only",
+ "-processor", ConfigFileGenerator.class.getName(),
+ "-classpath",
Config.class.getProtectionDomain().getCodeSource().getLocation().getPath(),
+ "-d", tempDir.toString());
+
+ try (StandardJavaFileManager files =
+ compiler.getStandardFileManager(diagnostics, null, null)) {
+ assertThat(compiler.getTask(null, files, diagnostics, options, null,
+ Arrays.asList(source)).call())
+ .isFalse();
+ }
+
+ assertThat(diagnostics.getDiagnostics())
+ .anyMatch(diagnostic ->
+ diagnostic.getMessage(null).contains(
+ "Duplicated config key 'ozone.test.config.nested.same'"));
+ }
}
diff --git
a/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
b/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
index 534dada9c7f..a0f416ea5e8 100644
---
a/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
+++
b/hadoop-hdds/config/src/test/java/org/apache/hadoop/hdds/conf/TestConfigurationReflectionUtil.java
@@ -17,9 +17,7 @@
package org.apache.hadoop.hdds.conf;
-import static
org.apache.hadoop.hdds.conf.ConfigurationReflectionUtil.assertNoDuplicatedKeys;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import com.google.common.collect.ImmutableSet;
@@ -105,24 +103,4 @@ void listReconfigurableProperties() {
), props);
}
- @Test
- void testDuplicatedConfig() {
- assertThrows(IllegalStateException.class, () ->
assertNoDuplicatedKeys(DuplicatedConfig.class));
- }
-
- static class DuplicatedConfig {
- @Config(key = "key",
- defaultValue = "1",
- tags = ConfigTag.HDDS,
- description = "key1"
- )
- private String key1;
-
- @Config(key = "key",
- defaultValue = "2",
- tags = ConfigTag.HDDS,
- description = "key2"
- )
- private String key2;
- }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]