-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[tryout fix] concurrency bug in NamespacedHierarchicalStore#computeIfAbsent(Object, Object, Function) #5171 #5209
#5223
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
Closed
+918
−17
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,7 +214,7 @@ public void close() { | |
| StoredValue storedValue = getStoredValue(compositeKey); | ||
| if (storedValue == null) { | ||
| storedValue = this.storedValues.computeIfAbsent(compositeKey, | ||
| __ -> newStoredValue(new MemoizingSupplier(() -> { | ||
| __ -> newStoredValue(new MemorizingSupplier(() -> { | ||
| rejectIfClosed(); | ||
| return defaultCreator.apply(key); | ||
| }))); | ||
|
|
@@ -240,27 +240,35 @@ public void close() { | |
| @API(status = MAINTAINED, since = "6.0") | ||
| public <K, V> Object computeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator) { | ||
| Preconditions.notNull(defaultCreator, "defaultCreator must not be null"); | ||
| CompositeKey<N> compositeKey = new CompositeKey<>(namespace, key); | ||
| StoredValue storedValue = getStoredValue(compositeKey); | ||
| var compositeKey = new CompositeKey<>(namespace, key); | ||
| var storedValue = getStoredValue(compositeKey); | ||
| var result = StoredValue.evaluateIfNotNull(storedValue); | ||
| if (result == null) { | ||
| StoredValue newStoredValue = this.storedValues.compute(compositeKey, (__, oldStoredValue) -> { | ||
| if (StoredValue.evaluateIfNotNull(oldStoredValue) == null) { | ||
| rejectIfClosed(); | ||
| var computedValue = Preconditions.notNull(defaultCreator.apply(key), | ||
| "defaultCreator must not return null"); | ||
| return newStoredValue(() -> { | ||
| rejectIfClosed(); | ||
| return computedValue; | ||
| }); | ||
| var value = storedValues.compute(compositeKey, | ||
| (__, currentValue) -> currentValue == null || currentValue.equals(storedValue) | ||
| ? storeNewValue(key, defaultCreator) | ||
| : currentValue); | ||
| try { | ||
| return requireNonNull(value.evaluate()); | ||
| } | ||
| catch (Throwable t) { // remove failed entry to allow retry. | ||
| if (value.equals(storedValues.get(compositeKey))) { | ||
| storedValues.remove(compositeKey, value); | ||
| } | ||
| return oldStoredValue; | ||
| }); | ||
| return requireNonNull(newStoredValue.evaluate()); | ||
| throw t; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private <K, V> StoredValue storeNewValue(K key, Function<? super K, ? extends V> defaultCreator) { | ||
| rejectIfClosed(); | ||
| return newStoredValue(new MemorizingSupplier(() -> { | ||
| rejectIfClosed(); | ||
| return requireNonNull(defaultCreator.apply(key)); | ||
| })); | ||
| } | ||
|
|
||
| /** | ||
| * Get the value stored for the supplied namespace and key in this store or | ||
| * the parent store, if present, or call the supplied function to compute it | ||
|
|
@@ -469,7 +477,7 @@ private void close(CloseAction<N> closeAction) throws Throwable { | |
| * | ||
| * @see StoredValue | ||
| */ | ||
| private static class MemoizingSupplier implements Supplier<@Nullable Object> { | ||
| private static class MemorizingSupplier implements Supplier<@Nullable Object> { | ||
|
|
||
| private static final Object NO_VALUE_SET = new Object(); | ||
|
|
||
|
|
@@ -478,7 +486,7 @@ private static class MemoizingSupplier implements Supplier<@Nullable Object> { | |
| @Nullable | ||
| private volatile Object value = NO_VALUE_SET; | ||
|
|
||
| private MemoizingSupplier(Supplier<@Nullable Object> delegate) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like a typo missing
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They're similar words but mean different things. |
||
| private MemorizingSupplier(Supplier<@Nullable Object> delegate) { | ||
| this.delegate = delegate; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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 see that it is harder to review. On the other hand, it is also difficult (for me) to access this code without. This can be helpful when working on a close, low-level perspective.
I do not consider code at all, because everything ultimately becomes some kind of random implementation detail.
If I need to consider code seriously, I must structure it properly and understand how it actually flows, while adhering to best practices and SOLID design principles that reflect what the code is truly doing and how it whispers how it wants to be structured. I know not everyone has the sense to recognize this level of intent.
I cannot process an endless number of requirements and concerns. I can only operate like a computer: one thing at a time, within a very limited scope—effectively binary. While 0 and 1 represent two states, they never truly add up to 2. Because of this, I have to work at a one-to-one level; anything beyond that exceeds my strict mental limitations. Please excuse my shortcomings in this regard.