{"record":{"id":"d609d59cf9277803","repo":"alibaba/nacos","slug":"cumulative-weight-wrong-the-array-length-is-equa","errorCode":null,"errorMessage":"Cumulative Weight wrong , the array length is equal to 0.","messagePattern":"Cumulative Weight wrong , the array length is equal to 0\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"critical","filePath":"client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java","lineNumber":68,"sourceCode":"     */\n    public T randomWithWeight() {\n        Ref<T> ref = this.ref;\n        double random = ThreadLocalRandom.current().nextDouble(0, 1);\n        int index = Arrays.binarySearch(ref.weights, random);\n        if (index < 0) {\n            index = -index - 1;\n        } else {\n            return ref.items.get(index);\n        }\n        \n        if (index < ref.weights.length) {\n            if (random < ref.weights[index]) {\n                return ref.items.get(index);\n            }\n        }\n        \n        if (ref.weights.length == 0) {\n            throw new IllegalStateException(\n                \"Cumulative Weight wrong , the array length is equal to 0.\");\n        }\n        \n        /* This should never happen, but it ensures we will return a correct\n         * object in case there is some floating point inequality problem\n         * wrt the cumulative probabilities. */\n        return ref.items.get(ref.items.size() - 1);\n    }\n    \n    public K getUniqueKey() {\n        return uniqueKey;\n    }\n    \n    public Ref<T> getRef() {\n        return ref;\n    }\n    \n    /**","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java#L50-L86","documentation":"Thrown by Chooser.randomWithWeight when the weights array is empty (IllegalStateException). This means every item in the chooser has weight <= 0 (zero or negative weights are ignored during refresh). With no positive-weight items, there is nothing to select and the weighted-random algorithm has no valid candidate.","triggerScenarios":"Calling randomWithWeight() on a Chooser whose item list contains only zero-weight or negative-weight instances. The binarySearch returns a negative index, the fallback checks fail, and the empty-weights guard fires.","commonSituations":"A service has all instances with weight 0 (e.g., manually set to 0 or defaulting to 0 in a custom setup); instances were registered with explicit zero weight for load shedding; a bug in weight assignment logic produces all-zero weights.","solutions":["Ensure at least one instance has a positive weight (> 0).","Check the instance weight assignment logic — Nacos default weight is 1.0, so all-zero usually indicates explicit override or a bug.","If zero-weight instances are intentional (e.g., drained), remove them from the chooser/service before calling randomWithWeight."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"List<Instance> validInstances = instances.stream()\n    .filter(i -> i.getWeight() > 0)\n    .collect(Collectors.toList());\nif (validInstances.isEmpty()) {\n    throw new IllegalStateException(\"No instances with positive weight for service \" + serviceName);\n}\n// proceed with validInstances","typeGuard":null,"tryCatchPattern":"try {\n    Instance selected = chooser.randomWithWeight();\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"array length is equal to 0\")) {\n        // no positive-weight instances — fall back to round-robin or default\n        LOGGER.warn(\"No weighted instances available, using fallback selection\");\n    } else {\n        throw e;\n    }\n}","preventionTips":["Ensure at least one instance has a positive weight before calling randomWithWeight.","Filter out zero-weight instances from the chooser if they represent drained nodes.","Validate instance weights at registration time to catch accidental zero-weight assignments."],"tags":["naming","load-balancing","weight","chooser","client"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}