MyCATApache/Mycat-Server · error · IllegalArgumentException

datacenter Id can't be greater than

Error message

datacenter Id can't be greater than %d or less than 0

What it means

The IdWorker reserves 5 bits for the datacenter Id, so datacenterId must be between 0 and 31 (maxDatacenterId = -1L ^ (-1L << 5)); otherwise the constructor throws IllegalArgumentException to prevent bit overflow into the timestamp portion of the generated ID. Like the worker-Id check, the %d placeholder in the message is never formatted.

Solutions

  1. Set DATAACENTERID in sequence_time_conf.properties to an integer in [0, 31], unique per datacenter
  2. Restart MyCAT to reload the properties
  3. If more than 32 datacenters are needed, widen datacenterIdBits in IdWorker and recompute maxDatacenterId

Example fix

// before (sequence_time_conf.properties)
DATAACENTERID=99
// after
DATAACENTERID=1
Defensive patterns

Strategy: validation

Validate before calling

long datacenterId = Long.parseLong(props.getProperty("DATAACENTERID"));
if (datacenterId < 0 || datacenterId > 31) {
    throw new IllegalArgumentException("DATAACENTERID must be in [0,31], got " + datacenterId);
}

Try / catch

try {
    handler.load();
} catch (IllegalArgumentException e) {
    LOGGER.error("Invalid DATAACENTERID in sequence_time_conf.properties: {}", e.getMessage());
    throw new ConfigurationException(e);
}

Prevention

When it happens

Trigger: Constructing IdWorker with a DATAACENTERID from sequence_time_conf.properties that is negative or greater than 31; IncrSequenceTimeHandler.load() calls new IdWorker(workid, dataCenterId) with the unvalidated property value.

Common situations: DATAACENTERID copied from a scheme with more datacenter bits; typos such as DATAACENTERID=100; reusing another deployment's properties file without adjusting the value; negative values produced by script-generated configs.

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/3180892c9a5fe32a. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/route/sequence/handler/IncrSequenceTimeHandler.java:90

		private final static long workerIdShift = sequenceBits;
		private final static long datacenterIdShift = sequenceBits + workerIdBits;
		// 时间毫秒左移22位
		private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;

		private final static long sequenceMask = -1L ^ (-1L << sequenceBits);

		private static long lastTimestamp = -1L;

		private long sequence = 0L;
		private final long workerId;
		private final long datacenterId;

		public IdWorker(long workerId, long datacenterId) {
			if (workerId > maxWorkerId || workerId < 0) {
				throw new IllegalArgumentException("worker Id can't be greater than %d or less than 0");
			}
			if (datacenterId > maxDatacenterId || datacenterId < 0) {
				throw new IllegalArgumentException("datacenter Id can't be greater than %d or less than 0");
			}
			this.workerId = workerId;
			this.datacenterId = datacenterId;
		}

		public synchronized long nextId() {
			long timestamp = timeGen();
			if (timestamp < lastTimestamp) {
			try {
				throw new Exception("Clock moved backwards.  Refusing to generate id for "+ (lastTimestamp - timestamp) + " milliseconds");
			} catch (Exception e) {
				LOGGER.error("error",e);
			}
			}

			if (lastTimestamp == timestamp) {
				// 当前毫秒内,则+1
				sequence = (sequence + 1) & sequenceMask;

View on GitHub (pinned to 65f8d8beb7)