Update git submodules
[mediawiki.git] / includes / deferred / DeferredUpdatesScopeMediaWikiStack.php
blobadd54ff337a7685b477c7f95506864913df5457d
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\MediaWikiServices;
22 use Wikimedia\Rdbms\DBTransactionError;
24 /**
25 * This class decouples DeferredUpdates's awareness of MediaWikiServices to ease unit testing.
27 * NOTE: As a process-level utility, it is important that MediaWikiServices::getInstance() is
28 * referenced explicitly each time, so as to not cache potentially stale references.
29 * For example after the Installer, or MediaWikiIntegrationTestCase, replace the service container.
31 * @internal For use by DeferredUpdates only
32 * @since 1.41
34 class DeferredUpdatesScopeMediaWikiStack extends DeferredUpdatesScopeStack {
36 private function areDatabaseTransactionsActive(): bool {
37 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
38 if ( $lbFactory->hasTransactionRound()
39 || !$lbFactory->isReadyForRoundOperations()
40 ) {
41 return true;
44 foreach ( $lbFactory->getAllLBs() as $lb ) {
45 if ( $lb->hasPrimaryChanges() || $lb->explicitTrxActive() ) {
46 return true;
50 return false;
53 public function allowOpportunisticUpdates(): bool {
54 global $wgCommandLineMode;
56 if ( !$wgCommandLineMode ) {
57 // In web req
58 return false;
61 // Run the updates only if they will have outer transaction scope
62 if ( $this->areDatabaseTransactionsActive() ) {
63 // transaction round is active or connection is not ready for commit()
64 return false;
67 return true;
70 public function queueDataUpdate( EnqueueableDataUpdate $update ): void {
71 $spec = $update->getAsJobSpecification();
72 $jobQueueGroupFactory = MediaWikiServices::getInstance()->getJobQueueGroupFactory();
73 $jobQueueGroupFactory->makeJobQueueGroup( $spec['domain'] )->push( $spec['job'] );
76 public function onRunUpdateStart( DeferrableUpdate $update ): void {
77 global $wgCommandLineMode;
79 // Increment a counter metric
80 $type = get_class( $update )
81 . ( $update instanceof DeferrableCallback ? '_' . $update->getOrigin() : '' );
82 $httpMethod = $wgCommandLineMode ? 'cli' : strtolower( $_SERVER['REQUEST_METHOD'] ?? 'GET' );
83 $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
84 $stats->increment( "deferred_updates.$httpMethod.$type" );
86 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
87 $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
88 if ( !$ticket || $lbFactory->hasTransactionRound() ) {
89 throw new DBTransactionError( null, "A database transaction round is pending." );
92 if ( $update instanceof DataUpdate ) {
93 $update->setTransactionTicket( $ticket );
96 // Designate $update::doUpdate() as the write round owner
97 $fnameTrxOwner = ( $update instanceof DeferrableCallback )
98 ? $update->getOrigin()
99 : get_class( $update ) . '::doUpdate';
101 // Determine whether the write round will be explicit or implicit
102 $useExplicitTrxRound = !(
103 $update instanceof TransactionRoundAwareUpdate &&
104 $update->getTransactionRoundRequirement() == $update::TRX_ROUND_ABSENT
107 // Ensure any stale repeatable-read snapshot on the primary DB have been flushed
108 // before running the update. E.g. left-over from an implicit transaction round
109 if ( $useExplicitTrxRound ) {
110 // new explicit round
111 $lbFactory->beginPrimaryChanges( $fnameTrxOwner );
112 } else {
113 // new implicit round
114 $lbFactory->commitPrimaryChanges( $fnameTrxOwner );
118 public function onRunUpdateEnd( DeferrableUpdate $update ): void {
119 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
121 $fnameTrxOwner = ( $update instanceof DeferrableCallback )
122 ? $update->getOrigin()
123 : get_class( $update ) . '::doUpdate';
125 // Commit any pending changes from the explicit or implicit transaction round
126 $lbFactory->commitPrimaryChanges( $fnameTrxOwner );
129 public function onRunUpdateFailed( DeferrableUpdate $update ): void {
130 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
131 $lbFactory->rollbackPrimaryChanges( __METHOD__ );