MemcacheLockingProvider.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC\Lock;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IMemcache;
  11. use OCP\IMemcacheTTL;
  12. use OCP\Lock\LockedException;
  13. class MemcacheLockingProvider extends AbstractLockingProvider {
  14. /** @var array<string, array{time: int, ttl: int}> */
  15. private array $oldTTLs = [];
  16. public function __construct(
  17. private IMemcache $memcache,
  18. private ITimeFactory $timeFactory,
  19. int $ttl = 3600,
  20. ) {
  21. parent::__construct($ttl);
  22. }
  23. private function setTTL(string $path, ?int $ttl = null, mixed $compare = null): void {
  24. if (is_null($ttl)) {
  25. $ttl = $this->ttl;
  26. }
  27. if ($this->memcache instanceof IMemcacheTTL) {
  28. if ($compare !== null) {
  29. $this->memcache->compareSetTTL($path, $compare, $ttl);
  30. } else {
  31. $this->memcache->setTTL($path, $ttl);
  32. }
  33. }
  34. }
  35. private function getTTL(string $path): int {
  36. if ($this->memcache instanceof IMemcacheTTL) {
  37. $ttl = $this->memcache->getTTL($path);
  38. return $ttl === false ? -1 : $ttl;
  39. } else {
  40. return -1;
  41. }
  42. }
  43. public function isLocked(string $path, int $type): bool {
  44. $lockValue = $this->memcache->get($path);
  45. if ($type === self::LOCK_SHARED) {
  46. return is_int($lockValue) && $lockValue > 0;
  47. } elseif ($type === self::LOCK_EXCLUSIVE) {
  48. return $lockValue === 'exclusive';
  49. } else {
  50. return false;
  51. }
  52. }
  53. public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
  54. if ($type === self::LOCK_SHARED) {
  55. // save the old TTL to for `restoreTTL`
  56. $this->oldTTLs[$path] = [
  57. 'ttl' => $this->getTTL($path),
  58. 'time' => $this->timeFactory->getTime()
  59. ];
  60. if (!$this->memcache->inc($path)) {
  61. throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
  62. }
  63. } else {
  64. // when getting exclusive locks, we know there are no old TTLs to restore
  65. $this->memcache->add($path, 0);
  66. // ttl is updated automatically when the `set` succeeds
  67. if (!$this->memcache->cas($path, 0, 'exclusive')) {
  68. throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
  69. }
  70. unset($this->oldTTLs[$path]);
  71. }
  72. $this->setTTL($path);
  73. $this->markAcquire($path, $type);
  74. }
  75. public function releaseLock(string $path, int $type): void {
  76. if ($type === self::LOCK_SHARED) {
  77. $ownSharedLockCount = $this->getOwnSharedLockCount($path);
  78. $newValue = 0;
  79. if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it
  80. return;
  81. }
  82. if ($ownSharedLockCount === 1) {
  83. $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
  84. if (!$removed) { //someone else also has a shared lock, decrease only
  85. $newValue = $this->memcache->dec($path);
  86. }
  87. } else {
  88. // if we own more than one lock ourselves just decrease
  89. $newValue = $this->memcache->dec($path);
  90. }
  91. if ($newValue > 0) {
  92. $this->restoreTTL($path);
  93. } else {
  94. unset($this->oldTTLs[$path]);
  95. }
  96. // if we somehow release more locks then exists, reset the lock
  97. if ($newValue < 0) {
  98. $this->memcache->cad($path, $newValue);
  99. }
  100. } elseif ($type === self::LOCK_EXCLUSIVE) {
  101. $this->memcache->cad($path, 'exclusive');
  102. }
  103. $this->markRelease($path, $type);
  104. }
  105. public function changeLock(string $path, int $targetType): void {
  106. if ($targetType === self::LOCK_SHARED) {
  107. if (!$this->memcache->cas($path, 'exclusive', 1)) {
  108. throw new LockedException($path, null, $this->getExistingLockForException($path));
  109. }
  110. } elseif ($targetType === self::LOCK_EXCLUSIVE) {
  111. // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
  112. if (!$this->memcache->cas($path, 1, 'exclusive')) {
  113. $this->restoreTTL($path);
  114. throw new LockedException($path, null, $this->getExistingLockForException($path));
  115. }
  116. unset($this->oldTTLs[$path]);
  117. }
  118. $this->setTTL($path);
  119. $this->markChange($path, $targetType);
  120. }
  121. /**
  122. * With shared locks, each time the lock is acquired, the ttl for the path is reset.
  123. *
  124. * Due to this "ttl extension" when a shared lock isn't freed correctly for any reason
  125. * the lock won't expire until no shared locks are required for the path for 1h.
  126. * This can lead to a client repeatedly trying to upload a file, and failing forever
  127. * because the lock never gets the opportunity to expire.
  128. *
  129. * To help the lock expire in this case, we lower the TTL back to what it was before we
  130. * took the shared lock *only* if nobody else got a shared lock after we did.
  131. *
  132. * This doesn't handle all cases where multiple requests are acquiring shared locks
  133. * but it should handle some of the more common ones and not hurt things further
  134. */
  135. private function restoreTTL(string $path): void {
  136. if (isset($this->oldTTLs[$path])) {
  137. $saved = $this->oldTTLs[$path];
  138. $elapsed = $this->timeFactory->getTime() - $saved['time'];
  139. // old value to compare to when setting ttl in case someone else changes the lock in the middle of this function
  140. $value = $this->memcache->get($path);
  141. $currentTtl = $this->getTTL($path);
  142. // what the old ttl would be given the time elapsed since we acquired the lock
  143. // note that if this gets negative the key will be expired directly when we set the ttl
  144. $remainingOldTtl = $saved['ttl'] - $elapsed;
  145. // what the currently ttl would be if nobody else acquired a lock since we did (+1 to cover rounding errors)
  146. $expectedTtl = $this->ttl - $elapsed + 1;
  147. // check if another request has acquired a lock (and didn't release it yet)
  148. if ($currentTtl <= $expectedTtl) {
  149. $this->setTTL($path, $remainingOldTtl, $value);
  150. }
  151. }
  152. }
  153. private function getExistingLockForException(string $path): string {
  154. $existing = $this->memcache->get($path);
  155. if (!$existing) {
  156. return 'none';
  157. } elseif ($existing === 'exclusive') {
  158. return $existing;
  159. } else {
  160. return $existing . ' shared locks';
  161. }
  162. }
  163. }