MediaWiki  1.27.2
PoolCounter.php
Go to the documentation of this file.
1 <?php
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 
58  protected $key;
60  protected $workers;
67  protected $slots = 0;
69  protected $maxqueue;
71  protected $timeout;
72 
76  private $isMightWaitKey;
80  private static $acquiredMightWaitKey = 0;
81 
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 
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 
124  public function getKey() {
125  return $this->key;
126  }
127 
133  abstract public function acquireForMe();
134 
141  abstract public function acquireForAnyone();
142 
150  abstract public function release();
151 
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 
182  final protected function onAcquire() {
183  self::$acquiredMightWaitKey |= $this->isMightWaitKey;
184  }
185 
190  final protected function onRelease() {
191  self::$acquiredMightWaitKey &= !$this->isMightWaitKey;
192  }
193 
205  protected function hashKeyIntoSlots( $key, $slots ) {
206  return hexdec( substr( sha1( $key ), 0, 4 ) ) % $slots;
207  }
208 }
209 
210 // @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
212  // @codingStandardsIgnoreEnd
213 
214  public function __construct() {
215  /* No parameters needed */
216  }
217 
218  public function acquireForMe() {
220  }
221 
222  public function acquireForAnyone() {
224  }
225 
226  public function release() {
228  }
229 }
acquireForMe()
I want to do this task and I need to do it myself.
precheckAcquire()
Checks that the lock request is sane.
const LOCKED
Definition: PoolCounter.php:47
onRelease()
Update any lock tracking information when the lock is released.
release()
I have successfully finished my task.
int $maxqueue
If this number of workers are already working/waiting, fail instead of wait.
Definition: PoolCounter.php:69
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
const NOT_LOCKED
Definition: PoolCounter.php:52
acquireForAnyone()
I want to do this task, but if anyone else does it instead, it's also fine for me.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:25
hashKeyIntoSlots($key, $slots)
Given a key (any string) and the number of lots, returns a slot number (an integer from the [0...
int $workers
Maximum number of workers working on tasks with the same key simultaneously.
Definition: PoolCounter.php:60
bool $isMightWaitKey
Whether the key is a "might wait" key.
Definition: PoolCounter.php:76
static bool $acquiredMightWaitKey
Whether this process holds a "might wait" lock key.
Definition: PoolCounter.php:80
string $key
All workers with the same key share the lock.
Definition: PoolCounter.php:58
int $slots
Maximum number of workers working on this task type, regardless of key.
Definition: PoolCounter.php:67
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
const QUEUE_FULL
Definition: PoolCounter.php:53
When you have many workers (threads/servers) giving service, and a cached item expensive to produce e...
Definition: PoolCounter.php:45
float $timeout
Maximum time in seconds to wait for the lock.
Definition: PoolCounter.php:71
const TIMEOUT
Definition: PoolCounter.php:54
__construct($conf, $type, $key)
Definition: PoolCounter.php:87
static factory($type, $key)
Create a Pool counter.
onAcquire()
Update any lock tracking information when the lock is acquired.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2338
const RELEASED
Definition: PoolCounter.php:48
const LOCK_HELD
Definition: PoolCounter.php:55
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101