Skip to content

Update the entityVersion also for downgrades#9786

Merged
hmlnarik merged 1 commit intokeycloak:mainfrom
ahus1:9716-downgrade-entityversion
Feb 7, 2022
Merged

Update the entityVersion also for downgrades#9786
hmlnarik merged 1 commit intokeycloak:mainfrom
ahus1:9716-downgrade-entityversion

Conversation

@ahus1
Copy link
Copy Markdown
Member

@ahus1 ahus1 commented Jan 25, 2022

as it needs to match the JSON and auxiliary tables

Closes #9716

@ahus1 ahus1 added area/storage Indicates an issue that touches storage (change in data layout or data manipulation) keycloak.x labels Jan 25, 2022
@ahus1 ahus1 marked this pull request as draft January 25, 2022 15:03
@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 25, 2022

As agreed this is a spike/proof of concept for using the @PreUpdate annotation to simplify the code in the entities.

I've tested it manually by updating the entityVersion in the database, then updating the entity and only attributes. In both instances the entityVersion is downgraded successfully.

I'd like to get feedback to this approach before I apply it to more entities. The three remaining calls to updateEntityVersion() are necessary when updating child tables (here: attributes).

This could be changed to Hibernate listener as shown in #9761 to avoid missing any call to that method. The downside is the additional technical obscurity of Hibernate listeners. While the effect used in #9761 couldn't be achieved in another way, calling this method three times per entity is IMHO tolerable. Therefore

This needs to be applied to roles as well. To void repeating the JavaDoc of updateEntityVersion(), it could be pulled to a superclass/interface JpaRootEntity that will be introduced in #9761.

@sguilhen
Copy link
Copy Markdown
Contributor

Thanks you @ahus1 - this gets a +1 from me and in my opinion it is perfectly fine to explicitly call updateEntityVersion where needed as opposed to having a listener in place. If necessary we can always revisit this decision in the future and refactor all entities accordingly, but for now your proposal looks good to me.

@hmlnarik
Copy link
Copy Markdown
Contributor

Thank you @ahus1 for the investigation!
I am wondering if the approach sketched in 4c0bcee solves an independent issue (object state versioning rather than entity format versioning solved in this PR) and is needed anyway?

@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

@hmlnarik - they solve two independent problems. While this solves the downgrading of the schema problem, the other PR solves a concurrent update of the row problem.

@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

@hmlnarik - update to my previous comment: there's already issue #9874 to track the concurrency problem.

@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from 6fdbc30 to 9c80579 Compare January 31, 2022 11:20
@ahus1 ahus1 marked this pull request as ready for review January 31, 2022 11:21
@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from 9c80579 to ee5c0bf Compare January 31, 2022 11:22
@ahus1 ahus1 requested a review from vramik January 31, 2022 11:23
@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

@sguilhen @vramik - finalized my changes and also implemented this for roles. Ready for final review. Build is still running.

@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

reverting to draft status, will rework to use Hibernate API to align implementation with #9894

@ahus1 ahus1 assigned ahus1 and unassigned hmlnarik Jan 31, 2022
@ahus1 ahus1 removed request for sguilhen and vramik January 31, 2022 13:04
@ahus1 ahus1 marked this pull request as draft January 31, 2022 13:30
@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

On hold until #9894 is merged.

@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from ee5c0bf to 190183d Compare February 2, 2022 10:20
@ahus1 ahus1 marked this pull request as ready for review February 2, 2022 10:21
@ahus1 ahus1 assigned hmlnarik and unassigned ahus1 Feb 2, 2022
@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from 190183d to 8f6edc7 Compare February 2, 2022 10:24
@ahus1 ahus1 requested review from sguilhen and vramik February 2, 2022 10:25
@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Feb 2, 2022

This PR is now updated to use the Hibernate SPI to update the entity version. I tested it manually locally and an entity version is downgraded successfully from 2 to 1 even if only an attribute was changed.

I decided to keep the two listeners separate as they serve two different purposes. Please let me know if you want name changes on new methods and classes.

As the listeners are hibernate specific, I moved the two listeners to the hibernate package.

Please let me know what you think, @vramik and @sguilhen. Thanks!

@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Feb 2, 2022

Note: #9944 and #9943 should be merged first, this PR then probably needs a rebase.

@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from 8f6edc7 to 5016996 Compare February 2, 2022 13:33
Copy link
Copy Markdown
Contributor

@sguilhen sguilhen left a comment

Choose a reason for hiding this comment

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

Thanks @ahus1 , I like what you did here with the listeners. Just left a minor javadoc comment, but overall this looks very good to me.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick: this javadoc was prob copied over from the other listener and doesn't correspond to what the method does.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, I updated that. The danger of writing comments: they might get out of date. I try to write comment why I do something or the intent, not necessarily how I do things. Think I got a bit off-track here. JavaDoc updated, please re-review.

@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from 5016996 to 1f05393 Compare February 3, 2022 07:54
@ahus1 ahus1 requested a review from sguilhen February 3, 2022 07:56
…e JSON and auxiliary tables.

Will trigger also when changes to a child occur, like for example when attributes change.

Closes keycloak#9716
@ahus1 ahus1 force-pushed the 9716-downgrade-entityversion branch from 1f05393 to bca0db1 Compare February 3, 2022 08:00
Copy link
Copy Markdown
Contributor

@vramik vramik left a comment

Choose a reason for hiding this comment

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

LGTM, thank you @ahus1. I'll rebase #9997 once this one got to be merged.

Copy link
Copy Markdown
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 @vramik and @sguilhen's reviews

@hmlnarik hmlnarik merged commit de2c1fb into keycloak:main Feb 7, 2022
@ahus1 ahus1 deleted the 9716-downgrade-entityversion branch February 7, 2022 11:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/storage Indicates an issue that touches storage (change in data layout or data manipulation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JPA Map storage doesn't downgrade entityVersion when modifying a row written with a future entityVersion

4 participants