Ensure invalid items are not written to FastBackend in ChainedFast

Created on 19 October 2023, over 1 year ago

Problem/Motivation

Avoid writing invalid data to the fast backend.

Only relevant if/when someone calls getMultiple() with invalid == TRUE.

Marking issue NOVICE ONLY for creating a patch out of the below code.

Steps to reproduce

- Call getMultiple() with $allow_invalid = TRUE.
- See that fastBackend is happily setting invalid data, which will never be useful

Proposed resolution

<code>
diff --git a/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php b/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php
--- a/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php
+++ b/web/core/lib/Drupal/Core/Cache/ChainedFastBackend.php
@@ -166,7 +166,10 @@ public function getMultiple(&$cids, $allow_invalid = FALSE) {
         $cache[$item->cid] = $item;
         // Don't write the cache tags to the fast backend as any cache tag
         // invalidation results in an invalidation of the whole fast backend.
-        $this->fastBackend->set($item->cid, $item->data, $item->expire);
+        if (!$allow_invalid || $item->valid) {
+          $this->fastBackend->set($item->cid, $item->data, $item->expire);
+        }
       }
     }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Cacheย  โ†’

Last updated 1 day ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany Fabianx

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @Fabianx
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia deborahblessy

    Iam working on this.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia deborahblessy

    Hi @Fabianx,

    I have created and attached the patch. Could you please verify this?

  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    30,417 pass
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ

    Thank you, @deborahblessy!

    Next step: test coverage.

  • Status changed to Needs work over 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    For #5

  • Assigned to Fabianx
  • Status changed to Needs review 4 months ago
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium wim leers Ghent ๐Ÿ‡ง๐Ÿ‡ช๐Ÿ‡ช๐Ÿ‡บ

    Catching up on long-forgotten tabs โ€ฆ from last yearโ€™s European DrupalCon just before this yearโ€™s starts ๐Ÿ˜„

    I think Fabian is best suited to determine what is next here.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Brought this up in #contribute in slack https://drupal.slack.com/archives/C1BMUQ9U6/p1727876410850979

    @fabianx agreed on the approach but mentioned still needs tests.

  • First commit to issue fork.
  • Merge request !9730Ensure invalid items are ignored โ†’ (Open) created by nicxvan
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States nicxvan

    Moved to MR so tests can run and be more easily written.

  • Pipeline finished with Success
    4 months ago
    Total: 651s
    #299129
  • First commit to issue fork.
  • ๐Ÿ‡ง๐Ÿ‡ทBrazil murilohp

    Hey, just created a new test to validate the proposed solution, moving this to NR and removing the tag for now, thanks!

  • Pipeline finished with Success
    4 months ago
    Total: 1407s
    #300441
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
    1) Drupal\Tests\Core\Cache\ChainedFastBackendTest::testSetInvalidDataFastBackend
    Invalid data was not saved on the fast cache.
    stdClass Object (...) does not match expected type "NULL".
    /builds/issue/drupal-3395212/core/tests/Drupal/Tests/Core/Cache/ChainedFastBackendTest.php:103
    FAILURES!
    Tests: 3, Assertions: 7, Failures: 1.
    Exiting with EXIT_CODE=1
    

    Shows test coverage.

    Not sure if this needs any kind of release note (assuming not)

    Looking at the change to core/lib/Drupal/Core/Cache/ChainedFastBackend.php and don't see anything glaringly wrong. Think adding the condition makes sense.

    believe this one is good.

  • Issue was unassigned.
  • Status changed to RTBC about 1 month ago
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium kristiaanvandeneynde Antwerp, Belgium

    Gave the IS some thought and at first felt like this was wrong:

    See that fastBackend is happily setting invalid data, which will never be useful

    After all, if you're getting the data while note caring about its validity, you may as well store that in memory for future gets which do not care about validity. However, then I realized that a cache get which allows invalid could the poison the memory cache for a subsequent cache get which does not allow invalid. And that would be really bad.

    I still have the eerie feeling that there are edge cases we're not seeing here, but for now I'd say it makes more sense to commit this change rather than keep the old behavior.

    So RTBC +1.

    • catch โ†’ committed 91e01793 on 10.4.x
      Issue #3395212 by nicxvan, deborahblessy, murilohp, smustgrave, fabianx...
    • catch โ†’ committed b8c4c25f on 10.5.x
      Issue #3395212 by nicxvan, deborahblessy, murilohp, smustgrave, fabianx...
    • catch โ†’ committed 6f4f3a88 on 11.1.x
      Issue #3395212 by nicxvan, deborahblessy, murilohp, smustgrave, fabianx...
    • catch โ†’ committed e6b0b85e on 11.x
      Issue #3395212 by nicxvan, deborahblessy, murilohp, smustgrave, fabianx...
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    I think this is worth skipping just from a cache efficiency point of view if nothing else - allow_invalid is most likely to be used in the case that stale content is served and then a cache refresh job is queued (or done end of request or similar), so there'll be a new cache write pretty fast after this gets set anyway. Cache poisoning seems theoretical because the fast backend should handle invalid items correctly, but it doesn't hurt.

    Committed/pushed to 11.x/11.1.x/10.5.x/10.4.x, thanks!

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

Production build 0.71.5 2024