Merge "Add checkDependencies.php"
[lhc/web/wiklou.git] / includes / deferred / DeferredUpdates.php
1 <?php
2 /**
3 * Interface and manager for deferred updates.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22 use Wikimedia\Rdbms\IDatabase;
23 use MediaWiki\MediaWikiServices;
24 use Wikimedia\Rdbms\LBFactory;
25 use Wikimedia\Rdbms\ILBFactory;
26 use Wikimedia\Rdbms\LoadBalancer;
27
28 /**
29 * Class for managing the deferred updates
30 *
31 * In web request mode, deferred updates can be run at the end of the request, either before or
32 * after the HTTP response has been sent. In either case, they run after the DB commit step. If
33 * an update runs after the response is sent, it will not block clients. If sent before, it will
34 * run synchronously. These two modes are defined via PRESEND and POSTSEND constants, the latter
35 * being the default for addUpdate() and addCallableUpdate().
36 *
37 * Updates that work through this system will be more likely to complete by the time the client
38 * makes their next request after this one than with the JobQueue system.
39 *
40 * In CLI mode, deferred updates will run:
41 * - a) During DeferredUpdates::addUpdate if no LBFactory DB handles have writes pending
42 * - b) On commit of an LBFactory DB handle if no other such handles have writes pending
43 * - c) During an LBFactory::waitForReplication call if no LBFactory DBs have writes pending
44 * - d) When the queue is large and an LBFactory DB handle commits (EnqueueableDataUpdate only)
45 * - e) At the completion of Maintenance::execute()
46 *
47 * @see Maintenance::setLBFactoryTriggers
48 *
49 * When updates are deferred, they go into one two FIFO "top-queues" (one for pre-send and one
50 * for post-send). Updates enqueued *during* doUpdate() of a "top" update go into the "sub-queue"
51 * for that update. After that method finishes, the sub-queue is run until drained. This continues
52 * for each top-queue job until the entire top queue is drained. This happens for the pre-send
53 * top-queue, and later on, the post-send top-queue, in execute().
54 *
55 * @since 1.19
56 */
57 class DeferredUpdates {
58 /** @var DeferrableUpdate[] Updates to be deferred until before request end */
59 private static $preSendUpdates = [];
60 /** @var DeferrableUpdate[] Updates to be deferred until after request end */
61 private static $postSendUpdates = [];
62
63 const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
64 const PRESEND = 1; // for updates that should run before flushing output buffer
65 const POSTSEND = 2; // for updates that should run after flushing output buffer
66
67 const BIG_QUEUE_SIZE = 100;
68
69 /** @var array|null Information about the current execute() call or null if not running */
70 private static $executeContext;
71
72 /**
73 * Add an update to the deferred list to be run later by execute()
74 *
75 * In CLI mode, callback magic will also be used to run updates when safe
76 *
77 * @param DeferrableUpdate $update Some object that implements doUpdate()
78 * @param int $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
79 */
80 public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) {
81 global $wgCommandLineMode;
82
83 if (
84 self::$executeContext &&
85 self::$executeContext['stage'] >= $stage &&
86 !( $update instanceof MergeableUpdate )
87 ) {
88 // This is a sub-DeferredUpdate; run it right after its parent update.
89 // Also, while post-send updates are running, push any "pre-send" jobs to the
90 // active post-send queue to make sure they get run this round (or at all).
91 self::$executeContext['subqueue'][] = $update;
92
93 return;
94 }
95
96 if ( $stage === self::PRESEND ) {
97 self::push( self::$preSendUpdates, $update );
98 } else {
99 self::push( self::$postSendUpdates, $update );
100 }
101
102 // Try to run the updates now if in CLI mode and no transaction is active.
103 // This covers scripts that don't/barely use the DB but make updates to other stores.
104 if ( $wgCommandLineMode ) {
105 self::tryOpportunisticExecute( 'run' );
106 }
107 }
108
109 /**
110 * Add a callable update. In a lot of cases, we just need a callback/closure,
111 * defining a new DeferrableUpdate object is not necessary
112 *
113 * @see MWCallableUpdate::__construct()
114 *
115 * @param callable $callable
116 * @param int $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
117 * @param IDatabase|IDatabase[]|null $dbw Abort if this DB is rolled back [optional] (since 1.28)
118 */
119 public static function addCallableUpdate(
120 $callable, $stage = self::POSTSEND, $dbw = null
121 ) {
122 self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage );
123 }
124
125 /**
126 * Do any deferred updates and clear the list
127 *
128 * If $stage is self::ALL then the queue of PRESEND updates will be resolved,
129 * followed by the queue of POSTSEND updates
130 *
131 * @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"]
132 * @param int $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27)
133 */
134 public static function doUpdates( $mode = 'run', $stage = self::ALL ) {
135 $stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage;
136 // For ALL mode, make sure that any PRESEND updates added along the way get run.
137 // Normally, these use the subqueue, but that isn't true for MergeableUpdate items.
138 do {
139 if ( $stage === self::ALL || $stage === self::PRESEND ) {
140 self::handleUpdateQueue( self::$preSendUpdates, $mode, $stageEffective );
141 }
142
143 if ( $stage === self::ALL || $stage == self::POSTSEND ) {
144 self::handleUpdateQueue( self::$postSendUpdates, $mode, $stageEffective );
145 }
146 } while ( $stage === self::ALL && self::$preSendUpdates );
147 }
148
149 /**
150 * @param DeferrableUpdate[] $queue
151 * @param DeferrableUpdate $update
152 */
153 private static function push( array &$queue, DeferrableUpdate $update ) {
154 if ( $update instanceof MergeableUpdate ) {
155 $class = get_class( $update ); // fully-qualified class
156 if ( isset( $queue[$class] ) ) {
157 /** @var MergeableUpdate $existingUpdate */
158 $existingUpdate = $queue[$class];
159 $existingUpdate->merge( $update );
160 // Move the update to the end to handle things like mergeable purge
161 // updates that might depend on the prior updates in the queue running
162 unset( $queue[$class] );
163 $queue[$class] = $existingUpdate;
164 } else {
165 $queue[$class] = $update;
166 }
167 } else {
168 $queue[] = $update;
169 }
170 }
171
172 /**
173 * Immediately run or enqueue a list of updates
174 *
175 * @param DeferrableUpdate[] &$queue List of DeferrableUpdate objects
176 * @param string $mode Either "run" or "enqueue" (to use the job queue when possible)
177 * @param int $stage Class constant (PRESEND, POSTSEND) (since 1.28)
178 * @throws ErrorPageError Happens on top-level calls
179 * @throws Exception Happens on second-level calls
180 */
181 protected static function handleUpdateQueue( array &$queue, $mode, $stage ) {
182 $services = MediaWikiServices::getInstance();
183 $stats = $services->getStatsdDataFactory();
184 $lbFactory = $services->getDBLoadBalancerFactory();
185 $method = RequestContext::getMain()->getRequest()->getMethod();
186
187 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
188
189 /** @var ErrorPageError $reportableError */
190 $reportableError = null;
191 /** @var DeferrableUpdate[] $updates Snapshot of queue */
192 $updates = $queue;
193
194 // Keep doing rounds of updates until none get enqueued...
195 while ( $updates ) {
196 $queue = []; // clear the queue
197
198 // Order will be DataUpdate followed by generic DeferrableUpdate tasks
199 $updatesByType = [ 'data' => [], 'generic' => [] ];
200 foreach ( $updates as $du ) {
201 if ( $du instanceof DataUpdate ) {
202 $du->setTransactionTicket( $ticket );
203 $updatesByType['data'][] = $du;
204 } else {
205 $updatesByType['generic'][] = $du;
206 }
207
208 $name = ( $du instanceof DeferrableCallback )
209 ? get_class( $du ) . '-' . $du->getOrigin()
210 : get_class( $du );
211 $stats->increment( 'deferred_updates.' . $method . '.' . $name );
212 }
213
214 // Execute all remaining tasks...
215 foreach ( $updatesByType as $updatesForType ) {
216 foreach ( $updatesForType as $update ) {
217 self::$executeContext = [ 'stage' => $stage, 'subqueue' => [] ];
218 try {
219 /** @var DeferrableUpdate $update */
220 $guiError = self::handleUpdate( $update, $lbFactory, $mode, $stage );
221 $reportableError = $reportableError ?: $guiError;
222 // Do the subqueue updates for $update until there are none
223 while ( self::$executeContext['subqueue'] ) {
224 $subUpdate = reset( self::$executeContext['subqueue'] );
225 $firstKey = key( self::$executeContext['subqueue'] );
226 unset( self::$executeContext['subqueue'][$firstKey] );
227
228 if ( $subUpdate instanceof DataUpdate ) {
229 $subUpdate->setTransactionTicket( $ticket );
230 }
231
232 $guiError = self::handleUpdate( $subUpdate, $lbFactory, $mode, $stage );
233 $reportableError = $reportableError ?: $guiError;
234 }
235 } finally {
236 // Make sure we always clean up the context.
237 // Losing updates while rewinding the stack is acceptable,
238 // losing updates that are added later is not.
239 self::$executeContext = null;
240 }
241 }
242 }
243
244 $updates = $queue; // new snapshot of queue (check for new entries)
245 }
246
247 if ( $reportableError ) {
248 throw $reportableError; // throw the first of any GUI errors
249 }
250 }
251
252 /**
253 * Run or enqueue an update
254 *
255 * @param DeferrableUpdate $update
256 * @param LBFactory $lbFactory
257 * @param string $mode
258 * @param int $stage
259 * @return ErrorPageError|null
260 */
261 private static function handleUpdate(
262 DeferrableUpdate $update, LBFactory $lbFactory, $mode, $stage
263 ) {
264 $guiError = null;
265 try {
266 if ( $mode === 'enqueue' && $update instanceof EnqueueableDataUpdate ) {
267 // Run only the job enqueue logic to complete the update later
268 $spec = $update->getAsJobSpecification();
269 $domain = $spec['domain'] ?? $spec['wiki'];
270 JobQueueGroup::singleton( $domain )->push( $spec['job'] );
271 } else {
272 self::attemptUpdate( $update, $lbFactory );
273 }
274 } catch ( Exception $e ) {
275 // Reporting GUI exceptions does not work post-send
276 if ( $e instanceof ErrorPageError && $stage === self::PRESEND ) {
277 $guiError = $e;
278 }
279 $lbFactory->rollbackMasterChanges( __METHOD__ );
280
281 // VW-style hack to work around T190178, so we can make sure
282 // PageMetaDataUpdater doesn't throw exceptions.
283 if ( defined( 'MW_PHPUNIT_TEST' ) ) {
284 throw $e;
285 }
286 }
287
288 return $guiError;
289 }
290
291 /**
292 * Attempt to run an update with the appropriate transaction round state it expects
293 *
294 * DeferredUpdate classes that wrap the execution of bundles of other DeferredUpdate
295 * instances can use this method to run the updates. Any such wrapper class should
296 * always use TRX_ROUND_ABSENT itself.
297 *
298 * @param DeferrableUpdate $update
299 * @param ILBFactory $lbFactory
300 * @since 1.34
301 */
302 public static function attemptUpdate( DeferrableUpdate $update, ILBFactory $lbFactory ) {
303 if (
304 $update instanceof TransactionRoundAwareUpdate &&
305 $update->getTransactionRoundRequirement() == $update::TRX_ROUND_ABSENT
306 ) {
307 $update->doUpdate();
308 } else {
309 // Run the bulk of the update now
310 $fnameTrxOwner = get_class( $update ) . '::doUpdate';
311 $lbFactory->beginMasterChanges( $fnameTrxOwner );
312 $update->doUpdate();
313 $lbFactory->commitMasterChanges( $fnameTrxOwner );
314 }
315 }
316
317 /**
318 * Run all deferred updates immediately if there are no DB writes active
319 *
320 * If there are many deferred updates pending, $mode is 'run', and there
321 * are still busy LBFactory database handles, then any EnqueueableDataUpdate
322 * tasks might be enqueued as jobs to be executed later.
323 *
324 * @param string $mode Use "enqueue" to use the job queue when possible
325 * @return bool Whether updates were allowed to run
326 * @since 1.28
327 */
328 public static function tryOpportunisticExecute( $mode = 'run' ) {
329 // execute() loop is already running
330 if ( self::$executeContext ) {
331 return false;
332 }
333
334 // Avoiding running updates without them having outer scope
335 if ( !self::areDatabaseTransactionsActive() ) {
336 self::doUpdates( $mode );
337 return true;
338 }
339
340 if ( self::pendingUpdatesCount() >= self::BIG_QUEUE_SIZE ) {
341 // If we cannot run the updates with outer transaction context, try to
342 // at least enqueue all the updates that support queueing to job queue
343 self::$preSendUpdates = self::enqueueUpdates( self::$preSendUpdates );
344 self::$postSendUpdates = self::enqueueUpdates( self::$postSendUpdates );
345 }
346
347 return !self::pendingUpdatesCount();
348 }
349
350 /**
351 * Enqueue a job for each EnqueueableDataUpdate item and return the other items
352 *
353 * @param DeferrableUpdate[] $updates A list of deferred update instances
354 * @return DeferrableUpdate[] Remaining updates that do not support being queued
355 */
356 private static function enqueueUpdates( array $updates ) {
357 $remaining = [];
358
359 foreach ( $updates as $update ) {
360 if ( $update instanceof EnqueueableDataUpdate ) {
361 $spec = $update->getAsJobSpecification();
362 $domain = $spec['domain'] ?? $spec['wiki'];
363 JobQueueGroup::singleton( $domain )->push( $spec['job'] );
364 } else {
365 $remaining[] = $update;
366 }
367 }
368
369 return $remaining;
370 }
371
372 /**
373 * @return int Number of enqueued updates
374 * @since 1.28
375 */
376 public static function pendingUpdatesCount() {
377 return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
378 }
379
380 /**
381 * @param int $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL)
382 * @return DeferrableUpdate[]
383 * @since 1.29
384 */
385 public static function getPendingUpdates( $stage = self::ALL ) {
386 $updates = [];
387 if ( $stage === self::ALL || $stage === self::PRESEND ) {
388 $updates = array_merge( $updates, self::$preSendUpdates );
389 }
390 if ( $stage === self::ALL || $stage === self::POSTSEND ) {
391 $updates = array_merge( $updates, self::$postSendUpdates );
392 }
393 return $updates;
394 }
395
396 /**
397 * Clear all pending updates without performing them. Generally, you don't
398 * want or need to call this. Unit tests need it though.
399 */
400 public static function clearPendingUpdates() {
401 self::$preSendUpdates = [];
402 self::$postSendUpdates = [];
403 }
404
405 /**
406 * @return bool If a transaction round is active or connection is not ready for commit()
407 */
408 private static function areDatabaseTransactionsActive() {
409 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
410 if ( $lbFactory->hasTransactionRound() || !$lbFactory->isReadyForRoundOperations() ) {
411 return true;
412 }
413
414 $connsBusy = false;
415 $lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
416 $lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
417 if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
418 $connsBusy = true;
419 }
420 } );
421 } );
422
423 return $connsBusy;
424 }
425 }