{"record":{"id":"56afd8e246e082b7","repo":"apache/shenyu","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":"shenyu-common/src/main/java/org/apache/shenyu/common/utils/UUIDUtils.java","lineNumber":65,"sourceCode":"    private static final UUIDUtils ID_WORKER_UTILS = new UUIDUtils();\n\n    private final long workerId;\n\n    private final long datacenterId;\n\n    private final long idepoch;\n\n    private long sequence = '0';\n\n    private long lastTimestamp = -1L;\n\n    private UUIDUtils() {\n        this(RANDOM.nextInt((int) MAX_WORKER_ID), RANDOM.nextInt((int) MAX_DATACENTER_ID), 1288834974657L);\n    }\n\n    private UUIDUtils(final long workerId, final long datacenterId, final long idepoch) {\n        if (workerId > MAX_WORKER_ID || workerId < 0) {\n            throw new IllegalArgumentException(String.format(\"worker Id can't be greater than %d or less than 0\", MAX_WORKER_ID));\n        }\n        if (datacenterId > MAX_DATACENTER_ID || datacenterId < 0) {\n            throw new IllegalArgumentException(String.format(\"datacenter Id can't be greater than %d or less than 0\", MAX_DATACENTER_ID));\n        }\n        this.workerId = workerId;\n        this.datacenterId = datacenterId;\n        this.idepoch = idepoch;\n    }\n\n    /**\n     * Gets instance.\n     *\n     * @return the instance\n     */\n    public static UUIDUtils getInstance() {\n        return ID_WORKER_UTILS;\n    }\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-common/src/main/java/org/apache/shenyu/common/utils/UUIDUtils.java#L47-L83","documentation":"UUIDUtils implements a Snowflake-style ID generator. Its constructor validates that workerId is within [0, MAX_WORKER_ID]; out-of-range values throw IllegalArgumentException 'worker Id can't be greater than %d or less than 0' with MAX_WORKER_ID formatted in.","triggerScenarios":"Constructing UUIDUtils (private) with a workerId > MAX_WORKER_ID or < 0 — typically when the random/generated worker id computation or a manually supplied id from config violates the bound.","commonSituations":"Deploying many instances where auto-assigned worker ids collide and custom id logic overflows the range; copying Snowflake code with different MAX_WORKER_ID; config file supplying negative worker id.","solutions":["Pass a workerId within 0..MAX_WORKER_ID (check the constant value in UUIDUtils).","Compute workerId via modulo: workerId % (MAX_WORKER_ID + 1).","Ensure configured worker-id properties are non-negative integers within range before constructing.","Use distinct, validated worker ids per instance to also avoid duplicate-id issues."],"exampleFix":"// before\nlong workerId = Long.parseLong(env.get(\"WORKER_ID\")); // e.g. -3\n// after\nlong workerId = Long.parseLong(env.getOrDefault(\"WORKER_ID\", \"0\")) % (MAX_WORKER_ID + 1);\nif (workerId < 0) { workerId += MAX_WORKER_ID + 1; }","handlingStrategy":"validation","validationCode":"if (workerId < 0 || workerId > MAX_WORKER_ID) throw new IllegalArgumentException(\"workerId must be in [0,\" + MAX_WORKER_ID + \"]\");","typeGuard":null,"tryCatchPattern":"try { idGen = new UUIDUtils(workerId, dcId, epoch); } catch (IllegalArgumentException e) { LOG.error(\"bad worker id\", e); idGen = new UUIDUtils(0, dcId, epoch); }","preventionTips":["Validate worker-id config at startup","Derive ids with modulo bound checks","Keep per-instance ids unique and in range","Document MAX_WORKER_ID in deployment docs"],"tags":["snowflake","id-generation","argument-validation","config"],"backgroundTag":"value-out-of-range","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}