Don't fallback from uk to ru
[lhc/web/wiklou.git] / includes / objectcache / RedisBagOStuff.php
index b8a0dd5..c3e0c96 100644 (file)
  * @file
  */
 
+/**
+ * Redis-based caching module for redis server >= 2.6.12
+ *
+ * @note: avoid use of Redis::MULTI transactions for twemproxy support
+ */
 class RedisBagOStuff extends BagOStuff {
        /** @var RedisConnectionPool */
        protected $redisPool;
@@ -60,8 +65,8 @@ class RedisBagOStuff extends BagOStuff {
         */
        function __construct( $params ) {
                parent::__construct( $params );
-               $redisConf = array( 'serializer' => 'none' ); // manage that in this class
-               foreach ( array( 'connectTimeout', 'persistent', 'password' ) as $opt ) {
+               $redisConf = [ 'serializer' => 'none' ]; // manage that in this class
+               foreach ( [ 'connectTimeout', 'persistent', 'password' ] as $opt ) {
                        if ( isset( $params[$opt] ) ) {
                                $redisConf[$opt] = $params[$opt];
                        }
@@ -80,15 +85,13 @@ class RedisBagOStuff extends BagOStuff {
                }
        }
 
-       public function get( $key, &$casToken = null ) {
-
+       protected function doGet( $key, $flags = 0 ) {
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
                try {
                        $value = $conn->get( $key );
-                       $casToken = $value;
                        $result = $this->unserialize( $value );
                } catch ( RedisException $e ) {
                        $result = false;
@@ -99,8 +102,7 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       public function set( $key, $value, $expiry = 0 ) {
-
+       public function set( $key, $value, $expiry = 0, $flags = 0 ) {
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
@@ -122,41 +124,7 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       protected function cas( $casToken, $key, $value, $expiry = 0 ) {
-
-               list( $server, $conn ) = $this->getConnection( $key );
-               if ( !$conn ) {
-                       return false;
-               }
-               $expiry = $this->convertToRelative( $expiry );
-               try {
-                       $conn->watch( $key );
-
-                       if ( $this->serialize( $this->get( $key ) ) !== $casToken ) {
-                               $conn->unwatch();
-                               return false;
-                       }
-
-                       // multi()/exec() will fail atomically if the key changed since watch()
-                       $conn->multi();
-                       if ( $expiry ) {
-                               $conn->setex( $key, $expiry, $this->serialize( $value ) );
-                       } else {
-                               // No expiry, that is very different from zero expiry in Redis
-                               $conn->set( $key, $this->serialize( $value ) );
-                       }
-                       $result = ( $conn->exec() == array( true ) );
-               } catch ( RedisException $e ) {
-                       $result = false;
-                       $this->handleException( $conn, $e );
-               }
-
-               $this->logRequest( 'cas', $key, $server, $result );
-               return $result;
-       }
-
        public function delete( $key ) {
-
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
@@ -174,10 +142,9 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       public function getMulti( array $keys ) {
-
-               $batches = array();
-               $conns = array();
+       public function getMulti( array $keys, $flags = 0 ) {
+               $batches = [];
+               $conns = [];
                foreach ( $keys as $key ) {
                        list( $server, $conn ) = $this->getConnection( $key );
                        if ( !$conn ) {
@@ -186,7 +153,7 @@ class RedisBagOStuff extends BagOStuff {
                        $conns[$server] = $conn;
                        $batches[$server][] = $key;
                }
-               $result = array();
+               $result = [];
                foreach ( $batches as $server => $batchKeys ) {
                        $conn = $conns[$server];
                        try {
@@ -220,9 +187,8 @@ class RedisBagOStuff extends BagOStuff {
         * @return bool
         */
        public function setMulti( array $data, $expiry = 0 ) {
-
-               $batches = array();
-               $conns = array();
+               $batches = [];
+               $conns = [];
                foreach ( $data as $key => $value ) {
                        list( $server, $conn ) = $this->getConnection( $key );
                        if ( !$conn ) {
@@ -265,7 +231,6 @@ class RedisBagOStuff extends BagOStuff {
        }
 
        public function add( $key, $value, $expiry = 0 ) {
-
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
@@ -273,11 +238,11 @@ class RedisBagOStuff extends BagOStuff {
                $expiry = $this->convertToRelative( $expiry );
                try {
                        if ( $expiry ) {
-                               $conn->multi();
-                               $conn->setnx( $key, $this->serialize( $value ) );
-                               // @FIXME: this always bumps the TTL; use Redis 2.8 or Lua
-                               $conn->expire( $key, $expiry );
-                               $result = ( $conn->exec() == array( true, true ) );
+                               $result = $conn->set(
+                                       $key,
+                                       $this->serialize( $value ),
+                                       [ 'nx', 'ex' => $expiry ]
+                               );
                        } else {
                                $result = $conn->setnx( $key, $this->serialize( $value ) );
                        }
@@ -303,15 +268,14 @@ class RedisBagOStuff extends BagOStuff {
         * @return int|bool New value or false on failure
         */
        public function incr( $key, $value = 1 ) {
-
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
-               if ( !$conn->exists( $key ) ) {
-                       return null;
-               }
                try {
+                       if ( !$conn->exists( $key ) ) {
+                               return null;
+                       }
                        // @FIXME: on races, the key may have a 0 TTL
                        $result = $conn->incrBy( $key, $value );
                } catch ( RedisException $e ) {
@@ -323,12 +287,22 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
-               if ( !is_callable( $callback ) ) {
-                       throw new Exception( "Got invalid callback." );
+       public function changeTTL( $key, $expiry = 0 ) {
+               list( $server, $conn ) = $this->getConnection( $key );
+               if ( !$conn ) {
+                       return false;
+               }
+
+               $expiry = $this->convertToRelative( $expiry );
+               try {
+                       $result = $conn->expire( $key, $expiry );
+               } catch ( RedisException $e ) {
+                       $result = false;
+                       $this->handleException( $conn, $e );
                }
 
-               return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+               $this->logRequest( 'expire', $key, $server, $result );
+               return $result;
        }
 
        public function modifySimpleRelayEvent( array $event ) {
@@ -354,7 +328,8 @@ class RedisBagOStuff extends BagOStuff {
         * @return mixed
         */
        protected function unserialize( $data ) {
-               return ctype_digit( $data ) ? intval( $data ) : unserialize( $data );
+               $int = intval( $data );
+               return $data === (string)$int ? $int : unserialize( $data );
        }
 
        /**
@@ -372,39 +347,53 @@ class RedisBagOStuff extends BagOStuff {
                        }
                }
 
-               foreach ( $candidates as $tag ) {
+               while ( ( $tag = array_shift( $candidates ) ) !== null ) {
                        $server = $this->serverTagMap[$tag];
-
                        $conn = $this->redisPool->getConnection( $server );
                        if ( !$conn ) {
                                continue;
                        }
 
-                       try {
-                               $info = $conn->info();
-                               // Check if this server has an unreachable redis master
-                               if ( $info['role'] === 'slave'
-                                       && $info['master_link_status'] === 'down'
-                                       && $this->automaticFailover
-                               ) {
-                                       // If the master cannot be reached, fail-over to the next server.
-                                       // If masters are in data-center A, and slaves in data-center B,
-                                       // this helps avoid the case were fail-over happens in A but not
-                                       // to the corresponding server in B (e.g. read/write mismatch).
+                       // If automatic failover is enabled, check that the server's link
+                       // to its master (if any) is up -- but only if there are other
+                       // viable candidates left to consider. Also, getMasterLinkStatus()
+                       // does not work with twemproxy, though $candidates will be empty
+                       // by now in such cases.
+                       if ( $this->automaticFailover && $candidates ) {
+                               try {
+                                       if ( $this->getMasterLinkStatus( $conn ) === 'down' ) {
+                                               // If the master cannot be reached, fail-over to the next server.
+                                               // If masters are in data-center A, and slaves in data-center B,
+                                               // this helps avoid the case were fail-over happens in A but not
+                                               // to the corresponding server in B (e.g. read/write mismatch).
+                                               continue;
+                                       }
+                               } catch ( RedisException $e ) {
+                                       // Server is not accepting commands
+                                       $this->handleException( $conn, $e );
                                        continue;
                                }
-                       } catch ( RedisException $e ) {
-                               // Server is not accepting commands
-                               $this->handleException( $conn, $e );
-                               continue;
                        }
 
-                       return array( $server, $conn );
+                       return [ $server, $conn ];
                }
 
                $this->setLastError( BagOStuff::ERR_UNREACHABLE );
 
-               return array( false, false );
+               return [ false, false ];
+       }
+
+       /**
+        * Check the master link status of a Redis server that is configured as a slave.
+        * @param RedisConnRef $conn
+        * @return string|null Master link status (either 'up' or 'down'), or null
+        *  if the server is not a slave.
+        */
+       protected function getMasterLinkStatus( RedisConnRef $conn ) {
+               $info = $conn->info();
+               return isset( $info['master_link_status'] )
+                       ? $info['master_link_status']
+                       : null;
        }
 
        /**