Add @deprecated tags to various class_alias calls
[lhc/web/wiklou.git] / includes / libs / rdbms / lbfactory / LBFactory.php
index c272147..e8ec250 100644 (file)
@@ -88,6 +88,16 @@ abstract class LBFactory implements ILBFactory {
        /** @var string Agent name for query profiling */
        protected $agent;
 
+       /** @var string One of the ROUND_* class constants */
+       private $trxRoundStage = self::ROUND_CURSORY;
+
+       const ROUND_CURSORY = 'cursory';
+       const ROUND_BEGINNING = 'within-begin';
+       const ROUND_COMMITTING = 'within-commit';
+       const ROUND_ROLLING_BACK = 'within-rollback';
+       const ROUND_COMMIT_CALLBACKS = 'within-commit-callbacks';
+       const ROUND_ROLLBACK_CALLBACKS = 'within-rollback-callbacks';
+
        private static $loggerFields =
                [ 'replLogger', 'connLogger', 'queryLogger', 'perfLogger' ];
 
@@ -129,6 +139,7 @@ abstract class LBFactory implements ILBFactory {
                        'IPAddress' => isset( $_SERVER[ 'REMOTE_ADDR' ] ) ? $_SERVER[ 'REMOTE_ADDR' ] : '',
                        'UserAgent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '',
                        'ChronologyProtection' => 'true',
+                       // phpcs:ignore MediaWiki.Usage.SuperGlobalsUsage.SuperGlobals -- library can't use $wgRequest
                        'ChronologyPositionIndex' => isset( $_GET['cpPosIndex'] ) ? $_GET['cpPosIndex'] : null
                ];
 
@@ -162,28 +173,28 @@ abstract class LBFactory implements ILBFactory {
        /**
         * @see ILBFactory::newMainLB()
         * @param bool $domain
-        * @return LoadBalancer
+        * @return ILoadBalancer
         */
        abstract public function newMainLB( $domain = false );
 
        /**
         * @see ILBFactory::getMainLB()
         * @param bool $domain
-        * @return LoadBalancer
+        * @return ILoadBalancer
         */
        abstract public function getMainLB( $domain = false );
 
        /**
         * @see ILBFactory::newExternalLB()
         * @param string $cluster
-        * @return LoadBalancer
+        * @return ILoadBalancer
         */
        abstract public function newExternalLB( $cluster );
 
        /**
         * @see ILBFactory::getExternalLB()
         * @param string $cluster
-        * @return LoadBalancer
+        * @return ILoadBalancer
         */
        abstract public function getExternalLB( $cluster );
 
@@ -206,12 +217,15 @@ abstract class LBFactory implements ILBFactory {
                $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] );
        }
 
