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
21 use MediaWiki\MediaWikiServices
;
22 use Wikimedia\Rdbms\DBTransactionError
;
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
34 class DeferredUpdatesScopeMediaWikiStack
extends DeferredUpdatesScopeStack
{
36 private function areDatabaseTransactionsActive(): bool {
37 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
38 if ( $lbFactory->hasTransactionRound()
39 ||
!$lbFactory->isReadyForRoundOperations()
44 foreach ( $lbFactory->getAllLBs() as $lb ) {
45 if ( $lb->hasPrimaryChanges() ||
$lb->explicitTrxActive() ) {
53 public function allowOpportunisticUpdates(): bool {
54 global $wgCommandLineMode;
56 if ( !$wgCommandLineMode ) {
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()
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 );
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__
);