KAFKA-13971: Fix atomicity violations caused by improper usage of ConcurrentHashMap - part2#12281
KAFKA-13971: Fix atomicity violations caused by improper usage of ConcurrentHashMap - part2#12281divijvaidya wants to merge 3 commits intoapache:trunkfrom
Conversation
|
Is the relevant code specified as thread safe? |
Thank you for your review @ijuma. I appreciate it. Though, I am afraid I don't understand your question. Are you asking whether the existing code is supposed to be thread safe? Are you asking whether the changed code is thread safe? |
9945769 to
5e4401f
Compare
|
@C0urante please review when you get a chance. |
| public void addMetricsRecorder(final RocksDBMetricsRecorder metricsRecorder) { | ||
| final String metricsRecorderName = metricsRecorderName(metricsRecorder); | ||
| if (metricsRecordersToTrigger.containsKey(metricsRecorderName)) { | ||
| final RocksDBMetricsRecorder existingRocksDBMetricsRecorder = metricsRecordersToTrigger.putIfAbsent(metricsRecorderName, metricsRecorder); |
There was a problem hiding this comment.
This is a nice improvement in readability, but are we certain it's necessary? I commented on the change to the plugin scanning logic in Connect because I'm familiar with that part of the code base; I don't have the same familiarity with Streams, though.
I think it's fine to merge this change, but if this method isn't intended to be invoked concurrently, we should modify the PR title so that the commit message doesn't imply this is a bug fix and instead recognizes it as a cosmetic improvement.
There was a problem hiding this comment.
I agree with @C0urante here, if the method isn't meant to be invoked in a concurrent situation then we should either revert to the original containsKey OR add a comment along the lines of "Currently this code isn't called in a concurrent situation"
There was a problem hiding this comment.
Thanks for your code review folks. On deeper analysis, it doesn't look like that this code will be invoked by multiple threads. I will discard this PR now and I thank you again for looking into this PR.
## Problem #1 in DelegatingClassLoader.javaAtomicity violation in example such as:
Consider thread T1 reaches line 228, but before executing context switches to thread T2 which also reaches line 228. Again context switches to T1 which reaches line 232 and adds a value to the map. T2 will execute line 228 and creates a new map which overwrites the value written by T1, hence change done by T1 would be lost. This code change ensures that two threads cannot initiate the TreeMap, instead only one of them will.
Problem #2 in RocksDBMetricsRecordingTrigger.java
Atomicity violation in example such as:
Consider thread T1 reaches line 40 but before executing it context switches to thread T2 which also reaches line 40. In a serialized execution order, thread T2 should have thrown the exception but it won't in this case. The code change fixes that.
Note that some other problems associated with use of concurrent hashmap has been fixed in #12277