/** * {@inheritdoc} */ public function executeCommand(CommandInterface $command) { if (null === $this->logger) { return $this->connection->executeCommand($command); } $startTime = microtime(true); $result = $this->connection->executeCommand($command); $duration = (microtime(true) - $startTime) * 1000; $error = $result instanceof Error ? (string) $result : false; $this->logger->logCommand($this->commandToString($command), $duration, $this->getParameters()->alias, $error); return $result; }
/** * Queries the specified node of the cluster to fetch the updated slots map. * * When the connection fails, this method tries to execute the same command * on a different connection picked at random from the pool of known nodes, * up until the retry limit is reached. * * @param NodeConnectionInterface $connection Connection to a node of the cluster. * * @return mixed */ private function queryClusterNodeForSlotMap(NodeConnectionInterface $connection) { $retries = 0; $command = RawCommand::create('CLUSTER', 'SLOTS'); RETRY_COMMAND: try { $response = $connection->executeCommand($command); } catch (ConnectionException $exception) { $connection = $exception->getConnection(); $connection->disconnect(); $this->remove($connection); if ($retries === $this->retryLimit) { throw $exception; } if (!($connection = $this->getRandomConnection())) { throw new ClientException('No connections left in the pool for `CLUSTER SLOTS`'); } ++$retries; goto RETRY_COMMAND; } return $response; }
/** * Asserts that the specified connection matches an expected role. * * @param NodeConnectionInterface $sentinel Connection to a redis server. * @param string $role Expected role of the server ("master", "slave" or "sentinel"). */ protected function assertConnectionRole(NodeConnectionInterface $connection, $role) { $role = strtolower($role); $actualRole = $connection->executeCommand(RawCommand::create('ROLE')); if ($role !== $actualRole[0]) { throw new RoleException($connection, "Expected {$role} but got {$actualRole['0']} [{$connection}]"); } }
/** * Discovers the replication configuration by contacting one of the slaves. * * @param NodeConnectionInterface $connection Connection to one of the slaves. * @param FactoryInterface $connectionFactory Connection factory instance. */ protected function discoverFromSlave(NodeConnectionInterface $connection, FactoryInterface $connectionFactory) { $response = $connection->executeCommand(RawCommand::create('INFO', 'REPLICATION')); $replication = $this->handleInfoResponse($response); if ($replication['role'] !== 'slave') { throw new ClientException("Role mismatch (expected slave, got master) [{$connection}]"); } $masterConnection = $connectionFactory->create(array('host' => $replication['master_host'], 'port' => $replication['master_port'], 'role' => 'master')); $this->add($masterConnection); $this->discoverFromMaster($masterConnection, $connectionFactory); }