| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Deferred; |
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use Wikimedia\Rdbms\DBTransactionError; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class DeferredUpdatesScopeMediaWikiStack extends DeferredUpdatesScopeStack { |
| 37 |
|
| 38 |
private function areDatabaseTransactionsActive(): bool { |
| 39 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 40 |
if ( $lbFactory->hasTransactionRound() |
| 41 |
|| !$lbFactory->isReadyForRoundOperations() |
| 42 |
) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
foreach ( $lbFactory->getAllLBs() as $lb ) { |
| 47 |
if ( $lb->hasPrimaryChanges() || $lb->explicitTrxActive() ) { |
| 48 |
return true; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
public function allowOpportunisticUpdates(): bool { |
| 56 |
if ( MW_ENTRY_POINT !== 'cli' ) { |
| 57 |
|
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
if ( $this->areDatabaseTransactionsActive() ) { |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
public function queueDataUpdate( EnqueueableDataUpdate $update ): void { |
| 71 |
$spec = $update->getAsJobSpecification(); |
| 72 |
$jobQueueGroupFactory = MediaWikiServices::getInstance()->getJobQueueGroupFactory(); |
| 73 |
$jobQueueGroupFactory->makeJobQueueGroup( $spec['domain'] )->push( $spec['job'] ); |
| 74 |
} |
| 75 |
|
| 76 |
public function onRunUpdateStart( DeferrableUpdate $update ): void { |
| 77 |
|
| 78 |
$type = get_class( $update ) |
| 79 |
. ( $update instanceof DeferrableCallback ? '_' . $update->getOrigin() : '' ); |
| 80 |
$httpMethod = MW_ENTRY_POINT === 'cli' ? 'cli' : strtolower( $_SERVER['REQUEST_METHOD'] ?? 'GET' ); |
| 81 |
$stats = MediaWikiServices::getInstance()->getStatsFactory(); |
| 82 |
$stats->getCounter( 'deferred_updates_total' ) |
| 83 |
->setLabel( 'http_method', $httpMethod ) |
| 84 |
->setLabel( 'type', $type ) |
| 85 |
->copyToStatsdAt( "deferred_updates.$httpMethod.$type" ) |
| 86 |
->increment(); |
| 87 |
|
| 88 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 89 |
$ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ ); |
| 90 |
if ( !$ticket || $lbFactory->hasTransactionRound() ) { |
| 91 |
throw new DBTransactionError( null, "A database transaction round is pending." ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $update instanceof DataUpdate ) { |
| 95 |
$update->setTransactionTicket( $ticket ); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
$fnameTrxOwner = ( $update instanceof DeferrableCallback ) |
| 100 |
? $update->getOrigin() |
| 101 |
: get_class( $update ) . '::doUpdate'; |
| 102 |
|
| 103 |
|
| 104 |
$useExplicitTrxRound = !( |
| 105 |
$update instanceof TransactionRoundAwareUpdate && |
| 106 |
$update->getTransactionRoundRequirement() == $update::TRX_ROUND_ABSENT |
| 107 |
); |
| 108 |
if ( $useExplicitTrxRound ) { |
| 109 |
|
| 110 |
$lbFactory->beginPrimaryChanges( $fnameTrxOwner ); |
| 111 |
} else { |
| 112 |
|
| 113 |
$lbFactory->commitPrimaryChanges( $fnameTrxOwner ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public function onRunUpdateEnd( DeferrableUpdate $update ): void { |
| 118 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 119 |
|
| 120 |
$fnameTrxOwner = ( $update instanceof DeferrableCallback ) |
| 121 |
? $update->getOrigin() |
| 122 |
: get_class( $update ) . '::doUpdate'; |
| 123 |
|
| 124 |
|
| 125 |
$lbFactory->commitPrimaryChanges( $fnameTrxOwner ); |
| 126 |
} |
| 127 |
|
| 128 |
public function onRunUpdateFailed( DeferrableUpdate $update ): void { |
| 129 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 130 |
$lbFactory->rollbackPrimaryChanges( __METHOD__ ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
class_alias( DeferredUpdatesScopeMediaWikiStack::class, 'DeferredUpdatesScopeMediaWikiStack' ); |