Tim Starling has submitted this change and it was merged.

Change subject: Factored JobQueueRedis::redisEval() into 
RedisConnRef::luaEval().
......................................................................


Factored JobQueueRedis::redisEval() into RedisConnRef::luaEval().

Change-Id: I1b4bb4f4bb5e25fc3f358c9e2b16e4672584c68b
---
M includes/clientpool/RedisConnectionPool.php
M includes/job/JobQueueRedis.php
2 files changed, 38 insertions(+), 33 deletions(-)

Approvals:
  Tim Starling: Verified; Looks good to me, approved



diff --git a/includes/clientpool/RedisConnectionPool.php 
b/includes/clientpool/RedisConnectionPool.php
index 7a29154..65fe58f 100644
--- a/includes/clientpool/RedisConnectionPool.php
+++ b/includes/clientpool/RedisConnectionPool.php
@@ -302,6 +302,38 @@
                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;
        }
diff --git a/includes/job/JobQueueRedis.php b/includes/job/JobQueueRedis.php
index f17c295..891d48f 100644
--- a/includes/job/JobQueueRedis.php
+++ b/includes/job/JobQueueRedis.php
@@ -264,7 +264,7 @@
                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 @@
                -- 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 @@
                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 @@
                                -- 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 @@
                        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 @@
                        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]
@@ -677,33 +677,6 @@
                        );
                }
                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;
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/59800
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I1b4bb4f4bb5e25fc3f358c9e2b16e4672584c68b
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Tim Starling <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to