Factored JobQueueRedis::redisEval() into RedisConnRef::luaEval().
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 18 Apr 2013 05:20:50 +0000 (22:20 -0700)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 19 Apr 2013 01:55:45 +0000 (01:55 +0000)
Change-Id: I1b4bb4f4bb5e25fc3f358c9e2b16e4672584c68b

includes/clientpool/RedisConnectionPool.php
includes/job/JobQueueRedis.php

index 7a29154..65fe58f 100644 (file)
@@ -302,6 +302,38 @@ class RedisConnRef {
                return call_user_func_array( array( $this->conn, $name ), $arguments );
        }
 
+       /**
+        * @param string $script
+        * @param array $params
+        * @param integer $numKeys
+        * @return mixed
+        * @throws RedisException
+        */
+       public function luaEval( $script, array $params, $numKeys ) {
+               $sha1 = sha1( $script ); // 40 char hex
+               $conn = $this->conn; // convenience
+
+               // Try to run the server-side cached copy of the script
+               $conn->clearLastError();
+               $res = $conn->evalSha( $sha1, $params, $numKeys );
+               // If the script is not in cache, use eval() to retry and cache it
+               if ( preg_match( '/^NOSCRIPT/', $conn->getLastError() ) ) {
+                       $conn->clearLastError();
+                       $res = $conn->eval( $script, $params, $numKeys );
+                       wfDebugLog( 'redis', "Used eval() for Lua script $sha1." );
+               }
+
+               if ( $conn->getLastError() ) { // script bug?
+                       wfDebugLog( 'redis', "Lua script error: " . $conn->getLastError() );
+               }
+
+               return $res;
+       }
+
+       /**
+        * @param RedisConnRef $conn
+        * @return bool
+        */
        public function isConnIdentical( Redis $conn ) {
                return $this->conn === $conn;
        }
index f17c295..891d48f 100644 (file)
@@ -264,7 +264,7 @@ class JobQueueRedis extends JobQueue {
                end
                return pushed
 LUA;
-               return $this->redisEval( $conn, $script,
+               return $conn->luaEval( $script,
                        array_merge(
                                array(
                                        $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
@@ -347,7 +347,7 @@ LUA;
                -- Return the job data
                return item
 LUA;
-               return $this->redisEval( $conn, $script,
+               return $conn->luaEval( $script,
                        array(
                                $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
                                $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
@@ -378,7 +378,7 @@ LUA;
                redis.call('hIncrBy',KEYS[5],id,1)
                return redis.call('hGet',KEYS[6],id)
 LUA;
-               return $this->redisEval( $conn, $script,
+               return $conn->luaEval( $script,
                        array(
                                $this->getQueueKey( 'l-unclaimed' ), # KEYS[1]
                                $this->getQueueKey( 'h-sha1ById' ), # KEYS[2]
@@ -414,7 +414,7 @@ LUA;
                                -- Delete the job data itself
                                return redis.call('hDel',KEYS[3],ARGV[1])
 LUA;
-                               $res = $this->redisEval( $conn, $script,
+                               $res = $conn->luaEval( $script,
                                        array(
                                                $this->getQueueKey( 'z-claimed' ), # KEYS[1]
                                                $this->getQueueKey( 'h-attempts' ), # KEYS[2]
@@ -568,7 +568,7 @@ LUA;
                        end
                        return #ids
 LUA;
-                       $count += (int)$this->redisEval( $conn, $script,
+                       $count += (int)$conn->luaEval( $script,
                                array(
                                        $this->getQueueKey( 'z-delayed' ), // KEYS[1]
                                        $this->getQueueKey( 'l-unclaimed' ), // KEYS[2]
@@ -634,7 +634,7 @@ LUA;
                        end
                        return {released,abandoned,pruned}
 LUA;
-                       $res = $this->redisEval( $conn, $script,
+                       $res = $conn->luaEval( $script,
                                array(
                                        $this->getQueueKey( 'z-claimed' ), # KEYS[1]
                                        $this->getQueueKey( 'h-attempts' ), # KEYS[2]
@@ -679,33 +679,6 @@ LUA;
                return $tasks;
        }
 
-       /**
-        * @param RedisConnRef $conn
-        * @param string $script
-        * @param array $params
-        * @param integer $numKeys
-        * @return mixed
-        */
-       protected function redisEval( RedisConnRef $conn, $script, array $params, $numKeys ) {
-               $sha1 = sha1( $script ); // 40 char hex
-
-               // Try to run the server-side cached copy of the script
-               $conn->clearLastError();
-               $res = $conn->evalSha( $sha1, $params, $numKeys );
-               // If the script is not in cache, use eval() to retry and cache it
-               if ( $conn->getLastError() && $conn->script( 'exists', $sha1 ) === array( 0 ) ) {
-                       $conn->clearLastError();
-                       $res = $conn->eval( $script, $params, $numKeys );
-                       wfDebugLog( 'JobQueueRedis', "Used eval() for Lua script $sha1." );
-               }
-
-               if ( $conn->getLastError() ) { // script bug?
-                       wfDebugLog( 'JobQueueRedis', "Lua script error: " . $conn->getLastError() );
-               }
-
-               return $res;
-       }
-
        /**
         * @param $job Job
         * @return array