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