-
Notifications
You must be signed in to change notification settings - Fork 656
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
base: 4.6.x-old
Are you sure you want to change the base?
Conversation
…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) { |
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.
Better to start synchronized
block only if if (roleListOfUserFromCache == null) {
right?
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.
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.
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.
@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;}
PR builder started |
PR builder completed |
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.
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); |
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.
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) { |
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.
@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;}
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