{"record":{"id":"5a7986bf6977d552","repo":"dgraph-io/dgraph","slug":"error-cache-percentage-s-cannot-be-negative","errorCode":null,"errorMessage":"ERROR: cache percentage(%s) cannot be negative","messagePattern":"ERROR: cache percentage\\((.+?)\\) cannot be negative","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/x.go","lineNumber":1400,"sourceCode":"// GetCachePercentages returns the slice of cache percentages given the \",\" (comma) separated\n// cache percentages(integers) string and expected number of caches.\nfunc GetCachePercentages(cpString string, numExpected int) ([]int64, error) {\n\tcp := strings.Split(cpString, \",\")\n\t// Sanity checks\n\tif len(cp) != numExpected {\n\t\treturn nil, errors.Errorf(\"ERROR: expected %d cache percentages, got %d\",\n\t\t\tnumExpected, len(cp))\n\t}\n\n\tvar cachePercent []int64\n\tpercentSum := 0\n\tfor _, percent := range cp {\n\t\tx, err := strconv.Atoi(percent)\n\t\tif err != nil {\n\t\t\treturn nil, errors.Errorf(\"ERROR: unable to parse cache percentage(%s)\", percent)\n\t\t}\n\t\tif x < 0 {\n\t\t\treturn nil, errors.Errorf(\"ERROR: cache percentage(%s) cannot be negative\", percent)\n\t\t}\n\t\tcachePercent = append(cachePercent, int64(x))\n\t\tpercentSum += x\n\t}\n\n\tif percentSum != 100 {\n\t\treturn nil, errors.Errorf(\"ERROR: cache percentages (%s) does not sum up to 100\",\n\t\t\tstrings.Join(cp, \"+\"))\n\t}\n\n\treturn cachePercent, nil\n}\n\n// ParseCompression returns badger.compressionType and compression level given compression string\n// of format compression-type:compression-level\nfunc ParseCompression(cStr string) (bo.CompressionType, int) {\n\tcStrSplit := strings.Split(cStr, \":\")\n\tcType := cStrSplit[0]","sourceCodeStart":1382,"sourceCodeEnd":1418,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/x/x.go#L1382-L1418","documentation":"This error comes from a helper that parses a list of cache percentage strings (e.g. cache tier sizes as percentages of total capacity). Each entry must parse as an integer via strconv.Atoi and be non-negative; if any parsed value is negative the function rejects the entire list with this error. It exists to fail fast on invalid configuration rather than silently accepting negative cache sizing.","triggerScenarios":"Calling the cache-configuration setup API (the function containing the loop at x/x.go:1400) with a cache percentage entry that parses to a negative integer, e.g. \"-10\", such as a config string like \"cache_percentages=-10,110\". A typo'd leading '-' or a sign produced by variable interpolation triggers it.","commonSituations":"Hand-edited config files or environment variables where a '-' was accidentally typed; scripts computing percentages from differences (e.g. new-old) that yield negative values; copy-pasted YAML/INI values with stray signs.","solutions":["Check the cache percentage values in your configuration and remove any negative entries (every value must be >= 0).","If percentages are computed by a script, clamp or validate them before passing to the API (reject values < 0).","Re-run with the offending value corrected so all entries are non-negative integers that sum to 100."],"exampleFix":"// before\npercentages := strings.Split(os.Getenv(\"CACHE_PERCENT\"), \",\") // e.g. \"-10,110\"\ncachePercent, err := setCachePercentages(percentages)\n\n// after\nraw := strings.Split(os.Getenv(\"CACHE_PERCENT\"), \",\")\nfor i, p := range raw {\n    v, err := strconv.Atoi(strings.TrimSpace(p))\n    if err != nil || v < 0 {\n        raw[i] = \"0\" // or fail earlier with a clear message\n    }\n}\ncachePercent, err := setCachePercentages(raw)","handlingStrategy":"validation","validationCode":"func validateCachePercentages(cp []string) error {\n    for _, p := range cp {\n        v, err := strconv.Atoi(p)\n        if err != nil {\n            return fmt.Errorf(\"cache percentage %q is not an integer\", p)\n        }\n        if v < 0 {\n            return fmt.Errorf(\"cache percentage %q is negative\", p)\n        }\n    }\n    return nil\n}","typeGuard":"func isNonNegativeInt(s string) bool {\n    v, err := strconv.Atoi(s)\n    return err == nil && v >= 0\n}","tryCatchPattern":"cachePercent, err := setCachePercentages(cp)\nif err != nil {\n    if strings.Contains(err.Error(), \"cannot be negative\") {\n        return fmt.Errorf(\"invalid cache config %v: percentages must be >= 0\", cp)\n    }\n    return err\n}","preventionTips":["Validate every percentage entry is a non-negative integer before calling the API.","When computing percentages programmatically, clamp results with max(0, value).","Strip whitespace and stray sign characters from config values during parsing.","Add a config-load unit test covering negative-value inputs."],"tags":["configuration","validation","cache","input-validation"],"backgroundTag":"invalid-config-value","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}