MediaWiki REL1_35
PoolCounterWork.php
Go to the documentation of this file.
1<?php
27abstract class PoolCounterWork {
29 protected $type = 'generic';
31 protected $cacheable = false; // does this override getCachedWork() ?
33 private $poolCounter;
34
40 public function __construct( string $type, string $key, PoolCounter $poolCounter = null ) {
41 $this->type = $type;
42 // MW >= 1.35
43 $this->poolCounter = $poolCounter ?? PoolCounter::factory( $type, $key );
44 }
45
51 abstract public function doWork();
52
58 public function getCachedWork() {
59 return false;
60 }
61
69 public function fallback( $fast ) {
70 return false;
71 }
72
79 public function error( $status ) {
80 return false;
81 }
82
88 protected function isFastStaleEnabled() {
89 return $this->poolCounter->isFastStaleEnabled();
90 }
91
98 public function logError( $status ) {
99 $key = $this->poolCounter->getKey();
100
101 wfDebugLog( 'poolcounter', "Pool key '$key' ({$this->type}): "
102 . $status->getMessage()->inLanguage( 'en' )->useDatabase( false )->text() );
103 }
104
127 public function execute( $skipcache = false ) {
128 if ( $this->cacheable && !$skipcache ) {
129 if ( $this->isFastStaleEnabled() ) {
130 // In fast stale mode, do not wait if fallback() would succeed.
131 // Try to acquire the lock with timeout=0
132 $status = $this->poolCounter->acquireForAnyone( 0 );
133 if ( $status->isOK() && $status->value === PoolCounter::TIMEOUT ) {
134 // Lock acquisition would block: try fallback
135 $staleResult = $this->fallback( true );
136 if ( $staleResult !== false ) {
137 return $staleResult;
138 }
139 // No fallback available, so wait for the lock
140 $status = $this->poolCounter->acquireForAnyone();
141 } // else behave as if $status were returned in slow mode
142 } else {
143 $status = $this->poolCounter->acquireForAnyone();
144 }
145 } else {
146 $status = $this->poolCounter->acquireForMe();
147 }
148
149 if ( !$status->isOK() ) {
150 // Respond gracefully to complete server breakage: just log it and do the work
151 $this->logError( $status );
152 return $this->doWork();
153 }
154
155 switch ( $status->value ) {
157 // Better to ignore nesting pool counter limits than to fail.
158 // Assume that the outer pool limiting is reasonable enough.
159 /* no break */
161 try {
162 $result = $this->doWork();
163 } finally {
164 $this->poolCounter->release();
165 }
166 return $result;
167
169 $result = $this->getCachedWork();
170 if ( $result === false ) {
171 /* That someone else work didn't serve us.
172 * Acquire the lock for me
173 */
174 return $this->execute( true );
175 }
176 return $result;
177
180 $result = $this->fallback( false );
181
182 if ( $result !== false ) {
183 return $result;
184 }
185 /* no break */
186
187 /* These two cases should never be hit... */
189 default:
190 $errors = [
191 PoolCounter::QUEUE_FULL => 'pool-queuefull',
192 PoolCounter::TIMEOUT => 'pool-timeout' ];
193
194 $status = Status::newFatal( $errors[$status->value] ?? 'pool-errorunknown' );
195 $this->logError( $status );
196 return $this->error( $status );
197 }
198 }
199}
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Class for dealing with PoolCounters using class members.
error( $status)
Do something with the error, like showing it to the user.
getCachedWork()
Retrieve the work from cache.
PoolCounter $poolCounter
fallback( $fast)
A work not so good (eg.
execute( $skipcache=false)
Get the result of the work (whatever it is), or the result of the error() function.
isFastStaleEnabled()
Should fast stale mode be used?
doWork()
Actually perform the work, caching it if needed.
__construct(string $type, string $key, PoolCounter $poolCounter=null)
logError( $status)
Log an error.
When you have many workers (threads/servers) giving service, and a cached item expensive to produce e...
const QUEUE_FULL
static factory(string $type, string $key)
Create a Pool counter.
const LOCK_HELD