dgraph-io/dgraph · error
ERROR: unable to parse cache percentage(%s)
Error message
ERROR: unable to parse cache percentage(%s)
What it means
Each comma-separated element of the cache percentage string must parse as an integer via strconv.Atoi. When an element is not a valid integer, GetCachePercentages returns this error naming the offending element.
Source
Thrown at x/x.go:1397
return aCopy
}
// GetCachePercentages returns the slice of cache percentages given the "," (comma) separated
// cache percentages(integers) string and expected number of caches.
func GetCachePercentages(cpString string, numExpected int) ([]int64, error) {
cp := strings.Split(cpString, ",")
// Sanity checks
if len(cp) != numExpected {
return nil, errors.Errorf("ERROR: expected %d cache percentages, got %d",
numExpected, len(cp))
}
var cachePercent []int64
percentSum := 0
for _, percent := range cp {
x, err := strconv.Atoi(percent)
if err != nil {
return nil, errors.Errorf("ERROR: unable to parse cache percentage(%s)", percent)
}
if x < 0 {
return nil, errors.Errorf("ERROR: cache percentage(%s) cannot be negative", percent)
}
cachePercent = append(cachePercent, int64(x))
percentSum += x
}
if percentSum != 100 {
return nil, errors.Errorf("ERROR: cache percentages (%s) does not sum up to 100",
strings.Join(cp, "+"))
}
return cachePercent, nil
}
// ParseCompression returns badger.compressionType and compression level given compression string
// of format compression-type:compression-levelView on GitHub (pinned to 759e242be6)
Solutions
- Ensure every comma-separated value is a plain non-negative integer (e.g. "50,50")
- Remove units, spaces, or decimals from the cache percentage config
- Validate the string by splitting on ',' and strconv.Atoi-ing each item before calling
Example fix
// before
cp, err := GetCachePercentages("50 %,50", 2) // unparsable "50 %"
// after
cp, err := GetCachePercentages("50,50", 2) Defensive patterns
Strategy: validation
Validate before calling
func validCachePctString(cpString string) bool {
for _, p := range strings.Split(cpString, ",") {
if _, err := strconv.Atoi(strings.TrimSpace(p)); err != nil { return false }
}
return true
} Type guard
func isParsablePercent(s string) bool { _, err := strconv.Atoi(strings.TrimSpace(s)); return err == nil } Prevention
- Store cache percentages as plain integers without '%' or spaces
- Trim whitespace on each CSV element before parsing
- Use typed config (list of ints) instead of raw strings where possible
When it happens
Trigger: Calling GetCachePercentages with a string containing a non-numeric item, e.g. "50,abc" or values with units/spaces like "50 %,50".
Common situations: Typo'd config values, pasting percentages with '%' or spaces, decimal values like '33.3' (not integers), or locale-formatted numbers.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- ERROR: expected %d cache percentages, got %d
- ERROR: cache percentage(%s) cannot be negative
- ERROR: cache percentages (%s) does not sum up to 100
- NQuad failed sanity check. Subject: %q, Predicate: %q, Objec
- expected '(', found: %s
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/14212eb2803346b6.
Report an issue: GitHub.