dgraph-io/dgraph · error

ERROR: expected %d cache percentages, got %d

Error message

ERROR: expected %d cache percentages, got %d

What it means

GetCachePercentages splits a comma-separated string of cache percentage integers and first checks that the count matches numExpected. When the number of comma-separated values differs from the expected number of caches, it returns this error instead of guessing a distribution.

Source

Thrown at x/x.go:1388

		switch val := v.(type) {
		case map[string]interface{}:
			aCopy = append(aCopy, DeepCopyJsonMap(val))
		case []interface{}:
			aCopy = append(aCopy, DeepCopyJsonArray(val))
		default:
			aCopy = append(aCopy, val)
		}
	}
	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 {

View on GitHub (pinned to 759e242be6)

Solutions

  1. Provide exactly numExpected comma-separated values, e.g. "40,30,30" for 3 caches
  2. Update the cache percentage config to match the current number of caches/drives
  3. Check for stray commas or whitespace entries that alter the split count

Example fix

// before
cp, err := GetCachePercentages("50,50", 3) // expected 3, got 2
// after
cp, err := GetCachePercentages("40,30,30", 3)
Defensive patterns

Strategy: validation

Validate before calling

func checkCachePercentages(cpString string, expected int) error {
    n := len(strings.Split(cpString, ","))
    if n != expected {
        return fmt.Errorf("config has %d cache percentages, need %d", n, expected)
    }
    return nil
}

Type guard

func hasExpectedParts(cpString string, expected int) bool { return len(strings.Split(cpString, ",")) == expected }

Prevention

When it happens

Trigger: Calling GetCachePercentages(cpString, numExpected) with a cpString that splits into a different number of items than numExpected — e.g. "50,50" when 3 caches are expected.

Common situations: Config values like 'cache-pct: 60,40' copied from a different drive count, trailing/missing commas changing the split count, or drive/cache configuration updated without updating the percentage list.

Related errors


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