0
0

LockRegistry.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Contracts\Cache\CacheInterface;
  13. use Symfony\Contracts\Cache\ItemInterface;
  14. /**
  15. * LockRegistry is used internally by existing adapters to protect against cache stampede.
  16. *
  17. * It does so by wrapping the computation of items in a pool of locks.
  18. * Foreach each apps, there can be at most 20 concurrent processes that
  19. * compute items at the same time and only one per cache-key.
  20. *
  21. * @author Nicolas Grekas <p@tchwork.com>
  22. */
  23. final class LockRegistry
  24. {
  25. private static $openedFiles = [];
  26. private static $lockedKeys;
  27. /**
  28. * The number of items in this list controls the max number of concurrent processes.
  29. */
  30. private static $files = [
  31. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AbstractAdapter.php',
  32. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AbstractTagAwareAdapter.php',
  33. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AdapterInterface.php',
  34. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ApcuAdapter.php',
  35. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ArrayAdapter.php',
  36. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ChainAdapter.php',
  37. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'CouchbaseBucketAdapter.php',
  38. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'CouchbaseCollectionAdapter.php',
  39. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineAdapter.php',
  40. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineDbalAdapter.php',
  41. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemAdapter.php',
  42. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemTagAwareAdapter.php',
  43. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'MemcachedAdapter.php',
  44. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'NullAdapter.php',
  45. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ParameterNormalizer.php',
  46. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PdoAdapter.php',
  47. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpArrayAdapter.php',
  48. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpFilesAdapter.php',
  49. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ProxyAdapter.php',
  50. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'Psr16Adapter.php',
  51. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'RedisAdapter.php',
  52. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'RedisTagAwareAdapter.php',
  53. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapter.php',
  54. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapterInterface.php',
  55. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TraceableAdapter.php',
  56. __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TraceableTagAwareAdapter.php',
  57. ];
  58. /**
  59. * Defines a set of existing files that will be used as keys to acquire locks.
  60. *
  61. * @return array The previously defined set of files
  62. */
  63. public static function setFiles(array $files): array
  64. {
  65. $previousFiles = self::$files;
  66. self::$files = $files;
  67. foreach (self::$openedFiles as $file) {
  68. if ($file) {
  69. flock($file, \LOCK_UN);
  70. fclose($file);
  71. }
  72. }
  73. self::$openedFiles = self::$lockedKeys = [];
  74. return $previousFiles;
  75. }
  76. public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null, LoggerInterface $logger = null)
  77. {
  78. if ('\\' === \DIRECTORY_SEPARATOR && null === self::$lockedKeys) {
  79. // disable locking on Windows by default
  80. self::$files = self::$lockedKeys = [];
  81. }
  82. $key = unpack('i', md5($item->getKey(), true))[1];
  83. if (!\function_exists('sem_get')) {
  84. $key = self::$files ? abs($key) % \count(self::$files) : null;
  85. }
  86. if (null === $key || (self::$lockedKeys[$key] ?? false) || !$lock = self::open($key)) {
  87. return $callback($item, $save);
  88. }
  89. while (true) {
  90. try {
  91. $locked = false;
  92. // race to get the lock in non-blocking mode
  93. if ($wouldBlock = \function_exists('sem_get')) {
  94. $locked = @sem_acquire($lock, true);
  95. } else {
  96. $locked = flock($lock, \LOCK_EX | \LOCK_NB, $wouldBlock);
  97. }
  98. if ($locked || !$wouldBlock) {
  99. $logger && $logger->info(sprintf('Lock %s, now computing item "{key}"', $locked ? 'acquired' : 'not supported'), ['key' => $item->getKey()]);
  100. self::$lockedKeys[$key] = true;
  101. $value = $callback($item, $save);
  102. if ($save) {
  103. if ($setMetadata) {
  104. $setMetadata($item);
  105. }
  106. $pool->save($item->set($value));
  107. $save = false;
  108. }
  109. return $value;
  110. }
  111. // if we failed the race, retry locking in blocking mode to wait for the winner
  112. $logger && $logger->info('Item "{key}" is locked, waiting for it to be released', ['key' => $item->getKey()]);
  113. if (\function_exists('sem_get')) {
  114. $lock = sem_get($key);
  115. @sem_acquire($lock);
  116. } else {
  117. flock($lock, \LOCK_SH);
  118. }
  119. } finally {
  120. if ($locked) {
  121. if (\function_exists('sem_get')) {
  122. sem_remove($lock);
  123. } else {
  124. flock($lock, \LOCK_UN);
  125. }
  126. }
  127. unset(self::$lockedKeys[$key]);
  128. }
  129. static $signalingException, $signalingCallback;
  130. $signalingException = $signalingException ?? unserialize("O:9:\"Exception\":1:{s:16:\"\0Exception\0trace\";a:0:{}}");
  131. $signalingCallback = $signalingCallback ?? function () use ($signalingException) { throw $signalingException; };
  132. try {
  133. $value = $pool->get($item->getKey(), $signalingCallback, 0);
  134. $logger && $logger->info('Item "{key}" retrieved after lock was released', ['key' => $item->getKey()]);
  135. $save = false;
  136. return $value;
  137. } catch (\Exception $e) {
  138. if ($signalingException !== $e) {
  139. throw $e;
  140. }
  141. $logger && $logger->info('Item "{key}" not found while lock was released, now retrying', ['key' => $item->getKey()]);
  142. }
  143. }
  144. return null;
  145. }
  146. private static function open(int $key)
  147. {
  148. if (\function_exists('sem_get')) {
  149. return sem_get($key);
  150. }
  151. if (null !== $h = self::$openedFiles[$key] ?? null) {
  152. return $h;
  153. }
  154. set_error_handler(function () {});
  155. try {
  156. $h = fopen(self::$files[$key], 'r+');
  157. } finally {
  158. restore_error_handler();
  159. }
  160. return self::$openedFiles[$key] = $h ?: @fopen(self::$files[$key], 'r');
  161. }
  162. }