/**
	 * Get a connection to the server that handles all sub-queues for this queue
	 *
	 * @return Array (server name, Redis instance)
	 * @throws MWException
	 */
	protected function getConnection() {
		$conn = $this->redisPool->getConnection( $this->server );
		if ( !$conn ) {
			throw new JobQueueConnectionError( "Unable to connect to redis server." );
		}
		return $conn;
	}
 /**
  * $param string $to (master/slave)
  * @return RedisConnRef|bool Returns false on failure
  */
 protected function getConnection($to)
 {
     if ($to === 'master') {
         $conn = $this->redisPool->getConnection($this->servers[0]);
     } else {
         static $lastServer = null;
         $conn = false;
         if ($lastServer) {
             $conn = $this->redisPool->getConnection($lastServer);
             if ($conn) {
                 return $conn;
                 // reuse connection
             }
         }
         $servers = $this->servers;
         $attempts = min(3, count($servers));
         for ($i = 1; $i <= $attempts; ++$i) {
             $index = mt_rand(0, count($servers) - 1);
             $conn = $this->redisPool->getConnection($servers[$index]);
             if ($conn) {
                 $lastServer = $servers[$index];
                 return $conn;
             }
             unset($servers[$index]);
             // skip next time
         }
     }
     return $conn;
 }
Exemple #3
0
 /**
  * Get a Redis object with a connection suitable for fetching the specified key
  * @param string $key
  * @return array (server, RedisConnRef) or (false, false)
  */
 protected function getConnection($key)
 {
     $candidates = array_keys($this->serverTagMap);
     if (count($this->servers) > 1) {
         ArrayUtils::consistentHashSort($candidates, $key, '/');
         if (!$this->automaticFailover) {
             $candidates = array_slice($candidates, 0, 1);
         }
     }
     foreach ($candidates as $tag) {
         $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).
                 continue;
             }
         } catch (RedisException $e) {
             // Server is not accepting commands
             $this->handleException($conn, $e);
             continue;
         }
         return array($server, $conn);
     }
     $this->setLastError(BagOStuff::ERR_UNREACHABLE);
     return array(false, false);
 }
 /**
  * Get a connection to the server that handles all sub-queues for this queue
  *
  * @return RedisConnRef|bool Returns false on failure
  * @throws MWException
  */
 protected function getConnection()
 {
     $conn = false;
     foreach ($this->servers as $server) {
         $conn = $this->redisPool->getConnection($server);
         if ($conn) {
             break;
         }
     }
     return $conn;
 }
	/**
	 * Get a Redis object with a connection suitable for fetching the specified key
	 * @return Array (server, RedisConnRef) or (false, false)
	 */
	protected function getConnection( $key ) {
		if ( count( $this->servers ) === 1 ) {
			$candidates = $this->servers;
		} else {
			$candidates = $this->servers;
			ArrayUtils::consistentHashSort( $candidates, $key, '/' );
			if ( !$this->automaticFailover ) {
				$candidates = array_slice( $candidates, 0, 1 );
			}
		}

		foreach ( $candidates as $server ) {
			$conn = $this->redisPool->getConnection( $server );
			if ( $conn ) {
				return array( $server, $conn );
			}
		}
		return array( false, false );
	}
 /**
  * @return Status Uses RediConnRef as value on success
  */
 protected function getConnection()
 {
     if (!isset($this->conn)) {
         $conn = false;
         $servers = $this->ring->getLocations($this->key, 3);
         ArrayUtils::consistentHashSort($servers, $this->key);
         foreach ($servers as $server) {
             $conn = $this->pool->getConnection($this->serversByLabel[$server], $this->logger);
             if ($conn) {
                 break;
             }
         }
         if (!$conn) {
             return Status::newFatal('pool-servererror', implode(', ', $servers));
         }
         $this->conn = $conn;
     }
     return Status::newGood($this->conn);
 }
 /**
  * Get a Redis object with a connection suitable for fetching the specified key
  * @param string $key
  * @return array (server, RedisConnRef) or (false, false)
  */
 protected function getConnection($key)
 {
     $candidates = array_keys($this->serverTagMap);
     if (count($this->servers) > 1) {
         ArrayUtils::consistentHashSort($candidates, $key, '/');
         if (!$this->automaticFailover) {
             $candidates = array_slice($candidates, 0, 1);
         }
     }
     foreach ($candidates as $tag) {
         $server = $this->serverTagMap[$tag];
         $conn = $this->redisPool->getConnection($server);
         if ($conn) {
             return array($server, $conn);
         }
     }
     $this->setLastError(BagOStuff::ERR_UNREACHABLE);
     return array(false, false);
 }
 /**
  * Get a Redis object with a connection suitable for fetching the specified key
  * @param string $key
  * @return array (server, RedisConnRef) or (false, false)
  */
 protected function getConnection($key)
 {
     $candidates = array_keys($this->serverTagMap);
     if (count($this->servers) > 1) {
         ArrayUtils::consistentHashSort($candidates, $key, '/');
         if (!$this->automaticFailover) {
             $candidates = array_slice($candidates, 0, 1);
         }
     }
     while (($tag = array_shift($candidates)) !== null) {
         $server = $this->serverTagMap[$tag];
         $conn = $this->redisPool->getConnection($server);
         if (!$conn) {
             continue;
         }
         // 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;
             }
         }
         return array($server, $conn);
     }
     $this->setLastError(BagOStuff::ERR_UNREACHABLE);
     return array(false, false);
 }
 /**
  * Get a connection to the server that handles all sub-queues for this queue
  *
  * @return Array (server name, Redis instance)
  * @throws MWException
  */
 protected function getConnection()
 {
     return $this->redisPool->getConnection($this->server);
 }
 protected function isServerUp($lockSrv)
 {
     return (bool) $this->redisPool->getConnection($this->lockServers[$lockSrv]);
 }
 protected function isServerUp($lockSrv)
 {
     $conn = $this->redisPool->getConnection($this->lockServers[$lockSrv], $this->logger);
     return (bool) $conn;
 }