{"record":{"id":"4521a06b1c132368","repo":"apache/cassandra","slug":"capacity-should-not-be-negative","errorCode":null,"errorMessage":"capacity should not be negative.","messagePattern":"capacity should not be negative\\.","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/CacheService.java","lineNumber":330,"sourceCode":"        {\n            CounterCacheKey key = counterCacheIterator.next();\n            if (key.sameTable(tableMetadata))\n                counterCacheIterator.remove();\n        }\n    }\n\n    public void invalidateCounterCache()\n    {\n        counterCache.clear();\n    }\n\n\n\n\n    public void setRowCacheCapacityInMB(long capacity)\n    {\n        if (capacity < 0)\n            throw new RuntimeException(\"capacity should not be negative.\");\n\n        rowCache.setCapacity(capacity * 1024 * 1024);\n    }\n\n\n    public void setKeyCacheCapacityInMB(long capacity)\n    {\n        if (capacity < 0)\n            throw new RuntimeException(\"capacity should not be negative.\");\n\n        keyCache.setCapacity(capacity * 1024 * 1024);\n    }\n\n    public void setCounterCacheCapacityInMB(long capacity)\n    {\n        if (capacity < 0)\n            throw new RuntimeException(\"capacity should not be negative.\");\n","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/CacheService.java#L312-L348","documentation":"CacheService.setRowCacheCapacityInMB resizes the row cache in MiB and rejects negative capacities with an untyped RuntimeException before calling rowCache.setCapacity. Cache capacity cannot be negative because it maps directly to a byte size (capacity * 1024 * 1024). Usually invoked via the CacheService JMX mbean at runtime.","triggerScenarios":"Calling setRowCacheCapacityInMB(capacity) with capacity < 0 via JMX or internal code.","commonSituations":"Auto-tuning scripts that compute new capacity from free-memory deltas which can go negative under memory pressure; manual JMX edits with a minus sign; configuration migrators passing negative sentinels.","solutions":["Pass a non-negative capacity in MiB; use 0 to disable the row cache.","Clamp with Math.max(0, capacityMB) before the call.","Fix the sizing calculation (e.g. freeMemory deltas) that produced the negative value.","Validate JMX inputs in automation before resizing caches."],"exampleFix":"// before\nlong newCap = currentFreeMB - usedMB;\nmbean.setRowCacheCapacityInMB(newCap); // may be negative\n// after\nlong newCap = Math.max(0, currentFreeMB - usedMB);\nmbean.setRowCacheCapacityInMB(newCap);","handlingStrategy":"validation","validationCode":"if (capacityMB < 0) throw new IllegalArgumentException(\"Row cache capacity must be >= 0 MiB\");\ncacheService.setRowCacheCapacityInMB(capacityMB);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Floor capacity calculations at 0 MiB.","Use 0 MiB to disable a cache rather than negative values.","Guard auto-tuning loops against negative free-memory deltas.","Persist validated values only after range checks."],"tags":["jmx","configuration","cache","memory"],"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"}