{"record":{"id":"29143a4f9350a137","repo":"alibaba/Sentinel","slug":"cache-max-capacity-should-be-positive-size","errorCode":null,"errorMessage":"Cache max capacity should be positive: ${size}","messagePattern":"Cache max capacity should be positive: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java","lineNumber":37,"sourceCode":"\nimport com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;\nimport com.googlecode.concurrentlinkedhashmap.Weighers;\n\n/**\n * A {@link ConcurrentLinkedHashMap} wrapper for the universal {@link CacheMap}.\n *\n * @author Eric Zhao\n * @since 0.2.0\n */\npublic class ConcurrentLinkedHashMapWrapper<T, R> implements CacheMap<T, R> {\n\n    private static final int DEFAULT_CONCURRENCY_LEVEL = 16;\n\n    private final ConcurrentLinkedHashMap<T, R> map;\n\n    public ConcurrentLinkedHashMapWrapper(long size) {\n        if (size <= 0) {\n            throw new IllegalArgumentException(\"Cache max capacity should be positive: \" + size);\n        }\n        this.map = new ConcurrentLinkedHashMap.Builder<T, R>()\n            .concurrencyLevel(DEFAULT_CONCURRENCY_LEVEL)\n            .maximumWeightedCapacity(size)\n            .weigher(Weighers.singleton())\n            .build();\n    }\n\n    public ConcurrentLinkedHashMapWrapper(ConcurrentLinkedHashMap<T, R> map) {\n        if (map == null) {\n            throw new IllegalArgumentException(\"Invalid map instance\");\n        }\n        this.map = map;\n    }\n\n    @Override\n    public boolean containsKey(T key) {\n        return map.containsKey(key);","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/alibaba/Sentinel/blob/a3f40ba8e900c8489bd520274739f17235a7721c/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java#L19-L55","documentation":"ConcurrentLinkedHashMapWrapper is the CacheMap implementation backing Sentinel's hot-parameter flow statistics. Its constructor rejects a non-positive max capacity with IllegalArgumentException, because the underlying ConcurrentLinkedHashMap.Builder().maximumWeightedCapacity(size) requires a weight limit greater than zero. The capacity typically comes from the param flow rule's durationInSec-related cache sizing (e.g. ParameterMetric creation) or user-supplied cache size configuration.","triggerScenarios":"new ConcurrentLinkedHashMapWrapper<>(size) with size == 0 or negative; in practice, a ParamFlowStatistic/CacheMap creation path where the computed or configured cache capacity is 0 (e.g. misconfigured capacity constant or a rule/config value of 0 fed into cache construction).","commonSituations":"Setting a cache-capacity-related configuration to 0 intending \"unlimited\" (0 actually means invalid here); arithmetic that computes capacity from a difference that evaluates to 0 or negative; upgrading Sentinel where the cache wrapper constructor gained this validation.","solutions":["Pass a positive long as the max capacity, e.g. at least 1 (typical production values are thousands, like CacheMap defaults 4 * 1024)","Find where the size value originates (rule config or constant) and clamp it to a sane positive minimum before constructing the cache","If you intended \"no eviction\", pick a large bounded value instead of 0"],"exampleFix":"// before\nlong size = computeCapacity(rule); // may return 0\nCacheMap<Object, AtomicLong> cache = new ConcurrentLinkedHashMapWrapper<>(size);\n\n// after\nlong size = Math.max(1, computeCapacity(rule));\nCacheMap<Object, AtomicLong> cache = new ConcurrentLinkedHashMapWrapper<>(size);","handlingStrategy":"validation","validationCode":"long capacity = Math.max(1, configuredCacheCapacity);\nCacheMap<Object, AtomicLong> cache = new ConcurrentLinkedHashMapWrapper<>(capacity);","typeGuard":null,"tryCatchPattern":"try {\n    return new ConcurrentLinkedHashMapWrapper<>(size);\n} catch (IllegalArgumentException e) {\n    return new ConcurrentLinkedHashMapWrapper<>(DEFAULT_CAPACITY);\n}","preventionTips":["Clamp all size-derived config to a positive minimum before cache construction","Remember 0 means invalid here, not unlimited"],"tags":["sentinel","cache","constructor","validation"],"backgroundTag":null,"analyzedSha":"a3f40ba8e900c8489bd520274739f17235a7721c","analyzedAt":"2026-08-14T11:10:30.678Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}