Long overflow in cache statistics
See original GitHub issueWe use Guava caches inside a large enterprise application.
After a few days of runtime calling the stats()
method on heavily-used caches throws an IllegalArgumentException
at the equivalent line to CacheStats.java:87, presumably because the total load time has overflowed a long and looped back round to a negative value.
It looks like this is down to the load time stopwatch being started at the point that an entry is submitted for asynchronous refresh, i.e. added to the refresh queue. Our largest queues are drained in batches and end-up with usual sizes in the tens of thousands — so the nanosecond-precision load time increases very quickly under these circumstances.
I am aware that it is a design decision for the cache statistics to be monotonically increasing, so a reasonably sane solution might be to simply drop the non-negative validation as part of the CacheStats constructor, or try to somehow wrap back to zero instead of Long.MIN_VALUE
. Metrics systems using similar to Graphite’s nonNegativeDerivative
should handle that gracefully anyway.
NB: I think this issue also affects Caffeine caches in the same way.
Issue Analytics
- State:
- Created 4 years ago
- Comments:25 (23 by maintainers)
Top GitHub Comments
LongAccumulator
would work, as you can push thesaturatedAdd
as the accumulation function.I think if we consider overflow non-deterministic and only promise to avoid exceptions, then it is not our responsibility to make counts correct. That means either is okay, and better handling is the user’s responsibility by providing an alternative
StatsCounter
(note - Guava doesn’t allow that currently).We could possibly use
LongAccumulator
withLongMath.saturatedAdd
?