1
0

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