Skip to content

Avoid adding values to cache if the current value is the same as adding value #6673

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

Merged
merged 11 commits into from
May 22, 2025

Conversation

RushanNanayakkara
Copy link
Contributor

@RushanNanayakkara RushanNanayakkara commented Apr 9, 2025

Purpose

When there is a cache miss the value is added to the cache. But in a clustered environment many nodes may simultaneously read the initial value as null and then try to add the value. But at the time of adding the value, the first one to complete adding the value succeeds but other processes proceed to update the value instead of adding the value.

However while adding doesn't invalidate cache, updating does. But since if the new value is the same as the existing one, there's no need to update the value and then invalidate the cache. This behaviour causes unnecessary cache invalidations in clustered environments. This then leads to unnecessary db reads to retrieve the same value again and again.

Changes of this PR performs an equality check between the new value and the value in the cache and skips updating if the values are the same. This avoids unnecessary cache invalidations.

Related Issue/s

Dependent on

@RushanNanayakkara RushanNanayakkara changed the title Avoid adding values to cache if the current value is the same as addi… Avoid adding values to cache if the current value is the same as adding value Apr 9, 2025
@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/14357216988

Copy link

codecov bot commented Apr 9, 2025

Codecov Report

Attention: Patch coverage is 53.12500% with 15 lines in your changes missing coverage. Please review.

Project coverage is 48.69%. Comparing base (f4d10c6) to head (9549119).
Report is 35 commits behind head on master.

Files with missing lines Patch % Lines
...org/wso2/carbon/identity/core/cache/BaseCache.java 31.25% 9 Missing and 2 partials ⚠️
...arbon/identity/claim/metadata/mgt/model/Claim.java 66.66% 1 Missing and 1 partial ⚠️
...aim/metadata/mgt/dao/CacheBackedLocalClaimDAO.java 0.00% 1 Missing ⚠️
.../identity/claim/metadata/mgt/model/LocalClaim.java 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #6673      +/-   ##
============================================
+ Coverage     47.73%   48.69%   +0.96%     
+ Complexity    16806    16687     -119     
============================================
  Files          1937     1881      -56     
  Lines        113988   110677    -3311     
  Branches      21504    20999     -505     
============================================
- Hits          54416    53898     -518     
+ Misses        52284    49510    -2774     
+ Partials       7288     7269      -19     
Flag Coverage Δ
unit 32.85% <53.12%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/14357216988
Status: success

Copy link

@jenkins-is-staging jenkins-is-staging left a comment

Choose a reason for hiding this comment

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

Approving the pull request based on the successful pr build https://github.com/wso2/product-is/actions/runs/14357216988

Thumimku
Thumimku previously approved these changes Apr 17, 2025
@RushanNanayakkara
Copy link
Contributor Author

This PR is dependent on a kernel PR. Hence until a Kernel release happens this will not be merged.

@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/14656903858

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/14656903858
Status: failure

@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/14922475855

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/14922475855
Status: failure

@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/15179735905

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/15179735905
Status: success

Copy link

@jenkins-is-staging jenkins-is-staging left a comment

Choose a reason for hiding this comment

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

Approving the pull request based on the successful pr build https://github.com/wso2/product-is/actions/runs/15179735905

Claim claim = (Claim) o;
return Objects.equals(claimDialectURI, claim.claimDialectURI)
&& Objects.equals(claimURI, claim.claimURI)
&& Objects.equals(claimProperties, claim.claimProperties);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do maps equal check work with Objects.equals? Please double check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Map.equals checks for content equality.

@@ -0,0 +1,140 @@
package org.wso2.carbon.identity.claim.metadata.mgt.model;
Copy link
Contributor

Choose a reason for hiding this comment

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

License headers

Copy link
Contributor

Choose a reason for hiding this comment

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

Check other new files as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed with 9549119

assertEquals(localClaim.getMappedAttributes().size(), 1, "Mapped attribute should be added correctly");
assertEquals(localClaim.getMappedAttributes().get(0).getAttributeName(), "email", "Mapped attribute name should match");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

add a new line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed with 9549119

}

try {
startTenantFlow(tenantId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Will the startTenantFlow work with both tenantId and tenantDomain?? Please check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

startTenantFlow(tenantId);
Cache<K, V> cache = getBaseCache();
if (cache != null) {
cache.putIfNoDuplicate(key, entry);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a method given by baseCache or something newly implemented?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's newly implemented

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link

@RushanNanayakkara RushanNanayakkara merged commit 9307a3b into wso2:master May 22, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants