Prevent race when performing cache maintenance - #125
Open
mezz wants to merge 2 commits into
Open
Conversation
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
There is a race condition, where maintenance can run after a cache hit is confirmed but before the files can be written.
Fix
To simplify the things I need to protect from races, I moved the "writing" logic out of
RunNeoFormCommand#executeand intoNeoFormEngine#writeResults, then I sealed off everything else inNeoFormEngineasprivateso that it only has that one public entry-point.From there, I could lock the whole method with a read lock, and made it so that the cache maintenance needs to get a write lock so it cannot run when the NeoFormEngine is active.
ReadWriteFileLock
That just left one problem: we need a way for multiple threads and processes across multiple JVMs to have a read-write lock, and there's no re-entrant ReadWrite File Lock in Java.
Unfortunately (sorry) I created a new library to support this: https://github.com/mezz/ReadWriteFileLock
Please see the readme for justification for why I created this instead of using another library.
I wrote a test suite based on studying the other implementations, so it should cover all the necessary requirements for this project.