Problem/Motivation
It is now necessary that a module is enabled, so that the service exists to use it.
I don't think that is bad but I think we could handle the fact that the service doesn't exist much more nicely and simply fall back to the default if not provided.
Proposed resolution
In CacheFactory::get(), check if the specified service in $cache_settings exists before calling it (bin specific and default). Don't check the default backends. Should be possible to simply specify that inline in the existing if cases.
Add a hook_requirements() to tell people something is wrong.
Remaining tasks
User interface changes
API changes
Comment | File | Size | Author |
---|---|---|---|
#104 | 2429321-104.patch | 8.69 KB | shubhangi1995 |
#102 | core-9.5.x-cache-factory-service-check-2429321-102.patch | 17.3 KB | hswong3i |
#98 | 2429321-nr-bot.txt | 154 bytes | needs-review-queue-bot |
#94 | 2429321-94-TEST-ONLY.patch | 8.93 KB | danflanagan8 |
| |||
#92 | 2429321-92-TEST-ONLY.patch | 8.84 KB | danflanagan8 |
|
Issue fork drupal-2429321
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
Comment #1
joshi.rohit100patch attached
Comment #2
BerdirThanks, looks good.
A test would be great, somewhere. Write a bogus value in settings ($this->settingSet() in tests), and then verify that you get the default implementation back (DatabaseBackend).
Comment #3
joshi.rohit100Should there be unit-test case for this or a simpletest as I am only able to find a place to write test case for this and I am not sure its right or wrong - \Drupal\Tests\Core\Cache\CacheFactoryTest also I am not able to find set a value in settings as settings doesn't have set() method. Please help me on this.
Comment #4
Berdirunit test sounds good to me, adding it to that existing class makes sense. See how it's done in that class, you just create a new settings object and pass in the configuration to the constructor.
Comment #5
joshi.rohit100Updated patch with unit test. I think this will only cover first IF condition.
Comment #6
BerdirTest looks good, let's do the same for the default bin. Just copy it again and specify configuration for the default backend instead.
Comment #7
BerdirComment #8
joshi.rohit100Updated patch with unit test for default bin.
Comment #9
BerdirNot sure about the coding standard for this, standard dictates that the first sentence must only be a single line.
However, for unit tests, we often don't even include a description at all, and instead have a self-speaking method, which I think we have in this case.
Comment #10
BerdirNeeds work for changing the description there.
Comment #11
cilefen CreditAttribution: cilefen commentedComment #12
himanshupathak3 CreditAttribution: himanshupathak3 as a volunteer and at Material commentedModified documentation as suggested on #9. Adding interdiff for #8 and #12
Comment #13
Manjit.SinghComment #14
mgiffordRe-uploading patch for the bots.
Comment #18
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedThis works, this does what I want, let's get this in...
Currently it takes two passes to install an alternative cache backed, this should have been merged years ago.
Comment #19
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedComment #20
marcelovaniI've been following this issue for ages and I have been using this patch from #14. I forgot to mention before, but it seems to do the job.
Comment #21
MiSc CreditAttribution: MiSc at Wunder commentedPatch does not work against 8.4.x, so it needs some work.
Comment #22
MiSc CreditAttribution: MiSc at Wunder commentedUpdated patch against 8.4.x
Comment #23
MiSc CreditAttribution: MiSc at Wunder commentedComment #24
marcelovaniLooks good to me
Comment #25
alexpottAre we sure we want to do this? Doesn't this mean that you might not detect that you are missing the service?
I can imagine this causing quite a few lost hours of wondering why the expected backend is not being used.
Comment #26
alexpottDiscussed with @Berdir on IRC. We agreed that this "fix" might hide errors. Does anyone have any real use cases for the current code?
@Berdir suggests that maybe an
Comment #27
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedThe critical use case is when enabling a new cache backend, the setting will get read and throw an exception in the CacheFactory before the module has a chance to be enabled. This means its not possible to do one seamless deployment.
If hiding errors is the only issue?? Surely it would be much better for this to be clearly indicated in system_requirements()??
Comment #28
marcelovaniThe real use is when we want to use Memcache
Following the instruction on the Readme http://cgit.drupalcode.org/memcache/tree/README.txt?h=8.x-2.x#n37
We enable the module, export the configuration, which goes into core.extension.yml
Then we added
to settings.php
The problem is that before the configuration is loaded, the backend is switched and causes a fatal error.
This patch helps to temporarily fallback to database until the configuration is imported and the module is effectively loaded.
Comment #29
BerdirBut all kinds of things could go wrong if at a later point, memcache is for some reason accidently disabled and it would then again fall back to database caches, which might be very outdated and could break your site.
You have a bunch of options, one is to do two deployments, first enable the module and then set the memcache backend, depending on how the memcache module works, you can also explicitly loads its services.yml file so that the services are loaded even if the module is not enabled (yet). This is also necessary to support the container cache bin. This is documented for redis here: https://docs.platform.sh/frameworks/drupal8/redis.html, something similar might also work for memcache, but again, I don't know that module in 8.x
Comment #30
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedAlso spotted that the ChainedFastBackendFactory was using the default bin without checking. IMO I think this is important not to throw errors if incorrectly configured or if the module isn't yet enabled, the warning should satisfy if this is the only concern...
Comment #31
kalpaitch CreditAttribution: kalpaitch as a volunteer and commented@berdir fair enough, just to run with this idea though...
The same would be true if you removed the cache settings at a 'later point' then. Also disabling a module clears the caches after it's run...
Comment #32
marcelovaniThese "includes" via settings.php might do the job, but what happens if for some reason we want to disable the memcache module?
How about if I decide to disable memcache and enable redis instead?
Do we have to make multiple deployments?
In my opinion, its a lot more elegant to fail gracefully with some sort of alert rather than a fatal error
Comment #33
kalpaitch CreditAttribution: kalpaitch as a volunteer and commentedComment #34
MiSc CreditAttribution: MiSc at Wunder commented@Berdir for us that works with automated deploys of drupal, this is a big issue. And we would really like to have this solved in core.
Comment #35
O'BriatMy 2c about enabling redis after initial deployment :
In settings.php add comments around the redis settings that contain a placeholder, so the install process use the default bdd backend.
At the end of your deploy script, just remove the comments with a sed command :
Comment #38
impalash CreditAttribution: impalash at Google Summer of Code commentedUpdated for 8.6.x
Comment #40
dbjpanda CreditAttribution: dbjpanda commentedI applied the patch from #38 It works perfectly. Marking it RTBC. However I haven't gone through the logic or code quality of the patch.
Space needs to be removed.
Comment #41
alexpott#26 and ##2 still have not been addressed. We really need to understand why hiding the missing service is the correct behaviour here.
Comment #43
Anonymous (not verified) CreditAttribution: Anonymous at Digitaliseringsdirektoratet commentedUpdated for 8.7.x
Comment #45
mpotter CreditAttribution: mpotter at Phase2 commentedNot sure I'd say that its "hiding" the service. This is fallback behavior: If the desired cache backend does not exist, use the Drupal default rather than breaking the site. The patch already adds a message to the Drupal Status page like we do for many other requirements.
Without this patch it is very difficult to add Memcache to a site without breaking it. It requires a 2-step deployment (first enable module, then update settings.php). If the module gets disabled accidentally, the site breaks with no way to re-install the module other than via another deployment.
I agree that some sort of test is needed.
Comment #46
mpotter CreditAttribution: mpotter at Phase2 commentedRolled patch for Drupal 8.8.
When re-rolling this, I see two Test functions. So along with those tests and the warning on the status page, I think the issues mentioned in #41 are resolved.
Comment #48
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedHere this patch might fix failed tests.
Comment #49
xavier.massonRolled patch for Drupal 8.8.x.
In the reroll patch of the comment #46 there was a problem with duplicatre
PluralTranslatableMarkup
in thesystem.install
.Comment #50
xavier.massonFix the #50 patch.
Comment #52
prabha1997 CreditAttribution: prabha1997 at Valuebound for Valuebound commentedComment #53
prabha1997 CreditAttribution: prabha1997 at Valuebound for Valuebound commented/var/lib/drupalci/workspace/jenkins-drupal_patches-40654/source/core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php
155 Short array syntax must be used to define arrays
156 Short array syntax must be used to define arrays
157 Short array syntax must be used to define arrays
187 Short array syntax must be used to define arrays
188 Short array syntax must be used to define arrays
I found deprecation function
\Drupal\Tests\PhpunitCompatibilityTrait::getMock() is deprecated in drupal:8.5.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\PhpunitCompatibilityTrait::createMock() instead. See https://www.drupal.org/node/2907725
Here i upload a new patch .Please review patch and i resolve coding standard issues and deprecation functions in latest patch
Comment #54
prabha1997 CreditAttribution: prabha1997 at Valuebound for Valuebound commentedI upload a patch for wrong version Kindly review new patch
Comment #55
prabha1997 CreditAttribution: prabha1997 at Valuebound for Valuebound commentedI upload a patch with no coding standard issues in core/tests/Drupal/Tests/Core/Cache/CacheFactoryTest.php file
Comment #56
jungle@prabha1997 thank you!
To make reviewing easier, would you attach an interdiff.txt? See https://www.drupal.org/documentation/git/interdiff
Comment #57
jungleComment #58
Neslee Canil PintoRerolled the patch for 8.9.x
Comment #59
Neslee Canil PintoComment #60
prabha1997 CreditAttribution: prabha1997 at Valuebound for Valuebound commentedComment #61
jungleA review of #58
prefer using === instead of ==
Prefer Mocking with Prophecy in PHPUnit https://www.drupal.org/docs/8/phpunit/using-prophecy
Better to use "tests that ..." and the first one exceeds 80 chars, the second one, there should have a blank line between the first line and the second line.
Suggestion:
Comment #62
Neslee Canil PintoComment #63
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedComment #64
Neslee Canil PintoComment #65
Rade CreditAttribution: Rade at Wunder commentedRerolling patch from #62 to apply to 8.8.x.
Comment #66
benroy CreditAttribution: benroy at State of Geneva commentedThis issue seems to be similar to the https://www.drupal.org/project/drupal/issues/2766509, can somebody tell me if I'm wrong ? And if the two issues are doing the same, which one choose ?
Comment #67
jungleThanks, @benroy! Agree with you. This one was filed earlier than that one, so I think that one should be closed. Commented there. Tests did not pass. Setting back to NW.
Comment #68
longwavePersonally I think the approach of using the "null" cache service as taken in the other issue is perhaps a bit better for this special case.
Tagging to get the opinion of a cache maintainer.
Comment #69
Rade CreditAttribution: Rade at Wunder commentedSmall fix to patch from #65 since it had missing parenthesis in method calls.
Comment #70
aleevasTrying to re-roll latest patch
Comment #71
xjmWe should keep bugs filed against the bugfix branch (currently 8.8.x; soon to be 8.9.x). We don't need to change the version selector to queue tests because the UI also has a selector to queue tests against any branch when uploading the patch. Thanks!
Comment #72
xjmI guess it's more a minor-only fix since there's a new public method.
Comment #73
aleevasAdded patch for 8.9.x
Comment #75
aleevasAdded patch for 9.1 branch
Comment #76
jofitz CreditAttribution: jofitz at jofitz commentedRemoved "Needs reroll" tag
Comment #78
mpotter CreditAttribution: mpotter at Phase2 commentedBeen using this very important patch on many sites to prevent problems when a site is initially deployed and the memcache module hasn't yet been enabled (until config-import runs). Using the README instructions in the memcache module for "preloading" the cache service in settings.php results in very bad performance issues, whereas this patch just solves the problem simply by ignoring the cache settings when the module isn't enabled.
Would really like to see this get the subsystem review it needs and get pushed forward. It solves a very real problem on every one of our Drupal projects.
Comment #79
tyler36 CreditAttribution: tyler36 commented#75 prevents WSOD as intended on 9.1
After 6 years, someone needs to make a call; should it fail gracefully or not? Fixed or Closed(won't fix)?
Comment #80
catchI'm currently the only maintainer of the cache system in MAINTAINERS.txt, and I'm fine with the overall approach here given we have the system_requirements() check to inform people something is wrong.
Comment #81
alexpottI think rather then do this we can be even safer and remove the final else and do..
This doesn't need to be public API. I think also this logic could be inlined into the ::get() method. Then we wouldn't have to check if $this->consistentServiceName is set because that method assumes the constructor has correctly set it.
This is not tested.
This is an out-of-scope change.
Are we sure we want a warning on install for this? This change could break automated tests of install profiles which have the memcache module.
I don't think
Cache Backend
needs to be capitalised.Also this message could be more helpful and say with bin is incorrectly configured - or if it the default settings.
It would nice if the message pointing at least to settings.php where it is likely the misconfiguration is.
Also this requirement is untested.
Comment #84
star-szrNot currently a good novice issue so removing the tag.
Comment #85
danflanagan8Here's an attempt to wrap up all the loose ends here. This addresses all of the feedback in #81. It includes a couple new test classes to specifically address 81.3 and 81.6.
It looks like
ChainedFastBackendFactory
doesn't have any direct test coverage currently. So I added a unit test mostly based onCacheFactoryTest
. The final of the five test cases fails without the fix. I can post that as a test only patch if anyone wants.Comment #87
danflanagan8Hmmm, that test passes locally. I'm going to set this back to NR to get eyes on it maybe.
Comment #88
danflanagan8Here's a screenshot of what the warnings on the status page look like. This is what happens if you install the
cache_test
module on your site and go to the status report page. (After applying the patch of course.)Comment #91
hswong3i CreditAttribution: hswong3i at PantaRei Design Limited (Hong Kong) commentedPR created from #85
Comment #92
danflanagan8I'm posting a test-only patch to investigate that failing test.
ChainedFastBackendFactoryTest
is hard to unit test because there are a couple places it doesn't use service injection. So I'm explicitly unsetting some global values that may be different on the test runner than they are for me locally?I'm hoping that only the fifth test case fails here. Fingers crossed!
Comment #94
danflanagan8I think I had myself confused. Let's try setting the variable instead of unsetting it.
Comment #95
danflanagan8Well I'm stumped with this test fail. Maybe this is why there aren't tests for ChainedFastBackendFactory to begin with.
I'm going to set back to NR to maybe get some eyes on the patch in #85.
Comment #98
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #99
larowlanWhat's left here? Getting it passing?
Comment #101
hswong3i CreditAttribution: hswong3i at PantaRei Design Limited (Hong Kong) commentedUpdate MR https://git.drupalcode.org/project/drupal/-/merge_requests/1521 with 10.1.x
Comment #102
hswong3i CreditAttribution: hswong3i at PantaRei Design Limited (Hong Kong) commentedRe-roll https://git.drupalcode.org/project/drupal/-/commit/a5117b293edc1211c0e72... via 9.5.x-dev
Comment #103
ThirstySix CreditAttribution: ThirstySix as a volunteer and at GTECH commented#102 Patch works well with D9.5.x version.
Comment #104
shubhangi1995Patch as per 10.1.x