3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\PoolCounter
;
23 use MediaWiki\Status\Status
;
24 use Psr\Log\LoggerAwareInterface
;
25 use Psr\Log\LoggerInterface
;
26 use Psr\Log\NullLogger
;
29 * Semaphore semantics to restrict how many workers may concurrently perform a task.
31 * When you have many workers (threads/servers) in service, and a
32 * cached item expensive to produce expires, you may get several workers
33 * computing the same expensive item at the same time.
35 * Given enough incoming requests and the item expiring quickly (non-cacheable,
36 * or lots of edits or other invalidation events) that single task can end up
37 * unfairly using most (or all) of the CPUs of the server cluster.
38 * This is also known as "Michael Jackson effect", as this scenario happened on
39 * the English Wikipedia in 2009 on the day Michael Jackson died.
40 * See also <https://wikitech.wikimedia.org/wiki/Michael_Jackson_effect>.
42 * PoolCounter was created to provide semaphore semantics to restrict the number
43 * of workers that may be concurrently performing a given task. Only one key
44 * can be locked by any PoolCounter instance of a process, except for keys
45 * that start with "nowait:". However, only non-blocking requests (timeout=0)
46 * may be used with a "nowait:" key.
48 * By default PoolCounterNull is used, which provides no locking.
49 * Install the poolcounterd service from
50 * <https://gerrit.wikimedia.org/g/mediawiki/services/poolcounter> to
51 * enable this feature.
56 abstract class PoolCounter
implements LoggerAwareInterface
{
58 public const LOCKED
= 1; /* Lock acquired */
59 public const RELEASED
= 2; /* Lock released */
60 public const DONE
= 3; /* Another worker did the work for you */
62 public const ERROR
= -1; /* Indeterminate error */
63 public const NOT_LOCKED
= -2; /* Called release() with no lock held */
64 public const QUEUE_FULL
= -3; /* There are already maxqueue workers on this lock */
65 public const TIMEOUT
= -4; /* Timeout exceeded */
66 public const LOCK_HELD
= -5; /* Cannot acquire another lock while you have one lock held */
68 /** @var string All workers with the same key share the lock */
70 /** @var int Maximum number of workers working on tasks with the same key simultaneously */
73 * Maximum number of workers working on this task type, regardless of key.
74 * 0 means unlimited. Max allowed value is 65536.
75 * The way the slot limit is enforced is overzealous - this option should be used with caution.
79 /** @var int If this number of workers are already working/waiting, fail instead of wait */
81 /** @var int Maximum time in seconds to wait for the lock */
83 protected LoggerInterface
$logger;
86 * @var bool Whether the key is a "might wait" key
88 private $isMightWaitKey;
90 * @var int Whether this process holds a "might wait" lock key
92 private static $acquiredMightWaitKey = 0;
95 * @var bool Enable fast stale mode (T250248). This may be overridden by the work class.
101 * @param string $type The class of actions to limit concurrency for (task type)
104 public function __construct( array $conf, string $type, string $key ) {
105 $this->workers
= $conf['workers'];
106 $this->maxqueue
= $conf['maxqueue'];
107 $this->timeout
= $conf['timeout'];
108 if ( isset( $conf['slots'] ) ) {
109 $this->slots
= $conf['slots'];
111 $this->fastStale
= $conf['fastStale'] ??
false;
112 $this->logger
= new NullLogger();
114 if ( $this->slots
) {
115 $key = $this->hashKeyIntoSlots( $type, $key, $this->slots
);
119 $this->isMightWaitKey
= !preg_match( '/^nowait:/', $this->key
);
125 public function getKey() {
130 * I want to do this task and I need to do it myself.
132 * @param int|null $timeout Wait timeout, or null to use value passed to
134 * @return Status Value is one of Locked/Error
136 abstract public function acquireForMe( $timeout = null );
139 * I want to do this task, but if anyone else does it
140 * instead, it's also fine for me. I will read its cached data.
142 * @param int|null $timeout Wait timeout, or null to use value passed to
144 * @return Status Value is one of Locked/Done/Error
146 abstract public function acquireForAnyone( $timeout = null );
149 * I have successfully finished my task.
150 * Lets another one grab the lock, and returns the workers
151 * waiting on acquireForAnyone()
153 * @return Status Value is one of Released/NotLocked/Error
155 abstract public function release();
158 * Checks that the lock request is sensible.
159 * @return Status good for sensible requests, fatal for the not so sensible
162 final protected function precheckAcquire() {
163 if ( $this->isMightWaitKey
) {
164 if ( self
::$acquiredMightWaitKey ) {
166 * The poolcounter itself is quite happy to allow you to wait
167 * on another lock while you have a lock you waited on already
168 * but we think that it is unlikely to be a good idea. So we
169 * made it an error. If you are _really_ _really_ sure it is a
170 * good idea then feel free to implement an unsafe flag or
173 return Status
::newFatal(
174 'poolcounter-usage-error',
175 'You may only aquire a single non-nowait lock.'
178 } elseif ( $this->timeout
!== 0 ) {
179 return Status
::newFatal(
180 'poolcounter-usage-error',
181 'Locks starting in nowait: must have 0 timeout.'
184 return Status
::newGood();
188 * Update any lock tracking information when the lock is acquired
191 final protected function onAcquire() {
192 self
::$acquiredMightWaitKey |
= $this->isMightWaitKey
;
196 * Update any lock tracking information when the lock is released
199 final protected function onRelease() {
200 self
::$acquiredMightWaitKey &= !$this->isMightWaitKey
;
204 * Given a key (any string) and the number of lots, returns a slot key (a prefix with a suffix
205 * integer from the [0..($slots-1)] range). This is used for a global limit on the number of
206 * instances of a given type that can acquire a lock. The hashing is deterministic so that
207 * PoolCounter::$workers is always an upper limit of how many instances with the same key
208 * can acquire a lock.
210 * @param string $type The class of actions to limit concurrency for (task type)
211 * @param string $key PoolCounter instance key (any string)
212 * @param int $slots The number of slots (max allowed value is 65536)
213 * @return string Slot key with the type and slot number
215 protected function hashKeyIntoSlots( $type, $key, $slots ) {
216 return $type . ':' . ( hexdec( substr( sha1( $key ), 0, 4 ) ) %
$slots );
220 * Is fast stale mode (T250248) enabled? This may be overridden by the
221 * PoolCounterWork subclass.
225 public function isFastStaleEnabled() {
226 return $this->fastStale
;
231 * @param LoggerInterface $logger
234 public function setLogger( LoggerInterface
$logger ) {
235 $this->logger
= $logger;
239 * @internal For use in PoolCounterWork only
240 * @return LoggerInterface
242 public function getLogger(): LoggerInterface
{
243 return $this->logger
;
247 /** @deprecated class alias since 1.42 */
248 class_alias( PoolCounter
::class, 'PoolCounter' );