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