You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
4.7 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jaakko Salo <jaakkos@gmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Lock;
  27. use OCP\IMemcache;
  28. use OCP\IMemcacheTTL;
  29. use OCP\Lock\LockedException;
  30. class MemcacheLockingProvider extends AbstractLockingProvider {
  31. /**
  32. * @var \OCP\IMemcache
  33. */
  34. private $memcache;
  35. /**
  36. * @param \OCP\IMemcache $memcache
  37. * @param int $ttl
  38. */
  39. public function __construct(IMemcache $memcache, int $ttl = 3600) {
  40. $this->memcache = $memcache;
  41. $this->ttl = $ttl;
  42. }
  43. private function setTTL(string $path) {
  44. if ($this->memcache instanceof IMemcacheTTL) {
  45. $this->memcache->setTTL($path, $this->ttl);
  46. }
  47. }
  48. /**
  49. * @param string $path
  50. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  51. * @return bool
  52. */
  53. public function isLocked(string $path, int $type): bool {
  54. $lockValue = $this->memcache->get($path);
  55. if ($type === self::LOCK_SHARED) {
  56. return $lockValue > 0;
  57. } elseif ($type === self::LOCK_EXCLUSIVE) {
  58. return $lockValue === 'exclusive';
  59. } else {
  60. return false;
  61. }
  62. }
  63. /**
  64. * @param string $path
  65. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  66. * @param string $readablePath human readable path to use in error messages
  67. * @throws \OCP\Lock\LockedException
  68. */
  69. public function acquireLock(string $path, int $type, string $readablePath = null) {
  70. if ($type === self::LOCK_SHARED) {
  71. if (!$this->memcache->inc($path)) {
  72. throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
  73. }
  74. } else {
  75. $this->memcache->add($path, 0);
  76. if (!$this->memcache->cas($path, 0, 'exclusive')) {
  77. throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
  78. }
  79. }
  80. $this->setTTL($path);
  81. $this->markAcquire($path, $type);
  82. }
  83. /**
  84. * @param string $path
  85. * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  86. */
  87. public function releaseLock(string $path, int $type) {
  88. if ($type === self::LOCK_SHARED) {
  89. $ownSharedLockCount = $this->getOwnSharedLockCount($path);
  90. $newValue = 0;
  91. if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it
  92. return;
  93. }
  94. if ($ownSharedLockCount === 1) {
  95. $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
  96. if (!$removed) { //someone else also has a shared lock, decrease only
  97. $newValue = $this->memcache->dec($path);
  98. }
  99. } else {
  100. // if we own more than one lock ourselves just decrease
  101. $newValue = $this->memcache->dec($path);
  102. }
  103. // if we somehow release more locks then exists, reset the lock
  104. if ($newValue < 0) {
  105. $this->memcache->cad($path, $newValue);
  106. }
  107. } elseif ($type === self::LOCK_EXCLUSIVE) {
  108. $this->memcache->cad($path, 'exclusive');
  109. }
  110. $this->markRelease($path, $type);
  111. }
  112. /**
  113. * Change the type of an existing lock
  114. *
  115. * @param string $path
  116. * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
  117. * @throws \OCP\Lock\LockedException
  118. */
  119. public function changeLock(string $path, int $targetType) {
  120. if ($targetType === self::LOCK_SHARED) {
  121. if (!$this->memcache->cas($path, 'exclusive', 1)) {
  122. throw new LockedException($path, null, $this->getExistingLockForException($path));
  123. }
  124. } elseif ($targetType === self::LOCK_EXCLUSIVE) {
  125. // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
  126. if (!$this->memcache->cas($path, 1, 'exclusive')) {
  127. throw new LockedException($path, null, $this->getExistingLockForException($path));
  128. }
  129. }
  130. $this->setTTL($path);
  131. $this->markChange($path, $targetType);
  132. }
  133. private function getExistingLockForException($path) {
  134. $existing = $this->memcache->get($path);
  135. if (!$existing) {
  136. return 'none';
  137. } elseif ($existing === 'exclusive') {
  138. return $existing;
  139. } else {
  140. return $existing . ' shared locks';
  141. }
  142. }
  143. }