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