{"record":{"id":"2f779902bb8f4c7c","repo":"apache/cassandra","slug":"keycachesaveperiodinseconds-must-be-non-negative","errorCode":null,"errorMessage":"KeyCacheSavePeriodInSeconds must be non-negative.","messagePattern":"KeyCacheSavePeriodInSeconds must be non-negative\\.","errorType":"validation","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/CacheService.java","lineNumber":217,"sourceCode":"\n    public void setRowCacheSavePeriodInSeconds(int seconds)\n    {\n        if (seconds < 0)\n            throw new RuntimeException(\"RowCacheSavePeriodInSeconds must be non-negative.\");\n\n        DatabaseDescriptor.setRowCacheSavePeriod(seconds);\n        rowCache.scheduleSaving(seconds, DatabaseDescriptor.getRowCacheKeysToSave());\n    }\n\n    public int getKeyCacheSavePeriodInSeconds()\n    {\n        return DatabaseDescriptor.getKeyCacheSavePeriod();\n    }\n\n    public void setKeyCacheSavePeriodInSeconds(int seconds)\n    {\n        if (seconds < 0)\n            throw new RuntimeException(\"KeyCacheSavePeriodInSeconds must be non-negative.\");\n\n        DatabaseDescriptor.setKeyCacheSavePeriod(seconds);\n        keyCache.scheduleSaving(seconds, DatabaseDescriptor.getKeyCacheKeysToSave());\n    }\n\n    public int getCounterCacheSavePeriodInSeconds()\n    {\n        return DatabaseDescriptor.getCounterCacheSavePeriod();\n    }\n\n    public void setCounterCacheSavePeriodInSeconds(int seconds)\n    {\n        if (seconds < 0)\n            throw new RuntimeException(\"CounterCacheSavePeriodInSeconds must be non-negative.\");\n\n        DatabaseDescriptor.setCounterCacheSavePeriod(seconds);\n        counterCache.scheduleSaving(seconds, DatabaseDescriptor.getCounterCacheKeysToSave());\n    }","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/CacheService.java#L199-L235","documentation":"CacheService.setKeyCacheSavePeriodInSeconds validates that the key-cache autosave interval is not negative before storing it and rescheduling the key cache saver. A negative value has no meaning as a periodic save interval, so the setter rejects it eagerly with an untyped RuntimeException. This setter is typically invoked via JMX at runtime rather than from application code.","triggerScenarios":"Calling CacheService.setKeyCacheSavePeriodInSeconds(seconds) with seconds < 0, e.g. via JMX (org.apache.cassandra.service.CacheService mbean) or internal code passing a negative duration value.","commonSituations":"Operators pushing a runtime JMX config change computed from a bad expression (e.g. unit conversion yielding a negative number), scripts that subtract timestamps, or hand-editing JMX console fields with a minus sign.","solutions":["Pass a non-negative value: clamp with Math.max(0, seconds) before calling the setter.","Use 0 to disable periodic key cache saving instead of a negative number.","Check the source of the value (config parsing, unit conversion) for sign errors.","If the intent is a disabled interval, also consider removing the runtime update entirely so the cassandra.yaml value is kept."],"exampleFix":"// before\ncacheService.setKeyCacheSavePeriodInSeconds(getInterval());\n// after\nint seconds = getInterval();\nif (seconds >= 0)\n    cacheService.setKeyCacheSavePeriodInSeconds(seconds);","handlingStrategy":"validation","validationCode":"if (seconds < 0) throw new IllegalArgumentException(\"KeyCacheSavePeriodInSeconds must be >= 0\");\ncacheService.setKeyCacheSavePeriodInSeconds(seconds);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat all CacheService JMX setters as accepting non-negative values only.","Use 0 to disable periodic cache saving, never negative numbers.","Validate computed durations at the config layer before pushing via JMX.","Sanity-check unit conversions (ms/s/min) for sign errors."],"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"}