Problem/Motivation

We have an enterprise website with a big amount of users and the update hook password_policy_update_8302 takes too long to complete (over 3 hours).

Proposed resolution

Switch from a user update to a direct table input in password_policy_update_8302. Please, note that this will not update the user account though. However, we are adding this here in case anyone would prefer to use this method to speed up the update path.

CommentFileSizeAuthor
#6 3258563-6.patch3.39 KBandysipple
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

idimopoulos created an issue. See original summary.

dimilias’s picture

Status: Active » Needs review
claudiu.cristea’s picture

Status: Needs review » Needs work
dimilias’s picture

@claudiu.cristea, the direct update does not update the user. So it is a matter of whether the direct insert is an actual acceptable solution. I can provide the update you are requesting, it might still get rejected as it does not use the Drupal API in order to get the users updated.

andysipple’s picture

StatusFileSize
new3.39 KB

Adding a patch

eelkeblok’s picture

While we're at it, we should also move the installation of the field into the clause that initialises the batch. The way it functions now, the process attempts to install the installation config on every iteration, which must be quite a lot of overhead too.

eelkeblok’s picture

@idimopoulos What exactly do you mean by "the direct update does not update the user"? Do you mean caches are not invalidated? Doesn't that happen for the update process anyway?

dimilias’s picture

@eelkeblok yes but not only. A direct insert in the database generally skips Drupal API. Meaning for example, user changed date is not updated (since the entity is not updated). Possible hooks are not fired, maybe rules, I don't know. I cannot predict what is not changed when we skip Drupal API. This is why I said that this might not be a suitable change.

While we're at it, we should also move the installation of the field into the clause that initialises the batch.

I will do it now.

eelkeblok’s picture

Status: Needs work » Needs review

Did just that:

- Moved the import of the field into the batch initialization so it happens once.
- Added a check on the type of storage
- Changed the query to a single query per batch. Even faster 🚀
- Updated the update hook description to say it also initializes the field; sets expectations
- Removed the check for finished for the return message; it was not working (anything other than 0 for finished is truthy anyway) and it is a useful progress indicator (although with the direct query, it is hardly useful anymore; very fast even with 10s of thousands of users)

dimilias’s picture

Oh sorry :) And I was wondering because I saw the MR having it already correctly :)
Ok that is fine :)

eelkeblok’s picture

Sorry about that, had not anticipated you reacting that quickly :)

You make a good point about the hooks and the like not firing. Although that might be a little tricky anyway, for a hook_update_N. Hm...

dimilias’s picture

Status: Needs review » Reviewed & tested by the community

It is RTBC for me. If you think that initialization of the field does not need to fire everything in Drupal API (for me there is no problem not going through the API tbh) you can merge - if you have access.

eelkeblok’s picture

I don't :)

WRT the updates not needing to run, I actually think the solution should be the other way. All we are really doing is initialising a field here; I wouldn't really want update dates to be changed for that, for example. Also, the old (which is now fallback) behaviour is actually a little dangerous, because we are in a hook_update_N, in which case we can't be sure that any modules that implement any update hooks for users have had their updates ran, and might actually break trying. This entity manipulation through the API should happen in a hook_post_update. I guess that should be spun off into a separate issue, though, this change makes it both safer and a *lot* faster for all sites that run on SQL databases (which I suppose is "most"). It should probably somehow involve moving the API-based logic to a hook_post_update and a check whether the hook_update_N was able to make the changes already.

eelkeblok’s picture

Hiding patch to avoid confusion (should still be there for those who want it). Tip: if you need a patch from a merge request, open the merge request, go to the changes and append ".diff" to the URL. You'll get the changes as a patch. Save it locally (you don't want changes to the PR to magically show up in your project at unexpected times) and apply it as any other (local) patch.

paulocs made their first commit to this issue’s fork.

  • paulocs committed 66624a8 on 8.x-3.x authored by idimopoulos
    Issue #3258563 by AndySipple, paulocs: password_policy_update_8302 takes...
paulocs’s picture

Status: Reviewed & tested by the community » Fixed

Thank you all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.