Skip to content
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

Fix caching mechanism for Sagas #2517

Merged
merged 11 commits into from Dec 19, 2022
Merged

Fix caching mechanism for Sagas #2517

merged 11 commits into from Dec 19, 2022

Conversation

CodeDrivenMitch
Copy link
Member

@CodeDrivenMitch CodeDrivenMitch commented Dec 15, 2022

This PR expands the Cache interface with a computeIfAbsent(Object key, Supplier<T> valueSupplier) method, which lazily fetches data when data is not found in the cache.

The current implementation always queries the Saga store, and puts it in the cache afterwards. The NPE experienced by a customer might be because of eventual consistency in the cache. We get it, right after we put it, so it might not have replicated yet (this depends on the configuration) and produce a null value.

This is not the case when using the new method. This will check the cache and only query for the value when necessary. In addition, it will return the produced value instead of the value in the cache, ensuring there actually is a value upon return.

@CodeDrivenMitch CodeDrivenMitch requested review from abuijze, a team, gklijs and smcvb and removed request for a team December 15, 2022 07:04
@CodeDrivenMitch CodeDrivenMitch self-assigned this Dec 15, 2022
@CodeDrivenMitch CodeDrivenMitch added Type: Bug Use to signal issues that describe a bug within the system. Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Status: In Progress Use to signal this issue is actively worked on. labels Dec 15, 2022
@CodeDrivenMitch CodeDrivenMitch added this to the Release 4.6.3 milestone Dec 15, 2022
- Fix indentations
- Solve warnings
- Make Cache#computeIfAbsent a default implementation, throwing an
unsupported exception when not implemented.

#2517
smcvb
smcvb previously requested changes Dec 15, 2022
Copy link
Member

@smcvb smcvb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good, but as discussed offline, an integration test validating bulk operations against a cached Saga work would be a great addition. Let's get that in, before we merge this.

@smcvb smcvb self-assigned this Dec 15, 2022
Add integration tests for EhCache and WeakReferenceCache, using a common
 test suite for validation.

#2517
CodeDrivenMitch and others added 4 commits December 19, 2022 11:01
…exception if the value supplier returns null to differentiate between faulty cache and faulty saga stores
Provide working default implementation of computeIfAbsent. As, if users
do have their own implementation of the Cache, their migration to a more
 recent version of Axon Framework would horribly break.

#2517
- Consistently use newValue as the result of the valueSupplier
- Suppress warnings

#2517
- Move common used fields to constants
- Remove manual cache clearing, as that's a job of the IT
implementations to define when these are cleared.
- Maintain references to the associations set, to ensure the weak
reference implementation doesn't fail on the missing associations
- Validate whether the saga cache entry is present before verifying the
contents, instead of an assertion. This it to cover for cache
implementation that may have cleared out the entries between the (bulk)
updates and validation. As this is a perfectly valid scenario, just
don't validate the cache if it's not there instead of breaking the test
case.
- Suppress unimportant warnings

#2517
@smcvb smcvb dismissed their stale review December 19, 2022 12:33

As I'm working on these changes myself, my review may be dismissed.

@smcvb smcvb requested a review from abuijze December 19, 2022 12:33
By introducing the default implementation on the Cache interface, we can
 get rid of the concrete implementation on the EhCache and JCache
 adapters.

#2517
Copy link
Member

@abuijze abuijze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitty gritty stuff

- Reuse existingValue instead of retrieving it from the entry.
- Use ObjectUtils.getOrDefault

#2517
Copy link
Member

@smcvb smcvb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good as it is right now...let's wait for a third pair of eyes before merging, though.

@smcvb
Copy link
Member

smcvb commented Dec 19, 2022

As discussed offline, we (Mitchell, Allard, me) approve of this current solution.
As such, I will proceed and merge this fix.

@smcvb smcvb merged commit 032f4ac into axon-4.6.x Dec 19, 2022
@smcvb smcvb deleted the bugfix/saga-caching-npe branch December 19, 2022 13:22
@smcvb smcvb added Status: Resolved Use to signal that work on this issue is done. and removed Status: In Progress Use to signal this issue is actively worked on. labels Dec 19, 2022
@sonarcloud
Copy link

sonarcloud bot commented Dec 19, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

37.3% 37.3% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority 1: Must Highest priority. A release cannot be made if this issue isn’t resolved. Status: Resolved Use to signal that work on this issue is done. Type: Bug Use to signal issues that describe a bug within the system.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants