After updating to 8.2.1 from 8.1.10 on our Acquia Cloud based site, the log files began filling with error like:

Warning: apcu_store(): Unable to allocate memory for pool. in Drupal\Core\Cache\ApcuBackend->set()...

The settings.conf initially had memcache (dev branch which works with Acquia) configure with:

$settings['memcache']['servers'] = $conf['memcache_servers'];
$settings['memcache']['key_prefix'] = $conf['memcache_key_prefix'];
$settings['memcache']['bins'] = ['default' => 'default'];

// Use memcache as the default bin
$settings['cache']['default'] = 'cache.backend.memcache';

In debugging, this was converted to just:

$settings['cache']['default'] = 'cache.backend.database';

This did not stop the errors.

Notes:

  • Acquia Cloud has apcu enabled but only allocates 8M to it.
  • The error did not immediately appear after clearing cache but only after some normal web access (multiple users).
  • Once it started, any request would start creating multiple entries in the log.

The expected behavior is what is documented in the Cache Api Configuration documentation. This says the settings default cache method should be used and not ChainedFastCache which uses APCu.

A way to "reproduce" this, is to just make the cache.backend.database mechanism the default in a Dev site's settings.php. Then run the following code in the devel php page (assumes devel / kint modules enabled):

kint(Drupal::cache('bootstrap'));

This will show that the cache class used is the ChainedFastCache class and not the expected DB Cache class.

The source of this problem was tracked down to the logic change made in the CacheFactory code for this issue:

CacheFactory::get() should not use default cache backend before cache bin defaults

This makes it so the settings file cannot override the default_backend service settings. This is not the documented behaviour.

The 'quick' fix for this was to define the cache mechanisms for each specific bin in addition to the default one. E.g.:

# https://docs.acquia.com/article/drupal-8-cache-backend
$settings['cache']['default'] = 'cache.backend.database';

# Force common chainedfast bins to use database.
$settings['cache']['bins']['discovery'] = 'cache.backend.database';
$settings['cache']['bins']['bootstrap'] = 'cache.backend.database';
$settings['cache']['bins']['render'] = 'cache.backend.database';
$settings['cache']['bins']['data'] = 'cache.backend.database';
$settings['cache']['bins']['config'] = 'cache.backend.database';

Not sure if the best solution is to roll back the logic change in the CacheFactory or update the documentation.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

cgmonroe created an issue. See original summary.

dawehner’s picture

Wim Leers’s picture

Title: Watchdog log filling with apcu_store errors on Acquia site » Drupal >=8.2.x doesn't allow to override all cache bins with $settings['cache']['default'] anymore, documentation says otherwise
Version: 8.2.1 » 8.2.x-dev
Status: Active » Needs review
Issue tags: +Documentation
FileSize
2.28 KB

First, note that the Acquia Cloud apcu_store() errors are a separate matter, one we cannot fix here. See #2800605: Warn/inform users when the hosting environment has a too low limit of APCU cache + #2765271: Rationalize use of the 'discovery' cache bin, since it's stored in the limited size APCu by default for that.


AFAICT this is because the cache.bootstrap service definition looks like this:

  cache.bootstrap:
    class: Drupal\Core\Cache\CacheBackendInterface
    tags:
      - { name: cache.bin, default_backend: cache.backend.chainedfast }
    factory: cache_factory:get
    arguments: [bootstrap]

Specifically, this part: default_backend: cache.backend.chainedfast. However, $settings['cache']['bins']… should definitely override this.


So, let's look at \Drupal\Core\Cache\CacheFactory. We see this:

  /**
   * A map of cache bin to default cache backend service name.
   *
   * All mappings in $settings takes precedence over this, but this can be used
   * to optimize cache storage for a Drupal installation without cache
   * customizations in settings.php. For example, this can be used to map the
   * 'bootstrap' bin to 'cache.backend.chainedfast', while allowing other bins
   * to fall back to the global default of 'cache.backend.database'.
   *
   * @var array
   */
  protected $defaultBinBackends;

