Merge "Exclude redirects from Special:Fewestrevisions"
[lhc/web/wiklou.git] / includes / objectcache / SqlBagOStuff.php
index 8bc053d..9226875 100644 (file)
@@ -36,7 +36,7 @@ use Wikimedia\WaitConditionLoop;
  *
  * @ingroup Cache
  */
-class SqlBagOStuff extends BagOStuff {
+class SqlBagOStuff extends MediumSpecificBagOStuff {
        /** @var array[] (server index => server config) */
        protected $serverInfos;
        /** @var string[] (server index => tag/host name) */
@@ -55,8 +55,6 @@ class SqlBagOStuff extends BagOStuff {
        protected $tableName = 'objectcache';
        /** @var bool */
        protected $replicaOnly = false;
-       /** @var int */
-       protected $syncTimeout = 3;
 
        /** @var LoadBalancer|null */
        protected $separateMainLB;
@@ -110,7 +108,7 @@ class SqlBagOStuff extends BagOStuff {
         *                  MySQL bugs 61735 <https://bugs.mysql.com/bug.php?id=61735>
         *                  and 61736 <https://bugs.mysql.com/bug.php?id=61736>.
         *
-        *   - slaveOnly:   Whether to only use replica DBs and avoid triggering
+        *   - replicaOnly: Whether to only use replica DBs and avoid triggering
         *                  garbage collection logic of expired items. This only
         *                  makes sense if the primary DB is used and only if get()
         *                  calls will be used. This is used by ReplicatedBagOStuff.
@@ -159,10 +157,8 @@ class SqlBagOStuff extends BagOStuff {
                if ( isset( $params['shards'] ) ) {
                        $this->shards = intval( $params['shards'] );
                }
-               if ( isset( $params['syncTimeout'] ) ) {
-                       $this->syncTimeout = $params['syncTimeout'];
-               }
-               $this->replicaOnly = !empty( $params['slaveOnly'] );
+               // Backwards-compatibility for < 1.34
+               $this->replicaOnly = $params['replicaOnly'] ?? ( $params['slaveOnly'] ?? false );
        }
 
        /**
@@ -173,19 +169,20 @@ class SqlBagOStuff extends BagOStuff {
         * @throws MWException
         */
        protected function getDB( $serverIndex ) {
-               if ( !isset( $this->conns[$serverIndex] ) ) {
-                       if ( $serverIndex >= $this->numServers ) {
-                               throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
-                       }
+               if ( $serverIndex >= $this->numServers ) {
+                       throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
+               }
 
-                       # Don't keep timing out trying to connect for each call if the DB is down
-                       if ( isset( $this->connFailureErrors[$serverIndex] )
-                               && ( time() - $this->connFailureTimes[$serverIndex] ) < 60
-                       ) {
-                               throw $this->connFailureErrors[$serverIndex];
-                       }
+               # Don't keep timing out trying to connect for each call if the DB is down
+               if (
+                       isset( $this->connFailureErrors[$serverIndex] ) &&
+                       ( time() - $this->connFailureTimes[$serverIndex] ) < 60
+               ) {
+                       throw $this->connFailureErrors[$serverIndex];
+               }
 
-                       if ( $this->serverInfos ) {
+               if ( $this->serverInfos ) {
+                       if ( !isset( $this->conns[$serverIndex] ) ) {
                                // Use custom database defined by server connection info
                                $info = $this->serverInfos[$serverIndex];
                                $type = $info['type'] ?? 'mysql';
@@ -193,25 +190,26 @@ class SqlBagOStuff extends BagOStuff {
                                $this->logger->debug( __CLASS__ . ": connecting to $host" );
                                $db = Database::factory( $type, $info );
                                $db->clearFlag( DBO_TRX ); // auto-commit mode
+                               $this->conns[$serverIndex] = $db;
+                       }
+                       $db = $this->conns[$serverIndex];
+               } else {
+                       // Use the main LB database
+                       $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+                       $index = $this->replicaOnly ? DB_REPLICA : DB_MASTER;
+                       if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) {
+                               // Keep a separate connection to avoid contention and deadlocks
+                               $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT );
                        } else {
-                               // Use the main LB database
-                               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
-                               $index = $this->replicaOnly ? DB_REPLICA : DB_MASTER;
-                               if ( $lb->getServerType( $lb->getWriterIndex() ) !== 'sqlite' ) {
-                                       // Keep a separate connection to avoid contention and deadlocks
-                                       $db = $lb->getConnection( $index, [], false, $lb::CONN_TRX_AUTOCOMMIT );
-                               } else {
-                                       // However, SQLite has the opposite behavior due to DB-level locking.
-                                       // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead.
-                                       $db = $lb->getConnection( $index );
-                               }
+                               // However, SQLite has the opposite behavior due to DB-level locking.
+                               // Stock sqlite MediaWiki installs use a separate sqlite cache DB instead.
+                               $db = $lb->getConnection( $index );
                        }
-
-                       $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
-                       $this->conns[$serverIndex] = $db;
                }
 
-               return $this->conns[$serverIndex];
+               $this->logger->debug( sprintf( "Connection %s will be used for SqlBagOStuff", $db ) );
+
+               return $db;
        }
 
        /**
@@ -339,7 +337,7 @@ class SqlBagOStuff extends BagOStuff {
                return $values;
        }
 
-       public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
+       protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
                return $this->modifyMulti( $data, $exptime, $flags, self::$OP_SET );
        }
 
@@ -508,7 +506,7 @@ class SqlBagOStuff extends BagOStuff {
                return (bool)$db->affectedRows();
        }
 
-       public function doDeleteMulti( array $keys, $flags = 0 ) {
+       protected function doDeleteMulti( array $keys, $flags = 0 ) {
                return $this->modifyMulti(
                        array_fill_keys( $keys, null ),
                        0,
@@ -564,7 +562,7 @@ class SqlBagOStuff extends BagOStuff {
                return $ok;
        }
 
-       public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
+       protected function doChangeTTLMulti( array $keys, $exptime, $flags = 0 ) {
                return $this->modifyMulti(
                        array_fill_keys( $keys, null ),
                        $exptime,
@@ -609,8 +607,6 @@ class SqlBagOStuff extends BagOStuff {
                if (
                        // Random purging is enabled
                        $this->purgePeriod &&
-                       // This is not using a replica DB
-                       !$this->replicaOnly &&
                        // Only purge on one in every $this->purgePeriod writes
                        mt_rand( 0, $this->purgePeriod - 1 ) == 0 &&
                        // Avoid repeating the delete within a few seconds
@@ -635,7 +631,7 @@ class SqlBagOStuff extends BagOStuff {
 
        public function deleteObjectsExpiringBefore(
                $timestamp,
-               callable $progressCallback = null,
+               callable $progress = null,
                $limit = INF
        ) {
                /** @noinspection PhpUnusedLocalVariableInspection */
@@ -654,7 +650,7 @@ class SqlBagOStuff extends BagOStuff {
                                $this->deleteServerObjectsExpiringBefore(
                                        $db,
                                        $timestamp,
-                                       $progressCallback,
+                                       $progress,
                                        $limit,
                                        $numServersDone,
                                        $keysDeletedCount