{"record":{"id":"95b46b9d4b013dcf","repo":"nats-io/nats-server","slug":"total-weight-needs-to-be-100","errorCode":null,"errorMessage":"total weight needs to be <= 100","messagePattern":"total weight needs to be <= 100","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/accounts.go","lineNumber":816,"sourceCode":"\tif !IsValidSubject(src) {\n\t\treturn ErrBadSubject\n\t}\n\n\tm := &mapping{src: src, wc: subjectHasWildcard(src), dests: make([]*destination, 0, len(dests)+1)}\n\tseen := make(map[string]struct{})\n\n\tvar tw = make(map[string]uint8)\n\tfor _, d := range dests {\n\t\tif _, ok := seen[d.Subject]; ok {\n\t\t\treturn fmt.Errorf(\"duplicate entry for %q\", d.Subject)\n\t\t}\n\t\tseen[d.Subject] = struct{}{}\n\t\tif d.Weight > 100 {\n\t\t\treturn fmt.Errorf(\"individual weights need to be <= 100\")\n\t\t}\n\t\ttw[d.Cluster] += d.Weight\n\t\tif tw[d.Cluster] > 100 {\n\t\t\treturn fmt.Errorf(\"total weight needs to be <= 100\")\n\t\t}\n\t\terr := ValidateMapping(src, d.Subject)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\ttr, err := NewSubjectTransform(src, d.Subject)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif d.Cluster == _EMPTY_ {\n\t\t\tm.dests = append(m.dests, &destination{tr, d.Weight})\n\t\t} else {\n\t\t\t// We have a cluster scoped filter.\n\t\t\tif m.cdests == nil {\n\t\t\t\tm.cdests = make(map[string][]*destination)\n\t\t\t}\n\t\t\tad := m.cdests[d.Cluster]\n\t\t\tad = append(ad, &destination{tr, d.Weight})","sourceCodeStart":798,"sourceCodeEnd":834,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/accounts.go#L798-L834","documentation":"Account subject-mapping validation: the summed weights of all destinations within a single cluster exceed 100 — weighted mappings must total at most 100 per cluster. The dests list (weights and cluster labels) passed to the mapping add is the input at fault; individual weights above 100 are rejected separately just before this check.","triggerScenarios":"Adding multiple WeightedDestination entries for the same Cluster whose combined Weight sums to more than 100, via AddMapping or config load.","commonSituations":"Adding a second mapping entry for the same cluster without accounting for existing weights; splitting traffic 60/60 across two subjects on one cluster.","solutions":["Sum the weights per cluster and rebalance so each cluster totals <= 100.","Reduce individual entry weights proportionally to their desired traffic split.","Move overflow traffic to a different cluster entry."],"exampleFix":"// before\ndests = []WeightedDestination{{Cluster:\"A\",Subject:\"s1\",Weight:60},{Cluster:\"A\",Subject:\"s2\",Weight:60}}\n// after\ndests = []WeightedDestination{{Cluster:\"A\",Subject:\"s1\",Weight:60},{Cluster:\"A\",Subject:\"s2\",Weight:40}}","handlingStrategy":"validation","validationCode":"totals := map[string]int{}\nfor _, d := range dests {\n    totals[d.Cluster] += d.Weight\n    if totals[d.Cluster] > 100 {\n        return fmt.Errorf(\"cluster %s exceeds 100%%\", d.Cluster)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := acc.AddMapping(...); err != nil {\n    if strings.Contains(err.Error(), \"total weight\") {\n        // rebalance cluster weights\n    }\n}","preventionTips":["Track per-cluster weight totals before submitting","Sum weights when generating mappings programmatically","Normalize weights so each cluster sums to 100"],"tags":["nats-server","config-validation","weighted-mapping"],"backgroundTag":"mapping-weight-out-of-range","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}