Skip to content

[KEYCLOAK-16780] - Allow batching writes to storage when running migration - #7717

Merged
pedroigor merged 2 commits into
keycloak:masterfrom
pedroigor:KEYCLOAK-16780
Jan 29, 2021
Merged

pedroigor merged 2 commits into
keycloak:masterfrom
pedroigor:KEYCLOAK-16780

Conversation

@pedroigor

@pedroigor pedroigor commented Jan 13, 2021 •

Copy link
Copy Markdown
Contributor

These changes aim to:

  • Allow setting whether migration changes should be done in batches
  • Allow setting how many migration changes should be done in batch
  • Possible useful to other scenarios where a large number of changes to storage is made

Expected result:

  • Time to migrate should be drastically reduced. Tests using the latest version migrate 1K realms in 555528ms, whereas with these changes migration happens in 54595ms.

During migration, we should expect degradation due to the number of changes being made. By using a batch approach (as recommended by Hibernate, btw), we can improve performance and startup.

We should expect better results if running using #7712.

@pedroigor pedroigor added area/core Core server — startup, configuration, providers/SPIs, cross-cutting concerns impact/low kind/enhancement Categorizes a PR related to an enhancement labels Jan 13, 2021
@hmlnarik
hmlnarik requested a review from vramik January 13, 2021 13:27

@hmlnarik hmlnarik left a comment

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.

Thanks @pedroigor , looks good to me. This seems a good idea and I like the fact that this is limited to the migration. Have you tried any different limit, e.g. 500?

@hmlnarik

Copy link
Copy Markdown
Contributor

Also, has the pipeline been run on this PR? Since it touches migration, I'd rather have its results prior to merging.

Co-authored-by: Hynek Mlnařík <hmlnarik@users.noreply.github.com>
@mposolda

Copy link
Copy Markdown
Contributor

@pedroigor Thanks!

Will be good to have pipeline results for migration tests as @hmlnarik mentioned.

Also I wonder that we may still reach the limits during migration for example with 5000 realms? I wonder that we may also need to increase the transaction timeout for the migration transaction? This can be done with KeycloakModelUtils.runJobInTransactionWithTimeout . But maybe can be done as a follow-up (if needed), so I don't have anything against merge this PR as is.

@vramik vramik left a comment

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.

Thank you @pedroigor for the PR. Great work, I have just one question.

private PersistenceExceptionConverter(EntityManager em) {
private PersistenceExceptionConverter(KeycloakSession session, EntityManager em) {
batchEnabled = session.getAttributeOrDefault(Constants.STORAGE_BATCH_ENABLED, false);
batchSize = session.getAttributeOrDefault(Constants.STORAGE_BATCH_SIZE, 100);

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.

Do we have some performance results with different STORAGE_BATCH_SIZE values? I think we should set the default value based on best results, wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure. That number is what worked well for the use case we are testing using 1k realms.

Personally, I don't think it is critical for this PR. We can always change the value and users are free to set any value they want.

There is more work in the backlog that could help with this. But they are a follow-up to this PR and the other one related to multi-realm.

@pedroigor

Copy link
Copy Markdown
Contributor Author

Also I wonder that we may still reach the limits during migration for example with 5000 realms? I wonder that we may also need to increase the transaction timeout for the migration transaction? This can be done with KeycloakModelUtils.runJobInTransactionWithTimeout . But maybe can be done as a follow-up (if needed), so I don't have anything against merge this PR as is.

It works very well for our current scenario using 1k realms. I won't be surprised if running 5k the results are also good.

This is already an improvement. So I think any other could be done as a follow up.

@vramik

vramik commented Jan 14, 2021

Copy link
Copy Markdown
Contributor

Thanks @pedroigor, let's wait for the pipeline results. If those are ok, changes looks good to me.

@mposolda

Copy link
Copy Markdown
Contributor

@pedroigor +1 to do as a follow-up if needed. We have https://issues.redhat.com/browse/KEYCLOAK-16615 for that. I will approve once you have this migration test passing in the pipeline.

@hmlnarik hmlnarik left a comment

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.

LGTM, thanks @pedroigor ! The pipeline has revealed no regressions.
Please squash the two commits before merging.

@pedroigor
pedroigor merged commit cdf0ead into keycloak:master Jan 29, 2021
@pedroigor

Copy link
Copy Markdown
Contributor Author

@hmlnarik Going to send changes to documentation.

@pedroigor
pedroigor deleted the KEYCLOAK-16780 branch August 3, 2021 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Core server — startup, configuration, providers/SPIs, cross-cutting concerns kind/enhancement Categorizes a PR related to an enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants