{"record":{"id":"e6e1374b888c7128","repo":"MyCATApache/Mycat-Server","slug":"worker-id-can-t-be-greater-than-d-or-less-than-0","errorCode":null,"errorMessage":"worker Id can't be greater than %d or less than 0","messagePattern":"worker Id can't be greater than (.+?) or less than 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/route/sequence/handler/IncrSequenceTimeHandler.java","lineNumber":87,"sourceCode":"\t\t// 毫秒内自增位\n\t\tprivate final static long sequenceBits = 12L;\n\t\t// 机器ID偏左移12位\n\t\tprivate final static long workerIdShift = sequenceBits;\n\t\tprivate final static long datacenterIdShift = sequenceBits + workerIdBits;\n\t\t// 时间毫秒左移22位\n\t\tprivate final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;\n\n\t\tprivate final static long sequenceMask = -1L ^ (-1L << sequenceBits);\n\n\t\tprivate static long lastTimestamp = -1L;\n\n\t\tprivate long sequence = 0L;\n\t\tprivate final long workerId;\n\t\tprivate final long datacenterId;\n\n\t\tpublic IdWorker(long workerId, long datacenterId) {\n\t\t\tif (workerId > maxWorkerId || workerId < 0) {\n\t\t\t\tthrow new IllegalArgumentException(\"worker Id can't be greater than %d or less than 0\");\n\t\t\t}\n\t\t\tif (datacenterId > maxDatacenterId || datacenterId < 0) {\n\t\t\t\tthrow new IllegalArgumentException(\"datacenter Id can't be greater than %d or less than 0\");\n\t\t\t}\n\t\t\tthis.workerId = workerId;\n\t\t\tthis.datacenterId = datacenterId;\n\t\t}\n\n\t\tpublic synchronized long nextId() {\n\t\t\tlong timestamp = timeGen();\n\t\t\tif (timestamp < lastTimestamp) {\n\t\t\ttry {\n\t\t\t\tthrow new Exception(\"Clock moved backwards.  Refusing to generate id for \"+ (lastTimestamp - timestamp) + \" milliseconds\");\n\t\t\t} catch (Exception e) {\n\t\t\t\tLOGGER.error(\"error\",e);\n\t\t\t}\n\t\t\t}\n","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/route/sequence/handler/IncrSequenceTimeHandler.java#L69-L105","documentation":"The IdWorker used by IncrSequenceTimeHandler implements a Snowflake-style 64-bit ID in which 5 bits are reserved for the worker Id, so it must be an integer between 0 and 31 (maxWorkerId = -1L ^ (-1L << 5)). The constructor validates this and throws IllegalArgumentException before any ID is generated. The message contains an unformatted %d placeholder (String.format is never applied), a known bug in this codebase.","triggerScenarios":"Constructing IdWorker with a WORKID read from sequence_time_conf.properties that is negative or greater than 31 — e.g. WORKID=32, WORKID=100, or a negative value; load() in IncrSequenceTimeHandler calls new IdWorker(workid, dataCenterId) on handler initialization.","commonSituations":"Operators copy WORKID values from another cluster (where 6+ bits are used) into sequence_time_conf.properties; a typo like WORKID=032 is fine but WORKID=320 is not; automated config generation produces out-of-range IDs; WORKID accidentally set to a non-zero-based index.","solutions":["Open sequence_time_conf.properties and set WORKID to a value in [0, 31], unique per MyCAT instance","Restart MyCAT so the handler reloads the properties and constructs IdWorker successfully","If IDs must exceed 31 instances, increase workerIdBits in IdWorker and recompute maxWorkerId (accepting fewer sequence bits)"],"exampleFix":"// before (sequence_time_conf.properties)\nWORKID=64\n// after\nWORKID=4","handlingStrategy":"validation","validationCode":"long workerId = Long.parseLong(props.getProperty(\"WORKID\"));\nif (workerId < 0 || workerId > 31) {\n    throw new IllegalArgumentException(\"WORKID must be in [0,31], got \" + workerId);\n}","typeGuard":null,"tryCatchPattern":"try {\n    handler.load();\n} catch (IllegalArgumentException e) {\n    LOGGER.error(\"Invalid WORKID/DATAACENTERID in sequence_time_conf.properties: {}\", e.getMessage());\n    throw new ConfigurationException(e);\n}","preventionTips":["Keep a per-node table mapping MyCAT instances to WORKID values 0-31","Validate WORKID/DATAACENTERID ranges in your config deployment pipeline","Remember both IDs share the same 0-31 bound (5 bits each)"],"tags":["snowflake-id","config-validation","argument-out-of-range"],"backgroundTag":"invalid-config-value","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"}