(Note that this is computed by \Drupal\Core\Cache\ListCacheBinsPass.)

Specifically, this part: All mappings in $settings takes precedence over this, but this can be used to…. This confirms that $settings takes precedence over default_backend: ….

But if we then look at the code that should make that happen:

  public function get($bin) {
    $cache_settings = $this->settings->get('cache');
    // First, look for a cache bin specific setting.
    if (isset($cache_settings['bins'][$bin])) {
      $service_name = $cache_settings['bins'][$bin];
    }
    // Second, use the default backend specified by the cache bin.
    elseif (isset($this->defaultBinBackends[$bin])) {
      $service_name = $this->defaultBinBackends[$bin];
    }
    // Third, use configured default backend.
    elseif (isset($cache_settings['default'])) {
      $service_name = $cache_settings['default'];
    }
    else {
      // Fall back to the database backend if nothing else is configured.
      $service_name = 'cache.backend.database';
    }
    return $this->container->get($service_name)->get($bin);
  }

We first look for a bin-specific override in $settings. If that is not set, we look at default_backend: … in service definitions. If that is also not set, we use the default backend from $settings.

So, this is a slightly different order compared to what you expect. And also compared to what the documentation for $defaultBinBackends suggests.


This was changed in #2753989: CacheFactory::get() should not use default cache backend before cache bin defaults, and it intentionally introduced this behavior. See the CR at https://www.drupal.org/node/2754947 for the full explanation. It's indeed now necessary to explicitly override a certain cache bin.

So we should update the documentation, both the docs for \Drupal\Core\Cache\CacheFactory::$defaultBinBackends and those at https://api.drupal.org/api/drupal/core!core.api.php/group/cache/8.2.x#co....

Patch attached.

Wim Leers’s picture

Berdir’s picture

Docs are OK I think.

Only question is if we need a better way to override the used fast backend or some other way to disable that completely even if you have apcu theoretically available? That seems pretty hard right now, the only way to do so is the constructor argument for \Drupal\Core\Cache\ChainedFastBackendFactory::__construct, for which you'd have to alter the service definition?

Wim Leers’s picture

better way to override the used fast backend

This is needed so rarely, I think those people can alter the service definitions. In any case, separate issue.

or some other way to disable that completely even if you have apcu theoretically available

Overriding individual cache bins' backends seems good enough for this case.

catch’s picture

Status: Needs review » Reviewed & tested by the community

Docs look good to me.

cilefen’s picture

Status: Reviewed & tested by the community » Needs work

