LockBackend
2 years ago
DistributedList.php
2 years ago
Lock.php
2 years ago
LockBackend.php
2 years ago
Lock.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | * |
| 9 | */ |
| 10 | namespace Piwik\Concurrency; |
| 11 | |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Date; |
| 14 | class Lock |
| 15 | { |
| 16 | const MAX_KEY_LEN = 70; |
| 17 | const DEFAULT_TTL = 60; |
| 18 | /** |
| 19 | * @var LockBackend |
| 20 | */ |
| 21 | private $backend; |
| 22 | private $lockKeyStart; |
| 23 | private $lockKey = null; |
| 24 | private $lockValue = null; |
| 25 | private $defaultTtl = null; |
| 26 | private $lastExpireTime = null; |
| 27 | public function __construct(\Piwik\Concurrency\LockBackend $backend, $lockKeyStart, $defaultTtl = null) |
| 28 | { |
| 29 | $this->backend = $backend; |
| 30 | $this->lockKeyStart = $lockKeyStart; |
| 31 | $this->lockKey = $this->lockKeyStart; |
| 32 | $this->defaultTtl = $defaultTtl ?: self::DEFAULT_TTL; |
| 33 | } |
| 34 | public function reexpireLock() |
| 35 | { |
| 36 | $timeBetweenReexpires = $this->defaultTtl - $this->defaultTtl / 4; |
| 37 | $now = Date::getNowTimestamp(); |
| 38 | if (!empty($this->lastExpireTime) && $now <= $this->lastExpireTime + $timeBetweenReexpires) { |
| 39 | return false; |
| 40 | } |
| 41 | return $this->expireLock($this->defaultTtl); |
| 42 | } |
| 43 | public function getNumberOfAcquiredLocks() |
| 44 | { |
| 45 | return count($this->getAllAcquiredLockKeys()); |
| 46 | } |
| 47 | public function getAllAcquiredLockKeys() |
| 48 | { |
| 49 | return $this->backend->getKeysMatchingPattern($this->lockKeyStart . '*'); |
| 50 | } |
| 51 | public function execute($id, $callback) |
| 52 | { |
| 53 | $i = 0; |
| 54 | while (!$this->acquireLock($id)) { |
| 55 | $i++; |
| 56 | usleep(100 * 1000); |
| 57 | // 100ms |
| 58 | if ($i > 50) { |
| 59 | // give up after 5seconds (50 * 100ms) |
| 60 | throw new \Exception('Could not get the lock for ID: ' . $id); |
| 61 | } |
| 62 | } |
| 63 | try { |
| 64 | return $callback(); |
| 65 | } finally { |
| 66 | $this->unlock(); |
| 67 | } |
| 68 | } |
| 69 | public function acquireLock($id, $ttlInSeconds = 60) |
| 70 | { |
| 71 | $this->lockKey = $this->lockKeyStart . $id; |
| 72 | if (mb_strlen($this->lockKey) > self::MAX_KEY_LEN) { |
| 73 | // Lock key might be too long for DB column, so we hash it but leave the start of the original as well |
| 74 | // to make it more readable |
| 75 | $md5Len = 32; |
| 76 | $this->lockKey = mb_substr($id, 0, self::MAX_KEY_LEN - $md5Len - 1) . md5($id); |
| 77 | } |
| 78 | $lockValue = substr(Common::generateUniqId(), 0, 12); |
| 79 | $locked = $this->backend->setIfNotExists($this->lockKey, $lockValue, $ttlInSeconds); |
| 80 | if ($locked) { |
| 81 | $this->lockValue = $lockValue; |
| 82 | $this->lastExpireTime = Date::getNowTimestamp(); |
| 83 | } |
| 84 | return $locked; |
| 85 | } |
| 86 | public function isLocked() |
| 87 | { |
| 88 | if (!$this->lockValue) { |
| 89 | return false; |
| 90 | } |
| 91 | return $this->lockValue === $this->backend->get($this->lockKey); |
| 92 | } |
| 93 | public function unlock() |
| 94 | { |
| 95 | if ($this->lockValue) { |
| 96 | $this->backend->deleteIfKeyHasValue($this->lockKey, $this->lockValue); |
| 97 | $this->lockValue = null; |
| 98 | } |
| 99 | } |
| 100 | public function expireLock($ttlInSeconds) |
| 101 | { |
| 102 | if ($ttlInSeconds > 0) { |
| 103 | if ($this->lockValue) { |
| 104 | $success = $this->backend->expireIfKeyHasValue($this->lockKey, $this->lockValue, $ttlInSeconds); |
| 105 | if (!$success) { |
| 106 | $value = $this->backend->get($this->lockKey); |
| 107 | $message = sprintf('Failed to expire key %s (%s / %s).', $this->lockKey, $this->lockValue, (string) $value); |
| 108 | if ($value === false) { |
| 109 | Common::printDebug($message . ' It seems like the key already expired as it no longer exists.'); |
| 110 | } elseif (!empty($value) && $value == $this->lockValue) { |
| 111 | Common::printDebug($message . ' We still have the lock but for some reason it did not expire.'); |
| 112 | } elseif (!empty($value)) { |
| 113 | Common::printDebug($message . ' This lock has been acquired by another process/server.'); |
| 114 | } else { |
| 115 | Common::printDebug($message . ' Failed to expire key.'); |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | $this->lastExpireTime = Date::getNowTimestamp(); |
| 120 | return true; |
| 121 | } else { |
| 122 | Common::printDebug('Lock is not acquired, cannot update expiration.'); |
| 123 | } |
| 124 | } else { |
| 125 | Common::printDebug('Provided TTL ' . $ttlInSeconds . ' is in valid in Lock::expireLock().'); |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 |