MyCATApache/Mycat-Server · error · RuntimeException

error,check your scope & scopeLength definition.

Error message

error,check your scope & scopeLength definition.

What it means

PartitionUtil computes a hash-partition mapping from paired count[] and length[] arrays whose dot product must equal the total range (e.g. 1024). The constructor throws RuntimeException if either array is null or their lengths differ, since the partition definition is malformed.

Solutions

  1. Fix the partition config so count and length arrays have the same number of elements.
  2. Also verify sum(count[i]*length[i]) equals the expected total range (e.g. 1024) to avoid later mapping errors.
  3. Add a config sanity check at startup that validates array presence and equal lengths.

Example fix

// before
int[] count = {4};
int[] length = {256, 256}; // lengths differ -> throw
// after
int[] count = {4, 4};
int[] length = {256, 256}; // equal lengths, 4*256+4*256 = 2048... adjust to constraint
Defensive patterns

Strategy: validation

Validate before calling

if (count == null || length == null || count.length != length.length) {
    throw new IllegalArgumentException("count and length arrays must be non-null and of equal length");
}

Try / catch

try { PartitionUtil p = new PartitionUtil(count, length); } catch (RuntimeException e) { throw new ConfigException("bad partition definition", e); }

Prevention

When it happens

Trigger: new PartitionUtil(count, length) with count == null, length == null, or count.length != length.length.

Common situations: Mis-edited partition function config in rule.xml/partition definitions where one array has more entries than the other; programmatically built arrays where one list is filtered/empty.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/main/java/io/mycat/route/util/PartitionUtil.java:54

    private static final int PARTITION_LENGTH = 1024;

    // %转换为&操作的换算数值
    private static final long AND_VALUE = PARTITION_LENGTH - 1;

    // 分区线段
    private final int[] segment = new int[PARTITION_LENGTH];

    /**
     * <pre>
     * @param count 表示定义的分区数
     * @param length 表示对应每个分区的取值长度
     * 注意:其中count,length两个数组的长度必须是一致的。
     * 约束:1024 = sum((count[i]*length[i])). count和length两个向量的点积恒等于1024
     * </pre>
     */
    public PartitionUtil(int[] count, int[] length) {
        if (count == null || length == null || (count.length != length.length)) {
            throw new RuntimeException("error,check your scope & scopeLength definition.");
        }
        int segmentLength = 0;
        for (int i = 0; i < count.length; i++) {
            segmentLength += count[i];
        }
        int[] ai = new int[segmentLength + 1];

        int index = 0;
        for (int i = 0; i < count.length; i++) {
            for (int j = 0; j < count[i]; j++) {
                ai[++index] = ai[index - 1] + length[i];
            }
        }
        if (ai[ai.length - 1] != PARTITION_LENGTH) {
            throw new RuntimeException("error,check your partitionScope definition.");
        }

        // 数据映射操作

View on GitHub (pinned to 65f8d8beb7)