\Drupal\block_content\Plugin\Derivative\BlockContent::getDerivativeDefinitions could use an aggregate query instead of loadMultiple

Created on 24 January 2024, 10 months ago

Problem/Motivation

Similar issue as πŸ› block_content block derivatives do not scale to thousands of block_content entities Needs work but a different approach to a fix

Steps to reproduce

Have 1000s of reusable block content entities.
See your site die with OOM on plugin definition rebuild.

Proposed resolution

Use an aggregate query instead of ::loadMultiple

Merge request link

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
Block contentΒ  β†’

Last updated 4 days ago

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @larowlan
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
  • Merge request !6298Use aggregate query β†’ (Open) created by larowlan
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    Tested this on a large site with over 3000+ block content entities where I was running into OOM issues.

    After saving a block and therefore having the Block definition caches cleared:

    No patch: ~152MB memory usage
    With patch: 76MB memory usage

  • Status changed to Needs work 10 months ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    This looks great! One point on the MR to add a code comment. I still think we should do πŸ› block_content block derivatives do not scale to thousands of block_content entities Needs work but this will stop a lot of sites crashing in the meantime.

  • Status changed to Needs review 10 months ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Added a comment

  • Status changed to RTBC 10 months ago
  • πŸ‡¬πŸ‡§United Kingdom catch

    Looks great. We've got the other issue(s) open to move away from derivatives, I think we could backport this to 10.2 as well.

  • πŸ‡¨πŸ‡­Switzerland berdir Switzerland

    What happens when you add translations? I suspect that a translation with a different info/label will result in a separate key, and then it gets unpredictable which one you actually end up with? Maybe add a default langcode condition?

    FWIW, this feels like a misuse of aggregate queries to me, but it is obviously a big improvement, so...

  • Status changed to Needs work 10 months ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
  • Status changed to Needs review 10 months ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    FWIW, this feels like a misuse of aggregate queries to me, but it is obviously a big improvement, so...

    Can you elaborate on why, I do this fairly regularly in contrib and custom code. Trying to understand the implications.

    Maybe add a default langcode condition?

    Done

  • Pipeline finished with Failed
    10 months ago
    Total: 619s
    #83987
  • πŸ‡¨πŸ‡­Switzerland berdir Switzerland

    > Can you elaborate on why, I do this fairly regularly in contrib and custom code. Trying to understand the implications.

    We always say that the only correct way to interact with entities is to load them, so that the appropriate hooks are running which *might* alter the data or also things like language negotiation*. We do not support any partial loading of field values or direct DB queries and an aggregate query is essentially that. Usually an aggregate query is combined with an aggregation function, where you count all nodes grouped by type or some other field and don't access the raw values. This is different.

    *I just realized that this isn't working at all right now anyway, block content labels always show up in the default language only (we kind of implicitly assume in most plugin caches that all labels are t() objects and translated when accessed so we don't cache per language), that's not the case here. This is already not working in any useful way in HEAD, but I could imagine something like a ContentTranslation object that you feed with all the translations of a given content entity that then acts like a TranslatableMarkup object.

  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    We always say that the only correct way to interact with entities is to load them, so that the appropriate hooks are running which *might* alter the data or also things like language negotiation*

    Thanks, I was thinking that might be the reason, so its good to get confirmation.

    Does that mean that this approach is a BC break, because someone could be implementing a load hook for block content and modifying the titles? Do we need to put this behind a feature flag that's opt-in? Thoughts @catch with your RM hat on?

  • Pipeline finished with Failed
    10 months ago
    Total: 477s
    #84455
  • Pipeline finished with Success
    10 months ago
    Total: 564s
    #84467
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I'm not aware of how trash module works but I got a bug report for a contrib module that leads me to suspect it might unset entities in a load hook if they're trashed. That might be something that has a side effect here.

  • Status changed to Needs work 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Seems 10.2.3 is out. Should this go to 10.3?

  • Assigned to catch
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    Per @larowlan in #12.

  • πŸ‡¨πŸ‡¦Canada phjou Vancouver πŸ‡¨πŸ‡¦ πŸ‡ͺπŸ‡Ί

    Patch doesn't seem to apply anymore.

  • Pipeline finished with Failed
    6 months ago
    Total: 580s
    #166799
  • First commit to issue fork.
  • Pipeline finished with Failed
    10 days ago
    Total: 85s
    #330922
Production build 0.71.5 2024