-
Notifications
You must be signed in to change notification settings - Fork 74
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
[JENKINS-65821] Fix race condition by using ConcurrentHashMap #160
Conversation
965dd97
to
ea384e4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess? I trust you have examined the code flow carefully and verified that there is no risk of one thread having written to just one of these maps when another thread comes along and tries to read from them. I suppose it cannot be worse than using HashMap
. @dwnusbaum mentioned that the usages here were idempotent and thus safe to use locklessly.
I was wondering if one possible fix would be, in each entry point of a |
@jglick From what I can see, this is approach is safe. As you say, idempotent, the two caches do not depend on each other to update, and any read failure results in calling the "brute force" method the recalculate missing values. |
[JENKINS-65821] Fix race condition by using ConcurrentHashMap
@@ -25,10 +25,10 @@ | |||
static final String INCOMPLETE = ""; | |||
|
|||
/** Map the blockStartNode to its endNode, to accellerate a range of operations */ | |||
HashMap<String, String> blockStartToEnd = new HashMap<>(); | |||
ConcurrentHashMap<String, String> blockStartToEnd = new ConcurrentHashMap<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fine since the value type is just String
that already exists. Just a reminder that in general you need to be careful about having one thread add an entry to a map (or whatever) that another thread might pull off without the use of any synchronization barriers: if the object were recently constructed, its constructor might not have completed by the time the other thread uses it. One of those weird theoretical problems that probably does not happen often in reality but is hard to reason about.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the object were recently constructed, its constructor might not have completed by the time the other thread uses it.
Wow, good to know. Thanks!
JENKINS-65821 described a race condition resulting in high cpu usage. #153 attempted to address these using locking, but resulted in deadlocks.
This change goes the other direction and attempts to address the issue using
ConcurrentHashMap
instead ofHashMap
.@twasyl