From c8f2738848f408fad3ed545f32e73e45238f9358 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 29 Mar 2023 18:03:03 +0200 Subject: [PATCH] restore shared lock ttl when releasing Signed-off-by: Robin Appelman --- lib/private/Lock/MemcacheLockingProvider.php | 78 ++++++++++++++++++- lib/private/Memcache/LoggerWrapperCache.php | 10 ++- lib/private/Memcache/ProfilerWrapperCache.php | 10 ++- lib/private/Memcache/Redis.php | 16 ++++ lib/public/IMemcacheTTL.php | 18 ++++- 5 files changed, 125 insertions(+), 7 deletions(-) diff --git a/lib/private/Lock/MemcacheLockingProvider.php b/lib/private/Lock/MemcacheLockingProvider.php index d4eebd7c30221..1ab00765a3a04 100644 --- a/lib/private/Lock/MemcacheLockingProvider.php +++ b/lib/private/Lock/MemcacheLockingProvider.php @@ -27,21 +27,43 @@ */ namespace OC\Lock; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IMemcache; use OCP\IMemcacheTTL; use OCP\Lock\LockedException; class MemcacheLockingProvider extends AbstractLockingProvider { private IMemcache $memcache; + private ITimeFactory $timeFactory; + + /** @var array */ + private $oldTTLs = []; public function __construct(IMemcache $memcache, int $ttl = 3600) { $this->memcache = $memcache; $this->ttl = $ttl; + $this->timeFactory = \OC::$server->get(ITimeFactory::class); + } + + private function setTTL(string $path, int $ttl = null, $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 setTTL(string $path): void { + private function getTTL(string $path): int { if ($this->memcache instanceof IMemcacheTTL) { - $this->memcache->setTTL($path, $this->ttl); + $ttl = $this->memcache->getTTL($path); + return $ttl === false ? -1 : $ttl; + } else { + return -1; } } @@ -58,13 +80,20 @@ public function isLocked(string $path, int $type): bool { public function acquireLock(string $path, int $type, ?string $readablePath = null): void { if ($type === self::LOCK_SHARED) { + // save the old TTL to for `restoreTTL` + $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); + throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath, $this->getTTL($path)); } } else { + // when getting exclusive locks, we know there are no old TTLs to restore $this->memcache->add($path, 0); + // ttl is updated automatically when the `set` succeeds if (!$this->memcache->cas($path, 0, 'exclusive')) { - throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); + throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath, $this->getTTL($path)); } } $this->setTTL($path); @@ -88,6 +117,10 @@ public function releaseLock(string $path, int $type): void { $newValue = $this->memcache->dec($path); } + if ($newValue > 0) { + $this->restoreTTL($path); + } + // if we somehow release more locks then exists, reset the lock if ($newValue < 0) { $this->memcache->cad($path, $newValue); @@ -106,6 +139,7 @@ public function changeLock(string $path, int $targetType): void { } elseif ($targetType === self::LOCK_EXCLUSIVE) { // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock if (!$this->memcache->cas($path, 1, 'exclusive')) { + $this->restoreTTL($path); throw new LockedException($path, null, $this->getExistingLockForException($path)); } } @@ -113,6 +147,42 @@ public function changeLock(string $path, int $targetType): void { $this->markChange($path, $targetType); } + /** + * With shared locks, each time the lock is acquired, the ttl for the path is reset. + * + * Due to this "ttl extension" when a shared lock isn't freed correctly for any reason + * the lock won't expire until no shared locks are required for the path for 1h. + * This can lead to a client repeatedly trying to upload a file, and failing forever + * because the lock never gets the opportunity to expire. + * + * To help the lock expire in this case, we lower the TTL back to what it was before we + * took the shared lock *only* if nobody else got a shared lock after we did. + * + * This doesn't handle all cases where multiple requests are acquiring shared locks + * but it should handle some of the more common ones and not hurt things further + */ + private function restoreTTL(string $path): void { + if (isset($this->oldTTLs[$path])) { + $saved = $this->oldTTLs[$path]; + $elapsed = $this->timeFactory->getTime() - $saved['time']; + + // old value to compare to when setting ttl in case someone else changes the lock in the middle of this function + $value = $this->memcache->get($path); + + $currentTtl = $this->getTTL($path); + + // what the old ttl would be given the time elapsed since we acquired the lock + $remainingOldTtl = $saved['ttl'] - $elapsed; + // what the currently ttl would be if nobody else acquired a lock since we did (+1 to cover rounding errors) + $expectedTtl = $this->ttl - $elapsed + 1; + + // check if another request has acquired a lock (and didn't release it yet) + if ($currentTtl <= $expectedTtl) { + $this->setTTL($path, $remainingOldTtl, $value); + } + } + } + private function getExistingLockForException(string $path): string { $existing = $this->memcache->get($path); if (!$existing) { diff --git a/lib/private/Memcache/LoggerWrapperCache.php b/lib/private/Memcache/LoggerWrapperCache.php index 55c0e76db792c..a57cc51378329 100644 --- a/lib/private/Memcache/LoggerWrapperCache.php +++ b/lib/private/Memcache/LoggerWrapperCache.php @@ -167,10 +167,18 @@ public function cad($key, $old) { } /** @inheritDoc */ - public function setTTL($key, $ttl) { + public function setTTL(string $key, int $ttl) { $this->wrappedCache->setTTL($key, $ttl); } + public function getTTL(string $key) { + $this->wrappedCache->getTTL($key); + } + + public function compareSetTTL(string $key, mixed $value, int $ttl): bool { + $this->wrappedCache->compareSetTTL($key, $value, $ttl); + } + public static function isAvailable(): bool { return true; } diff --git a/lib/private/Memcache/ProfilerWrapperCache.php b/lib/private/Memcache/ProfilerWrapperCache.php index 6e76989ddddea..a85062b1d4799 100644 --- a/lib/private/Memcache/ProfilerWrapperCache.php +++ b/lib/private/Memcache/ProfilerWrapperCache.php @@ -183,10 +183,18 @@ public function cad($key, $old) { } /** @inheritDoc */ - public function setTTL($key, $ttl) { + public function setTTL(string $key, int $ttl) { $this->wrappedCache->setTTL($key, $ttl); } + public function getTTL(string $key) { + $this->wrappedCache->getTTL($key); + } + + public function compareSetTTL(string $key, mixed $value, int $ttl): bool { + $this->wrappedCache->compareSetTTL($key, $value, $ttl); + } + public function offsetExists($offset): bool { return $this->hasKey($offset); } diff --git a/lib/private/Memcache/Redis.php b/lib/private/Memcache/Redis.php index b6f96fffba4eb..7e941f6940b01 100644 --- a/lib/private/Memcache/Redis.php +++ b/lib/private/Memcache/Redis.php @@ -182,6 +182,22 @@ public function setTTL($key, $ttl) { $this->getCache()->expire($this->getPrefix() . $key, $ttl); } + public function getTTL($key) { + return $this->getCache()->ttl($this->getPrefix() . $key); + } + + public function compareSetTTL(string $key, mixed $value, int $ttl): bool { + $fullKey = $this->getPrefix() . $key; + $redis = $this->getCache(); + if ($this->get($key) === $value) { + $result = $redis->multi() + ->expire($fullKey, $ttl) + ->exec(); + return $result !== false; + } + return false; + } + public static function isAvailable(): bool { return \OC::$server->getGetRedisFactory()->isAvailable(); } diff --git a/lib/public/IMemcacheTTL.php b/lib/public/IMemcacheTTL.php index 8424e5b2bfa7f..02e1afc5859cc 100644 --- a/lib/public/IMemcacheTTL.php +++ b/lib/public/IMemcacheTTL.php @@ -35,5 +35,21 @@ interface IMemcacheTTL extends IMemcache { * @param int $ttl time to live in seconds * @since 8.2.2 */ - public function setTTL($key, $ttl); + public function setTTL(string $key, int $ttl); + + /** + * Get the ttl for an existing value, in seconds till expiry + * + * @return int|false + */ + public function getTTL(string $key); + /** + * Set the ttl for an existing value if the value matches + * + * @param string $key + * @param mixed $value + * @param int $ttl time to live in seconds + * @since 26 + */ + public function compareSetTTL(string $key, $value, int $ttl): bool; }