{"record":{"id":"a4130c4eaaea2433","repo":"MyCATApache/Mycat-Server","slug":"rule-function-name-duplicated","errorCode":null,"errorMessage":"rule function ${name} duplicated!","messagePattern":"rule function (.+?) duplicated!","errorType":"exception","errorClass":"ConfigException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java","lineNumber":199,"sourceCode":"\t * @param root\r\n\t * @throws ClassNotFoundException\r\n\t * @throws InstantiationException\r\n\t * @throws IllegalAccessException\r\n\t * @throws InvocationTargetException\r\n     */\r\n\tprivate void loadFunctions(Element root) throws ClassNotFoundException,\r\n\t\t\tInstantiationException, IllegalAccessException,\r\n\t\t\tInvocationTargetException {\r\n\t\tNodeList list = root.getElementsByTagName(\"function\");\r\n\t\tfor (int i = 0, n = list.getLength(); i < n; ++i) {\r\n\t\t\tNode node = list.item(i);\r\n\t\t\tif (node instanceof Element) {\r\n\t\t\t\tElement e = (Element) node;\r\n\t\t\t\t//获取name标签\r\n\t\t\t\tString name = e.getAttribute(\"name\");\r\n\t\t\t\t//如果Map已有，则function重复\r\n\t\t\t\tif (functions.containsKey(name)) {\r\n\t\t\t\t\tthrow new ConfigException(\"rule function \" + name\r\n\t\t\t\t\t\t\t+ \" duplicated!\");\r\n\t\t\t\t}\r\n\t\t\t\t//获取class标签\r\n\t\t\t\tString clazz = e.getAttribute(\"class\");\r\n\t\t\t\t//根据class利用反射新建分片算法\r\n\t\t\t\tAbstractPartitionAlgorithm function = createFunction(name, clazz);\r\n\t\t\t\t//根据读取参数配置分片算法\r\n\t\t\t\tParameterMapping.mapping(function, ConfigUtil.loadElements(e));\r\n\t\t\t\t//每个AbstractPartitionAlgorithm可能会实现init来初始化\r\n\t\t\t\tfunction.init();\r\n\t\t\t\t//放入functions map\r\n\t\t\t\tfunctions.put(name, function);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tprivate AbstractPartitionAlgorithm createFunction(String name, String clazz)\r\n\t\t\tthrows ClassNotFoundException, InstantiationException,\r","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java#L181-L217","documentation":"loadFunctions iterates <function> elements in rule.xml and stores them by name in a map. If two <function> blocks share the same name attribute, the second is rejected with this ConfigException. Duplicate function names would make algorithm lookup ambiguous.","triggerScenarios":"rule.xml defines two <function name=\"X\" ...> elements with identical names, possibly with different class attributes; load() -> loadFunctions() hits functions.containsKey(name).","commonSituations":"Appending a new function definition without removing the old one during tuning (e.g. changing count); merging rule.xml from two environments; copy-paste duplication.","solutions":["Search rule.xml for repeated <function name=\"...\"> names; delete or rename the duplicate.","If you need the same algorithm with different parameters, create uniquely named functions and reference them from the corresponding tableRules.","Restart/reload and verify MyCat starts cleanly."],"exampleFix":"// before (rule.xml)\n<function name=\"mod-long\" class=\"io.mycat.route.function.PartitionByMod\">\n  <property name=\"count\">4</property>\n</function>\n<function name=\"mod-long\" class=\"io.mycat.route.function.PartitionByMod\">\n  <property name=\"count\">8</property>\n</function>\n// after\n<function name=\"mod-long-4\" class=\"io.mycat.route.function.PartitionByMod\">\n  <property name=\"count\">4</property>\n</function>\n<function name=\"mod-long-8\" class=\"io.mycat.route.function.PartitionByMod\">\n  <property name=\"count\">8</property>\n</function>","handlingStrategy":"validation","validationCode":"// Detect duplicate <function> names before deployment\nSet<String> seen = new HashSet<>();\nNodeList fns = doc.getElementsByTagName(\"function\");\nfor (int i = 0; i < fns.getLength(); i++) {\n    String name = ((Element) fns.item(i)).getAttribute(\"name\");\n    if (!seen.add(name)) throw new IllegalStateException(\"duplicate function: \" + name);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ruleLoader.load();\n} catch (ConfigException e) {\n    if (e.getMessage().startsWith(\"rule function\") && e.getMessage().contains(\"duplicated!\")) {\n        LOG.error(\"Duplicate <function> name in rule.xml: {}\", e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Use uniquely named functions per parameter set (e.g. mod-long-4 vs mod-long-8).","Review rule.xml diffs for appended function blocks.","Add a uniqueness lint to the config pipeline."],"tags":["configuration","xml","duplicate-key","sharding"],"backgroundTag":"conflicting-config-options","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}