Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java collection with expiring entries

I need to track certain events over a specified time frame and act if the number of events reaches a certain number. In more detail, I connect to an external service and submit requests which are acknowledged with a status that equals CONF or FAIL. I need to be able to monitor the responses to detect if I get an unusual number of fails in a given time frame, e.g. >3 fails during the last 5 seconds, so that I can check for errors and act accordingly. I could alternatively check for 3 fails in a row but I prefer a time based approach.

I have been testing Guava's CacheLoader after reading this post but while entries (I only store FAIL-events) in the Cache appears to expire as expected, a call to size() (to determine number of fails) includes also the expired entries. This appears to be how it is supposed to work according to the documentation, if I have not misunderstood things?? Is there any way to get the number of 'active' events from a Cache?

I guess an alternative solution is to use a CEP-framework like Esper but it seems like overkill and cumbersome for my simple needs. Does anyone have a completely different approach to suggest that would facilitate my requirement? Thanks

like image 606
hgus1294 Avatar asked Feb 22 '23 08:02

hgus1294


2 Answers

Getting the exact number of active elements from the Cache would require locking the entire cache, which is extremely expensive. You might be able to use the cleanUp() method to make sure that size is not accidentally counting entries that have been quietly evicted, though.

I would not depend on this giving you exact results, but it should improve the accuracy of the results significantly.

like image 58
Louis Wasserman Avatar answered Mar 05 '23 15:03

Louis Wasserman


I think Guava collection with the nearest functionality to what you want is MinMaxPriorityQueue with a limited maximum size. You'd have to put failure events in chronological order and check periodically for the difference between first and last element and whether it is full.

But what you essentially want is a meter. You can try this Meter from Coda Hale's Metrics library.

like image 29
sanjary Avatar answered Mar 05 '23 14:03

sanjary