-
Notifications
You must be signed in to change notification settings - Fork 658
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
DinikaSen
wants to merge
3
commits into
wso2:4.6.x-old
Choose a base branch
from
DinikaSen:user-role-cache-issue1
base: 4.6.x-old
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -8402,22 +8402,43 @@ public List<String> doGetRoleListOfUserWithID(String userID, String filter) thro | |
return (List<String>) object; | ||
} | ||
|
||
List<String> roleList; | ||
String username = getUserNameFromUserID(userID); | ||
if (username != null) { | ||
String[] roleListOfUserFromCache = getRoleListOfUserFromCache(this.tenantId, username); | ||
if (roleListOfUserFromCache != null) { | ||
List<String> roleList = Arrays.asList(roleListOfUserFromCache); | ||
roleList = Arrays.asList(roleListOfUserFromCache); | ||
if (!roleList.isEmpty()) { | ||
return roleList; | ||
} | ||
} | ||
} | ||
|
||
return getUserRolesWithID(userID, filter); | ||
synchronized (userID.intern()) { | ||
if (username != null) { | ||
String[] roleListOfUserFromCache = getRoleListOfUserFromCache(this.tenantId, username); | ||
if (roleListOfUserFromCache != null) { | ||
roleList = Arrays.asList(roleListOfUserFromCache); | ||
if (!roleList.isEmpty()) { | ||
return roleList; | ||
} | ||
} | ||
} | ||
String[] roleListOfUserFromDatabase = getUserRolesWithIDFromDatabase(userID, filter); | ||
roleList = Arrays.asList(roleListOfUserFromDatabase); | ||
// Add to user role cache using username. | ||
if (username != null) { | ||
addToUserRolesCache(this.tenantId, username, roleListOfUserFromDatabase); | ||
} | ||
} | ||
return roleList; | ||
} | ||
|
||
private List<String> getUserRolesWithID(String userID, String filter) throws UserStoreException { | ||
private String[] getUserRolesWithIDFromDatabase(String userID, String filter) throws UserStoreException { | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug("Retrieving user role list for userID: " + userID + " from database"); | ||
} | ||
List<String> internalRoles = doGetInternalRoleListOfUserWithID(userID, filter); | ||
Set<String> modifiedInternalRoles = new HashSet<>(); | ||
String[] modifiedExternalRoleList = new String[0]; | ||
|
@@ -8452,13 +8473,7 @@ private List<String> getUserRolesWithID(String userID, String filter) throws Use | |
} | ||
} | ||
} | ||
|
||
// Add to user role cache uisng username. | ||
String username = getUserNameFromUserID(userID); | ||
if (username != null) { | ||
addToUserRolesCache(this.tenantId, username, roleList); | ||
} | ||
return Arrays.asList(roleList); | ||
return roleList; | ||
} | ||
|
||
/** | ||
|
@@ -8482,11 +8497,23 @@ public final String[] doGetRoleListOfUser(String userName, String filter) throws | |
return roleList; | ||
} | ||
|
||
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 commentThe 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. |
||
if (roleList != null && roleList.length > 0) { | ||
return roleList; | ||
} | ||
roleList = getUserRolesFromDatabase(userName, filter); | ||
addToUserRolesCache(this.tenantId, userName, roleList); | ||
} | ||
return roleList; | ||
} | ||
|
||
private String[] getUserRoles(String username, String filter) throws UserStoreException { | ||
private String[] getUserRolesFromDatabase(String username, String filter) throws UserStoreException { | ||
|
||
if (log.isDebugEnabled()) { | ||
log.debug("Retrieving user role list for user: " + username + " from database"); | ||
} | ||
String[] internalRoles = doGetInternalRoleListOfUser(username, filter); | ||
String[] modifiedExternalRoleList = new String[0]; | ||
|
||
|
@@ -8520,7 +8547,6 @@ private String[] getUserRoles(String username, String filter) throws UserStoreEx | |
} | ||
} | ||
} | ||
addToUserRolesCache(this.tenantId, username, roleList); | ||
return roleList; | ||
} | ||
|
||
|
@@ -8547,9 +8573,9 @@ public final String[] getRoleListOfUserFromDatabase(String username, String filt | |
// According to implementation, getRoleListOfUser method would return everyone role name for all users. | ||
return new String[]{realmConfig.getEveryOneRoleName()}; | ||
} | ||
return getUserRolesWithID(userID, filter).toArray(new String[0]); | ||
return getUserRolesWithIDFromDatabase(userID, filter); | ||
} else { | ||
return getUserRoles(username, filter); | ||
return getUserRolesFromDatabase(username, filter); | ||
} | ||
} | ||
|
||
|
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.
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 ifif (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 anotherif (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;}