MyCATApache/Mycat-Server · error · IllegalArgumentException

rule function must implements

Error message

rule function must implements ${AbstractPartitionAlgorithm.class.getName()}, name=${name}

What it means

createFunction loads the class named by a <function> element's class attribute via reflection and verifies it extends AbstractPartitionAlgorithm. If the class exists but does not inherit from AbstractPartitionAlgorithm, this IllegalArgumentException is thrown, aborting config load. MyCat requires all sharding functions to implement its algorithm contract.

Solutions

  1. Change the class attribute to a class that extends AbstractPartitionAlgorithm shipped with your MyCat version.
  2. If it is custom code, make the class extend AbstractPartitionAlgorithm and implement calculate(String)/init() appropriately for your version.
  3. Verify the algorithm jar version matches the MyCat server version; recompile against the correct API.

Example fix

// before
public class MyModHash implements SomeOtherFunction { ... }
// after
import io.mycat.route.function.AbstractPartitionAlgorithm;
public class MyModHash extends AbstractPartitionAlgorithm {
  @Override public void init() { ... }
  @Override public Integer calculate(String columnValue) { ... }
}
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check every function class before load
for (String clazz : functionClasses) {
    Class<?> c = Class.forName(clazz);
    if (!AbstractPartitionAlgorithm.class.isAssignableFrom(c))
        throw new IllegalStateException(clazz + " does not extend AbstractPartitionAlgorithm");
}

Type guard

static boolean isValidShardingFunction(String clazz) {
    try {
        return AbstractPartitionAlgorithm.class.isAssignableFrom(Class.forName(clazz));
    } catch (ClassNotFoundException e) {
        return false;
    }
}

Try / catch

try {
    ruleLoader.load();
} catch (ConfigException | IllegalArgumentException e) {
    if (String.valueOf(e.getMessage()).contains("must implements")) {
        LOG.error("Sharding function class incompatible: {}", e.getMessage());
    }
    throw e;
}

Prevention

When it happens

Trigger: A rule.xml <function class="..."> pointing at a class that implements a different interface (e.g. an older rule-function interface or a plain class); load() -> loadFunctions() -> createFunction() fails the isAssignableFrom check.

Common situations: Migrating configs across MyCat versions where the function API changed (old io.mycat.route.function classes vs custom code written against a different base); using a third-party algorithm jar built for another version; typo'd class name resolving to an unrelated class.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/34d72e33a5d43872. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/config/loader/xml/XMLRuleLoader.java:222

				//根据class利用反射新建分片算法
				AbstractPartitionAlgorithm function = createFunction(name, clazz);
				//根据读取参数配置分片算法
				ParameterMapping.mapping(function, ConfigUtil.loadElements(e));
				//每个AbstractPartitionAlgorithm可能会实现init来初始化
				function.init();
				//放入functions map
				functions.put(name, function);
			}
		}
	}

	private AbstractPartitionAlgorithm createFunction(String name, String clazz)
			throws ClassNotFoundException, InstantiationException,
			IllegalAccessException, InvocationTargetException {
		Class<?> clz = Class.forName(clazz);
		//判断是否继承AbstractPartitionAlgorithm
		if (!AbstractPartitionAlgorithm.class.isAssignableFrom(clz)) {
			throw new IllegalArgumentException("rule function must implements "
					+ AbstractPartitionAlgorithm.class.getName() + ", name=" + name);
		}
		return (AbstractPartitionAlgorithm) clz.newInstance();
	}

}

View on GitHub (pinned to 65f8d8beb7)