Merge "Made SqlBagOStuff set() wrap setMulti()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 2 Jul 2015 08:14:36 +0000 (08:14 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 2 Jul 2015 08:14:36 +0000 (08:14 +0000)
1  2 
includes/objectcache/SqlBagOStuff.php

@@@ -30,7 -30,6 +30,7 @@@ class SqlBagOStuff extends BagOStuff 
        /** @var LoadBalancer */
        protected $lb;
  
 +      /** @var array */
        protected $serverInfos;
  
        /** @var array */
@@@ -54,9 -53,6 +54,9 @@@
        /** @var string */
        protected $tableName = 'objectcache';
  
 +      /** @var bool */
 +      protected $slaveOnly = false;
 +
        /** @var array UNIX timestamps */
        protected $connFailureTimes = array();
  
         *                  required to hold the largest shard index. Data will be
         *                  distributed across all tables by key hash. This is for
         *                  MySQL bugs 61735 and 61736.
 +       *   - slaveOnly:   Whether to only use slave 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.
         *
         * @param array $params
         */
                if ( isset( $params['shards'] ) ) {
                        $this->shards = intval( $params['shards'] );
                }
 +              $this->slaveOnly = !empty( $params['slaveOnly'] );
        }
  
        /**
                                 * However, SQLite has an opposite behavior. And PostgreSQL needs to know
                                 * if we are in transaction or no
                                 */
 -                              if ( wfGetDB( DB_MASTER )->getType() == 'mysql' ) {
 +                              $index = $this->slaveOnly ? DB_SLAVE : DB_MASTER;
 +                              if ( wfGetDB( $index )->getType() == 'mysql' ) {
                                        $this->lb = wfGetLBFactory()->newMainLB();
 -                                      $db = $this->lb->getConnection( DB_MASTER );
 +                                      $db = $this->lb->getConnection( $index );
                                        $db->clearFlag( DBO_TRX ); // auto-commit mode
                                } else {
 -                                      $db = wfGetDB( DB_MASTER );
 +                                      $db = wfGetDB( $index );
                                }
                        }
                        if ( $wgDebugDBTransactions ) {
                                try {
                                        $db = $this->getDB( $row->serverIndex );
                                        if ( $this->isExpired( $db, $row->exptime ) ) { // MISS
 -                                              $this->debug( "get: key has expired, deleting" );
 -                                              # Put the expiry time in the WHERE condition to avoid deleting a
 -                                              # newly-inserted value
 -                                              $db->delete( $row->tableName,
 -                                                      array( 'keyname' => $key, 'exptime' => $row->exptime ),
 -                                                      __METHOD__ );
 +                                              $this->debug( "get: key has expired" );
                                        } else { // HIT
                                                $values[$key] = $this->unserialize( $db->decodeBlob( $row->value ) );
                                        }
         * @return bool
         */
        public function set( $key, $value, $exptime = 0 ) {
-               list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
-               try {
-                       $db = $this->getDB( $serverIndex );
-                       $exptime = intval( $exptime );
-                       if ( $exptime < 0 ) {
-                               $exptime = 0;
-                       }
-                       if ( $exptime == 0 ) {
-                               $encExpiry = $this->getMaxDateTime( $db );
-                       } else {
-                               $exptime = $this->convertExpiry( $exptime );
-                               $encExpiry = $db->timestamp( $exptime );
-                       }
-                       // (bug 24425) use a replace if the db supports it instead of
-                       // delete/insert to avoid clashes with conflicting keynames
-                       $db->replace(
-                               $tableName,
-                               array( 'keyname' ),
-                               array(
-                                       'keyname' => $key,
-                                       'value' => $db->encodeBlob( $this->serialize( $value ) ),
-                                       'exptime' => $encExpiry
-                               ), __METHOD__ );
-               } catch ( DBError $e ) {
-                       $this->handleWriteError( $e, $serverIndex );
-                       return false;
-               }
-               return true;
+               return $this->setMulti( array( $key => $value ), $exptime );
        }
  
        /**
        }
  
        protected function garbageCollect() {
 -              if ( !$this->purgePeriod ) {
 +              if ( !$this->purgePeriod || $this->slaveOnly ) {
                        // Disabled
                        return;
                }
         * @param int $serverIndex
         */
        protected function markServerDown( $exception, $serverIndex ) {
 +              unset( $this->conns[$serverIndex] ); // bug T103435
 +
                if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
                        if ( time() - $this->connFailureTimes[$serverIndex] >= 60 ) {
                                unset( $this->connFailureTimes[$serverIndex] );