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

Synchronize user role retrieval from DB and adding it to user role cache #2899

Draft
wants to merge 3 commits into
base: 4.6.x-old
Choose a base branch
from

Conversation

DinikaSen
Copy link

Purpose

This PR introduces thread synchronization to the process of 'user role retrieval from DB and adding it to user role cache' to avoid multiple threads querying the user roles from DB for the same user concurrently, and update the user role cache with the same value retrieved from DB concurrently.

Resolves : wso2/product-is#10869

…id multiple threads trying to get user roles from DB for the same user concurrently.
synchronized (userID.intern()) {
if (username != null) {
String[] roleListOfUserFromCache = getRoleListOfUserFromCache(this.tenantId, username);
if (roleListOfUserFromCache != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to start synchronized block only if if (roleListOfUserFromCache == null) { right?

Copy link
Author

Choose a reason for hiding this comment

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

Here the if (roleListOfUserFromCache == null) check is done inside the synchronized block for double-checked locking purpose. There is another if (roleListOfUserFromCache == null) check in the calling method right before this method is called.

Copy link

Choose a reason for hiding this comment

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

@DinikaSen There is a pattern to follow when using synchronise block. Always advisable to follow the pattern so that code and logic is simpler and less error prone

if (! checkCondition() ) {
syncronized (lock) {
if (checkCondition() ) {exit;}

darshanasbg
darshanasbg previously approved these changes Jan 18, 2021
@jenkins-is-staging
Copy link

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

@jenkins-is-staging
Copy link

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

Copy link

@ruwanta ruwanta left a comment

Choose a reason for hiding this comment

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

Need to improve the syncrhonize block to have double checking.

return getUserRoles(userName, filter);
String usernameWithTenantDomain = userName + "@" + this.getTenantDomain(this.tenantId);
synchronized (usernameWithTenantDomain.intern()) {
roleList = getRoleListOfUserFromCache(this.tenantId, userName);
Copy link

Choose a reason for hiding this comment

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

roleList = getRoleListOfUserFromCache(this.tenantId, userName); has to happen outside the synchronise block. Otherwise the synchronise block is called for each request and thus may cause unnecessary locks.

synchronized (userID.intern()) {
if (username != null) {
String[] roleListOfUserFromCache = getRoleListOfUserFromCache(this.tenantId, username);
if (roleListOfUserFromCache != null) {
Copy link

Choose a reason for hiding this comment

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

@DinikaSen There is a pattern to follow when using synchronise block. Always advisable to follow the pattern so that code and logic is simpler and less error prone

if (! checkCondition() ) {
syncronized (lock) {
if (checkCondition() ) {exit;}

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