Merge "CSSMin: version URLs based on content, not mtime"
[mediawiki.git] / includes / poolcounter / PoolCounter.php
blob1ec14aa1480d63a37662ff594b46a10eed87e011
1 <?php
2 /**
3 * Provides of semaphore semantics for restricting the number
4 * of workers that may be concurrently performing the same task.
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.
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.
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
21 * @file
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.
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.
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.
42 * By default PoolCounter_Stub is used, which provides no locking. You
43 * can get a useful one in the PoolCounter extension.
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 */
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 */
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
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;
73 /**
74 * @var boolean Whether the key is a "might wait" key
76 private $isMightWaitKey;
77 /**
78 * @var boolean Whether this process holds a "might wait" lock key
80 private static $acquiredMightWaitKey = 0;
82 /**
83 * @param array $conf
84 * @param string $type
85 * @param string $key
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'];
95 if ( $this->slots ) {
96 $key = $this->hashKeyIntoSlots( $key, $this->slots );
98 $this->key = $key;
99 $this->isMightWaitKey = !preg_match( '/^nowait:/', $this->key );
103 * Create a Pool counter. This should only be called from the PoolWorks.
105 * @param string $type
106 * @param string $key
108 * @return PoolCounter
110 public static function factory( $type, $key ) {
111 global $wgPoolCounterConf;
112 if ( !isset( $wgPoolCounterConf[$type] ) ) {
113 return new PoolCounter_Stub;
115 $conf = $wgPoolCounterConf[$type];
116 $class = $conf['class'];
118 return new $class( $conf, $type, $key );
122 * @return string
124 public function getKey() {
125 return $this->key;
129 * I want to do this task and I need to do it myself.
131 * @return Status Value is one of Locked/Error
133 abstract public function acquireForMe();
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.
139 * @return Status Value is one of Locked/Done/Error
141 abstract public function acquireForAnyone();
144 * I have successfully finished my task.
145 * Lets another one grab the lock, and returns the workers
146 * waiting on acquireForAnyone()
148 * @return Status Value is one of Released/NotLocked/Error
150 abstract public function release();
153 * Checks that the lock request is sane.
154 * @return Status - good for sane requests fatal for insane
155 * @since 1.25
157 final protected function precheckAcquire() {
158 if ( $this->isMightWaitKey ) {
159 if ( self::$acquiredMightWaitKey ) {
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.
168 return Status::newFatal( 'poolcounter-usage-error',
169 'You may only aquire a single non-nowait lock.' );
171 } elseif ( $this->timeout !== 0 ) {
172 return Status::newFatal( 'poolcounter-usage-error',
173 'Locks starting in nowait: must have 0 timeout.' );
175 return Status::newGood();
179 * Update any lock tracking information when the lock is acquired
180 * @since 1.25
182 final protected function onAcquire() {
183 self::$acquiredMightWaitKey |= $this->isMightWaitKey;
187 * Update any lock tracking information when the lock is released
188 * @since 1.25
190 final protected function onRelease() {
191 self::$acquiredMightWaitKey &= !$this->isMightWaitKey;
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.
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
205 protected function hashKeyIntoSlots( $key, $slots ) {
206 return hexdec( substr( sha1( $key ), 0, 4 ) ) % $slots;
210 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
211 class PoolCounter_Stub extends PoolCounter {
212 // @codingStandardsIgnoreEnd
214 public function __construct() {
215 /* No parameters needed */
218 public function acquireForMe() {
219 return Status::newGood( PoolCounter::LOCKED );
222 public function acquireForAnyone() {
223 return Status::newGood( PoolCounter::LOCKED );
226 public function release() {
227 return Status::newGood( PoolCounter::RELEASED );