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
Evicition with CacheWriter.delete does not appear atomic to Cache clients #186
Comments
It is atomic to other writes, as the removal operation is performed with a Most likely you need to perform some type of liveliness validation, e.g. in your gist retry if the read is determined to be stale. You might prefer using phantom references for resource cleanup instead to ensure no direct usage. Its hard to advise without more details, as unfortunately these are the types of races you have to design for. |
Actually in your case of lock striping the entry, there may not be a large penalty of using a |
Thanks for the lightning fast response. I guess the bit that puzzled me is why my separate lock doesn't protect me from this case. So what's happening is that the thread doing the eviction does a In my real code the external lock is actually a The other thought I had was to maybe use a |
When the If all operations were under a I would probably have the read/write operations validate under lock and retry if they have a dead entry. That requires using a loop around the |
Just tried using Thanks for your help! |
Oh, right. The The validate or retry seems to work in your test code. Good luck! |
Just thought I'd mention how I solved this in the end in case anyone runs across a similar scenario: Instead of a |
Nice job. You can also use |
I'm using a
LoadingCache
to manage meta-data for a cache where the actual data is stored in files on disk; theCacheWriter.delete
callback is used to delete the file from disk when the corresponding cache entry is evicted.Overall this seems to be working OK, however I am seeing cases where cached entries seem to still being returned even though
CacheWriter.delete
has already been called, which then results in a FileNotFoundException in the code trying to use that cache entry. Note that I do have additional locking in place outside of the cache in the form of aMap<CacheKey, Lock>
, and both the cache read (including the subsequent file access) and the delete callback are guarded by this lock.This gist demonstrates the issue (I'm using Caffeine 2.5.5) https://gist.github.com/ksperling/241c865526a2df9ad0dc9d02dac4393c
Not sure if this is a bug or if I'm interpreting the guarantees around CacheWriter incorrectly?
The text was updated successfully, but these errors were encountered: