{"record":{"id":"3a50c6dc95c25fed","repo":"apache/cassandra","slug":"countercachekeystosave-must-be-non-negative","errorCode":null,"errorMessage":"CounterCacheKeysToSave must be non-negative.","messagePattern":"CounterCacheKeysToSave must be non-negative\\.","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/CacheService.java","lineNumber":271,"sourceCode":"    }\n\n    public void setKeyCacheKeysToSave(int count)\n    {\n        if (count < 0)\n            throw new RuntimeException(\"KeyCacheKeysToSave must be non-negative.\");\n        DatabaseDescriptor.setKeyCacheKeysToSave(count);\n        keyCache.scheduleSaving(getKeyCacheSavePeriodInSeconds(), count);\n    }\n\n    public int getCounterCacheKeysToSave()\n    {\n        return DatabaseDescriptor.getCounterCacheKeysToSave();\n    }\n\n    public void setCounterCacheKeysToSave(int count)\n    {\n        if (count < 0)\n            throw new RuntimeException(\"CounterCacheKeysToSave must be non-negative.\");\n        DatabaseDescriptor.setCounterCacheKeysToSave(count);\n        counterCache.scheduleSaving(getCounterCacheSavePeriodInSeconds(), count);\n    }\n\n    public void invalidateKeyCache()\n    {\n        keyCache.clear();\n    }\n\n    public void invalidateKeyCacheForCf(TableMetadata tableMetadata)\n    {\n        Iterator<KeyCacheKey> keyCacheIterator = keyCache.keyIterator();\n        while (keyCacheIterator.hasNext())\n        {\n            KeyCacheKey key = keyCacheIterator.next();\n            if (key.sameTable(tableMetadata))\n                keyCacheIterator.remove();\n        }","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/CacheService.java#L253-L289","documentation":"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.","triggerScenarios":"Calling setCounterCacheKeysToSave(count) with count < 0 via JMX or internal code.","commonSituations":"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.","solutions":["Pass a non-negative count; 0 is the minimal accepted value.","Clamp with Math.max(0, count) at the call site.","Fix the source expression producing the negative number.","Validate all cache-related JMX arguments before invoking the mbean."],"exampleFix":"// before\nmbean.setCounterCacheKeysToSave(delta); // delta may be negative\n// after\nmbean.setCounterCacheKeysToSave(Math.max(0, delta));","handlingStrategy":"validation","validationCode":"if (count < 0) throw new IllegalArgumentException(\"CounterCacheKeysToSave must be >= 0\");\ncacheService.setCounterCacheKeysToSave(count);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed deltas with Math.max(0, value).","Validate JMX inputs before applying runtime config changes.","Avoid porting negative sentinels from other systems' cache settings.","Document accepted ranges for cache mbean operations."],"tags":["jmx","configuration","cache","runtime-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}