dgraph-io/dgraph · error

ERROR: cache percentage(%s) cannot be negative

Error message

ERROR: cache percentage(%s) cannot be negative

What it means

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.

Source

Thrown at x/x.go:1400

// 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-level
func ParseCompression(cStr string) (bo.CompressionType, int) {
	cStrSplit := strings.Split(cStr, ":")
	cType := cStrSplit[0]

View on GitHub (pinned to 759e242be6)

Solutions

  1. Check the cache percentage values in your configuration and remove any negative entries (every value must be >= 0).
  2. If percentages are computed by a script, clamp or validate them before passing to the API (reject values < 0).
  3. Re-run with the offending value corrected so all entries are non-negative integers that sum to 100.

Example fix

// before
percentages := strings.Split(os.Getenv("CACHE_PERCENT"), ",") // e.g. "-10,110"
cachePercent, err := setCachePercentages(percentages)

// after
raw := strings.Split(os.Getenv("CACHE_PERCENT"), ",")
for i, p := range raw {
    v, err := strconv.Atoi(strings.TrimSpace(p))
    if err != nil || v < 0 {
        raw[i] = "0" // or fail earlier with a clear message
    }
}
cachePercent, err := setCachePercentages(raw)
Defensive patterns

Strategy: validation

Validate before calling

func validateCachePercentages(cp []string) error {
    for _, p := range cp {
        v, err := strconv.Atoi(p)
        if err != nil {
            return fmt.Errorf("cache percentage %q is not an integer", p)
        }
        if v < 0 {
            return fmt.Errorf("cache percentage %q is negative", p)
        }
    }
    return nil
}

Type guard

func isNonNegativeInt(s string) bool {
    v, err := strconv.Atoi(s)
    return err == nil && v >= 0
}

Try / catch

cachePercent, err := setCachePercentages(cp)
if err != nil {
    if strings.Contains(err.Error(), "cannot be negative") {
        return fmt.Errorf("invalid cache config %v: percentages must be >= 0", cp)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/5a7986bf6977d552. Report an issue: GitHub.