PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Concurrency / LockBackend / MySqlLockBackend.php
matomo / app / core / Concurrency / LockBackend Last commit date
MySqlLockBackend.php 2 years ago
MySqlLockBackend.php
122 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\LockBackend;
11
12 use Piwik\Common;
13 use Piwik\Concurrency\LockBackend;
14 use Piwik\Db;
15 class MySqlLockBackend implements LockBackend
16 {
17 const TABLE_NAME = 'locks';
18 /**
19 * fyi: does not support list keys at the moment just because not really needed so much just yet
20 */
21 public function getKeysMatchingPattern($pattern)
22 {
23 $sql = sprintf('SELECT SQL_NO_CACHE distinct `key` FROM %s WHERE `key` like ? and %s', self::getTableName(), $this->getQueryPartExpiryTime());
24 $pattern = str_replace('*', '%', $pattern);
25 $keys = Db::fetchAll($sql, array($pattern));
26 $raw = array_column($keys, 'key');
27 return $raw;
28 }
29 public function setIfNotExists($key, $value, $ttlInSeconds)
30 {
31 if (empty($ttlInSeconds)) {
32 $ttlInSeconds = 999999999;
33 }
34 // FYI: We used to have an INSERT INTO ... ON DUPLICATE UPDATE ... However, this can be problematic in concurrency issues
35 // because the ON DUPLICATE UPDATE may work successfully for 2 jobs at the same time but only one of them got the lock then.
36 // This would be perfectly fine if we did something like `return $this->get($key) === $value` to 100% detect which process
37 // got the lock as we do now. However, maybe the expireTime gets overwritten with a wrong value or so. That's why we
38 // rather try to get the lock with the insert only because only one job can succeed with this. If below flow with the
39 // delete becomes to slow, we may be able to use the INSERT INTO ... ON DUPLICATE UPDATE again.
40 if ($this->get($key)) {
41 return false;
42 // a value is set, won't be possible to insert
43 }
44 $tablePrefixed = self::getTableName();
45 // remove any existing but expired lock
46 // todo: we could combine get() and keyExists() in one query!
47 if ($this->keyExists($key)) {
48 // most of the time an expired key should not exist... we don't want to lock the row unnecessarily therefore we check first
49 // if value exists...
50 $sql = sprintf('DELETE FROM %s WHERE `key` = ? and not (%s)', $tablePrefixed, $this->getQueryPartExpiryTime());
51 Db::query($sql, array($key));
52 }
53 $query = sprintf('INSERT INTO %s (`key`, `value`, `expiry_time`)
54 VALUES (?,?,(UNIX_TIMESTAMP() + ?))', $tablePrefixed);
55 // we make sure to update the row if the key is expired and consider it as "deleted"
56 try {
57 Db::query($query, array($key, $value, (int) $ttlInSeconds));
58 } catch (\Exception $e) {
59 if ($e->getCode() == 23000 || strpos($e->getMessage(), 'Duplicate entry') !== false || strpos($e->getMessage(), ' 1062 ') !== false) {
60 return false;
61 }
62 throw $e;
63 }
64 // we make sure we got the lock
65 return $this->get($key) === $value;
66 }
67 public function get($key)
68 {
69 $sql = sprintf('SELECT SQL_NO_CACHE `value` FROM %s WHERE `key` = ? AND %s LIMIT 1', self::getTableName(), $this->getQueryPartExpiryTime());
70 return Db::fetchOne($sql, array($key));
71 }
72 public function deleteIfKeyHasValue($key, $value)
73 {
74 if (empty($value)) {
75 return false;
76 }
77 $sql = sprintf('DELETE FROM %s WHERE `key` = ? and `value` = ?', self::getTableName());
78 return $this->queryDidMakeChange($sql, array($key, $value));
79 }
80 public function expireIfKeyHasValue($key, $value, $ttlInSeconds)
81 {
82 if (empty($value)) {
83 return false;
84 }
85 // we need to use unix_timestamp in mysql and not time() in php since the local time might be different on each server
86 // better to rely on one central DB server time only
87 $sql = sprintf('UPDATE %s SET expiry_time = (UNIX_TIMESTAMP() + ?) WHERE `key` = ? and `value` = ?', self::getTableName());
88 $success = $this->queryDidMakeChange($sql, array((int) $ttlInSeconds, $key, $value));
89 if (!$success) {
90 // the above update did not work because the same time was already set and we just tried to set the same ttl
91 // again too fast within one second
92 return $value === $this->get($key);
93 }
94 return true;
95 }
96 public function keyExists($key)
97 {
98 $sql = sprintf('SELECT SQL_NO_CACHE 1 FROM %s WHERE `key` = ? LIMIT 1', self::getTableName());
99 $value = Db::fetchOne($sql, array($key));
100 return !empty($value);
101 }
102 private function queryDidMakeChange($sql, $bind = array())
103 {
104 $query = Db::query($sql, $bind);
105 if (is_object($query) && method_exists($query, 'rowCount')) {
106 // anything else but mysqli in tracker mode
107 return (bool) $query->rowCount();
108 } else {
109 // mysqli in tracker mode
110 return (bool) Db::get()->rowCount($query);
111 }
112 }
113 private static function getTableName()
114 {
115 return Common::prefixTable(self::TABLE_NAME);
116 }
117 private function getQueryPartExpiryTime()
118 {
119 return 'UNIX_TIMESTAMP() <= expiry_time';
120 }
121 }
122