Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / poolcounter / PoolCounter.php
1 <?php
2 /**
3 * Provides of semaphore semantics for restricting the number
4 * of workers that may be concurrently performing the same task.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 */
23
24 /**
25 * When you have many workers (threads/servers) giving service, and a
26 * cached item expensive to produce expires, you may get several workers
27 * doing the job at the same time.
28 *
29 * Given enough requests and the item expiring fast (non-cacheable,
30 * lots of edits...) that single work can end up unfairly using most (all)
31 * of the cpu of the pool. This is also known as 'Michael Jackson effect'
32 * since this effect triggered on the english wikipedia on the day Michael
33 * Jackson died, the biographical article got hit with several edits per
34 * minutes and hundreds of read hits.
35 *
36 * The PoolCounter provides semaphore semantics for restricting the number
37 * of workers that may be concurrently performing such single task. Only one
38 * key can be locked by any PoolCounter instance of a process, except for keys
39 * that start with "nowait:". However, only 0 timeouts (non-blocking requests)
40 * can be used with "nowait:" keys.
41 *
42 * By default PoolCounter_Stub is used, which provides no locking. You
43 * can get a useful one in the PoolCounter extension.
44 */
45 abstract class PoolCounter {
46 /* Return codes */
47 const LOCKED = 1; /* Lock acquired */
48 const RELEASED = 2; /* Lock released */
49 const DONE = 3; /* Another worker did the work for you */
50
51 const ERROR = -1; /* Indeterminate error */
52 const NOT_LOCKED = -2; /* Called release() with no lock held */
53 const QUEUE_FULL = -3; /* There are already maxqueue workers on this lock */
54 const TIMEOUT = -4; /* Timeout exceeded */
55 const LOCK_HELD = -5; /* Cannot acquire another lock while you have one lock held */
56
57 /** @var string All workers with the same key share the lock */
58 protected $key;
59 /** @var int Maximum number of workers working on tasks with the same key simultaneously */
60 protected $workers;
61 /**
62 * Maximum number of workers working on this task type, regardless of key.
63 * 0 means unlimited. Max allowed value is 65536.
64 * The way the slot limit is enforced is overzealous - this option should be used with caution.
65 * @var int
66 */
67 protected $slots = 0;
68 /** @var int If this number of workers are already working/waiting, fail instead of wait */
69 protected $maxqueue;
70 /** @var float Maximum time in seconds to wait for the lock */
71 protected $timeout;
72
73 /**
74 * @var boolean Whether the key is a "might wait" key
75 */
76 private $isMightWaitKey;
77 /**
78 * @var boolean Whether this process holds a "might wait" lock key
79 */
80 private static $acquiredMightWaitKey = 0;
81
82 /**
83 * @param array $conf
84 * @param string $type
85 * @param string $key
86 */
87 protected function __construct( $conf, $type, $key ) {
88 $this->workers = $conf['workers'];
89 $this->maxqueue = $conf['maxqueue'];
90 $this->timeout = $conf['timeout'];
91 if ( isset( $conf['slots'] ) ) {
92 $this->slots = $conf['slots'];
93 }
94
95 if ( $this->slots ) {
96 $key = $this->hashKeyIntoSlots( $key, $this->slots );
97 }
98 $this->key = $key;
99 $this->isMightWaitKey = !preg_match( '/^nowait:/', $this->key );
100 }
101
102 /**
103 * Create a Pool counter. This should only be called from the PoolWorks.
104 *
105 * @param string $type
106 * @param string $key
107 *
108 * @return PoolCounter
109 */
110 public static function factory( $type, $key ) {
111 global $wgPoolCounterConf;
112 if ( !isset( $wgPoolCounterConf[$type] ) ) {
113 return new PoolCounter_Stub;
114 }
115 $conf = $wgPoolCounterConf[$type];
116 $class = $conf['class'];
117
118 return new $class( $conf, $type, $key );
119 }
120
121 /**
122 * @return string
123 */
124 public function getKey() {
125 return $this->key;
126 }
127
128 /**
129 * I want to do this task and I need to do it myself.
130 *
131 * @return Status Value is one of Locked/Error
132 */
133 abstract public function acquireForMe();
134
135 /**
136 * I want to do this task, but if anyone else does it
137 * instead, it's also fine for me. I will read its cached data.
138 *
139 * @return Status Value is one of Locked/Done/Error
140 */
141 abstract public function acquireForAnyone();
142
143 /**
144 * I have successfully finished my task.
145 * Lets another one grab the lock, and returns the workers
146 * waiting on acquireForAnyone()
147 *
148 * @return Status Value is one of Released/NotLocked/Error
149 */
150 abstract public function release();
151
152 /**
153 * Checks that the lock request is sane.
154 * @return Status - good for sane requests fatal for insane
155 * @since 1.25
156 */
157 final protected function precheckAcquire() {
158 if ( $this->isMightWaitKey ) {
159 if ( self::$acquiredMightWaitKey ) {
160 /*
161 * The poolcounter itself is quite happy to allow you to wait
162 * on another lock while you have a lock you waited on already
163 * but we think that it is unlikely to be a good idea. So we
164 * made it an error. If you are _really_ _really_ sure it is a
165 * good idea then feel free to implement an unsafe flag or
166 * something.
167 */
168 return Status::newFatal( 'poolcounter-usage-error',
169 'You may only aquire a single non-nowait lock.' );
170 }
171 } elseif ( $this->timeout !== 0 ) {
172 return Status::newFatal( 'poolcounter-usage-error',
173 'Locks starting in nowait: must have 0 timeout.' );
174 }
175 return Status::newGood();
176 }
177
178 /**
179 * Update any lock tracking information when the lock is acquired
180 * @since 1.25
181 */
182 final protected function onAcquire() {
183 self::$acquiredMightWaitKey |= $this->isMightWaitKey;
184 }
185
186 /**
187 * Update any lock tracking information when the lock is released
188 * @since 1.25
189 */
190 final protected function onRelease() {
191 self::$acquiredMightWaitKey &= !$this->isMightWaitKey;
192 }
193
194 /**
195 * Given a key (any string) and the number of lots, returns a slot number (an integer from
196 * the [0..($slots-1)] range). This is used for a global limit on the number of instances of
197 * a given type that can acquire a lock. The hashing is deterministic so that
198 * PoolCounter::$workers is always an upper limit of how many instances with the same key
199 * can acquire a lock.
200 *
201 * @param string $key PoolCounter instance key (any string)
202 * @param int $slots The number of slots (max allowed value is 65536)
203 * @return int
204 */
205 protected function hashKeyIntoSlots( $key, $slots ) {
206 return hexdec( substr( sha1( $key ), 0, 4 ) ) % $slots;
207 }
208 }
209
210 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
211 class PoolCounter_Stub extends PoolCounter {
212 // @codingStandardsIgnoreEnd
213
214 public function __construct() {
215 /* No parameters needed */
216 }
217
218 public function acquireForMe() {
219 return Status::newGood( PoolCounter::LOCKED );
220 }
221
222 public function acquireForAnyone() {
223 return Status::newGood( PoolCounter::LOCKED );
224 }
225
226 public function release() {
227 return Status::newGood( PoolCounter::RELEASED );
228 }
229 }