alibaba/nacos · critical · IllegalStateException

Cumulative Weight wrong , the array length is equal to 0.

Error message

Cumulative Weight wrong , the array length is equal to 0.

What it means

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.

Source

Thrown at client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java:68

     */
    public T randomWithWeight() {
        Ref<T> ref = this.ref;
        double random = ThreadLocalRandom.current().nextDouble(0, 1);
        int index = Arrays.binarySearch(ref.weights, random);
        if (index < 0) {
            index = -index - 1;
        } else {
            return ref.items.get(index);
        }
        
        if (index < ref.weights.length) {
            if (random < ref.weights[index]) {
                return ref.items.get(index);
            }
        }
        
        if (ref.weights.length == 0) {
            throw new IllegalStateException(
                "Cumulative Weight wrong , the array length is equal to 0.");
        }
        
        /* This should never happen, but it ensures we will return a correct
         * object in case there is some floating point inequality problem
         * wrt the cumulative probabilities. */
        return ref.items.get(ref.items.size() - 1);
    }
    
    public K getUniqueKey() {
        return uniqueKey;
    }
    
    public Ref<T> getRef() {
        return ref;
    }
    
    /**

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Ensure at least one instance has a positive weight (> 0).
  2. Check the instance weight assignment logic — Nacos default weight is 1.0, so all-zero usually indicates explicit override or a bug.
  3. If zero-weight instances are intentional (e.g., drained), remove them from the chooser/service before calling randomWithWeight.
Defensive patterns

Strategy: validation

Validate before calling

List<Instance> validInstances = instances.stream()
    .filter(i -> i.getWeight() > 0)
    .collect(Collectors.toList());
if (validInstances.isEmpty()) {
    throw new IllegalStateException("No instances with positive weight for service " + serviceName);
}
// proceed with validInstances

Try / catch

try {
    Instance selected = chooser.randomWithWeight();
} catch (IllegalStateException e) {
    if (e.getMessage().contains("array length is equal to 0")) {
        // no positive-weight instances — fall back to round-robin or default
        LOGGER.warn("No weighted instances available, using fallback selection");
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/d609d59cf9277803. Report an issue: GitHub.