123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- declare(strict_types=1);
- namespace OC\Lock;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\IMemcache;
- use OCP\IMemcacheTTL;
- use OCP\Lock\LockedException;
- class MemcacheLockingProvider extends AbstractLockingProvider {
-
- private array $oldTTLs = [];
- public function __construct(
- private IMemcache $memcache,
- private ITimeFactory $timeFactory,
- int $ttl = 3600,
- ) {
- parent::__construct($ttl);
- }
- private function setTTL(string $path, int $ttl = null, mixed $compare = null): void {
- if (is_null($ttl)) {
- $ttl = $this->ttl;
- }
- if ($this->memcache instanceof IMemcacheTTL) {
- if ($compare !== null) {
- $this->memcache->compareSetTTL($path, $compare, $ttl);
- } else {
- $this->memcache->setTTL($path, $ttl);
- }
- }
- }
- private function getTTL(string $path): int {
- if ($this->memcache instanceof IMemcacheTTL) {
- $ttl = $this->memcache->getTTL($path);
- return $ttl === false ? -1 : $ttl;
- } else {
- return -1;
- }
- }
- public function isLocked(string $path, int $type): bool {
- $lockValue = $this->memcache->get($path);
- if ($type === self::LOCK_SHARED) {
- return is_int($lockValue) && $lockValue > 0;
- } elseif ($type === self::LOCK_EXCLUSIVE) {
- return $lockValue === 'exclusive';
- } else {
- return false;
- }
- }
- public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
- if ($type === self::LOCK_SHARED) {
-
- $this->oldTTLs[$path] = [
- "ttl" => $this->getTTL($path),
- "time" => $this->timeFactory->getTime()
- ];
- if (!$this->memcache->inc($path)) {
- throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
- }
- } else {
-
- $this->memcache->add($path, 0);
-
- if (!$this->memcache->cas($path, 0, 'exclusive')) {
- throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath);
- }
- unset($this->oldTTLs[$path]);
- }
- $this->setTTL($path);
- $this->markAcquire($path, $type);
- }
- public function releaseLock(string $path, int $type): void {
- if ($type === self::LOCK_SHARED) {
- $ownSharedLockCount = $this->getOwnSharedLockCount($path);
- $newValue = 0;
- if ($ownSharedLockCount === 0) {
- return;
- }
- if ($ownSharedLockCount === 1) {
- $removed = $this->memcache->cad($path, 1);
- if (!$removed) {
- $newValue = $this->memcache->dec($path);
- }
- } else {
-
- $newValue = $this->memcache->dec($path);
- }
- if ($newValue > 0) {
- $this->restoreTTL($path);
- } else {
- unset($this->oldTTLs[$path]);
- }
-
- if ($newValue < 0) {
- $this->memcache->cad($path, $newValue);
- }
- } elseif ($type === self::LOCK_EXCLUSIVE) {
- $this->memcache->cad($path, 'exclusive');
- }
- $this->markRelease($path, $type);
- }
- public function changeLock(string $path, int $targetType): void {
- if ($targetType === self::LOCK_SHARED) {
- if (!$this->memcache->cas($path, 'exclusive', 1)) {
- throw new LockedException($path, null, $this->getExistingLockForException($path));
- }
- } elseif ($targetType === self::LOCK_EXCLUSIVE) {
-
- if (!$this->memcache->cas($path, 1, 'exclusive')) {
- $this->restoreTTL($path);
- throw new LockedException($path, null, $this->getExistingLockForException($path));
- }
- unset($this->oldTTLs[$path]);
- }
- $this->setTTL($path);
- $this->markChange($path, $targetType);
- }
-
- private function restoreTTL(string $path): void {
- if (isset($this->oldTTLs[$path])) {
- $saved = $this->oldTTLs[$path];
- $elapsed = $this->timeFactory->getTime() - $saved['time'];
-
- $value = $this->memcache->get($path);
- $currentTtl = $this->getTTL($path);
-
-
- $remainingOldTtl = $saved['ttl'] - $elapsed;
-
- $expectedTtl = $this->ttl - $elapsed + 1;
-
- if ($currentTtl <= $expectedTtl) {
- $this->setTTL($path, $remainingOldTtl, $value);
- }
- }
- }
- private function getExistingLockForException(string $path): string {
- $existing = $this->memcache->get($path);
- if (!$existing) {
- return 'none';
- } elseif ($existing === 'exclusive') {
- return $existing;
- } else {
- return $existing . ' shared locks';
- }
- }
- }
|