{"record":{"id":"7ae04a2705e340ee","repo":"shuzheng/zheng","slug":"datacenter-id-can-t-be-greater-than-d-or-less-tha","errorCode":null,"errorMessage":"datacenter Id can't be greater than %d or less than 0","messagePattern":"datacenter 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":100,"sourceCode":"\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\n\t\t//如果当前时间小于上一次ID生成的时间戳，说明系统时钟回退过这个时候应当抛出异常\n\t\tif (timestamp < lastTimestamp) {\n\t\t\tthrow new RuntimeException(","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/shuzheng/zheng/blob/7005c0a775e6d014d1dc8a8a809f7b1c13bf785a/zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java#L82-L118","documentation":"Same validation as workerId but for datacenterId: the constructor throws IllegalArgumentException when datacenterId > maxDatacenterId (31) or < 0. The 5-bit datacenter field must fit in the snowflake id layout.","triggerScenarios":"new SnowflakeIdWorker(workerId, datacenterId) with datacenterId outside 0..31, e.g. a datacenter number of 32+ in a multi-DC deployment or a negative/unset value.","commonSituations":"Multi-datacenter deployment where DC numbering starts at 1 but exceeds 31; misconfigured environment variable; copy-paste of workerId into datacenterId slot with an out-of-range value.","solutions":["Set datacenterId to a value in 0..31 (inclusive).","Remap datacenter numbers larger than 31 down to the valid range, combining with a distinct workerId to keep uniqueness.","Validate the value where it is read from config and fail early with a descriptive message."],"exampleFix":"// before\nSnowflakeIdWorker w = new SnowflakeIdWorker(1, 40); // datacenterId=40 out of range\n// after\nSnowflakeIdWorker w = new SnowflakeIdWorker(1, 40 % 32); // or renumber DC to 0..31","handlingStrategy":"validation","validationCode":"if (datacenterId < 0 || datacenterId > 31) {\n    throw new IllegalArgumentException(\"datacenterId must be in [0,31], got \" + datacenterId);\n}\nnew SnowflakeIdWorker(workerId, datacenterId);","typeGuard":"boolean isValidDatacenterId(long id) {\n    return id >= 0 && id <= 31;\n}","tryCatchPattern":"try {\n    idWorker = new SnowflakeIdWorker(workerId, datacenterId);\n} catch (IllegalArgumentException e) {\n    log.error(\"Invalid datacenter id: {}\", e.getMessage());\n    throw new IllegalStateException(\"Fix DATACENTER_ID config\", e);\n}","preventionTips":["Keep datacenter numbering within 0..31; document the mapping per environment.","Validate datacenterId wherever it is read from config/env.","Ensure (datacenterId, workerId) pairs are globally unique.","Fail fast at startup rather than lazily on first id request."],"tags":["configuration","id-generation","snowflake","illegal-argument"],"backgroundTag":"snowflake-datacenter-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"}