{"record":{"id":"3aa87dc3a83abc41","repo":"shuzheng/zheng","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":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java","lineNumber":97,"sourceCode":"\t */\n\tprivate long sequence = 0L;\n\n\t/**\n\t * 上次生成ID的时间截\n\t */\n\tprivate long lastTimestamp = -1L;\n\n\t//==============================Constructors=====================================\n\n\t/**\n\t * 构造函数\n\t *\n\t * @param workerId     工作ID (0~31)\n\t * @param datacenterId 数据中心ID (0~31)\n\t */\n\tpublic SnowflakeIdWorker(long workerId, long datacenterId) {\n\t\tif (workerId > maxWorkerId || workerId < 0) {\n\t\t\tthrow new IllegalArgumentException(String.format(\"worker Id can't be greater than %d or less than 0\", maxWorkerId));\n\t\t}\n\t\tif (datacenterId > maxDatacenterId || datacenterId < 0) {\n\t\t\tthrow new IllegalArgumentException(String.format(\"datacenter Id can't be greater than %d or less than 0\", maxDatacenterId));\n\t\t}\n\t\tthis.workerId = workerId;\n\t\tthis.datacenterId = datacenterId;\n\t}\n\n\t// ==============================Methods==========================================\n\n\t/**\n\t * 获得下一个ID (该方法是线程安全的)\n\t *\n\t * @return SnowflakeId\n\t */\n\tpublic synchronized long nextId() {\n\t\tlong timestamp = timeGen();\n","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/shuzheng/zheng/blob/7005c0a775e6d014d1dc8a8a809f7b1c13bf785a/zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java#L79-L115","documentation":"SnowflakeIdWorker's constructor validates the workerId argument against maxWorkerId (31, since 5 bits are used for worker id) and throws IllegalArgumentException when workerId > 31 or workerId < 0. This guarantees worker-id bits never overflow into other bit fields of the generated 64-bit snowflake id.","triggerScenarios":"new SnowflakeIdWorker(workerId, datacenterId) with workerId outside the inclusive range 0..31, e.g. reading an unconfigured/zero-value config field or computing workerId from a host index >= 32.","commonSituations":"Deploying more than 32 instances sharing a workerId source; env variable missing so a default like -1 or 0-configured value out of range is used; refactoring changed the number of allowed bits but not the callers.","solutions":["Assign each instance a workerId between 0 and 31 (inclusive) in configuration/environment.","If more than 32 workers are needed, also differentiate datacenterId (0..31) so the pair (datacenterId, workerId) is unique.","Clamp or modulo-validate the workerId at config load time and fail fast with a clear message.","Generate workerId from a coordination service (zookeeper/redis) ensuring uniqueness within the valid range."],"exampleFix":"// before\nlong workerId = Long.parseLong(env.get(\"WORKER_ID\")); // e.g. 55\nSnowflakeIdWorker w = new SnowflakeIdWorker(workerId, 1);\n// after\nlong workerId = Long.parseLong(env.get(\"WORKER_ID\")) % 32;\nif (workerId < 0) workerId += 32;\nSnowflakeIdWorker w = new SnowflakeIdWorker(workerId, 1);","handlingStrategy":"validation","validationCode":"if (workerId < 0 || workerId > 31) {\n    throw new IllegalArgumentException(\"workerId must be in [0,31], got \" + workerId);\n}\nnew SnowflakeIdWorker(workerId, datacenterId);","typeGuard":"boolean isValidWorkerId(long id) {\n    return id >= 0 && id <= 31;\n}","tryCatchPattern":"try {\n    idWorker = new SnowflakeIdWorker(workerId, dcId);\n} catch (IllegalArgumentException e) {\n    log.error(\"Invalid snowflake node config: {}\", e.getMessage());\n    throw new IllegalStateException(\"Fix WORKER_ID config\", e);\n}","preventionTips":["Assign unique workerIds 0..31 per node via config management or a coordinator service.","Validate workerId at application startup before any id generation.","Use (datacenterId, workerId) pairs to scale beyond 32 nodes.","Fail fast on negative values from missing environment variables (parse with explicit defaults)."],"tags":["configuration","id-generation","snowflake","illegal-argument"],"backgroundTag":"snowflake-worker-id-out-of-range","analyzedSha":"7005c0a775e6d014d1dc8a8a809f7b1c13bf785a","analyzedAt":"2026-09-04T16:58:32.852Z","contentChangedAt":"2026-09-04T16:58:32.852Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}