{"record":{"id":"eb7c04e6025a37d3","repo":"apache/shardingsphere","slug":"weight-s-of-available-target-s-should-be-num","errorCode":null,"errorMessage":"Weight `%s` of available target `%s` should be number.","messagePattern":"Weight `(.+?)` of available target `(.+?)` should be number\\.","errorType":"validation","errorClass":"AlgorithmInitializationException","httpStatus":null,"severity":"error","filePath":"infra/algorithm/type/load-balancer/type/weight/src/main/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithm.java","lineNumber":56,"sourceCode":"public final class WeightLoadBalanceAlgorithm implements LoadBalanceAlgorithm {\n    \n    private static final double ACCURACY_THRESHOLD = 0.0001;\n    \n    private final Map<String, double[]> weightMap = new ConcurrentHashMap<>();\n    \n    private final Map<String, Double> weightConfigMap = new HashMap<>();\n    \n    @Override\n    public void init(final Properties props) {\n        Collection<String> availableTargetNames = props.stringPropertyNames();\n        ShardingSpherePreconditions.checkNotEmpty(availableTargetNames, () -> new AlgorithmInitializationException(this, \"Available target is required.\"));\n        for (String each : availableTargetNames) {\n            String weight = props.getProperty(each);\n            ShardingSpherePreconditions.checkNotNull(weight, () -> new AlgorithmInitializationException(this, \"Weight of available target `%s` is required.\", each));\n            try {\n                weightConfigMap.put(each, Double.parseDouble(weight));\n            } catch (final NumberFormatException ex) {\n                throw new AlgorithmInitializationException(this, \"Weight `%s` of available target `%s` should be number.\", weight, each);\n            }\n        }\n    }\n    \n    @Override\n    public void check(final String databaseName, final Collection<String> configuredTargetNames) {\n        weightConfigMap.keySet().forEach(each -> ShardingSpherePreconditions.checkContains(configuredTargetNames, each,\n                () -> new AlgorithmInitializationException(this, \"Target `%s` is required in database `%s`.\", each, databaseName)));\n        configuredTargetNames.forEach(each -> ShardingSpherePreconditions.checkContains(weightConfigMap.keySet(), each,\n                () -> new AlgorithmInitializationException(this, \"Weight of target `%s` is required in database `%s`.\", each, databaseName)));\n    }\n    \n    @HighFrequencyInvocation\n    @Override\n    public String getTargetName(final String groupName, final List<String> availableTargetNames) {\n        double[] weight = weightMap.containsKey(groupName) && weightMap.get(groupName).length == availableTargetNames.size() ? weightMap.get(groupName) : initWeight(availableTargetNames);\n        weightMap.put(groupName, weight);\n        return getAvailableTargetName(availableTargetNames, weight);","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/infra/algorithm/type/load-balancer/type/weight/src/main/java/org/apache/shardingsphere/infra/algorithm/loadbalancer/weight/WeightLoadBalanceAlgorithm.java#L38-L74","documentation":"AlgorithmInitializationException thrown by WeightLoadBalanceAlgorithm.init when a configured weight property cannot be parsed as a number: Double.parseDouble(weight) raises NumberFormatException and the init fails naming the offending weight and target. Every property key in the algorithm props is treated as a target name whose value must be a numeric weight.","triggerScenarios":"Configuring the weight load balancer with a non-numeric weight, e.g. props { 'ds_0': '1', 'ds_1': 'abc' } or '0.5x', or accidentally including a non-weight property key (whose value is non-numeric) in the algorithm properties.","commonSituations":"YAML/DistSQL typos in weight values; units or percent signs left in values ('70%'); mixed-in unrelated algorithm properties because the weight algorithm treats every property key as a target.","solutions":["Correct the weight values to plain numbers ('1', '2.5') for every target.","Remove any non-target properties from this algorithm's props; the weight algorithm accepts only target-name -> weight entries.","Verify every configured replica/read-write-splitting target has its own numeric weight entry (the check() method enforces both directions)."],"exampleFix":"# before\nalgorithm:\n  type: WEIGHT\n  props:\n    read_ds_0: 70%\n    read_ds_1: 0.3x\n\n# after\nalgorithm:\n  type: WEIGHT\n  props:\n    read_ds_0: '70'\n    read_ds_1: '30'","handlingStrategy":"validation","validationCode":"// Validate weight props before building the algorithm\nfor (Map.Entry<String, String> e : weightProps.entrySet()) {\n    try {\n        Double.parseDouble(e.getValue());\n    } catch (NumberFormatException ex) {\n        throw new IllegalArgumentException(\"Weight for \" + e.getKey() + \" must be numeric: \" + e.getValue());\n    }\n}","typeGuard":"boolean isNumericWeight(final String value) {\n    return value != null && value.matches(\"-?\\\\d+(\\\\.\\\\d+)?([eE][+-]?\\\\d+)?\");\n}","tryCatchPattern":"try {\n    algorithm.init(props);\n} catch (final AlgorithmInitializationException ex) {\n    // message names the bad weight; fix config and re-init — do not continue with partial weights\n}","preventionTips":["Quote weight values in YAML and keep them unit-free.","Put only target-name -> weight entries in the WEIGHT algorithm props."],"tags":["load-balance","weight","algorithm","configuration","number-format"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}