PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.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 5 years ago
MySqlLockBackend.php
145 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9
10 namespace Piwik\Concurrency\LockBackend;
11
12
13 use Piwik\Common;
14 use Piwik\Concurrency\LockBackend;
15 use Piwik\Db;
16
17 class MySqlLockBackend implements LockBackend
18 {
19 const TABLE_NAME = 'locks';
20
21 /**
22 * fyi: does not support list keys at the moment just because not really needed so much just yet
23 */
24 public function getKeysMatchingPattern($pattern)
25 {
26 $sql = sprintf('SELECT SQL_NO_CACHE distinct `key` FROM %s WHERE `key` like ? and %s', self::getTableName(), $this->getQueryPartExpiryTime());
27 $pattern = str_replace('*', '%', $pattern);
28 $keys = Db::fetchAll($sql, array($pattern));
29 $raw = array_column($keys, 'key');
30 return $raw;
31 }
32
33 public function setIfNotExists($key, $value, $ttlInSeconds)
34 {
35 if (empty($ttlInSeconds)) {
36 $ttlInSeconds = 999999999;
37 }
38
39 // FYI: We used to have an INSERT INTO ... ON DUPLICATE UPDATE ... However, this can be problematic in concurrency issues
40 // because the ON DUPLICATE UPDATE may work successfully for 2 jobs at the same time but only one of them got the lock then.
41 // This would be perfectly fine if we did something like `return $this->get($key) === $value` to 100% detect which process
42 // got the lock as we do now. However, maybe the expireTime gets overwritten with a wrong value or so. That's why we
43 // rather try to get the lock with the insert only because only one job can succeed with this. If below flow with the
44 // delete becomes to slow, we may be able to use the INSERT INTO ... ON DUPLICATE UPDATE again.
45
46 if ($this->get($key)) {
47 return false; // a value is set, won't be possible to insert
48 }
49
50 $tablePrefixed = self::getTableName();
51
52 // remove any existing but expired lock
53 // todo: we could combine get() and keyExists() in one query!
54 if ($this->keyExists($key)) {
55 // most of the time an expired key should not exist... we don't want to lock the row unncessarily therefore we check first
56 // if value exists...
57 $sql = sprintf('DELETE FROM %s WHERE `key` = ? and not (%s)', $tablePrefixed, $this->getQueryPartExpiryTime());
58 Db::query($sql, array($key));
59 }
60
61 $query = sprintf('INSERT INTO %s (`key`, `value`, `expiry_time`)
62 VALUES (?,?,(UNIX_TIMESTAMP() + ?))',
63 $tablePrefixed);
64 // we make sure to update the row if the key is expired and consider it as "deleted"
65
66 try {
67 Db::query($query, array($key, $value, (int) $ttlInSeconds));
68 } catch (\Exception $e) {
69 if ($e->getCode() == 23000
70 || strpos($e->getMessage(), 'Duplicate entry') !== false
71 || strpos($e->getMessage(), ' 1062 ') !== false) {
72 return false;
73 }
74 throw $e;
75 }
76
77 // we make sure we got the lock
78 return $this->get($key) === $value;
79 }
80
81 public function get($key)
82 {
83 $sql = sprintf('SELECT SQL_NO_CACHE `value` FROM %s WHERE `key` = ? AND %s LIMIT 1', self::getTableName(), $this->getQueryPartExpiryTime());
84 return Db::fetchOne($sql, array($key));
85 }
86
87 public function deleteIfKeyHasValue($key, $value)
88 {
89 if (empty($value)) {
90 return false;
91 }
92
93 $sql = sprintf('DELETE FROM %s WHERE `key` = ? and `value` = ?', self::getTableName());
94 return $this->queryDidMakeChange($sql, array($key, $value));
95 }
96
97 public function expireIfKeyHasValue($key, $value, $ttlInSeconds)
98 {
99 if (empty($value)) {
100 return false;
101 }
102
103 // we need to use unix_timestamp in mysql and not time() in php since the local time might be different on each server
104 // better to rely on one central DB server time only
105 $sql = sprintf('UPDATE %s SET expiry_time = (UNIX_TIMESTAMP() + ?) WHERE `key` = ? and `value` = ?', self::getTableName());
106 $success = $this->queryDidMakeChange($sql, array((int) $ttlInSeconds, $key, $value));
107
108 if (!$success) {
109 // the above update did not work because the same time was already set and we just tried to set the same ttl
110 // again too fast within one second
111 return $value === $this->get($key);
112 }
113
114 return true;
115 }
116
117 public function keyExists($key)
118 {
119 $sql = sprintf('SELECT SQL_NO_CACHE 1 FROM %s WHERE `key` = ? LIMIT 1', self::getTableName());
120 $value = Db::fetchOne($sql, array($key));
121 return !empty($value);
122 }
123
124 private function queryDidMakeChange($sql, $bind = array())
125 {
126 $query = Db::query($sql, $bind);
127 if (is_object($query) && method_exists($query, 'rowCount')) {
128 // anything else but mysqli in tracker mode
129 return (bool) $query->rowCount();
130 } else {
131 // mysqli in tracker mode
132 return (bool) Db::get()->rowCount($query);
133 }
134 }
135
136 private static function getTableName()
137 {
138 return Common::prefixTable(self::TABLE_NAME);
139 }
140
141 private function getQueryPartExpiryTime()
142 {
143 return 'UNIX_TIMESTAMP() <= expiry_time';
144 }
145 }