Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add .clearStats() to Cache Interface #215

Closed
gmazza opened this issue Jan 8, 2018 · 3 comments
Closed

Feature Request: Add .clearStats() to Cache Interface #215

gmazza opened this issue Jan 8, 2018 · 3 comments

Comments

@gmazza
Copy link

gmazza commented Jan 8, 2018

When calling cache.invalidateAll(), I would also like to erase the CacheStats object returned by Cache.stats() so the stats all start at zero again. I can't see any way to that with Caffeine currently. One way I can see is to add .clearStats() to the Cache object, another option would be to add a boolean to the Cache.invalidateAll() method, i.e., .invalidateAll(boolean clearStats).

@ben-manes
Copy link
Owner

The statistics are documented as "monotonically increasing over the lifetime of the cache" so this is not directly supported. You can emulate it using the CacheStats.minus method, e.g.

volatile CacheStats snapshot = CacheStats.empty();

void clear() {
  cache.invalidateAll();
  snapshot = cache.stats();
}

CacheStats stats() {
  return cache.stats().minus(snapshot);
}

Alternatively, you can provide your own StatsCounter and reset the metrics.

Generally this isn't a problem if you export your statistics into a time series database, e.g. using Dropwizard Metrics. Then one can run computations, like a time window, over the data set. These systems tend to expect increasing values rather than each actor performing its own computations, like histograms, by delegating that work to the metrics application.

@ben-manes
Copy link
Owner

ben-manes commented Jan 8, 2018

The rational when designing this for Guava (Kevin's comment):

The stats that tend to matter most are things like "stats over the last 2 minutes" and "stats over the last 15 minutes" etc. If you reset, you'll be resetting constantly! And you will have to pick one increment or the other. If you reset every minute, then to get the stats for the last hour you have 60 things to sum up.

In contrast, just saving absolute snapshots and diffing any two endpoints you want to is a lot simpler and more flexible.

Google's monitoring tools are designed so that components who want to report stats only have to worry about reporting monotonically increasing counts, and they handle the rest, which I think is a good approach that keeps the burden of things like "resetting" from being shoved onto every individual component that wants to report stats.

@gmazza
Copy link
Author

gmazza commented Jan 9, 2018

Hi Ben, thanks for the alternative implementation suggestion! (This resetting, user-initiated, is so rare I'm considering also just recreating the cache when it occurs.) Closing this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants