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