8000 [Cache] Prevent stampede at warmup using apcu_entry() for locking by nicolas-grekas · Pull Request #27028 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Prevent stampede at warmup using apcu_entry() for locking #27028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ public static function createConnection($dsn, array $options = array())
throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn));
}

/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
return $this->doGet($this, $key, $callback, $beta ?? 1.0, '' !== $this->namespace ? $this->getId($key) : null);
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function (CacheItemInterface $innerItem, array $item) {
public function get(string $key, callable $callback, float $beta = null)
{
if (!$this->pool instanceof CacheInterface) {
return $this->doGet($this, $key, $callback, $beta ?? 1.0);
return $this->doGet($this, $key, $callback, $beta ?? 1.0, $this->namespaceLen ? $this->getId($key) : null);
}

return $this->pool->get($this->getId($key), function ($innerItem) use ($key, $callback) {
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Cache/Adapter/TagAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class TagAwareAdapter implements CacheInterface, TagAwareAdapterInterface, Prune
private $setCacheItemTags;
private $getTagsByKey;
private $invalidateTags;
private $getId;
private $tags;
private $knownTagVersions = array();
private $knownTagVersionsTtl;
Expand Down Expand Up @@ -105,6 +106,13 @@ function (AdapterInterface $tagsAdapter, array $tags) {
null,
CacheItem::class
);
$this->getId = \Closure::bind(
function (AbstractAdapter $pool, $key) {
return $pool->getId($key);
},
null,
AbstractAdapter::class
);
}

/**
Expand Down Expand Up @@ -150,6 +158,22 @@ public function invalidateTags(array $tags)
return $ok;
}

/**
* {@inheritdoc}
*/
public function get(string $key, callable $callback, float $beta = null)
{
if ($this->pool instanceof AbstractAdapter) {
$id = ($this->getId)($this->pool, $key);
} elseif ($this->pool instanceof ProxyAdapter) {
$id = ((array) $this->pool)["\0Symfony\\Component\\Cache\\Adapter\\ProxyAdapter\0namespace"].$key;
} else {
$id = null;
}

return $this->doGet($this, $key, $callback, $beta ?? 1.0, $id !== $key ? $id : null);
}

/**
* {@inheritdoc}
*/
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Cache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added `CacheInterface`, which provides stampede protection via probabilistic early expiration and should become the preferred way to use a cache
* added warmup-time stampede protection using `apcu_entry()` for locking when available
* added sub-second expiry accuracy for backends that support it
* throw `LogicException` when `CacheItem::tag()` is called on an item comin F8E5 g from a non tag-aware pool
* deprecated `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead
Expand Down
25 changes: 23 additions & 2 deletions src/Symfony/Component/Cache/Traits/GetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function get(string $key, callable $callback, float $beta = null)
return $this->doGet($this, $key, $callback, $beta ?? 1.0);
}

private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, float $beta)
private function doGet(CacheItemPoolInterface $pool, string $key, callable $callback, float $beta, string $lockId = null)
{
$t = 0;
$item = $pool->getItem($key);
Expand Down Expand Up @@ -64,6 +64,7 @@ private function doGet(CacheItemPoolInterface $pool, string $key, callable $call
}

static $save = null;
static $useApcu = null;

if (null === $save) {
$save = \Closure::bind(
Expand All @@ -81,6 +82,26 @@ function (CacheItemPoolInterface $pool, CacheItemInterface $item, $value, float
);
}

return $save($pool, $item, $callback($item), $t);
if (null === $lockId || !$useApcu = $useApcu ?? \function_exists('apcu_entry') && ini_get('apc.enabled') && ('cli' !== \PHP_SAPI || ini_get('apc.enable_cli'))) {
return $save($pool, $item, $callback($item), $t);
}

$t = $t ?: microtime(true);
$lockId = ':'.$lockId;
$isHit = true;
$value = apcu_entry($lockId, function () use ($callback, $item, &$isHit) {
$isHit = false;

return $callback($item);
}, 2);

if (!$isHit) {
$save($pool, $item, $value, $t);
// give some time to concurrent processes to get the value from APCu
usleep(min(300000, (microtime(true) - $t) * 150000));
apcu_delete($lockId);
}

return $value;
}
}
0