Load user entity in Cookie AuthenticationProvider instead of using manual queries

Created on 26 September 2014, over 10 years ago
Updated 29 January 2023, almost 2 years ago

Use entity API in cookie authentication provider.

=> No hardcoded queries on users and user roles tables
=> Should actually be faster because entity storage cache.
=> It is quite common to actually have to load the user entity anyway, this could simplify some code (keep AccountInterface, but if authenticated user, then you can assume that you have a user entity).

๐Ÿ“Œ Task
Status

Needs work

Version

10.1 โœจ

Component
User moduleย  โ†’

Last updated 5 days ago

Created by

๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine eugene.brit Kyiv ๐Ÿ‡บ๐Ÿ‡ฆ
  • Status changed to Needs review almost 2 years ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia medha kumari Jaipur

    Rerolled the patch #137 in Drupal 10.1.x.

  • Status changed to Needs work almost 2 years ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น
  • Status changed to Needs review almost 2 years ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia mrinalini9 New Delhi

    Rerolled patch #138, please review it.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia mrinalini9 New Delhi

    Fixing custom commands failure issues in patch #140, please review it.

  • Status changed to Needs work almost 2 years ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น
  • Status changed to Needs review almost 2 years ago
  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ฆUkraine eugene.brit Kyiv ๐Ÿ‡บ๐Ÿ‡ฆ

    Re-roll #137 for D10.0.x

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Needs a reroll for 11.x-dev. I'll stick around for reviews here. Find me in the sky lounge at DDD vienna if there are any questions.

    Also desperately needs an issue summary update.

  • last update over 1 year ago
    28,507 pass, 3 fail
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly apaderno Brescia, ๐Ÿ‡ฎ๐Ÿ‡น
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Issue summary: done.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol
    1. +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
      @@ -618,6 +618,58 @@ protected function getTranslatedField($name, $langcode) {
      +   * Only the first delta can be accessed with this method.
      

      This is not true. There is an optional $delta argument.

    2. +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
      @@ -618,6 +618,58 @@ protected function getTranslatedField($name, $langcode) {
      +  public function getFieldValue($field_name, $property, $delta = 0) {
      

      Let's add type hints here.

  • last update over 1 year ago
    29,933 pass, 3 fail
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia _utsavsharma

    Rerolled for 11.x as mentioned in #147.
    Tried to address pointer 2 in #151.

  • last update over 1 year ago
    29,933 pass, 3 fail
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vsujeetkumar Delhi

    This is not true. There is an optional $delta argument.

    In #151.1, True $delta argument is optional, As per my understanding, Either we should update the comment like

    By default the first delta can be accessed with this method.

    or we can remove this line from the comment.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    These are two of very few database queries executed on an authenticated page request when dynamic page cache is a cache hit, if we load the user it will usually come from the cache.
    See for example https://grafana.prod.cluster.tag1.io/explore?panes=%7B%22taO%22:%7B%22datasource%22:%22tempo%22,%22queries%22:%5B%7B%22query%22:%2269f4dbd9a3acdf0e49e11acc90cbae81%22,%22queryType%22:%22traceql%22,%22refId%22:%22A%22,%22datasource%22:%7B%22type%22:%22tempo%22,%22uid%22:%22tempo%22%7D,%22limit%22:20%7D%5D,%22range%22:%7B%22from%22:%22now-6h%22,%22to%22:%22now%22%7D%7D%7D&schemaVersion=1&orgId=1 this trace from the Umami authenticated performance test. Would only be a small optimization from that perspective but worth doing I think, so tagging with performance for that.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    This needs a reroll from #152 , and it should be converted to a merge request.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan

    prem suthar โ†’ made their first commit to this issueโ€™s fork.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 434s
    #371208
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia prem suthar Ahemdabad- Gujrat , Jodhpur - Rajsthan

    As per the #155 re-rolled the patch #152 and created the Mr . also Addressed the #151 Points
    Please review.

  • Pipeline finished with Canceled
    about 1 month ago
    Total: 513s
    #371303
  • Pipeline finished with Failed
    about 1 month ago
    Total: 455s
    #371319
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland znerol

    Thanks @prem suthar, the MR looks right. Regrettably, there are two test failures in @core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php@. I can reproduce them locally. The full output:

    % ../vendor/bin/phpunit modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php
    PHPUnit 10.5.38 by Sebastian Bergmann and contributors.
    
    Runtime:       PHP 8.3.14
    Configuration: /home/lo/src/drupal/core/phpunit.xml
    
    .EE                                                                 3 / 3 (100%)
    
    Time: 00:00.199, Memory: 16.00 MB
    
    There were 2 errors:
    
    1) Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest::testHasRole
    InvalidArgumentException: The entity object refers to a removed translation (x-default) and cannot be manipulated.
    
    /home/lo/src/drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php:609
    /home/lo/src/drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php:597
    /home/lo/src/drupal/core/modules/user/src/Entity/User.php:193
    /home/lo/src/drupal/core/modules/user/src/Entity/User.php:206
    /home/lo/src/drupal/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php:77
    /home/lo/src/drupal/vendor/bin/phpunit:122
    
    2) Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest::testUserGetRoles
    InvalidArgumentException: The entity object refers to a removed translation (x-default) and cannot be manipulated.
    
    /home/lo/src/drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php:609
    /home/lo/src/drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php:597
    /home/lo/src/drupal/core/modules/user/src/Entity/User.php:193
    /home/lo/src/drupal/core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php:56
    /home/lo/src/drupal/vendor/bin/phpunit:122
    
    --
    
    2 tests triggered 2 PHP warnings:
    
    1) /home/lo/src/drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php:608
    Undefined array key "x-default"
    
    Triggered by:
    
    * Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest::testHasRole
      /home/lo/src/drupal/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php:71
    
    * Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest::testUserGetRoles
      /home/lo/src/drupal/core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php:53
    
    2) /home/lo/src/drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php:608
    Trying to access array offset on null
    
    Triggered by:
    
    * Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest::testHasRole
      /home/lo/src/drupal/core/tests/Drupal/Tests/Core/Session/UserSessionTest.php:71
    
    * Drupal\Tests\user\Unit\Plugin\Core\Entity\UserTest::testUserGetRoles
      /home/lo/src/drupal/core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php:53
    
    ERRORS!
    Tests: 3, Assertions: 2, Errors: 2, Warnings: 2.
    

    Not sure, maybe it is necessary to modify User::getRoles() and use $this->getFieldValue('roles') instead of $this->get('roles'). Possibly @berdir could help here.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    The performance aspect of this is a tradeoff, that's also why this issue has been stuck so long.

    Yes, we save two queries, but the other side of the coin is that to access the information we need from (status, roles) requires that we load the field definitions and create the field objects for it, which is much slower than the UserSession object.

    (FWIW, core has been doing that anyway for years due to ๐Ÿ› Many calls to ContextRepository::getAvailableContexts() due to entity upcasting Needs review but that's fixed now).

    Especially when profiling, that cost is clearly visible and it's hard to compare that with the cost of database queries. On high load sites, possibly with multiple servers and redis/memcache, that cost is easier to balance against extra database queries, but that's not the default setup.

    I tried to address that with the getFieldValue() method. But that's complex and I'm not convinced it's entirely reliable and shouldn't be mixed up with this issue iMHO. So either we accept that downside of the change or we postpone it on an issue to improve that performance cost. Which might or might not ever happen.

  • ๐Ÿ‡ซ๐Ÿ‡ทFrance andypost

    Looking at CI jobs I see no viable perf regression, so except fixing the last test it looks ready to go (good to get it in in early days on 11.2)

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    Well yes, because there's a complex workaround in place to counter the performance problem. But even without that, it is very unlikely to have an impact on CI execution time. But it's been tested and shown a long time ago in comments around #60. I doubt things are fundamentally different now.

Production build 0.71.5 2024