There are a few typos:

  1. +++ b/core/core.api.php
    @@ -588,9 +588,11 @@
    + * backend for individual cache bins. For example APCu or Memcache.
    

    "For example APCu or Memcache." is not a sentence.

  2. +++ b/core/lib/Drupal/Core/Cache/CacheFactory.php
    @@ -23,11 +23,11 @@ class CacheFactory implements CacheFactoryInterface, ContainerAwareInterface {
    +   * All bin-specific mappings in $settings takes precedence over this, but this
    

    There is a plurality mismatch with "mappings" and "takes".

Wim Leers’s picture

Issue tags: +Novice, +Needs reroll
hardikpandya’s picture

Status: Needs work » Needs review
FileSize
1.44 KB
2.11 KB

Making text changes.

hardikpandya’s picture

Issue tags: -Needs reroll

I could apply the patch, so no reroll is needed.
I have applied the patch that includes the suggested changes in the above comment.

Status: Needs review » Needs work

The last submitted patch, 10: 2820580-10.patch, failed testing.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

anavarre’s picture

Version: 8.3.x-dev » 8.4.x-dev
Status: Needs work » Needs review
FileSize
2.11 KB

Reroll against 8.4.x

Berdir’s picture

Issue tags: -Novice
+++ b/core/core.api.php
@@ -588,9 +588,11 @@
  *
- * By default cached data is stored in the database. This can be configured
- * though so that all cached data, or that of an individual cache bin, uses a
- * different cache backend, such as APCu or Memcache, for storage.
+ * By default, cached data is stored in the database. Some cache bins use a
+ * different default cache backend (specified in their service definition). This
+ * can be configured though: both a different default cache backend (to change
+ * all of those using the database backend), as well as a different cache
+ * backend for individual cache bins. E.g. APCu or Memcache.

This is a bit hard to understand, was a long sentence before and now it is twice as long.

Why don't we just keep this, and add a Note: below the example that sets the default, something like:

Note: The default does not override a default backend set on the service definition, only a bin-specific configuration overrides that. This is used to use the chainedfast backend for the boostrap, discovery and config bins.

anavarre’s picture

Tried to simplify the text as suggested in #15.

Note: The default does not override a default backend set on the service definition, only a bin-specific configuration overrides that. This is used to use the chainedfast backend for the boostrap, discovery and config bins.

Sorry, didn't really know what to do with this.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
larowlan’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/core.api.php
@@ -588,9 +588,9 @@
+ * By default, cached data is stored in the database. This can be configured
+ * such as an individual cache bin or all cached data use a different cache
+ * backend (specified in their service definition), e.g. APCu or Memcache.

Suggest

By default cached data is stored in the database. However Drupal can be configured to use a different backend e.g. APCu or Memcache. 
This configuration can nominate a different backend for all cached data or for specific cache bins. 

The text in the current patch is still a bit disjointed.

moshe weitzman’s picture

Oh no, stuck on wording. C'est la vie.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

anavarre’s picture

Tried to incorporate the feedback from #19 but IDK which part of the patch is still disjointed. Maybe add your suggestions directly?

anavarre’s picture

The last submitted patch, 22: 2820580-22.patch, failed testing. View results

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback from #19 appears to be addressed in #23. Patch #23 also still applies

Amber Himes Matz’s picture

Wording in #23 seems clear to me. +1 RTBC

Not sure what's going on with the test failure though.

Amber Himes Matz’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Shoot, actually it does look like the patch failed to apply, see https://www.drupal.org/pift-ci-job/1007356. Needs a re-roll.

Amber Himes Matz’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll

Yes @smustgrave is correct that the patch still applies. Sorry, I misunderstood the test results page. I've added a re-test for Drupal 9.5 so that we can get a passing test. Let's see how that goes.

The comment changes look good, we just need passing tests. Setting back to RTBC which hopefully will stick after the test run.

  • larowlan committed 407057a on 10.0.x
    Issue #2820580 by anavarre, hardikpandya, Wim Leers, Berdir, cilefen:...
  • larowlan committed 28aebfd on 10.1.x
    Issue #2820580 by anavarre, hardikpandya, Wim Leers, Berdir, cilefen:...
  • larowlan committed b72220f on 9.4.x
    Issue #2820580 by anavarre, hardikpandya, Wim Leers, Berdir, cilefen:...
  • larowlan committed 0b90a61 on 9.5.x
    Issue #2820580 by anavarre, hardikpandya, Wim Leers, Berdir, cilefen:...
larowlan’s picture

Version: 9.5.x-dev » 9.4.x-dev
Status: Reviewed & tested by the community » Fixed

Crediting @Berdir and @cilefen for feedback that changed the patch.

Not crediting myself for a wording nit-pick that put this back 5 years. Tempted to credit @moshe weitzman for his prediction of said outcome 🧙🔮😉.

Not crediting @smustgrave and @Amber Himes Matz even though their efforts here revived a dead issue, but I do love your work fellow #bugsmashers. I have however credited you both on this fortnight's bugsmash triage meta for said efforts.

Committed to 10.1.x and backported all the way to 9.4.x to keep things consistent as there's no risk of disruption with a docs only patch.

Status: Fixed » Closed (fixed)

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