rpuch commented on code in PR #5166:
URL: https://github.com/apache/ignite-3/pull/5166#discussion_r1942336796


##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/validation/ConfigurationDuplicatesValidator.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.ignite.internal.configuration.validation;
+
+import com.typesafe.config.ConfigParseOptions;
+import com.typesafe.config.impl.Parseable;
+import com.typesafe.config.parser.ConfigDocument;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Queue;
+import java.util.Set;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.jetbrains.annotations.Nullable;
+
+/** Validates that there are no duplicate keys in the configuration. */
+public class ConfigurationDuplicatesValidator {
+    /**
+     * Validates that there are no duplicate keys in the passed configuration.
+     *
+     * @param cfg configuration in HOCON or JSON format.
+     */
+    public static Collection<ValidationIssue> validate(String cfg) {
+        Object root = getConfigRoot(cfg);
+
+        Queue<Node> queue = new ArrayDeque<>();
+        queue.add(new Node(root, new ArrayList<>(), null));
+
+        Set<String> paths = new HashSet<>();
+        Set<ValidationIssue> issues = new HashSet<>();
+
+        while (!queue.isEmpty()) {
+            Node currentNode = queue.poll();
+            List<Object> nodeChildren = 
getChildrenOrEmpty(currentNode.configNode);
+
+            if (nodeChildren.isEmpty()) {
+                continue;
+            }
+
+            Object firstChild = nodeChildren.get(0);
+            ArrayList<String> currentPath = new 
ArrayList<>(currentNode.basePath);

Review Comment:
   ```suggestion
               List<String> currentPath = new ArrayList<>(currentNode.basePath);
   ```



##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/validation/ConfigurationDuplicatesValidator.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.ignite.internal.configuration.validation;
+
+import com.typesafe.config.ConfigParseOptions;
+import com.typesafe.config.impl.Parseable;
+import com.typesafe.config.parser.ConfigDocument;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Queue;
+import java.util.Set;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.jetbrains.annotations.Nullable;
+
+/** Validates that there are no duplicate keys in the configuration. */
+public class ConfigurationDuplicatesValidator {
+    /**
+     * Validates that there are no duplicate keys in the passed configuration.
+     *
+     * @param cfg configuration in HOCON or JSON format.
+     */
+    public static Collection<ValidationIssue> validate(String cfg) {
+        Object root = getConfigRoot(cfg);
+
+        Queue<Node> queue = new ArrayDeque<>();
+        queue.add(new Node(root, new ArrayList<>(), null));
+
+        Set<String> paths = new HashSet<>();
+        Set<ValidationIssue> issues = new HashSet<>();
+
+        while (!queue.isEmpty()) {
+            Node currentNode = queue.poll();
+            List<Object> nodeChildren = 
getChildrenOrEmpty(currentNode.configNode);
+
+            if (nodeChildren.isEmpty()) {
+                continue;
+            }
+
+            Object firstChild = nodeChildren.get(0);
+            ArrayList<String> currentPath = new 
ArrayList<>(currentNode.basePath);
+
+            if (ConfigNodePath.isInstance(firstChild) || currentNode.index != 
null) {
+                currentPath.add(ConfigNodePath.isInstance(firstChild) ? 
ConfigNodePath.path(firstChild) : currentNode.index.toString());
+                String path = String.join(".", currentPath);
+
+                if (paths.contains(path)) {
+                    issues.add(new ValidationIssue(path, "Duplicated key"));
+                }
+
+                paths.add(path);
+            }
+
+            int index = 0;
+            for (Object child : nodeChildren) {
+                if (ConfigNodeComplexValue.isInstance(child) || 
ConfigNodeFieldClass.isInstance(child)) {
+                    queue.add(new Node(child, currentPath, 
ConfigNodeArray.isInstance(currentNode.configNode) ? index : null));
+                    index++;
+                }
+            }
+        }
+
+        return issues;
+    }
+
+    private static Object getConfigRoot(String cfg) {
+        ConfigDocument configDocument = Parseable.newString(cfg, 
ConfigParseOptions.defaults()).parseConfigDocument();
+        assert SimpleConfigDocument.isInstance(configDocument);
+
+        Object root = SimpleConfigDocument.root(configDocument);
+        assert ConfigNodeComplexValue.isInstance(root);
+
+        return root;
+    }
+
+    private static List<Object> getChildrenOrEmpty(Object configNode) {
+        if (ConfigNodeComplexValue.isInstance(configNode)) {
+            return ConfigNodeComplexValue.children(configNode);
+        } else if (ConfigNodeFieldClass.isInstance(configNode)) {
+            return ConfigNodeFieldClass.children(configNode);
+        } else {
+            return List.of();
+        }
+    }
+
+    private static class ConfigNodeComplexValue {
+        private static final Class<?> CLASS;
+        private static final Field CHILDREN_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeComplexValue");
+
+                CHILDREN_FIELD = CLASS.getDeclaredField("children");
+                CHILDREN_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static List<Object> children(Object configNodeComplexValue) {
+            try {
+                return (List<Object>) 
CHILDREN_FIELD.get(configNodeComplexValue);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class ConfigNodeArray {
+        private static final Class<?> CLASS;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeArray");
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+    }
+
+    private static class ConfigNodeFieldClass {
+        private static final Class<?> CLASS;
+        private static final Field CHILDREN_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeField");
+
+                CHILDREN_FIELD = CLASS.getDeclaredField("children");
+                CHILDREN_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static List<Object> children(Object configNode) {
+            try {
+                return (List<Object>) CHILDREN_FIELD.get(configNode);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class SimpleConfigDocument {
+        private static final Class<?> CLASS;
+        private static final Field NODE_TREE_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.SimpleConfigDocument");
+
+                NODE_TREE_FIELD = CLASS.getDeclaredField("configNodeTree");
+                NODE_TREE_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configDocument) {
+            return CLASS.isInstance(configDocument);
+        }
+
+        private static Object root(Object simpleConfigDocument) {
+            try {
+                return NODE_TREE_FIELD.get(simpleConfigDocument);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class ConfigNodePath {
+        private static final Class<?> CLASS;
+        private static final Class<?> PATH_CLASS;
+        private static final Field PATH_FIELD;
+        private static final Method PATH_RENDER_METHOD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodePath");
+                PATH_CLASS = Class.forName("com.typesafe.config.impl.Path");
+
+                PATH_FIELD = CLASS.getDeclaredField("path");
+                PATH_FIELD.setAccessible(true);
+
+                PATH_RENDER_METHOD = PATH_CLASS.getDeclaredMethod("render");
+                PATH_RENDER_METHOD.setAccessible(true);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static String path(Object firstChild) {
+            try {
+                Object path = PATH_FIELD.get(firstChild);
+
+                return PATH_RENDER_METHOD.invoke(path).toString();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class Node {
+        private final List<String> basePath;
+        private final Object configNode;
+        private final @Nullable Integer index;
+
+        private Node(Object configNode, List<String> basePath, @Nullable 
Integer index) {
+            this.configNode = configNode;
+            this.basePath = basePath;
+            this.index = index;

Review Comment:
   It would be more consistent if everywhere (field declarations, constructor 
parameters, assignments in the constructor) the fields were mentioned in the 
same order



##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/validation/ConfigurationDuplicatesValidator.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.ignite.internal.configuration.validation;
+
+import com.typesafe.config.ConfigParseOptions;
+import com.typesafe.config.impl.Parseable;
+import com.typesafe.config.parser.ConfigDocument;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Queue;
+import java.util.Set;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.jetbrains.annotations.Nullable;
+
+/** Validates that there are no duplicate keys in the configuration. */
+public class ConfigurationDuplicatesValidator {
+    /**
+     * Validates that there are no duplicate keys in the passed configuration.
+     *
+     * @param cfg configuration in HOCON or JSON format.
+     */
+    public static Collection<ValidationIssue> validate(String cfg) {
+        Object root = getConfigRoot(cfg);
+
+        Queue<Node> queue = new ArrayDeque<>();
+        queue.add(new Node(root, new ArrayList<>(), null));
+
+        Set<String> paths = new HashSet<>();
+        Set<ValidationIssue> issues = new HashSet<>();
+
+        while (!queue.isEmpty()) {
+            Node currentNode = queue.poll();
+            List<Object> nodeChildren = 
getChildrenOrEmpty(currentNode.configNode);
+
+            if (nodeChildren.isEmpty()) {
+                continue;
+            }
+
+            Object firstChild = nodeChildren.get(0);
+            ArrayList<String> currentPath = new 
ArrayList<>(currentNode.basePath);
+
+            if (ConfigNodePath.isInstance(firstChild) || currentNode.index != 
null) {
+                currentPath.add(ConfigNodePath.isInstance(firstChild) ? 
ConfigNodePath.path(firstChild) : currentNode.index.toString());
+                String path = String.join(".", currentPath);
+
+                if (paths.contains(path)) {
+                    issues.add(new ValidationIssue(path, "Duplicated key"));
+                }
+
+                paths.add(path);
+            }
+
+            int index = 0;
+            for (Object child : nodeChildren) {
+                if (ConfigNodeComplexValue.isInstance(child) || 
ConfigNodeFieldClass.isInstance(child)) {
+                    queue.add(new Node(child, currentPath, 
ConfigNodeArray.isInstance(currentNode.configNode) ? index : null));
+                    index++;
+                }
+            }
+        }
+
+        return issues;
+    }
+
+    private static Object getConfigRoot(String cfg) {
+        ConfigDocument configDocument = Parseable.newString(cfg, 
ConfigParseOptions.defaults()).parseConfigDocument();
+        assert SimpleConfigDocument.isInstance(configDocument);
+
+        Object root = SimpleConfigDocument.root(configDocument);
+        assert ConfigNodeComplexValue.isInstance(root);
+
+        return root;
+    }
+
+    private static List<Object> getChildrenOrEmpty(Object configNode) {
+        if (ConfigNodeComplexValue.isInstance(configNode)) {
+            return ConfigNodeComplexValue.children(configNode);
+        } else if (ConfigNodeFieldClass.isInstance(configNode)) {
+            return ConfigNodeFieldClass.children(configNode);
+        } else {
+            return List.of();
+        }
+    }
+
+    private static class ConfigNodeComplexValue {
+        private static final Class<?> CLASS;
+        private static final Field CHILDREN_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeComplexValue");
+
+                CHILDREN_FIELD = CLASS.getDeclaredField("children");
+                CHILDREN_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static List<Object> children(Object configNodeComplexValue) {
+            try {
+                return (List<Object>) 
CHILDREN_FIELD.get(configNodeComplexValue);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class ConfigNodeArray {
+        private static final Class<?> CLASS;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeArray");
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+    }
+
+    private static class ConfigNodeFieldClass {
+        private static final Class<?> CLASS;
+        private static final Field CHILDREN_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeField");
+
+                CHILDREN_FIELD = CLASS.getDeclaredField("children");
+                CHILDREN_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static List<Object> children(Object configNode) {
+            try {
+                return (List<Object>) CHILDREN_FIELD.get(configNode);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class SimpleConfigDocument {
+        private static final Class<?> CLASS;
+        private static final Field NODE_TREE_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.SimpleConfigDocument");
+
+                NODE_TREE_FIELD = CLASS.getDeclaredField("configNodeTree");
+                NODE_TREE_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configDocument) {
+            return CLASS.isInstance(configDocument);
+        }
+
+        private static Object root(Object simpleConfigDocument) {
+            try {
+                return NODE_TREE_FIELD.get(simpleConfigDocument);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class ConfigNodePath {
+        private static final Class<?> CLASS;
+        private static final Class<?> PATH_CLASS;
+        private static final Field PATH_FIELD;
+        private static final Method PATH_RENDER_METHOD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodePath");
+                PATH_CLASS = Class.forName("com.typesafe.config.impl.Path");
+
+                PATH_FIELD = CLASS.getDeclaredField("path");
+                PATH_FIELD.setAccessible(true);
+
+                PATH_RENDER_METHOD = PATH_CLASS.getDeclaredMethod("render");
+                PATH_RENDER_METHOD.setAccessible(true);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static String path(Object firstChild) {
+            try {
+                Object path = PATH_FIELD.get(firstChild);
+
+                return PATH_RENDER_METHOD.invoke(path).toString();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class Node {
+        private final List<String> basePath;
+        private final Object configNode;
+        private final @Nullable Integer index;

Review Comment:
   What does this index mean? When is it absent/present?



##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/storage/ConfigurationStorage.java:
##########
@@ -87,4 +90,10 @@ public interface ConfigurationStorage extends 
ManuallyCloseable {
      */
     @Override
     void close();
+
+    /** Validate that there are no duplicates in the stored configuration. */
+    default Collection<ValidationIssue> validateDuplicates() {

Review Comment:
   Validation method doesn't seem to belong to a 'storage' abstraction. I 
suggest moving it elsewhere



##########
modules/configuration/src/test/java/org/apache/ignite/internal/configuration/validation/ConfigurationDuplicatesValidatorTest.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.ignite.internal.configuration.validation;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.empty;
+
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest;
+import org.junit.jupiter.api.Test;
+
+class ConfigurationDuplicatesValidatorTest extends BaseIgniteAbstractTest {
+    private static final String DUPLICATE_CONFIG = "ignite {\n"
+            + "  network: {\n"
+            + "    port: 456,\n"
+            + "    nodeFinder.netClusterNodes: [ \"localhost\" ]\n"
+            + "  },\n"
+            + "  network: {}\n"
+            + "  network.nodeFinder.netClusterNodes: [ \"localhost\" ],\n"
+            + "  storage.profiles: {\n"
+            + "        test.engine: test, \n"
+            + "        default_aipersist.engine: aipersist, \n"
+            + "        default_aipersist.size: 123, \n"
+            + "        test.engine: aimem \n"
+            + "  },\n"
+            + "  clientConnector.port: 123,\n"
+            + "  rest {\n"
+            + "    port: 123,\n"
+            + "    port: 456\n"
+            + "  },\n"
+            + "  compute { threadPoolSize: 1 },\n"
+            + "  compute { threadPoolSize: 2 }\n"
+            + "  failureHandler.dumpThreadsOnFailure: false\n"
+            + "}";
+
+    private static final String OK_CONFIG = "ignite {\n"
+            + "  network: {\n"
+            + "    port: 456,\n"
+            + "    nodeFinder.netClusterNodes: [ {} ]\n"
+            + "  },\n"
+            + "  storage {\n"
+            + "      profiles = [\n"
+            + "          {\n"
+            + "              name = persistent,\n"
+            + "              engine = aipersist\n"
+            + "          }\n"
+            + "          {\n"
+            + "              name = rocksdb-example,\n"
+            + "              engine = rocksdb\n"
+            + "          }\n"
+            + "          {\n"
+            + "              name = in-memory,\n"
+            + "              engine = aimem\n"
+            + "          }\n"
+            + "      ]\n"
+            + "  },\n"
+            + "  clientConnector.port: 123,\n"
+            + "  rest {\n"
+            + "    port: 123\n"
+            + "  },\n"
+            + "  compute.threadPoolSize: 1,\n"
+            + "  failureHandler.dumpThreadsOnFailure: false\n"
+            + "}";
+
+    @Test
+    void testValidationSucceededWithNoDuplicates() {
+        assertThat(ConfigurationDuplicatesValidator.validate(OK_CONFIG), 
empty());
+    }
+
+    @Test
+    void testValidationFailedWithDuplicates() {
+        assertThat(ConfigurationDuplicatesValidator.validate(DUPLICATE_CONFIG),
+                containsInAnyOrder(
+                        new ValidationIssue("ignite.network", "Duplicated 
key"),
+                        new 
ValidationIssue("ignite.network.nodeFinder.netClusterNodes", "Duplicated key"),
+                        new 
ValidationIssue("ignite.storage.profiles.test.engine", "Duplicated key"),
+                        new ValidationIssue("ignite.rest.port", "Duplicated 
key"),
+                        new ValidationIssue("ignite.compute", "Duplicated 
key"),
+                        new ValidationIssue("ignite.compute.threadPoolSize", 
"Duplicated key")
+                )
+        );
+    }
+}

Review Comment:
   Please add a trailing line break



##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/validation/ConfigurationDuplicatesValidator.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.ignite.internal.configuration.validation;
+
+import com.typesafe.config.ConfigParseOptions;
+import com.typesafe.config.impl.Parseable;
+import com.typesafe.config.parser.ConfigDocument;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Queue;
+import java.util.Set;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.jetbrains.annotations.Nullable;
+
+/** Validates that there are no duplicate keys in the configuration. */
+public class ConfigurationDuplicatesValidator {
+    /**
+     * Validates that there are no duplicate keys in the passed configuration.
+     *
+     * @param cfg configuration in HOCON or JSON format.
+     */
+    public static Collection<ValidationIssue> validate(String cfg) {
+        Object root = getConfigRoot(cfg);
+
+        Queue<Node> queue = new ArrayDeque<>();
+        queue.add(new Node(root, new ArrayList<>(), null));
+
+        Set<String> paths = new HashSet<>();
+        Set<ValidationIssue> issues = new HashSet<>();
+
+        while (!queue.isEmpty()) {
+            Node currentNode = queue.poll();
+            List<Object> nodeChildren = 
getChildrenOrEmpty(currentNode.configNode);
+
+            if (nodeChildren.isEmpty()) {
+                continue;
+            }
+
+            Object firstChild = nodeChildren.get(0);
+            ArrayList<String> currentPath = new 
ArrayList<>(currentNode.basePath);
+
+            if (ConfigNodePath.isInstance(firstChild) || currentNode.index != 
null) {
+                currentPath.add(ConfigNodePath.isInstance(firstChild) ? 
ConfigNodePath.path(firstChild) : currentNode.index.toString());
+                String path = String.join(".", currentPath);
+
+                if (paths.contains(path)) {
+                    issues.add(new ValidationIssue(path, "Duplicated key"));
+                }
+
+                paths.add(path);
+            }
+
+            int index = 0;
+            for (Object child : nodeChildren) {
+                if (ConfigNodeComplexValue.isInstance(child) || 
ConfigNodeFieldClass.isInstance(child)) {
+                    queue.add(new Node(child, currentPath, 
ConfigNodeArray.isInstance(currentNode.configNode) ? index : null));
+                    index++;
+                }
+            }
+        }
+
+        return issues;
+    }
+
+    private static Object getConfigRoot(String cfg) {
+        ConfigDocument configDocument = Parseable.newString(cfg, 
ConfigParseOptions.defaults()).parseConfigDocument();
+        assert SimpleConfigDocument.isInstance(configDocument);
+
+        Object root = SimpleConfigDocument.root(configDocument);
+        assert ConfigNodeComplexValue.isInstance(root);
+
+        return root;
+    }
+
+    private static List<Object> getChildrenOrEmpty(Object configNode) {
+        if (ConfigNodeComplexValue.isInstance(configNode)) {
+            return ConfigNodeComplexValue.children(configNode);
+        } else if (ConfigNodeFieldClass.isInstance(configNode)) {
+            return ConfigNodeFieldClass.children(configNode);
+        } else {
+            return List.of();
+        }
+    }
+
+    private static class ConfigNodeComplexValue {
+        private static final Class<?> CLASS;
+        private static final Field CHILDREN_FIELD;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeComplexValue");
+
+                CHILDREN_FIELD = CLASS.getDeclaredField("children");
+                CHILDREN_FIELD.setAccessible(true);
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+
+        private static List<Object> children(Object configNodeComplexValue) {
+            try {
+                return (List<Object>) 
CHILDREN_FIELD.get(configNodeComplexValue);
+            } catch (IllegalAccessException e) {
+                // Shouldn't happen
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private static class ConfigNodeArray {
+        private static final Class<?> CLASS;
+
+        static {
+            try {
+                CLASS = 
Class.forName("com.typesafe.config.impl.ConfigNodeArray");
+            } catch (Exception e) {
+                // Shouldn't happen unless library structure is changed
+                throw new RuntimeException(e);
+            }
+        }
+
+        private static boolean isInstance(Object configNode) {
+            return CLASS.isInstance(configNode);
+        }
+    }
+
+    private static class ConfigNodeFieldClass {

Review Comment:
   Why isn't it named `ConfigNodeField`?



##########
modules/configuration/src/main/java/org/apache/ignite/internal/configuration/validation/ConfigurationDuplicatesValidator.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * 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.ignite.internal.configuration.validation;
+
+import com.typesafe.config.ConfigParseOptions;
+import com.typesafe.config.impl.Parseable;
+import com.typesafe.config.parser.ConfigDocument;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Queue;
+import java.util.Set;
+import org.apache.ignite.configuration.validation.ValidationIssue;
+import org.jetbrains.annotations.Nullable;
+
+/** Validates that there are no duplicate keys in the configuration. */
+public class ConfigurationDuplicatesValidator {
+    /**
+     * Validates that there are no duplicate keys in the passed configuration.
+     *
+     * @param cfg configuration in HOCON or JSON format.
+     */
+    public static Collection<ValidationIssue> validate(String cfg) {
+        Object root = getConfigRoot(cfg);
+
+        Queue<Node> queue = new ArrayDeque<>();
+        queue.add(new Node(root, new ArrayList<>(), null));
+
+        Set<String> paths = new HashSet<>();
+        Set<ValidationIssue> issues = new HashSet<>();
+
+        while (!queue.isEmpty()) {
+            Node currentNode = queue.poll();
+            List<Object> nodeChildren = 
getChildrenOrEmpty(currentNode.configNode);
+
+            if (nodeChildren.isEmpty()) {
+                continue;
+            }
+
+            Object firstChild = nodeChildren.get(0);
+            ArrayList<String> currentPath = new 
ArrayList<>(currentNode.basePath);
+
+            if (ConfigNodePath.isInstance(firstChild) || currentNode.index != 
null) {

Review Comment:
   This needs a comment explaing what happens here



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to