Repository navigation
ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap - #789
Conversation
…there is a race to add the vector to postingsMap
|
I don't love splitting out the |
|
Note, I can reproduce the problem (and confirm this fixes it) using an external Python script, but moving it into Java code does not reproduce the problem. Possibly there is some junk in the test machinery that reduces the effective concurrency. |
michaeljmarshall
left a comment
There was a problem hiding this comment.
I don't fully understand the fix yet, but since you can reproduce the bug and then show that this bug fixes it, I am approving the PR. I'll follow up if I have any questions.
| // in the other structures as well. | ||
| var postings = postingsMap.computeIfAbsent(vector, v -> { | ||
| var bytes = RamEstimation.concurrentHashMapRamUsed(1); // the new posting Map entry | ||
| var ordinal = nextOrdinal.getAndIncrement(); |
There was a problem hiding this comment.
Why wasn't computeIfAbsent in conjunction with and AtomicInteger correct? I am trying to see it, but I haven't seen it yet.
There was a problem hiding this comment.
Found it. The computeIfAbsent in ConcurrentSkipListMap is called optimistically, which resulted in skipping ordinal values, and incorrect state in the rest of the map class.
/**
* If the specified key is not already associated with a value,
* attempts to compute its value using the given mapping function
* and enters it into this map unless {@code null}. The function
* is <em>NOT</em> guaranteed to be applied once atomically only
* if the value is not present.
*
* @param key key with which the specified value is to be associated
* @param mappingFunction the function to compute a value
* @return the current (existing or computed) value associated with
* the specified key, or null if the computed value is null
* @throws NullPointerException if the specified key is null
* or the mappingFunction is null
* @since 1.8
*/
public V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
if (key == null || mappingFunction == null)
throw new NullPointerException();
V v, p, r;
if ((v = doGet(key)) == null &&
(r = mappingFunction.apply(key)) != null)
v = (p = doPut(key, r, true)) == null ? r : p;
return v;
}There was a problem hiding this comment.
It's particularly interesting because a concurrent hashmap does apply the function atomically once.
There was a problem hiding this comment.
Ah, no wonder that tripped me up. 99% of the time I'm using CHM and I could have sworn this method made an exactly-once guarantee. I feel slightly less dumb now.
There was a problem hiding this comment.
Me too! I was reading your solution and couldn't see how it fixed the concurrency bug.
There was a problem hiding this comment.
As a side benefit, I think the new code is less fragile without the special casing around newVector
There was a problem hiding this comment.
Definitely. One last nit is that since we aren't able to write a test for it, would you add a comment to prevent incorrect refactoring?
|
Kudos, SonarCloud Quality Gate passed!
|
…there is a race to add the vector to postingsMap (#789) * ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap * mark ordinal volatile * comment
…there is a race to add the vector to postingsMap (#789) * ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap * mark ordinal volatile * comment
…there is a race to add the vector to postingsMap (#789) * ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap * mark ordinal volatile * comment
…there is a race to add the vector to postingsMap (#789) * ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap * mark ordinal volatile * comment
…there is a race to add the vector to postingsMap (#789) * ensure that ordinal only gets incremented once per vector, even when there is a race to add the vector to postingsMap * mark ordinal volatile * comment









No description provided.