{"record":{"id":"0b2fcd33e3a2d29c","repo":"apache/cassandra","slug":"keycachekeystosave-must-be-non-negative","errorCode":null,"errorMessage":"KeyCacheKeysToSave must be non-negative.","messagePattern":"KeyCacheKeysToSave must be non-negative\\.","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/CacheService.java","lineNumber":258,"sourceCode":"    }\n\n    public void setRowCacheKeysToSave(int count)\n    {\n        if (count < 0)\n            throw new RuntimeException(\"RowCacheKeysToSave must be non-negative.\");\n        DatabaseDescriptor.setRowCacheKeysToSave(count);\n        rowCache.scheduleSaving(getRowCacheSavePeriodInSeconds(), count);\n    }\n\n    public int getKeyCacheKeysToSave()\n    {\n        return DatabaseDescriptor.getKeyCacheKeysToSave();\n    }\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()","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/CacheService.java#L240-L276","documentation":"CacheService.setKeyCacheKeysToSave sets the maximum number of key-cache entries persisted per save; negative counts are rejected with an untyped RuntimeException before DatabaseDescriptor is updated and keyCache saving is rescheduled. Typically invoked via the CacheService JMX mbean.","triggerScenarios":"Calling setKeyCacheKeysToSave(count) with count < 0 via JMX or internal callers.","commonSituations":"Automated tuning pipelines that derive KeyCacheKeysToSave from measurements which can go negative; manual JMX edits; assuming -1 means unlimited (it does not).","solutions":["Supply a non-negative count; use 0 if the saving limit should be effectively disabled.","Clamp the input with Math.max(0, count).","Correct the upstream metric/calculation that produced the negative value.","Add pre-call validation in any JMX automation that writes cache settings."],"exampleFix":"// before\nif (unlimited) mbean.setKeyCacheKeysToSave(-1);\n// after\nmbean.setKeyCacheKeysToSave(unlimited ? 0 : count);","handlingStrategy":"validation","validationCode":"if (count < 0) throw new IllegalArgumentException(\"KeyCacheKeysToSave must be >= 0\");\ncacheService.setKeyCacheKeysToSave(count);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Range-check all cache-tuning values before JMX calls.","Use 0 for 'no limit effective' semantics instead of negatives.","Verify upstream metrics used for sizing cannot be negative.","Add pre-flight validation to cache automation tooling."],"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"}