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

Improve active and inactive access token retrieval queries for MSSQL when con-app-key violation occurred #2728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sadilchamishka
Copy link
Contributor

Proposed changes in this pull request

When con-app-key constrain violation occurred, following two queries get executed.
As per the analysis done for the MSSQL database, we can notice that it takes considerable amount of time to invoke the both queries (roughly 300ms per each).

Screenshot 2025-02-21 at 08 15 52

The reason for the latency is due to the absence of NO LOCK syntax in the queries.

We have been using the same pattern when checking the existence of a token[2],[3] but we have missed in this place.

[1] - https://github.com/wso2-extensions/identity-inbound-auth-oauth/blob/master/components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/dao/AccessTokenDAOImpl.java#L2598~L2605
[2] -

"CONSENTED_TOKEN FROM IDN_OAUTH2_ACCESS_TOKEN WITH (NOLOCK) WHERE CONSUMER_KEY_ID = (SELECT ID FROM " +

[3] -
"IDN_OAUTH2_ACCESS_TOKEN WITH (NOLOCK) WHERE CONSUMER_KEY_ID = (SELECT ID FROM IDN_OAUTH_CONSUMER_APPS " +

Copy link

codecov bot commented Feb 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.91%. Comparing base (9db893b) to head (9964922).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2728      +/-   ##
============================================
- Coverage     56.91%   56.91%   -0.01%     
+ Complexity     8581     8569      -12     
============================================
  Files           654      654              
  Lines         48442    48442              
  Branches      10263    10263              
============================================
- Hits          27573    27572       -1     
  Misses        16962    16962              
- Partials       3907     3908       +1     
Flag Coverage Δ
unit 39.96% <ø> (-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.

@@ -1172,6 +1172,7 @@ public class SQLQueries {
public static final String RETRIEVE_LATEST_ACTIVE_ACCESS_TOKEN_BY_CLIENT_ID_USER_SCOPE_IDP_NAME_MSSQL =
"SELECT TOP 1 ACCESS_TOKEN, REFRESH_TOKEN, TIME_CREATED, REFRESH_TOKEN_TIME_CREATED, VALIDITY_PERIOD, " +
"REFRESH_TOKEN_VALIDITY_PERIOD, USER_TYPE, TOKEN_ID, SUBJECT_IDENTIFIER FROM IDN_OAUTH2_ACCESS_TOKEN " +
"WITH (NOLOCK) " +
Copy link
Contributor

@indeewari indeewari Feb 21, 2025

Choose a reason for hiding this comment

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

When con_app_key is supposed to prevent persisting the same token by multiple threads by synchronizing, how could removing this search lock not lead to data inconsistency ? I think this will increase write operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no point of having a read lock for the access-token table. Even we retrieve a data with a read lock, when we take the decision and take the action, the state might have been changed and again fail with con-app-key constraint violation.

The con-app-key violation reaching max attempt (default value 5) will be anyway reach having this improvement or not, but this improvement decrease the time to of reaching to the max failure attempts.

This limitation is only for the SQL server setups, as other databases works in the read with no lock status.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that using a NOLOCK query will increase concurrent reads, but since the AT table is crucial, I am thinking whether allowing dirty reads is a tradeoff we can consider for performance improvement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dirty read happens for other database types also. This approach has been taken by SQL server as its default behaviour. If @Thumimku your argument is correct we have to fix the queries for other database types.

There is no any gain we read a data with a lock. Here we interested in check the existence. It just an idea that we are ready to do an insert. It is not an guarantee that we can do the insert. Hence a dirty read is fine for such a usecase.

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.

3 participants