From: Aaron Schulz Date: Thu, 18 Apr 2013 05:20:50 +0000 (-0700) Subject: Factored JobQueueRedis::redisEval() into RedisConnRef::luaEval(). X-Git-Tag: 1.31.0-rc.0~19947 X-Git-Url: http://git.cyclocoop.org/fichier?a=commitdiff_plain;h=3e49fd4af29325a227f5980cdf1e59f1059a2fa2;p=lhc%2Fweb%2Fwiklou.git Factored JobQueueRedis::redisEval() into RedisConnRef::luaEval(). Change-Id: I1b4bb4f4bb5e25fc3f358c9e2b16e4672584c68b --- diff --git a/includes/clientpool/RedisConnectionPool.php b/includes/clientpool/RedisConnectionPool.php index 7a291540ca..65fe58fc16 100644 --- a/includes/clientpool/RedisConnectionPool.php +++ b/includes/clientpool/RedisConnectionPool.php @@ -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; } diff --git a/includes/job/JobQueueRedis.php b/includes/job/JobQueueRedis.php index f17c2952f8..891d48f346 100644 --- a/includes/job/JobQueueRedis.php +++ b/includes/job/JobQueueRedis.php @@ -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