apache/cassandra · error · RuntimeException

CounterCacheKeysToSave must be non-negative.

Error message

CounterCacheKeysToSave must be non-negative.

What it means

CacheService.setCounterCacheKeysToSave sets how many counter-cache entries are saved per cycle and throws an untyped RuntimeException for negative counts before persisting the setting and rescheduling counterCache saving. Reached primarily through the CacheService JMX mbean.

Source

Thrown at src/java/org/apache/cassandra/service/CacheService.java:271

    }

    public void setKeyCacheKeysToSave(int count)
    {
        if (count < 0)
            throw new RuntimeException("KeyCacheKeysToSave must be non-negative.");
        DatabaseDescriptor.setKeyCacheKeysToSave(count);
        keyCache.scheduleSaving(getKeyCacheSavePeriodInSeconds(), count);
    }

    public int getCounterCacheKeysToSave()
    {
        return DatabaseDescriptor.getCounterCacheKeysToSave();
    }

    public void setCounterCacheKeysToSave(int count)
    {
        if (count < 0)
            throw new RuntimeException("CounterCacheKeysToSave must be non-negative.");
        DatabaseDescriptor.setCounterCacheKeysToSave(count);
        counterCache.scheduleSaving(getCounterCacheSavePeriodInSeconds(), count);
    }

    public void invalidateKeyCache()
    {
        keyCache.clear();
    }

    public void invalidateKeyCacheForCf(TableMetadata tableMetadata)
    {
        Iterator<KeyCacheKey> keyCacheIterator = keyCache.keyIterator();
        while (keyCacheIterator.hasNext())
        {
            KeyCacheKey key = keyCacheIterator.next();
            if (key.sameTable(tableMetadata))
                keyCacheIterator.remove();
        }

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Pass a non-negative count; 0 is the minimal accepted value.
  2. Clamp with Math.max(0, count) at the call site.
  3. Fix the source expression producing the negative number.
  4. Validate all cache-related JMX arguments before invoking the mbean.

Example fix

// before
mbean.setCounterCacheKeysToSave(delta); // delta may be negative
// after
mbean.setCounterCacheKeysToSave(Math.max(0, delta));
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new IllegalArgumentException("CounterCacheKeysToSave must be >= 0");
cacheService.setCounterCacheKeysToSave(count);

Prevention

When it happens

Trigger: Calling setCounterCacheKeysToSave(count) with count < 0 via JMX or internal code.

Common situations: Runtime tuning with values computed from deltas that can be negative; typos in JMX consoles; porting settings that used -1 as an unlimited sentinel from other systems.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/3a50c6dc95c25fed. Report an issue: GitHub.