MemcacheLockingProvider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. private IMemcache $memcache;
  32. public function __construct(IMemcache $memcache, int $ttl = 3600) {
  33. $this->memcache = $memcache;
  34. $this->ttl = $ttl;
  35. }
  36. private function setTTL(string $path): void {
  37. if ($this->memcache instanceof IMemcacheTTL) {
  38. $this->memcache->setTTL($path, $this->ttl);
  39. }
  40. }
  41. public function isLocked(string $path, int $type): bool {
  42. $lockValue = $this->memcache->get($path);
  43. if ($type === self::LOCK_SHARED) {
  44. return is_int($lockValue) && $lockValue > 0;
  45. } elseif ($type === self::LOCK_EXCLUSIVE) {
  46. return $lockValue === 'exclusive';
  47. } else {
  48. return false;
  49. }
  50. }
  51. public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
  52. if ($type === self::LOCK_SHARED) {
  53. if (!$this->memcache->inc($path)) {
  54. throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
  55. }
  56. } else {
  57. $this->memcache->add($path, 0);
  58. if (!$this->memcache->cas($path, 0, 'exclusive')) {
  59. throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
  60. }
  61. }
  62. $this->setTTL($path);
  63. $this->markAcquire($path, $type);
  64. }
  65. public function releaseLock(string $path, int $type): void {
  66. if ($type === self::LOCK_SHARED) {
  67. $ownSharedLockCount = $this->getOwnSharedLockCount($path);
  68. $newValue = 0;
  69. if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it
  70. return;
  71. }
  72. if ($ownSharedLockCount === 1) {
  73. $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go
  74. if (!$removed) { //someone else also has a shared lock, decrease only
  75. $newValue = $this->memcache->dec($path);
  76. }
  77. } else {
  78. // if we own more than one lock ourselves just decrease
  79. $newValue = $this->memcache->dec($path);
  80. }
  81. // if we somehow release more locks then exists, reset the lock
  82. if ($newValue < 0) {
  83. $this->memcache->cad($path, $newValue);
  84. }
  85. } elseif ($type === self::LOCK_EXCLUSIVE) {
  86. $this->memcache->cad($path, 'exclusive');
  87. }
  88. $this->markRelease($path, $type);
  89. }
  90. public function changeLock(string $path, int $targetType): void {
  91. if ($targetType === self::LOCK_SHARED) {
  92. if (!$this->memcache->cas($path, 'exclusive', 1)) {
  93. throw new LockedException($path, null, $this->getExistingLockForException($path));
  94. }
  95. } elseif ($targetType === self::LOCK_EXCLUSIVE) {
  96. // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
  97. if (!$this->memcache->cas($path, 1, 'exclusive')) {
  98. throw new LockedException($path, null, $this->getExistingLockForException($path));
  99. }
  100. }
  101. $this->setTTL($path);
  102. $this->markChange($path, $targetType);
  103. }
  104. private function getExistingLockForException(string $path): string {
  105. $existing = $this->memcache->get($path);
  106. if (!$existing) {
  107. return 'none';
  108. } elseif ($existing === 'exclusive') {
  109. return $existing;
  110. } else {
  111. return $existing . ' shared locks';
  112. }
  113. }
  114. }