Merge "Add ext-dom to composer.json"
[lhc/web/wiklou.git] / includes / libs / rdbms / loadbalancer / LoadBalancer.php
index 5c172d6..771700c 100644 (file)
@@ -68,7 +68,9 @@ class LoadBalancer implements ILoadBalancer {
        /** @var DatabaseDomain Local DB domain ID and default for selectDB() calls */
        private $localDomain;
 
-       /** @var Database[][][] Map of (connection category => server index => IDatabase[]) */
+       /**
+        * @var IDatabase[][][]|Database[][][] Map of (connection category => server index => IDatabase[])
+        */
        private $conns;
 
        /** @var array[] Map of (server index => server config array) */
@@ -99,7 +101,7 @@ class LoadBalancer implements ILoadBalancer {
        private $tableAliases = [];
        /** @var string[] Map of (index alias => index) */
        private $indexAliases = [];
-       /** @var array[] Map of (name => callable) */
+       /** @var callable[] Map of (name => callable) */
        private $trxRecurringCallbacks = [];
        /** @var bool[] Map of (domain => whether to use "temp tables only" mode) */
        private $tempTablesOnlyMode = [];
@@ -970,17 +972,7 @@ class LoadBalancer implements ILoadBalancer {
                $serverIndex = $conn->getLBInfo( 'serverIndex' );
                $refCount = $conn->getLBInfo( 'foreignPoolRefCount' );
                if ( $serverIndex === null || $refCount === null ) {
-                       /**
-                        * This can happen in code like:
-                        *   foreach ( $dbs as $db ) {
-                        *     $conn = $lb->getConnection( $lb::DB_REPLICA, [], $db );
-                        *     ...
-                        *     $lb->reuseConnection( $conn );
-                        *   }
-                        * When a connection to the local DB is opened in this way, reuseConnection()
-                        * should be ignored
-                        */
-                       return;
+                       return; // non-foreign connection; no domain-use tracking to update
                } elseif ( $conn instanceof DBConnRef ) {
                        // DBConnRef already handles calling reuseConnection() and only passes the live
                        // Database instance to this method. Any caller passing in a DBConnRef is broken.
@@ -1312,7 +1304,7 @@ class LoadBalancer implements ILoadBalancer {
 
                // Create a live connection object
                try {
-                       $db = Database::factory( $server['type'], $server );
+                       $conn = Database::factory( $server['type'], $server );
                        // Log when many connection are made on requests
                        ++$this->connectionCounter;
                        $currentConnCount = $this->getCurrentConnectionCount();
@@ -1325,28 +1317,28 @@ class LoadBalancer implements ILoadBalancer {
                } catch ( DBConnectionError $e ) {
                        // FIXME: This is probably the ugliest thing I have ever done to
                        // PHP. I'm half-expecting it to segfault, just out of disgust. -- TS
-                       $db = $e->db;
+                       $conn = $e->db;
                }
 
-               $db->setLBInfo( $server );
-               $db->setLazyMasterHandle(
-                       $this->getLazyConnectionRef( self::DB_MASTER, [], $db->getDomainID() )
+               $conn->setLBInfo( $server );
+               $conn->setLazyMasterHandle(
+                       $this->getLazyConnectionRef( self::DB_MASTER, [], $conn->getDomainID() )
                );
-               $db->setTableAliases( $this->tableAliases );
-               $db->setIndexAliases( $this->indexAliases );
+               $conn->setTableAliases( $this->tableAliases );
+               $conn->setIndexAliases( $this->indexAliases );
 
                if ( $server['serverIndex'] === $this->getWriterIndex() ) {
                        if ( $this->trxRoundId !== false ) {
-                               $this->applyTransactionRoundFlags( $db );
+                               $this->applyTransactionRoundFlags( $conn );
                        }
                        foreach ( $this->trxRecurringCallbacks as $name => $callback ) {
-                               $db->setTransactionListener( $name, $callback );
+                               $conn->setTransactionListener( $name, $callback );
                        }
                }
 
                $this->lazyLoadReplicationPositions(); // session consistency
 
-               return $db;
+               return $conn;
        }
 
        /**
@@ -1508,6 +1500,11 @@ class LoadBalancer implements ILoadBalancer {
        }
 
        public function closeAll() {
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
+
                $fname = __METHOD__;
                $this->forEachOpenConnection( function ( IDatabase $conn ) use ( $fname ) {
                        $host = $conn->getServer();
@@ -1554,6 +1551,10 @@ class LoadBalancer implements ILoadBalancer {
        public function finalizeMasterChanges( $fname = __METHOD__, $owner = null ) {
                $this->assertOwnership( $fname, $owner );
                $this->assertTransactionRoundStage( [ self::ROUND_CURSORY, self::ROUND_FINALIZED ] );
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
                // Loop until callbacks stop adding callbacks on other connections
@@ -1579,6 +1580,10 @@ class LoadBalancer implements ILoadBalancer {
        public function approveMasterChanges( array $options, $fname = __METHOD__, $owner = null ) {
                $this->assertOwnership( $fname, $owner );
                $this->assertTransactionRoundStage( self::ROUND_FINALIZED );
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                $limit = $options['maxWriteDuration'] ?? 0;
 
@@ -1619,6 +1624,10 @@ class LoadBalancer implements ILoadBalancer {
                        );
                }
                $this->assertTransactionRoundStage( self::ROUND_CURSORY );
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                // Clear any empty transactions (no writes/callbacks) from the implicit round
                $this->flushMasterSnapshots( $fname );
@@ -1638,12 +1647,13 @@ class LoadBalancer implements ILoadBalancer {
        public function commitMasterChanges( $fname = __METHOD__, $owner = null ) {
                $this->assertOwnership( $fname, $owner );
                $this->assertTransactionRoundStage( self::ROUND_APPROVED );
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                $failures = [];
 
-               /** @noinspection PhpUnusedLocalVariableInspection */
-               $scope = ScopedCallback::newScopedIgnoreUserAbort(); // try to ignore client aborts
-
                $restore = ( $this->trxRoundId !== false );
                $this->trxRoundId = false;
                $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
@@ -1686,6 +1696,10 @@ class LoadBalancer implements ILoadBalancer {
                                "Transaction should be in the callback stage (not '{$this->trxRoundStage}')"
                        );
                }
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                $oldStage = $this->trxRoundStage;
                $this->trxRoundStage = self::ROUND_ERROR; // "failed" until proven otherwise
@@ -1756,6 +1770,10 @@ class LoadBalancer implements ILoadBalancer {
                                "Transaction should be in the callback stage (not '{$this->trxRoundStage}')"
                        );
                }
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                $e = null;
 
@@ -1774,6 +1792,10 @@ class LoadBalancer implements ILoadBalancer {
 
        public function rollbackMasterChanges( $fname = __METHOD__, $owner = null ) {
                $this->assertOwnership( $fname, $owner );
+               if ( $this->ownerId === null ) {
+                       /** @noinspection PhpUnusedLocalVariableInspection */
+                       $scope = ScopedCallback::newScopedIgnoreUserAbort();
+               }
 
                $restore = ( $this->trxRoundId !== false );
                $this->trxRoundId = false;
@@ -2283,9 +2305,9 @@ class LoadBalancer implements ILoadBalancer {
                ) );
 
                // Update the prefix for all local connections...
-               $this->forEachOpenConnection( function ( IDatabase $db ) use ( $prefix ) {
-                       if ( !$db->getLBInfo( 'foreign' ) ) {
-                               $db->tablePrefix( $prefix );
+               $this->forEachOpenConnection( function ( IDatabase $conn ) use ( $prefix ) {
+                       if ( !$conn->getLBInfo( 'foreign' ) ) {
+                               $conn->tablePrefix( $prefix );
                        }
                } );
        }