/** * @group disconnected */ public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault() { $cmdGET = RawCommand::create('GET', 'node:1001'); $rspMOVED = new ResponseError('MOVED 1970 127.0.0.1:6380'); $rspSlotsArray = array(array(0, 8191, array('127.0.0.1', 6379)), array(8192, 16383, array('127.0.0.1', 6380))); $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379'); $connection1->expects($this->once())->method('executeCommand')->with($cmdGET)->will($this->returnValue($rspMOVED)); $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380'); $connection2->expects($this->at(0))->method('executeCommand')->with($this->isRedisCommand('CLUSTER', array('SLOTS')))->will($this->returnValue($rspSlotsArray)); $connection2->expects($this->at(2))->method('executeCommand')->with($cmdGET)->will($this->returnValue('foobar')); $factory = $this->getMock('Predis\\Connection\\ConnectionFactory'); $factory->expects($this->once())->method('create')->with(array('host' => '127.0.0.1', 'port' => '6380'))->will($this->returnValue($connection2)); $cluster = new RedisCluster($factory); $cluster->add($connection1); $this->assertSame('foobar', $cluster->executeCommand($cmdGET)); $this->assertSame(2, count($cluster)); }
/** * Handles -ASK responses by executing again the command against the node * indicated by the Redis response. * * @param CommandInterface $command Command that generated the -ASK response. * @param string $details Parameters of the -ASK response. * * @return mixed */ protected function onAskResponse(CommandInterface $command, $details) { list($slot, $connectionID) = explode(' ', $details, 2); if (!($connection = $this->getConnectionById($connectionID))) { $connection = $this->createConnection($connectionID); } $connection->executeCommand(RawCommand::create('ASKING')); $response = $connection->executeCommand($command); return $response; }
/** * @group disconnected */ public function testFetchSlotsMapFromClusterWithClusterNodesCommand() { $output = <<<EOS 1284a54dc3245d305ae5e3f5507579f9a3e4bd80 10.1.0.51:6387 master - 0 1400000953094 20 connected 12288-13311 1fb644aa255ef92db789a93cc834f597e9a3800b 10.1.0.52:6392 master - 0 1400000955139 15 connected 3072-4095 fe814c66d4171db548b6dce537a88c3994cd7197 10.1.0.51:6391 slave 7ca7ed3780c405ee50757550e19ade5921097d25 0 1400000954111 5 connected b3a3b7564c75c096bdc72fc314500cdd666adf87 :0 myself,master - 0 0 18 connected 6144-7167 67b25b19a07b56a155e25145957dce82a6605f26 10.1.0.51:6388 master - 0 1400000954111 21 connected 14336-15359 ba6e596ae5382ad05f1df5abaaa1489a0e0b2449 10.1.0.52:6398 master - 0 1400000953094 13 connected 15360-16383 7ca7ed3780c405ee50757550e19ade5921097d25 10.1.0.52:6391 master - 0 1400000954111 5 connected 1024-2047 4d90ab772120a49b177eb7bd4799b592f5048e16 10.1.0.51:6394 slave fa9b9fae41e3d53d2a901dbfe6fd1527c874768c 0 1400000955139 26 connected 5731adc383f5f7afbfcca2ee75d17609e436b146 10.1.0.51:6398 slave ba6e596ae5382ad05f1df5abaaa1489a0e0b2449 0 1400000953094 21 connected fd0bd0cd8461bd44b759fefaabaf541788ec65cf 10.1.0.52:6396 master - 0 1400000955139 7 connected 11264-12287 d012ddf044d0ef5d72d171f8af1e700488b79241 10.1.0.52:6393 master - 0 1400000953604 10 connected 5120-6143 462ab20db112a007f3ffc4e7e105b0c963aa8981 10.1.0.52:6382 slave a1948d989b092bc96ff91019d9d3b74361edcdfa 0 1400000954111 18 connected f468a7018ff5a0701a1b614b8ed1b400cddcd578 10.1.0.52:6385 slave 2a1845f27d0f24904d67e2f2cd3e7380949e9d3a 0 1400000953604 25 connected a48e023927edcc0d001be0535130f41d8e93adde 10.1.0.52:6383 slave a547c6de8acfbf4a97a1c341bddc2efffea83032 0 1400000955139 12 connected 2530af9ee9b8d4e359cefedd7492ba777dd1e5aa 10.1.0.51:6392 slave 1fb644aa255ef92db789a93cc834f597e9a3800b 0 1400000953603 17 connected 2d3934ef56abbe25949063474d2e408ae2fb41af 10.1.0.51:6395 slave 0f70284073a6613343fd9a2da82a0549c1efaaae 0 1400000954627 27 connected 6a0430bf8e1beed8911aa69b002ac7a00ad60428 10.1.0.52:6387 slave 1284a54dc3245d305ae5e3f5507579f9a3e4bd80 0 1400000954111 20 connected 578ad5ff1970a22419a6afca0cc97bfb729f6b47 10.1.0.51:6397 slave fd637fc700d31395d1f324348b49ad38248ba8d4 0 1400000955139 18 connected c3083761437050f1cd80a5f95b3ff5aabba3bb97 10.1.0.51:6381 master - 0 1400000955139 11 connected 0-1023 846d3410ce8e7cea565617686f4685a35d376706 10.1.0.52:6384 slave b3a3b7564c75c096bdc72fc314500cdd666adf87 0 1400000953094 18 connected fd637fc700d31395d1f324348b49ad38248ba8d4 10.1.0.52:6397 master - 0 1400000954627 16 connected 13312-14335 a547c6de8acfbf4a97a1c341bddc2efffea83032 10.1.0.51:6383 master - 0 1400000954627 8 connected 4096-5119 0f70284073a6613343fd9a2da82a0549c1efaaae 10.1.0.52:6395 master - 0 1400000954627 27 connected 9216-10239 2a1845f27d0f24904d67e2f2cd3e7380949e9d3a 10.1.0.51:6385 master - 0 1400000953603 25 connected 8192-9215 ba018d6d154205a47b83de33efbbe846cb6f4b1f 10.1.0.52:6381 slave c3083761437050f1cd80a5f95b3ff5aabba3bb97 0 1400000955139 11 connected 5a830c9ef0d09eb25b521395466e803052ddbef4 10.1.0.51:6386 master - 0 1400000954627 9 connected 10240-11263 91d9840437794ab1ff77b59f831ce80ffaaa4c5a 10.1.0.51:6393 slave d012ddf044d0ef5d72d171f8af1e700488b79241 0 1400000954627 14 connected d6bdbb495fcb66ba3001c2a07c067d5aa354cf55 10.1.0.52:6388 slave 67b25b19a07b56a155e25145957dce82a6605f26 0 1400000954111 21 connected 2176380db50d687d37e37c3daeeea6e7f8feed0c 10.1.0.51:6396 slave fd0bd0cd8461bd44b759fefaabaf541788ec65cf 0 1400000953603 22 connected c93ffb4a2ff815f9029156383b00f106234b9546 10.1.0.52:6386 slave 5a830c9ef0d09eb25b521395466e803052ddbef4 0 1400000953604 9 connected a1948d989b092bc96ff91019d9d3b74361edcdfa 10.1.0.51:6382 master - 0 1400000953094 17 connected 2048-3071 fa9b9fae41e3d53d2a901dbfe6fd1527c874768c 10.1.0.52:6394 master - 0 1400000953094 1 connected 7168-8191 EOS; $command = RawCommand::create('CLUSTER', 'NODES'); $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384'); $connection1->expects($this->once())->method('executeCommand')->with($command)->will($this->returnValue($output)); $factory = $this->getMock('Predis\\Connection\\ConnectionFactoryInterface'); $cluster = new RedisCluster($factory); $cluster->add($connection1); $cluster->askClusterNodes(); $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1); }
/** * The signature of RawCommand::create() requires one argument which is the * ID of the command (other arguments are fetched dinamically). If the first * argument is missing, PHP emits an E_WARNING. * * @group disconnected * @expectedException PHPUnit_Framework_Error_Warning */ public function testPHPWarningOnMissingCommandIDWithStaticCreate() { RawCommand::create(); }
/** * 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); }
/** * Start listening to subscribed channels of the Redis PubSub mechanism. * Add a callback to a particular subscription channel. * * @param callable $callback * @return void */ public function listenToPubSub(callable $callback) { while (1) { $command = RawCommand::create('PSUBSCRIBE', sprintf('%s-%s', $this->pubsub_channel_prefix, '*')); $this->client->executeCommand($command); if ($this->client->getConnection() instanceof MasterSlaveReplication) { $payload = $this->client->getConnection()->getConnection($command)->read(); } else { $payload = $this->client->getConnection()->read(); } $channel = ltrim($payload[2], sprintf('%s%s', $this->pubsub_channel_prefix, '-')); $message = base64_decode($payload[3]); call_user_func($callback, ['channel' => $channel, 'message' => $message]); } }