Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mhajas
Copy link
Contributor

@mhajas mhajas commented Mar 10, 2023

Closes #17570

@mhajas mhajas requested a review from a team March 10, 2023 12:44
@mhajas mhajas requested review from a team as code owners March 10, 2023 12:46
@mhajas mhajas requested a review from martin-kanis March 10, 2023 12:46
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 N possible hits to the database or just one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stelewis-redhat thank you for the review. I am not following your question. Could you please elaborate a little bit?

Copy link
Contributor

@stelewis-redhat stelewis-redhat Mar 21, 2023

Choose a reason for hiding this comment

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

I think my mental model of the data is wrong, after thinking about this a bit more.

If this gets called in a loop, "removeAuthenticateClientSession" does something like "delete from blah where blah_id=id" which is fine. In the SQL world, one could optimize a little by "delete from blah where blah_id in (id1, id2)" which isn't really possible, since HotRod isn't the SQL world.

In sum: nevermind.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a correct suggestion, however, it is not applicable in current context.

What is happening here is the following:
If we have one user session (U1) that has 3 client sessions (C1, C2, C3). The user session entity contains 3 references to each client sessions.

In JSON form It would be something like this:

"U1": {
    // ..........
    clientSessions: [C1.id, C2.id, C3.id],
    // ..........
    }

Now, when any of the client sessions expire, they are removed from cache. However, the reference in the U1 is still there. Therefore, when Keycloak loads client sessions for U1, we need to check each client session whether it still exists, that is what we are doing on line 82 (this was done also prior to this PR). However, we also need to clean that reference from the U1 (which is added in this PR), otherwise it is still present in Infinispan and can cause some trouble.

So in this loop we are calling delete for each client session we find out is missing (it is already removed from Infinispan). What are doing is basically removing ids from the Set within U1 and at the end of the transaction we call replace on whole U1 entity.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok great. That is helpful! Thanks @mhajas

Copy link
Contributor

@martin-kanis martin-kanis left a comment

Choose a reason for hiding this comment

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

Thanks Michal for the PR. Please see my two comments.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assertThat(session.sessions().createClientSession(realm, client, uSession), notNullValue());
assertThat(uSession.getAuthenticatedClientSessions(), anEmptyMap());
assertThat(session.sessions().createClientSession(realm, client, uSession), notNullValue());

Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that exists method now defaults to "read by id". Shouldn't we override exists method in NoActionHotRodTransactionWrapper?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we should, I created a new issue for that: #19196

@mhajas mhajas force-pushed the 17570-UserSessionEntity-can-contain-stale-ClientSession-references-in-current-transaction branch from be967fe to b04a5bd Compare March 21, 2023 07:52
@mhajas mhajas requested a review from a team as a code owner March 21, 2023 07:52
Copy link
Contributor

@hmlnarik hmlnarik left a comment

Choose a reason for hiding this comment

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

Approving per @martin-kanis 's review

@hmlnarik hmlnarik merged commit beca223 into keycloak:main Mar 28, 2023
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.

UserSessionEntity can contain stale ClientSession references

4 participants