-       public function commitAll( $fname = __METHOD__, array $options = [] ) {
+       final public function commitAll( $fname = __METHOD__, array $options = [] ) {
                $this->commitMasterChanges( $fname, $options );
-               $this->forEachLBCallMethod( 'commitAll', [ $fname ] );
+               $this->forEachLBCallMethod( 'flushMasterSnapshots', [ $fname ] );
+               $this->forEachLBCallMethod( 'flushReplicaSnapshots', [ $fname ] );
        }
 
-       public function beginMasterChanges( $fname = __METHOD__ ) {
+       final public function beginMasterChanges( $fname = __METHOD__ ) {
+               $this->assertTransactionRoundStage( self::ROUND_CURSORY );
+               $this->trxRoundStage = self::ROUND_BEGINNING;
                if ( $this->trxRoundId !== false ) {
                        throw new DBTransactionError(
                                null,
@@ -221,9 +235,12 @@ abstract class LBFactory implements ILBFactory {
                $this->trxRoundId = $fname;
                // Set DBO_TRX flags on all appropriate DBs
                $this->forEachLBCallMethod( 'beginMasterChanges', [ $fname ] );
+               $this->trxRoundStage = self::ROUND_CURSORY;
        }
 
-       public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) {
+       final public function commitMasterChanges( $fname = __METHOD__, array $options = [] ) {
+               $this->assertTransactionRoundStage( self::ROUND_CURSORY );
+               $this->trxRoundStage = self::ROUND_COMMITTING;
                if ( $this->trxRoundId !== false && $this->trxRoundId !== $fname ) {
                        throw new DBTransactionError(
                                null,
@@ -233,7 +250,12 @@ abstract class LBFactory implements ILBFactory {
                /** @noinspection PhpUnusedLocalVariableInspection */
                $scope = $this->getScopedPHPBehaviorForCommit(); // try to ignore client aborts
                // Run pre-commit callbacks and suppress post-commit callbacks, aborting on failure
-               $this->forEachLBCallMethod( 'finalizeMasterChanges' );
+               do {
+                       $count = 0; // number of callbacks executed this iteration
+                       $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$count ) {
+                               $count += $lb->finalizeMasterChanges();
+                       } );
+               } while ( $count > 0 );
                $this->trxRoundId = false;
                // Perform pre-commit checks, aborting on failure
                $this->forEachLBCallMethod( 'approveMasterChanges', [ $options ] );
@@ -241,35 +263,56 @@ abstract class LBFactory implements ILBFactory {
                $this->logIfMultiDbTransaction();
                // Actually perform the commit on all master DB connections and revert DBO_TRX
                $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
-               // Run all post-commit callbacks
-               /** @var Exception $e */
-               $e = null; // first callback exception
-               $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$e ) {
-                       $ex = $lb->runMasterPostTrxCallbacks( IDatabase::TRIGGER_COMMIT );
-                       $e = $e ?: $ex;
-               } );
-               // Commit any dangling DBO_TRX transactions from callbacks on one DB to another DB
-               $this->forEachLBCallMethod( 'commitMasterChanges', [ $fname ] );
+               // Run all post-commit callbacks in a separate step
+               $this->trxRoundStage = self::ROUND_COMMIT_CALLBACKS;
+               $e = $this->executePostTransactionCallbacks();
+               $this->trxRoundStage = self::ROUND_CURSORY;
                // Throw any last post-commit callback error
                if ( $e instanceof Exception ) {
                        throw $e;
                }
        }
 
-       public function rollbackMasterChanges( $fname = __METHOD__ ) {
+       final public function rollbackMasterChanges( $fname = __METHOD__ ) {
+               $this->trxRoundStage = self::ROUND_ROLLING_BACK;
                $this->trxRoundId = false;
-               $this->forEachLBCallMethod( 'suppressTransactionEndCallbacks' );
+               // Actually perform the rollback on all master DB connections and revert DBO_TRX
                $this->forEachLBCallMethod( 'rollbackMasterChanges', [ $fname ] );
-               // Run all post-rollback callbacks
-               $this->forEachLB( function ( ILoadBalancer $lb ) {
-                       $lb->runMasterPostTrxCallbacks( IDatabase::TRIGGER_ROLLBACK );
+               // Run all post-commit callbacks in a separate step
+               $this->trxRoundStage = self::ROUND_ROLLBACK_CALLBACKS;
+               $this->executePostTransactionCallbacks();
+               $this->trxRoundStage = self::ROUND_CURSORY;
+       }
+
+       /**
+        * @return Exception|null
+        */
+       private function executePostTransactionCallbacks() {
+               // Run all post-commit callbacks until new ones stop getting added
+               $e = null; // first callback exception
+               do {
+                       $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$e ) {
+                               $ex = $lb->runMasterTransactionIdleCallbacks();
+                               $e = $e ?: $ex;
+                       } );
+               } while ( $this->hasMasterChanges() );
+               // Run all listener callbacks once
+               $this->forEachLB( function ( ILoadBalancer $lb ) use ( &$e ) {
+                       $ex = $lb->runMasterTransactionListenerCallbacks();
+                       $e = $e ?: $ex;
                } );
+
+               return $e;
        }
 
        public function hasTransactionRound() {
                return ( $this->trxRoundId !== false );
        }
 
+       public function isReadyForRoundOperations() {
+               return ( $this->trxRoundStage === self::ROUND_CURSORY );
+       }
+
        /**
         * Log query info if multi DB transactions are going to be committed now
         */
@@ -408,7 +451,7 @@ abstract class LBFactory implements ILBFactory {
                return $this->ticket;
        }
 
-       public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) {
+       final public function commitAndWaitForReplication( $fname, $ticket, array $opts = [] ) {
                if ( $ticket !== $this->ticket ) {
                        $this->perfLogger->error( __METHOD__ . ": $fname does not have outer scope.\n" .
                                ( new RuntimeException() )->getTraceAsString() );
@@ -509,10 +552,18 @@ abstract class LBFactory implements ILBFactory {
        }
 
        /**
-        * Base parameters to LoadBalancer::__construct()
+        * Base parameters to ILoadBalancer::__construct()
         * @return array
         */
        final protected function baseLoadBalancerParams() {
+               if ( $this->trxRoundStage === self::ROUND_COMMIT_CALLBACKS ) {
+                       $initStage = ILoadBalancer::STAGE_POSTCOMMIT_CALLBACKS;
+               } elseif ( $this->trxRoundStage === self::ROUND_ROLLBACK_CALLBACKS ) {
+                       $initStage = ILoadBalancer::STAGE_POSTROLLBACK_CALLBACKS;
+               } else {
+                       $initStage = null;
+               }
+
                return [
                        'localDomain' => $this->localDomain,
                        'readOnlyReason' => $this->readOnlyReason,
@@ -532,7 +583,8 @@ abstract class LBFactory implements ILBFactory {
                                // Defer ChronologyProtector construction in case setRequestInfo() ends up
                                // being called later (but before the first connection attempt) (T192611)
                                $this->getChronologyProtector()->initLB( $lb );
-                       }
+                       },
+                       'roundStage' => $initStage
                ];
        }
 
@@ -589,6 +641,36 @@ abstract class LBFactory implements ILBFactory {
                return strpos( $url, '?' ) === false ? "$url?cpPosIndex=$index" : "$url&cpPosIndex=$index";
        }
 
+       /**
+        * @param int $index Write index
+        * @param int $time UNIX timestamp
+        * @return string Timestamp-qualified write index of the form "<index>.<timestamp>"
+        * @since 1.32
+        */
+       public static function makeCookieValueFromCPIndex( $index, $time ) {
+               return $index . '@' . $time;
+       }
+
+       /**
+        * @param string $value String possibly of the form "<index>" or "<index>@<timestamp>"
+        * @param int $minTimestamp Lowest UNIX timestamp of non-expired values (if present)
+        * @return int|null Write index or null if $value is empty or expired
+        * @since 1.32
+        */
+       public static function getCPIndexFromCookieValue( $value, $minTimestamp ) {
+               if ( !preg_match( '/^(\d+)(?:@(\d+))?$/', $value, $m ) ) {
+                       return null;
+               }
+
+               $index = (int)$m[1];
+
+               if ( isset( $m[2] ) && $m[2] !== '' && (int)$m[2] < $minTimestamp ) {
+                       return null; // expired
+               }
+
+               return ( $index > 0 ) ? $index : null;
+       }
+
        public function setRequestInfo( array $info ) {
                if ( $this->chronProt ) {
                        throw new LogicException( 'ChronologyProtector already initialized.' );
@@ -597,6 +679,18 @@ abstract class LBFactory implements ILBFactory {
                $this->requestInfo = $info + $this->requestInfo;
        }
 
+       /**
+        * @param string $stage
+        */
+       private function assertTransactionRoundStage( $stage ) {
+               if ( $this->trxRoundStage !== $stage ) {
+                       throw new DBTransactionError(
+                               null,
+                               "Transaction round stage must be '$stage' (not '{$this->trxRoundStage}')"
+                       );
+               }
+       }
+
        /**
         * Make PHP ignore user aborts/disconnects until the returned
         * value leaves scope. This returns null and does nothing in CLI mode.
@@ -619,4 +713,7 @@ abstract class LBFactory implements ILBFactory {
        }
 }
 
+/**
+ * @deprecated since 1.29
+ */
 class_alias( LBFactory::class, 'LBFactory' );