MemcacheLockingProvider.php 4.2 KB

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