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