Merge "Remove some ancient upgrade information from release notes"
[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, updates run immediately if no DB writes are pending. Otherwise, they run when:
40 * - a) Any waitForReplication() call if no writes are pending on any DB
41 * - b) A commit happens on Maintenance::getDB( DB_MASTER ) if no writes are pending on any DB
42 * - c) EnqueueableDataUpdate tasks may enqueue on commit of Maintenance::getDB( DB_MASTER )
43 * - d) At the completion of Maintenance::execute()
44 *
45 * When updates are deferred, they go into one two FIFO "top-queues" (one for pre-send and one
46 * for post-send). Updates enqueued *during* doUpdate() of a "top" update go into the "sub-queue"
47 * for that update. After that method finishes, the sub-queue is run until drained. This continues
48 * for each top-queue job until the entire top queue is drained. This happens for the pre-send
49 * top-queue, and later on, the post-send top-queue, in execute().
50 *
51 * @since 1.19
52 */
53 class DeferredUpdates {
54 /** @var DeferrableUpdate[] Updates to be deferred until before request end */
55 private static $preSendUpdates = [];
56 /** @var DeferrableUpdate[] Updates to be deferred until after request end */
57 private static $postSendUpdates = [];
58 /** @var bool Whether to just run updates in addUpdate() */
59 private static $immediateMode = false;
60
61 const ALL = 0; // all updates; in web requests, use only after flushing the output buffer
62 const PRESEND = 1; // for updates that should run before flushing output buffer
63 const POSTSEND = 2; // for updates that should run after flushing output buffer
64
65 const BIG_QUEUE_SIZE = 100;
66
67 /** @var array|null Information about the current execute() call or null if not running */
68 private static $executeContext;
69
70 /**
71 * Add an update to the deferred list to be run later by execute()
72 *
73 * In CLI mode, callback magic will also be used to run updates when safe
74 *
75 * @param DeferrableUpdate $update Some object that implements doUpdate()
76 * @param integer $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
77 */
78 public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) {
79 global $wgCommandLineMode;
80
81 // This is a sub-DeferredUpdate, run it right after its parent update
82 if ( self::$executeContext && self::$executeContext['stage'] >= $stage ) {
83 self::$executeContext['subqueue'][] = $update;
84 return;
85 }
86
87 if ( $stage === self::PRESEND ) {
88 self::push( self::$preSendUpdates, $update );
89 } else {
90 self::push( self::$postSendUpdates, $update );
91 }
92
93 if ( self::$immediateMode ) {
94 // No more explicit doUpdates() calls will happen, so run this now
95 self::doUpdates( 'run' );
96 return;
97 }
98
99 // Try to run the updates now if in CLI mode and no transaction is active.
100 // This covers scripts that don't/barely use the DB but make updates to other stores.
101 if ( $wgCommandLineMode ) {
102 self::tryOpportunisticExecute( 'run' );
103 }
104 }
105
106 /**
107 * Add a callable update. In a lot of cases, we just need a callback/closure,
108 * defining a new DeferrableUpdate object is not necessary
109 *
110 * @see MWCallableUpdate::__construct()
111 *
112 * @param callable $callable
113 * @param integer $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27)
114 * @param IDatabase|null $dbw Abort if this DB is rolled back [optional] (since 1.28)
115 */
116 public static function addCallableUpdate(
117 $callable, $stage = self::POSTSEND, IDatabase $dbw = null
118 ) {
119 self::addUpdate( new MWCallableUpdate( $callable, wfGetCaller(), $dbw ), $stage );
120 }
121
122 /**
123 * Do any deferred updates and clear the list
124 *
125 * @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"]
126 * @param integer $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27)
127 */
128 public static function doUpdates( $mode = 'run', $stage = self::ALL ) {
129 $stageEffective = ( $stage === self::ALL ) ? self::POSTSEND : $stage;
130
131 if ( $stage === self::ALL || $stage === self::PRESEND ) {
132 self::execute( self::$preSendUpdates, $mode, $stageEffective );
133 }
134
135 if ( $stage === self::ALL || $stage == self::POSTSEND ) {
136 self::execute( self::$postSendUpdates, $mode, $stageEffective );
137 }
138 }
139
140 /**
141 * @param bool $value Whether to just immediately run updates in addUpdate()
142 * @since 1.28
143 */
144 public static function setImmediateMode( $value ) {
145 self::$immediateMode = (bool)$value;
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 $existingUpdate MergeableUpdate */
157 $existingUpdate = $queue[$class];
158 $existingUpdate->merge( $update );
159 } else {
160 $queue[$class] = $update;
161 }
162 } else {
163 $queue[] = $update;
164 }
165 }
166
167 /**
168 * Immediately run/queue a list of updates
169 *
170 * @param DeferrableUpdate[] &$queue List of DeferrableUpdate objects
171 * @param string $mode Use "enqueue" to use the job queue when possible
172 * @param integer $stage Class constant (PRESEND, POSTSEND) (since 1.28)
173 * @throws ErrorPageError Happens on top-level calls
174 * @throws Exception Happens on second-level calls
175 */
176 protected static function execute( array &$queue, $mode, $stage ) {
177 $services = MediaWikiServices::getInstance();
178 $stats = $services->getStatsdDataFactory();
179 $lbFactory = $services->getDBLoadBalancerFactory();
180 $method = RequestContext::getMain()->getRequest()->getMethod();
181
182 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
183
184 /** @var ErrorPageError $reportableError */
185 $reportableError = null;
186 /** @var DeferrableUpdate[] $updates Snapshot of queue */
187 $updates = $queue;
188
189 // Keep doing rounds of updates until none get enqueued...
190 while ( $updates ) {
191 $queue = []; // clear the queue
192
193 if ( $mode === 'enqueue' ) {
194 try {
195 // Push enqueuable updates to the job queue and get the rest
196 $updates = self::enqueueUpdates( $updates );
197 } catch ( Exception $e ) {
198 // Let other updates have a chance to run if this failed
199 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
200 }
201 }
202
203 // Order will be DataUpdate followed by generic DeferrableUpdate tasks
204 $updatesByType = [ 'data' => [], 'generic' => [] ];
205 foreach ( $updates as $du ) {
206 if ( $du instanceof DataUpdate ) {
207 $du->setTransactionTicket( $ticket );
208 $updatesByType['data'][] = $du;
209 } else {
210 $updatesByType['generic'][] = $du;
211 }
212
213 $name = ( $du instanceof DeferrableCallback )
214 ? get_class( $du ) . '-' . $du->getOrigin()
215 : get_class( $du );
216 $stats->increment( 'deferred_updates.' . $method . '.' . $name );
217 }
218
219 // Execute all remaining tasks...
220 foreach ( $updatesByType as $updatesForType ) {
221 foreach ( $updatesForType as $update ) {
222 self::$executeContext = [
223 'update' => $update,
224 'stage' => $stage,
225 'subqueue' => []
226 ];
227 /** @var DeferrableUpdate $update */
228 $guiError = self::runUpdate( $update, $lbFactory, $stage );
229 $reportableError = $reportableError ?: $guiError;
230 // Do the subqueue updates for $update until there are none
231 while ( self::$executeContext['subqueue'] ) {
232 $subUpdate = reset( self::$executeContext['subqueue'] );
233 $firstKey = key( self::$executeContext['subqueue'] );
234 unset( self::$executeContext['subqueue'][$firstKey] );
235
236 if ( $subUpdate instanceof DataUpdate ) {
237 $subUpdate->setTransactionTicket( $ticket );
238 }
239
240 $guiError = self::runUpdate( $subUpdate, $lbFactory, $stage );
241 $reportableError = $reportableError ?: $guiError;
242 }
243 self::$executeContext = null;
244 }
245 }
246
247 $updates = $queue; // new snapshot of queue (check for new entries)
248 }
249
250 if ( $reportableError ) {
251 throw $reportableError; // throw the first of any GUI errors
252 }
253 }
254
255 /**
256 * @param DeferrableUpdate $update
257 * @param LBFactory $lbFactory
258 * @param integer $stage
259 * @return ErrorPageError|null
260 */
261 private static function runUpdate( DeferrableUpdate $update, LBFactory $lbFactory, $stage ) {
262 $guiError = null;
263 try {
264 $fnameTrxOwner = get_class( $update ) . '::doUpdate';
265 $lbFactory->beginMasterChanges( $fnameTrxOwner );
266 $update->doUpdate();
267 $lbFactory->commitMasterChanges( $fnameTrxOwner );
268 } catch ( Exception $e ) {
269 // Reporting GUI exceptions does not work post-send
270 if ( $e instanceof ErrorPageError && $stage === self::PRESEND ) {
271 $guiError = $e;
272 }
273 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
274 }
275
276 return $guiError;
277 }
278
279 /**
280 * Run all deferred updates immediately if there are no DB writes active
281 *
282 * If $mode is 'run' but there are busy databates, EnqueueableDataUpdate
283 * tasks will be enqueued anyway for the sake of progress.
284 *
285 * @param string $mode Use "enqueue" to use the job queue when possible
286 * @return bool Whether updates were allowed to run
287 * @since 1.28
288 */
289 public static function tryOpportunisticExecute( $mode = 'run' ) {
290 // execute() loop is already running
291 if ( self::$executeContext ) {
292 return false;
293 }
294
295 // Avoiding running updates without them having outer scope
296 if ( !self::getBusyDbConnections() ) {
297 self::doUpdates( $mode );
298 return true;
299 }
300
301 if ( self::pendingUpdatesCount() >= self::BIG_QUEUE_SIZE ) {
302 // If we cannot run the updates with outer transaction context, try to
303 // at least enqueue all the updates that support queueing to job queue
304 self::$preSendUpdates = self::enqueueUpdates( self::$preSendUpdates );
305 self::$postSendUpdates = self::enqueueUpdates( self::$postSendUpdates );
306 }
307
308 return !self::pendingUpdatesCount();
309 }
310
311 /**
312 * Enqueue a job for each EnqueueableDataUpdate item and return the other items
313 *
314 * @param DeferrableUpdate[] $updates A list of deferred update instances
315 * @return DeferrableUpdate[] Remaining updates that do not support being queued
316 */
317 private static function enqueueUpdates( array $updates ) {
318 $remaining = [];
319
320 foreach ( $updates as $update ) {
321 if ( $update instanceof EnqueueableDataUpdate ) {
322 $spec = $update->getAsJobSpecification();
323 JobQueueGroup::singleton( $spec['wiki'] )->push( $spec['job'] );
324 } else {
325 $remaining[] = $update;
326 }
327 }
328
329 return $remaining;
330 }
331
332 /**
333 * @return integer Number of enqueued updates
334 * @since 1.28
335 */
336 public static function pendingUpdatesCount() {
337 return count( self::$preSendUpdates ) + count( self::$postSendUpdates );
338 }
339
340 /**
341 * @param integer $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL)
342 * @since 1.29
343 */
344 public static function getPendingUpdates( $stage = self::ALL ) {
345 $updates = [];
346 if ( $stage === self::ALL || $stage === self::PRESEND ) {
347 $updates = array_merge( $updates, self::$preSendUpdates );
348 }
349 if ( $stage === self::ALL || $stage === self::POSTSEND ) {
350 $updates = array_merge( $updates, self::$postSendUpdates );
351 }
352 return $updates;
353 }
354
355 /**
356 * Clear all pending updates without performing them. Generally, you don't
357 * want or need to call this. Unit tests need it though.
358 */
359 public static function clearPendingUpdates() {
360 self::$preSendUpdates = [];
361 self::$postSendUpdates = [];
362 }
363
364 /**
365 * @return IDatabase[] Connection where commit() cannot be called yet
366 */
367 private static function getBusyDbConnections() {
368 $connsBusy = [];
369
370 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
371 $lbFactory->forEachLB( function ( LoadBalancer $lb ) use ( &$connsBusy ) {
372 $lb->forEachOpenMasterConnection( function ( IDatabase $conn ) use ( &$connsBusy ) {
373 if ( $conn->writesOrCallbacksPending() || $conn->explicitTrxActive() ) {
374 $connsBusy[] = $conn;
375 }
376 } );
377 } );
378
379 return $connsBusy;
380 }
381 }