{"record":{"id":"67ce789ce9a1248e","repo":"alibaba/nacos","slug":"cumulative-weight-calculate-wrong-the-sum-of-pro","errorCode":null,"errorMessage":"Cumulative Weight calculate wrong , the sum of probabilities does not equals 1.","messagePattern":"Cumulative Weight calculate wrong , the sum of probabilities does not equals 1\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"critical","filePath":"client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java","lineNumber":160,"sourceCode":"            int index = 0;\n            for (Pair<T> item : itemsWithWeight) {\n                double singleWeight = item.weight();\n                //ignore item which weight is zero.see test_randomWithWeight_weight0 in ChooserTest\n                if (singleWeight <= 0) {\n                    continue;\n                }\n                \n                exactWeight = singleWeight / originWeightSum;\n                weights[index] = randomRange + exactWeight;\n                randomRange = weights[index++];\n            }\n            \n            double doublePrecisionDelta = 0.0001;\n            \n            if (index == 0 || (Math.abs(weights[index - 1] - 1) < doublePrecisionDelta)) {\n                return;\n            }\n            throw new IllegalStateException(\n                \"Cumulative Weight calculate wrong , the sum of probabilities does not equals 1.\");\n        }\n        \n        @Override\n        public int hashCode() {\n            return itemsWithWeight.hashCode();\n        }\n        \n        @SuppressWarnings(\"unchecked\")\n        @Override\n        public boolean equals(Object other) {\n            if (this == other) {\n                return true;\n            }\n            if (other == null) {\n                return false;\n            }\n            if (getClass() != other.getClass()) {","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/client/src/main/java/com/alibaba/nacos/client/naming/utils/Chooser.java#L142-L178","documentation":"Thrown by Chooser.Ref.refresh during cumulative-weight calculation when the final cumulative weight does not equal 1.0 within a 0.0001 tolerance (IllegalStateException). This is a floating-point consistency guard — the individual weights normalized by their sum should always produce a cumulative array ending at exactly 1.0. If it fires, the weight values contain extreme values (infinite, NaN) that the clamping logic did not fully stabilize.","triggerScenarios":"The weights array is non-empty (index > 0) but the last element differs from 1.0 by more than 0.0001. This can happen with pathological weight values such as Double.MAX_VALUE, very large numbers that become infinite, or combinations of infinite and finite weights that break the normalization.","commonSituations":"Instance metadata or config specifies extreme weight values (e.g., 1e308); a bug producing NaN weights that slip past the isFinite checks; edge cases with single very-large-weight instance alongside many tiny ones causing precision loss beyond delta.","solutions":["Review instance weight values for extremes (infinite, NaN, or extremely large magnitudes) and normalize them to reasonable positive values.","Ensure weights are finite positive doubles — the Chooser clamps infinite to 10000.0 and NaN to 1.0, but combinations can still break normalization.","Report as a bug if using only normal finite positive weights — the algorithm should handle those correctly."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"for (Instance inst : instances) {\n    double w = inst.getWeight();\n    if (w <= 0 || Double.isInfinite(w) || Double.isNaN(w) || w > 1e6) {\n        throw new IllegalArgumentException(\"Invalid instance weight: \" + w);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    chooser.refresh(weightedPairs);\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"sum of probabilities\")) {\n        // normalize weights manually and retry\n        LOGGER.error(\"Weight normalization failed, using equal weights\", e);\n        List<Pair<T>> equalWeights = items.stream()\n            .map(i -> new Pair<>(i, 1.0))\n            .collect(Collectors.toList());\n        chooser.refresh(equalWeights);\n    } else {\n        throw e;\n    }\n}","preventionTips":["Use reasonable finite positive weight values (e.g., 1.0 to 100.0).","Validate weights for infinite or NaN values before adding instances to the chooser.","Prevent extreme weight magnitudes that can break floating-point normalization.","Report persistent occurrences as a bug — normal finite positive weights should never trigger this."],"tags":["naming","load-balancing","weight","floating-point","chooser","client"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}