lowka commented on code in PR #6276:
URL: https://github.com/apache/ignite-3/pull/6276#discussion_r2218212560


##########
modules/runner/src/test/java/org/apache/ignite/internal/configuration/compatibility/framework/ConfigurationTreeComparator.java:
##########
@@ -232,12 +228,444 @@ public static ComparisonContext 
create(Set<ConfigurationModule> configurationMod
 
         private final KeyIgnorer deletedItems;
 
+        private final Set<String> skipAddRemoveKeys = new HashSet<>();
+
         ComparisonContext(Collection<String> deletedPrefixes) {
             this.deletedItems = 
KeyIgnorer.fromDeletedPrefixes(deletedPrefixes);
         }
 
         boolean shouldIgnore(String path) {
             return deletedItems.shouldIgnore(path);
         }
+
+        boolean shouldIgnore(ConfigNode node, String childName) {
+            return shouldIgnore(node.path() + "." + childName);
+        }
+
+        boolean ignoreAddOrRemove(String path) {
+            return skipAddRemoveKeys.contains(path);
+        }
+
+        boolean ignoreAddOrRemove(ConfigNode node, String childName) {
+            return ignoreAddOrRemove(node.path() + "." + childName);
+        }
+    }
+
+    private static void validateConfigNode(
+            @Nullable String instanceType,
+            ConfigNode original,
+            ConfigNode updated,
+            ComparisonContext context,
+            Errors errors
+    ) {
+        errors.push(instanceType);
+        doValidateConfigNode(original, updated, context, errors);
+        errors.pop();
+    }
+
+    private static void validateNewConfigNode(
+            @Nullable String instanceType,
+            ConfigNode updated,
+            ComparisonContext compContext,
+            Errors errors
+    ) {
+        if (compContext.shouldIgnore(updated.path())) {
+            return;
+        }
+        errors.push(instanceType);
+
+        for (Entry<String, Node> e : updated.children().entrySet()) {
+            Node childNode = e.getValue();
+            if (childNode.isValue() && !childNode.hasDefault()) {
+                errors.addChildError(updated, e.getKey(), "Added a node with 
no default value");
+            }
+
+            if (childNode.isPolymorphic()) {
+                for (Map.Entry<String, ConfigNode> p : 
childNode.nodes().entrySet()) {
+                    ConfigNode node = p.getValue();
+                    validateNewConfigNode(p.getKey(), node, compContext, 
errors);
+                }
+            } else {
+                ConfigNode node = childNode.node();
+                validateNewConfigNode(null, node, compContext, errors);
+            }
+        }
+
+        errors.pop();
+    }
+
+    private static void doValidateConfigNode(ConfigNode original, ConfigNode 
updated, ComparisonContext context, Errors errors) {
+        if (context.shouldIgnore(original.path())) {
+            return;
+        }
+
+        if (!match(original, updated)) {
+            errors.addError(original, "Node does not match. Previous: " + 
original + ". Current: " + updated);
+            return;
+        }
+
+        validateAnnotations(original, updated, errors);
+
+        Set<String> originalChildrenNames = original.children().keySet();
+        Set<String> updatedChildrenNames = updated.children().keySet();
+
+        // Check for removed nodes
+        for (String childName : originalChildrenNames) {
+            if (updatedChildrenNames.contains(childName)) {
+                continue;
+            }
+
+            String path = original.path() + "." + childName;
+            if (context.shouldIgnore(path)) {
+                continue;
+            }
+
+            if (updatedChildrenNames.stream().noneMatch(name -> {
+                Node node = updated.children().get(name);
+                return node != null && 
node.legacyPropertyNames().contains(childName);
+            })) {
+                if (context.ignoreAddOrRemove(original, childName)) {
+                    continue;
+                }
+                errors.addChildError(original, childName, "Node was removed");
+            }
+        }
+
+        validateChildren(original, updated, context, errors);
+    }
+
+    private static void validateChildren(
+            ConfigNode original,
+            ConfigNode updated,
+            ComparisonContext context,
+            Errors errors
+    ) {
+        for (Entry<String, Node> e : original.children().entrySet()) {
+            String childName = e.getKey();
+            // Removed noded have already been handled
+            if (!updated.children().containsKey(childName)) {
+                continue;
+            }
+
+            Node originalChild = e.getValue();
+            Node updatedChild = updated.children().get(childName);
+
+            validateChildNode(original, childName, originalChild, 
updatedChild, context, errors);
+        }
+
+        Set<String> originalChildrenNames = original.children().keySet();
+
+        // Validate new nodes
+        for (Entry<String, Node> e : updated.children().entrySet()) {
+            String childName = e.getKey();
+            Node childNode = updated.children().get(childName);
+
+            if (context.shouldIgnore(original, childName)) {
+                continue;
+            }
+
+            // This child exists in original node under this name or under one 
of its legacy names.
+            boolean existingChildNode = 
originalChildrenNames.contains(childName)
+                    || 
childNode.legacyPropertyNames().stream().anyMatch(originalChildrenNames::contains);
+
+            if (existingChildNode) {
+                continue;
+            }
+
+            if (!childNode.hasDefault() && childNode.isValue() && 
!context.ignoreAddOrRemove(original, childName)) {
+                errors.addChildError(original, childName, "Added a node with 
no default value");
+            }
+        }
+    }
+
+    private static void validateChildNode(
+            ConfigNode original,
+            String childName,
+            Node originalChild,
+            Node updatedChild,
+            ComparisonContext context,
+            Errors errors
+    ) {
+        if (!originalChild.isPolymorphic() && !updatedChild.isPolymorphic()) {
+            // Both are single nodes, recursively check
+            validateConfigNode(null, originalChild.node(), 
updatedChild.node(), context, errors);
+        } else if (originalChild.isPolymorphic() != 
updatedChild.isPolymorphic()) {
+            // Check if node type changed (single vs polymorphic)
+
+            // If changing from single to polymorphic, check if it's a legal 
conversion
+            if (!originalChild.isPolymorphic() && 
updatedChild.isPolymorphic()) {
+                validateSingleToPolymorphic(originalChild, updatedChild, 
context, errors);
+            } else {
+                validatePolymorphicToSingle(original, errors, context, 
childName, originalChild, updatedChild);
+            }
+        } else {
+            assert originalChild.isPolymorphic() && 
updatedChild.isPolymorphic() : "Expected poly vs poly";
+
+            validatePolymorphicToPolymorphic(original, context, errors, 
childName, originalChild, updatedChild);
+        }
+    }
+
+    private static void validateSingleToPolymorphic(
+            Node originalChild,
+            Node updatedChild,
+            ComparisonContext context,
+            Errors errors
+    ) {
+        ConfigNode singleNode = originalChild.node();
+        Map<String, ConfigNode> polymorphicNodes = updatedChild.nodes();
+
+        // Transaction from a single node to polymorphic node should be 
compatible
+        // as long as new nodes are added in compatible fashion. 
+
+        for (Entry<String, ConfigNode> e : polymorphicNodes.entrySet()) {
+            errors.push(e.getKey());
+            validateChildren(singleNode, e.getValue(), context, errors);
+            errors.pop();
+        }
+    }
+
+    private static void validatePolymorphicToSingle(
+            ConfigNode original,
+            Errors errors,
+            ComparisonContext comparisonContext,
+            String childName,
+            Node originalChild,
+            Node updatedChild
+    ) {
+        Map<String, ConfigNode> polymorphicNode = originalChild.nodes();
+
+        // Converting a polymorphic node with 1 subclass can be compatible
+        if (polymorphicNode.size() == 2) {
+            // Get a subclass
+            Map.Entry<String, ConfigNode> subclass = 
polymorphicNode.entrySet().stream()
+                    .filter(e -> !e.getKey().isEmpty())
+                    .findFirst()
+                    .orElseThrow();
+
+            // Validate whether a subclass is compatible with a single node
+            validateConfigNode(subclass.getKey(), subclass.getValue(), 
updatedChild.node(), comparisonContext, errors);
+        } else {
+            // Converting multiple subclasses to single node is not compatible
+            errors.addChildError(original, childName, "Node was changed from 
polymorphic-node to single-node");
+        }
+    }
+
+    private static void validatePolymorphicToPolymorphic(
+            ConfigNode original,
+            ComparisonContext context,
+            Errors errors,
+            String childName,
+            Node originalChild,
+            Node updatedChild
+    ) {
+        Map<String, ConfigNode> originalPolyNodes = originalChild.nodes();
+        Map<String, ConfigNode> updatedPolyNodes = updatedChild.nodes();
+
+        // All sub-fields per each sub-class : Original
+        Map<String, Set<String>> originalNodesChildren = new LinkedHashMap<>();
+        for (Entry<String, ConfigNode> e : originalPolyNodes.entrySet()) {
+            originalNodesChildren.computeIfAbsent(e.getKey(), k -> new 
HashSet<>()).addAll(e.getValue().children().keySet());
+        }
+
+        // All sub-fields per each sub-class : Updated
+        Map<String, Set<String>> updatedNodesChildren = new LinkedHashMap<>();
+        for (Entry<String, ConfigNode> e : updatedPolyNodes.entrySet()) {
+            updatedNodesChildren.computeIfAbsent(e.getKey(), k -> new 
HashSet<>()).addAll(e.getValue().children().keySet());
+        }
+
+        // No child properties has neither been added nor removed - check 
compatibility of every node
+        if (originalNodesChildren.equals(updatedNodesChildren)) {
+            for (String key : originalNodesChildren.keySet()) {
+                ConfigNode originalVariant = originalPolyNodes.get(key);
+                ConfigNode updatedVariant = updatedPolyNodes.get(key);
+
+                validateConfigNode(key, originalVariant, updatedVariant, 
context, errors);
+            }
+        } else {
+            // Build a map of changes per subclass
+            Map<String, List<ChildChange>> changes = 
findChangesBetweenNodesChildren(updatedNodesChildren, originalNodesChildren);
+
+            Set<String> skipErrorKeys = new HashSet<>();
+
+            // Valid polymorphic-node -> polymorphic-node transformations:
+            // 1. Moving a child node from a base class to all subclasses iff 
the child node has a default value
+            // 2. Moving a child node from a subclass to a base class iff the 
child node has a default value.
+            {
+                Set<String> existedInAllSubclasses = new HashSet<>();
+                for (List<ChildChange> change : changes.values()) {
+                    for (ChildChange c : change) {
+                        if (!c.baseClass && c.type == 0) {
+                            existedInAllSubclasses.add(c.name);
+                        }
+                    }
+                }
+                for (Entry<String, Set<String>> e : 
originalNodesChildren.entrySet()) {
+                    if (e.getKey().isEmpty()) {
+                        continue;
+                    }
+                    existedInAllSubclasses.retainAll(e.getValue());
+                }
+
+                Set<ChildChange> movedToOrMovedFromBaseClass = 
changes.get(Node.BASE_INSTANCE_TYPE).stream()
+                        .filter(c -> c.baseClass && (c.type == 1 || c.type == 
-1) && existedInAllSubclasses.contains(c.name))
+                        .collect(Collectors.toSet());
+
+                for (ChildChange c : movedToOrMovedFromBaseClass) {
+                    ConfigNode originalNode = 
originalPolyNodes.get(c.instanceType);
+                    ConfigNode updatedNode = 
updatedPolyNodes.get(c.instanceType);
+
+                    Node n1 = originalNode.children().get(c.name);
+                    Node n2 = updatedNode.children().get(c.name);
+                    //  1 : Added: use updated node because the node has been 
moved from the subclass
+                    // -1 : Removed: use original node because the has been 
moved from the base class
+                    Node n = c.type == 1 ? n2 : n1;
+
+                    // Skip removed node / added non-default
+                    String onErrorKey = original.path() + "." + childName + 
"." + c.name;
+                    skipErrorKeys.add(onErrorKey);
+
+                    boolean singleSubclass = updatedNodesChildren.size() == 2 
&& originalNodesChildren.size() == 2;
+
+                    if (!n.hasDefault() && !singleSubclass) {
+                        errors.addChildError(originalNode, c.name, "Cannot 
move node to base class / from base class");
+                    }
+                }
+            }
+
+            context.skipAddRemoveKeys.addAll(skipErrorKeys);

Review Comment:
   This should be a stack of sets to correct track child nodes that have the 
same names.



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to