RedisClusterTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection\Aggregate;
  11. use Predis\Command;
  12. use Predis\Connection;
  13. use Predis\Profile;
  14. use Predis\Response;
  15. use PredisTestCase;
  16. /**
  17. *
  18. */
  19. class RedisClusterTest extends PredisTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. */
  24. public function testAcceptsCustomConnectionFactory()
  25. {
  26. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  27. $cluster = new RedisCluster($factory);
  28. $this->assertSame($factory, $cluster->getConnectionFactory());
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testUsesRedisClusterStrategyByDefault()
  34. {
  35. $cluster = new RedisCluster(new Connection\Factory());
  36. $this->assertInstanceOf('Predis\Cluster\RedisStrategy', $cluster->getClusterStrategy());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testAcceptsCustomClusterStrategy()
  42. {
  43. $strategy = $this->getMock('Predis\Cluster\StrategyInterface');
  44. $cluster = new RedisCluster(new Connection\Factory(), $strategy);
  45. $this->assertSame($strategy, $cluster->getClusterStrategy());
  46. }
  47. /**
  48. * @group disconnected
  49. */
  50. public function testAddingConnectionsToCluster()
  51. {
  52. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  53. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  54. $cluster = new RedisCluster(new Connection\Factory());
  55. $cluster->add($connection1);
  56. $cluster->add($connection2);
  57. $this->assertSame(2, count($cluster));
  58. $this->assertSame($connection1, $cluster->getConnectionById('127.0.0.1:6379'));
  59. $this->assertSame($connection2, $cluster->getConnectionById('127.0.0.1:6380'));
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testRemovingConnectionsFromCluster()
  65. {
  66. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  67. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  68. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6371');
  69. $cluster = new RedisCluster(new Connection\Factory());
  70. $cluster->add($connection1);
  71. $cluster->add($connection2);
  72. $this->assertTrue($cluster->remove($connection1));
  73. $this->assertFalse($cluster->remove($connection3));
  74. $this->assertSame(1, count($cluster));
  75. }
  76. /**
  77. * @group disconnected
  78. */
  79. public function testRemovingConnectionsFromClusterByAlias()
  80. {
  81. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  82. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  83. $cluster = new RedisCluster(new Connection\Factory());
  84. $cluster->add($connection1);
  85. $cluster->add($connection2);
  86. $this->assertTrue($cluster->removeById('127.0.0.1:6380'));
  87. $this->assertFalse($cluster->removeById('127.0.0.1:6390'));
  88. $this->assertSame(1, count($cluster));
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testCountReturnsNumberOfConnectionsInPool()
  94. {
  95. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  96. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  97. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  98. $cluster = new RedisCluster(new Connection\Factory());
  99. $cluster->add($connection1);
  100. $cluster->add($connection2);
  101. $cluster->add($connection3);
  102. $this->assertSame(3, count($cluster));
  103. $cluster->remove($connection3);
  104. $this->assertSame(2, count($cluster));
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testConnectPicksRandomConnection()
  110. {
  111. $connect1 = false;
  112. $connect2 = false;
  113. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  114. $connection1->expects($this->any())
  115. ->method('connect')
  116. ->will($this->returnCallback(function () use (&$connect1) {
  117. $connect1 = true;
  118. }));
  119. $connection1->expects($this->any())
  120. ->method('isConnected')
  121. ->will($this->returnCallback(function () use (&$connect1) {
  122. return $connect1;
  123. }));
  124. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  125. $connection2->expects($this->any())
  126. ->method('connect')
  127. ->will($this->returnCallback(function () use (&$connect2) {
  128. $connect2 = true;
  129. }));
  130. $connection2->expects($this->any())
  131. ->method('isConnected')
  132. ->will($this->returnCallback(function () use (&$connect2) {
  133. return $connect2;
  134. }));
  135. $cluster = new RedisCluster(new Connection\Factory());
  136. $cluster->add($connection1);
  137. $cluster->add($connection2);
  138. $cluster->connect();
  139. $this->assertTrue($cluster->isConnected());
  140. if ($connect1) {
  141. $this->assertTrue($connect1);
  142. $this->assertFalse($connect2);
  143. } else {
  144. $this->assertFalse($connect1);
  145. $this->assertTrue($connect2);
  146. }
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testDisconnectForcesAllConnectionsToDisconnect()
  152. {
  153. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  154. $connection1->expects($this->once())->method('disconnect');
  155. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  156. $connection2->expects($this->once())->method('disconnect');
  157. $cluster = new RedisCluster(new Connection\Factory());
  158. $cluster->add($connection1);
  159. $cluster->add($connection2);
  160. $cluster->disconnect();
  161. }
  162. /**
  163. * @group disconnected
  164. */
  165. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  166. {
  167. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  168. $connection1->expects($this->once())
  169. ->method('isConnected')
  170. ->will($this->returnValue(false));
  171. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  172. $connection2->expects($this->once())
  173. ->method('isConnected')
  174. ->will($this->returnValue(true));
  175. $cluster = new RedisCluster(new Connection\Factory());
  176. $cluster->add($connection1);
  177. $cluster->add($connection2);
  178. $this->assertTrue($cluster->isConnected());
  179. }
  180. /**
  181. * @group disconnected
  182. */
  183. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  184. {
  185. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  186. $connection1->expects($this->once())
  187. ->method('isConnected')
  188. ->will($this->returnValue(false));
  189. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  190. $connection2->expects($this->once())
  191. ->method('isConnected')
  192. ->will($this->returnValue(false));
  193. $cluster = new RedisCluster(new Connection\Factory());
  194. $cluster->add($connection1);
  195. $cluster->add($connection2);
  196. $this->assertFalse($cluster->isConnected());
  197. }
  198. /**
  199. * @group disconnected
  200. */
  201. public function testCanReturnAnIteratorForConnections()
  202. {
  203. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  204. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  205. $cluster = new RedisCluster(new Connection\Factory());
  206. $cluster->add($connection1);
  207. $cluster->add($connection2);
  208. $this->assertInstanceOf('Iterator', $iterator = $cluster->getIterator());
  209. $connections = iterator_to_array($iterator);
  210. $this->assertSame($connection1, $connections[0]);
  211. $this->assertSame($connection2, $connections[1]);
  212. }
  213. /**
  214. * @group disconnected
  215. */
  216. public function testCanAssignConnectionsToCustomSlots()
  217. {
  218. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  219. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  220. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  221. $cluster = new RedisCluster(new Connection\Factory());
  222. $cluster->add($connection1);
  223. $cluster->add($connection2);
  224. $cluster->add($connection3);
  225. $cluster->setSlots(0, 1364, '127.0.0.1:6379');
  226. $cluster->setSlots(1365, 2729, '127.0.0.1:6380');
  227. $cluster->setSlots(2730, 4095, '127.0.0.1:6381');
  228. $expectedMap = array_merge(
  229. array_fill(0, 1365, '127.0.0.1:6379'),
  230. array_fill(1364, 1365, '127.0.0.1:6380'),
  231. array_fill(2729, 1366, '127.0.0.1:6381')
  232. );
  233. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  234. }
  235. /**
  236. * @group disconnected
  237. */
  238. public function testAddingConnectionResetsSlotsMap()
  239. {
  240. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  241. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  242. $cluster = new RedisCluster(new Connection\Factory());
  243. $cluster->add($connection1);
  244. $cluster->setSlots(0, 4095, '127.0.0.1:6379');
  245. $this->assertSame(array_fill(0, 4096, '127.0.0.1:6379'), $cluster->getSlotsMap());
  246. $cluster->add($connection2);
  247. $this->assertEmpty($cluster->getSlotsMap());
  248. }
  249. /**
  250. * @group disconnected
  251. */
  252. public function testRemovingConnectionResetsSlotsMap()
  253. {
  254. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  255. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  256. $cluster = new RedisCluster(new Connection\Factory());
  257. $cluster->add($connection1);
  258. $cluster->add($connection2);
  259. $cluster->setSlots(0, 2047, '127.0.0.1:6379');
  260. $cluster->setSlots(2048, 4095, '127.0.0.1:6380');
  261. $expectedMap = array_merge(
  262. array_fill(0, 2048, '127.0.0.1:6379'),
  263. array_fill(2048, 2048, '127.0.0.1:6380')
  264. );
  265. $this->assertSame($expectedMap, $cluster->getSlotsMap());
  266. $cluster->remove($connection1);
  267. $this->assertEmpty($cluster->getSlotsMap());
  268. }
  269. /**
  270. * @group disconnected
  271. */
  272. public function testCanAssignConnectionsToRangeOfSlotsFromParameters()
  273. {
  274. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460');
  275. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-10921');
  276. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-16383');
  277. $cluster = new RedisCluster(new Connection\Factory());
  278. $cluster->add($connection1);
  279. $cluster->add($connection2);
  280. $cluster->add($connection3);
  281. $cluster->buildSlotsMap();
  282. $expectedMap = array_merge(
  283. array_fill(0, 5461, '127.0.0.1:6379'),
  284. array_fill(5460, 5461, '127.0.0.1:6380'),
  285. array_fill(10921, 5462, '127.0.0.1:6381')
  286. );
  287. $actualMap = $cluster->getSlotsMap();
  288. ksort($actualMap);
  289. $this->assertSame($expectedMap, $actualMap);
  290. }
  291. /**
  292. * @group disconnected
  293. */
  294. public function testCanAssignConnectionsToSingleSlotOrRangesOfSlotsFromParameters()
  295. {
  296. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-5460,5500-5600,11000');
  297. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=5461-5499,5600-10921');
  298. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=10922-10999,11001-16383');
  299. $cluster = new RedisCluster(new Connection\Factory());
  300. $cluster->add($connection1);
  301. $cluster->add($connection2);
  302. $cluster->add($connection3);
  303. $cluster->buildSlotsMap();
  304. $expectedMap = array_merge(
  305. array_fill(0, 5461, '127.0.0.1:6379'),
  306. array_fill(5460, 39, '127.0.0.1:6380'),
  307. array_fill(5499, 101, '127.0.0.1:6379'),
  308. array_fill(5599, 5321, '127.0.0.1:6380'),
  309. array_fill(10921, 78, '127.0.0.1:6381'),
  310. array_fill(11000, 1, '127.0.0.1:6379'),
  311. array_fill(11000, 5383, '127.0.0.1:6381')
  312. );
  313. $actualMap = $cluster->getSlotsMap();
  314. ksort($actualMap);
  315. $this->assertSame($expectedMap, $actualMap);
  316. }
  317. /**
  318. * @group disconnected
  319. */
  320. public function testReturnsCorrectConnectionUsingSlotID()
  321. {
  322. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  323. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  324. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  325. $cluster = new RedisCluster(new Connection\Factory());
  326. $cluster->add($connection1);
  327. $cluster->add($connection2);
  328. $cluster->add($connection3);
  329. $this->assertSame($connection1, $cluster->getConnectionBySlot(0));
  330. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  331. $this->assertSame($connection3, $cluster->getConnectionBySlot(10922));
  332. $cluster->setSlots(5461, 7096, '127.0.0.1:6380');
  333. $this->assertSame($connection2, $cluster->getConnectionBySlot(5461));
  334. }
  335. /**
  336. * @group disconnected
  337. */
  338. public function testReturnsCorrectConnectionUsingCommandInstance()
  339. {
  340. $profile = Profile\Factory::getDefault();
  341. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  342. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  343. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  344. $cluster = new RedisCluster(new Connection\Factory());
  345. $cluster->add($connection1);
  346. $cluster->add($connection2);
  347. $cluster->add($connection3);
  348. $set = $profile->createCommand('set', array('node:1001', 'foobar'));
  349. $get = $profile->createCommand('get', array('node:1001'));
  350. $this->assertSame($connection1, $cluster->getConnection($set));
  351. $this->assertSame($connection1, $cluster->getConnection($get));
  352. $set = $profile->createCommand('set', array('node:1048', 'foobar'));
  353. $get = $profile->createCommand('get', array('node:1048'));
  354. $this->assertSame($connection2, $cluster->getConnection($set));
  355. $this->assertSame($connection2, $cluster->getConnection($get));
  356. $set = $profile->createCommand('set', array('node:1082', 'foobar'));
  357. $get = $profile->createCommand('get', array('node:1082'));
  358. $this->assertSame($connection3, $cluster->getConnection($set));
  359. $this->assertSame($connection3, $cluster->getConnection($get));
  360. }
  361. /**
  362. * @group disconnected
  363. */
  364. public function testWritesCommandToCorrectConnection()
  365. {
  366. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  367. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  368. $connection1->expects($this->once())->method('writeRequest')->with($command);
  369. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  370. $connection2->expects($this->never())->method('writeRequest');
  371. $cluster = new RedisCluster(new Connection\Factory());
  372. $cluster->useClusterSlots(false);
  373. $cluster->add($connection1);
  374. $cluster->add($connection2);
  375. $cluster->writeRequest($command);
  376. }
  377. /**
  378. * @group disconnected
  379. */
  380. public function testReadsCommandFromCorrectConnection()
  381. {
  382. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1050'));
  383. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  384. $connection1->expects($this->never())->method('readResponse');
  385. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  386. $connection2->expects($this->once())->method('readResponse')->with($command);
  387. $cluster = new RedisCluster(new Connection\Factory());
  388. $cluster->useClusterSlots(false);
  389. $cluster->add($connection1);
  390. $cluster->add($connection2);
  391. $cluster->readResponse($command);
  392. }
  393. /**
  394. * @group disconnected
  395. */
  396. public function testRetriesExecutingCommandOnConnectionFailureOnlyAfterFetchingNewSlotsMap()
  397. {
  398. $slotsmap = array(
  399. array(0, 5460, array('127.0.0.1', 9381), array()),
  400. array(5461, 10921, array('127.0.0.1', 6382), array()),
  401. array(10922, 16383, array('127.0.0.1', 6383), array()),
  402. );
  403. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
  404. $connection1->expects($this->once())
  405. ->method('executeCommand')
  406. ->with($this->isRedisCommand(
  407. 'GET', array('node:1001')
  408. ))
  409. ->will($this->throwException(
  410. new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
  411. ));
  412. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
  413. $connection2->expects($this->any())
  414. ->method('executeCommand')
  415. ->with($this->isRedisCommand(
  416. 'CLUSTER', array('SLOTS')
  417. ))
  418. ->will($this->returnValue($slotsmap));
  419. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
  420. $connection3->expects($this->any())
  421. ->method('executeCommand')
  422. ->with($this->isRedisCommand(
  423. 'CLUSTER', array('SLOTS')
  424. ))
  425. ->will($this->returnValue($slotsmap));
  426. $connection4 = $this->getMockConnection('tcp://127.0.0.1:9381');
  427. $connection4->expects($this->at(0))
  428. ->method('executeCommand')
  429. ->with($this->isRedisCommand(
  430. 'GET', array('node:1001')
  431. ))
  432. ->will($this->returnValue('value:1001'));
  433. $connection4->expects($this->at(1))
  434. ->method('executeCommand')
  435. ->with($this->isRedisCommand(
  436. 'GET', array('node:5001')
  437. ))
  438. ->will($this->returnValue('value:5001'));
  439. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  440. $factory->expects($this->once())
  441. ->method('create')
  442. ->with(array(
  443. 'host' => '127.0.0.1',
  444. 'port' => '9381',
  445. ))
  446. ->will($this->returnValue($connection4));
  447. $cluster = new RedisCluster($factory);
  448. $cluster->add($connection1);
  449. $cluster->add($connection2);
  450. $cluster->add($connection3);
  451. $this->assertSame('value:1001', $cluster->executeCommand(
  452. Command\RawCommand::create('get', 'node:1001')
  453. ));
  454. $this->assertSame('value:5001', $cluster->executeCommand(
  455. Command\RawCommand::create('get', 'node:5001')
  456. ));
  457. }
  458. /**
  459. * @group disconnected
  460. */
  461. public function testRetriesExecutingCommandOnConnectionFailureButDoNotAskSlotsMapWhenDisabled()
  462. {
  463. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5500');
  464. $connection1->expects($this->once())
  465. ->method('executeCommand')
  466. ->with($this->isRedisCommand(
  467. 'GET', array('node:1001')
  468. ))
  469. ->will($this->throwException(
  470. new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
  471. ));
  472. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5501-11000');
  473. $connection2->expects($this->once())
  474. ->method('executeCommand')
  475. ->with($this->isRedisCommand(
  476. 'GET', array('node:1001')
  477. ))
  478. ->will($this->returnValue(
  479. new Response\Error('MOVED 1970 127.0.0.1:9381')
  480. ));
  481. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=11101-16383');
  482. $connection3->expects($this->never())
  483. ->method('executeCommand');
  484. $connection4 = $this->getMockConnection('tcp://127.0.0.1:9381');
  485. $connection4->expects($this->once())
  486. ->method('executeCommand')
  487. ->with($this->isRedisCommand(
  488. 'GET', array('node:1001')
  489. ))
  490. ->will($this->returnValue('value:1001'));
  491. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  492. $factory->expects($this->once())
  493. ->method('create')
  494. ->with(array(
  495. 'host' => '127.0.0.1',
  496. 'port' => '9381',
  497. ))
  498. ->will($this->returnValue($connection4));
  499. // TODO: I'm not sure about mocking a protected method, but it'll do for now
  500. $cluster = $this->getMock('Predis\Connection\Aggregate\RedisCluster', array('getRandomConnection'), array($factory));
  501. $cluster->expects($this->never())
  502. ->method('getRandomConnection');
  503. $cluster->useClusterSlots(false);
  504. $cluster->add($connection1);
  505. $cluster->add($connection2);
  506. $cluster->add($connection3);
  507. $this->assertSame('value:1001', $cluster->executeCommand(
  508. Command\RawCommand::create('get', 'node:1001')
  509. ));
  510. }
  511. /**
  512. * @group disconnected
  513. * @expectedException \Predis\ClientException
  514. * @expectedExceptionMessage No connections available in the pool
  515. */
  516. public function testThrowsClientExceptionWhenExecutingCommandWithEmptyPool()
  517. {
  518. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  519. $factory->expects($this->never())->method('create');
  520. $cluster = new RedisCluster($factory);
  521. $cluster->executeCommand(Command\RawCommand::create('get', 'node:1001'));
  522. }
  523. /**
  524. * @group disconnected
  525. */
  526. public function testAskSlotsMapReturnEmptyArrayOnEmptyConnectionsPool()
  527. {
  528. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  529. $factory->expects($this->never())->method('create');
  530. $cluster = new RedisCluster($factory);
  531. $this->assertEmpty($cluster->askSlotsMap());
  532. }
  533. /**
  534. * @group disconnected
  535. */
  536. public function testAskSlotsMapRetriesOnDifferentNodeOnConnectionFailure()
  537. {
  538. $slotsmap = array(
  539. array(0, 5460, array('127.0.0.1', 9381), array()),
  540. array(5461, 10921, array('127.0.0.1', 6382), array()),
  541. array(10922, 16383, array('127.0.0.1', 6383), array()),
  542. );
  543. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
  544. $connection1->expects($this->once())
  545. ->method('executeCommand')
  546. ->with($this->isRedisCommand(
  547. 'CLUSTER', array('SLOTS')
  548. ))
  549. ->will($this->throwException(
  550. new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
  551. ));
  552. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
  553. $connection2->expects($this->once())
  554. ->method('executeCommand')
  555. ->with($this->isRedisCommand(
  556. 'CLUSTER', array('SLOTS')
  557. ))
  558. ->will($this->throwException(
  559. new Connection\ConnectionException($connection2, 'Unknown connection error [127.0.0.1:6383]')
  560. ));
  561. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
  562. $connection3->expects($this->once())
  563. ->method('executeCommand')
  564. ->with($this->isRedisCommand(
  565. 'CLUSTER', array('SLOTS')
  566. ))
  567. ->will($this->returnValue($slotsmap));
  568. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  569. $factory->expects($this->never())->method('create');
  570. // TODO: I'm not sure about mocking a protected method, but it'll do for now
  571. $cluster = $this->getMock('Predis\Connection\Aggregate\RedisCluster', array('getRandomConnection'), array($factory));
  572. $cluster->expects($this->exactly(3))
  573. ->method('getRandomConnection')
  574. ->will($this->onConsecutiveCalls($connection1, $connection2, $connection3));
  575. $cluster->add($connection1);
  576. $cluster->add($connection2);
  577. $cluster->add($connection3);
  578. $this->assertCount(16384, $cluster->askSlotsMap());
  579. }
  580. /**
  581. * @group disconnected
  582. * @expectedException \Predis\Connection\ConnectionException
  583. * @expectedExceptionMessage Unknown connection error [127.0.0.1:6382]
  584. */
  585. public function testAskSlotsMapHonorsRetryLimitOnMultipleConnectionFailures()
  586. {
  587. $slotsmap = array(
  588. array(0, 5460, array('127.0.0.1', 9381), array()),
  589. array(5461, 10921, array('127.0.0.1', 6382), array()),
  590. array(10922, 16383, array('127.0.0.1', 6383), array()),
  591. );
  592. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=0-5460');
  593. $connection1->expects($this->any())
  594. ->method('executeCommand')
  595. ->with($this->isRedisCommand(
  596. 'CLUSTER', array('SLOTS')
  597. ))
  598. ->will($this->throwException(
  599. new Connection\ConnectionException($connection1, 'Unknown connection error [127.0.0.1:6381]')
  600. ));
  601. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6382?slots=5461-10921');
  602. $connection2->expects($this->any())
  603. ->method('executeCommand')
  604. ->with($this->isRedisCommand(
  605. 'CLUSTER', array('SLOTS')
  606. ))
  607. ->will($this->throwException(
  608. new Connection\ConnectionException($connection2, 'Unknown connection error [127.0.0.1:6382]')
  609. ));
  610. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6383?slots=10922-16383');
  611. $connection3->expects($this->never())
  612. ->method('executeCommand');
  613. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  614. $factory->expects($this->never())->method('create');
  615. // TODO: I'm not sure about mocking a protected method, but it'll do for now
  616. $cluster = $this->getMock('Predis\Connection\Aggregate\RedisCluster', array('getRandomConnection'), array($factory));
  617. $cluster->expects($this->exactly(2))
  618. ->method('getRandomConnection')
  619. ->will($this->onConsecutiveCalls($connection1, $connection2));
  620. $cluster->add($connection1);
  621. $cluster->add($connection2);
  622. $cluster->add($connection3);
  623. $cluster->setRetryLimit(1);
  624. $cluster->askSlotsMap();
  625. }
  626. /**
  627. * @group disconnected
  628. */
  629. public function testSupportsKeyHashTags()
  630. {
  631. $profile = Profile\Factory::getDefault();
  632. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  633. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  634. $cluster = new RedisCluster(new Connection\Factory());
  635. $cluster->add($connection1);
  636. $cluster->add($connection2);
  637. $set = $profile->createCommand('set', array('{node:1001}:foo', 'foobar'));
  638. $get = $profile->createCommand('get', array('{node:1001}:foo'));
  639. $this->assertSame($connection1, $cluster->getConnection($set));
  640. $this->assertSame($connection1, $cluster->getConnection($get));
  641. $set = $profile->createCommand('set', array('{node:1001}:bar', 'foobar'));
  642. $get = $profile->createCommand('get', array('{node:1001}:bar'));
  643. $this->assertSame($connection1, $cluster->getConnection($set));
  644. $this->assertSame($connection1, $cluster->getConnection($get));
  645. }
  646. /**
  647. * @group disconnected
  648. */
  649. public function testAskResponseWithConnectionInPool()
  650. {
  651. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6380');
  652. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  653. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  654. $connection1->expects($this->exactly(2))
  655. ->method('executeCommand')
  656. ->with($command)
  657. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  658. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  659. $connection2->expects($this->at(2))
  660. ->method('executeCommand')
  661. ->with($this->isRedisCommand('ASKING'));
  662. $connection2->expects($this->at(3))
  663. ->method('executeCommand')
  664. ->with($command)
  665. ->will($this->returnValue('foobar'));
  666. $factory = $this->getMock('Predis\Connection\Factory');
  667. $factory->expects($this->never())->method('create');
  668. $cluster = new RedisCluster($factory);
  669. $cluster->useClusterSlots(false);
  670. $cluster->add($connection1);
  671. $cluster->add($connection2);
  672. $this->assertSame('foobar', $cluster->executeCommand($command));
  673. $this->assertSame('foobar', $cluster->executeCommand($command));
  674. $this->assertSame(2, count($cluster));
  675. }
  676. /**
  677. * @group disconnected
  678. */
  679. public function testAskResponseWithConnectionNotInPool()
  680. {
  681. $askResponse = new Response\Error('ASK 1970 127.0.0.1:6381');
  682. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  683. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  684. $connection1->expects($this->exactly(2))
  685. ->method('executeCommand')
  686. ->with($command)
  687. ->will($this->onConsecutiveCalls($askResponse, 'foobar'));
  688. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  689. $connection2->expects($this->never())
  690. ->method('executeCommand');
  691. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  692. $connection3->expects($this->at(0))
  693. ->method('executeCommand')
  694. ->with($this->isRedisCommand('ASKING'));
  695. $connection3->expects($this->at(1))
  696. ->method('executeCommand')
  697. ->with($command)
  698. ->will($this->returnValue('foobar'));
  699. $factory = $this->getMock('Predis\Connection\Factory');
  700. $factory->expects($this->once())
  701. ->method('create')
  702. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  703. ->will($this->returnValue($connection3));
  704. $cluster = new RedisCluster($factory);
  705. $cluster->useClusterSlots(false);
  706. $cluster->add($connection1);
  707. $cluster->add($connection2);
  708. $this->assertSame('foobar', $cluster->executeCommand($command));
  709. $this->assertSame('foobar', $cluster->executeCommand($command));
  710. $this->assertSame(2, count($cluster));
  711. }
  712. /**
  713. * @group disconnected
  714. */
  715. public function testMovedResponseWithConnectionInPool()
  716. {
  717. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6380');
  718. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  719. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  720. $connection1->expects($this->exactly(1))
  721. ->method('executeCommand')
  722. ->with($command)
  723. ->will($this->returnValue($movedResponse));
  724. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  725. $connection2->expects($this->exactly(2))
  726. ->method('executeCommand')
  727. ->with($command)
  728. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  729. $factory = $this->getMock('Predis\Connection\Factory');
  730. $factory->expects($this->never())->method('create');
  731. $cluster = new RedisCluster($factory);
  732. $cluster->useClusterSlots(false);
  733. $cluster->add($connection1);
  734. $cluster->add($connection2);
  735. $this->assertSame('foobar', $cluster->executeCommand($command));
  736. $this->assertSame('foobar', $cluster->executeCommand($command));
  737. $this->assertSame(2, count($cluster));
  738. }
  739. /**
  740. * @group disconnected
  741. */
  742. public function testMovedResponseWithConnectionNotInPool()
  743. {
  744. $movedResponse = new Response\Error('MOVED 1970 127.0.0.1:6381');
  745. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  746. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  747. $connection1->expects($this->once())
  748. ->method('executeCommand')
  749. ->with($command)
  750. ->will($this->returnValue($movedResponse));
  751. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  752. $connection2->expects($this->never())
  753. ->method('executeCommand');
  754. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381');
  755. $connection3->expects($this->exactly(2))
  756. ->method('executeCommand')
  757. ->with($command)
  758. ->will($this->onConsecutiveCalls('foobar', 'foobar'));
  759. $factory = $this->getMock('Predis\Connection\Factory');
  760. $factory->expects($this->once())
  761. ->method('create')
  762. ->with(array('host' => '127.0.0.1', 'port' => '6381'))
  763. ->will($this->returnValue($connection3));
  764. $cluster = new RedisCluster($factory);
  765. $cluster->useClusterSlots(false);
  766. $cluster->add($connection1);
  767. $cluster->add($connection2);
  768. $this->assertSame('foobar', $cluster->executeCommand($command));
  769. $this->assertSame('foobar', $cluster->executeCommand($command));
  770. $this->assertSame(3, count($cluster));
  771. }
  772. /**
  773. * @group disconnected
  774. */
  775. public function testParseIPv6AddresseAndPortPairInRedirectionPayload()
  776. {
  777. $movedResponse = new Response\Error('MOVED 1970 2001:db8:0:f101::2:6379');
  778. $command = Profile\Factory::getDefault()->createCommand('get', array('node:1001'));
  779. $connection1 = $this->getMockConnection('tcp://[2001:db8:0:f101::1]:6379');
  780. $connection1->expects($this->once())
  781. ->method('executeCommand')
  782. ->with($command)
  783. ->will($this->returnValue($movedResponse));
  784. $connection2 = $this->getMockConnection('tcp://[2001:db8:0:f101::2]:6379');
  785. $connection2->expects($this->once())
  786. ->method('executeCommand')
  787. ->with($command)
  788. ->will($this->returnValue('foobar'));
  789. $factory = $this->getMock('Predis\Connection\Factory');
  790. $factory->expects($this->once())
  791. ->method('create')
  792. ->with(array('host' => '2001:db8:0:f101::2', 'port' => '6379'))
  793. ->will($this->returnValue($connection2));
  794. $cluster = new RedisCluster($factory);
  795. $cluster->useClusterSlots(false);
  796. $cluster->add($connection1);
  797. $cluster->executeCommand($command);
  798. }
  799. /**
  800. * @group disconnected
  801. */
  802. public function testFetchSlotsMapFromClusterWithClusterSlotsCommand()
  803. {
  804. $response = array(
  805. array(12288, 13311, array('10.1.0.51', 6387), array('10.1.0.52', 6387)),
  806. array(3072, 4095, array('10.1.0.52', 6392), array('10.1.0.51', 6392)),
  807. array(6144, 7167, array('', 6384), array('10.1.0.52', 6384)),
  808. array(14336, 15359, array('10.1.0.51', 6388), array('10.1.0.52', 6388)),
  809. array(15360, 16383, array('10.1.0.52', 6398), array('10.1.0.51', 6398)),
  810. array(1024, 2047, array('10.1.0.52', 6391), array('10.1.0.51', 6391)),
  811. array(11264, 12287, array('10.1.0.52', 6396), array('10.1.0.51', 6396)),
  812. array(5120, 6143, array('10.1.0.52', 6393), array('10.1.0.51', 6393)),
  813. array(0, 1023, array('10.1.0.51', 6381), array('10.1.0.52', 6381)),
  814. array(13312, 14335, array('10.1.0.52', 6397), array('10.1.0.51', 6397)),
  815. array(4096, 5119, array('10.1.0.51', 6383), array('10.1.0.52', 6383)),
  816. array(9216, 10239, array('10.1.0.52', 6395), array('10.1.0.51', 6395)),
  817. array(8192, 9215, array('10.1.0.51', 6385), array('10.1.0.52', 6385)),
  818. array(10240, 11263, array('10.1.0.51', 6386), array('10.1.0.52', 6386)),
  819. array(2048, 3071, array('10.1.0.51', 6382), array('10.1.0.52', 6382)),
  820. array(7168, 8191, array('10.1.0.52', 6394), array('10.1.0.51', 6394)),
  821. );
  822. $command = Command\RawCommand::create('CLUSTER', 'SLOTS');
  823. $connection1 = $this->getMockConnection('tcp://10.1.0.51:6384');
  824. $connection1->expects($this->once())
  825. ->method('executeCommand')
  826. ->with($command)
  827. ->will($this->returnValue($response));
  828. $factory = $this->getMock('Predis\Connection\Factory');
  829. $cluster = new RedisCluster($factory);
  830. $cluster->add($connection1);
  831. $cluster->askSlotsMap();
  832. $this->assertSame($cluster->getConnectionBySlot('6144'), $connection1);
  833. }
  834. /**
  835. * @group disconnected
  836. */
  837. public function testAskSlotsMapToRedisClusterOnMovedResponseByDefault()
  838. {
  839. $cmdGET = Command\RawCommand::create('GET', 'node:1001');
  840. $rspMOVED = new Response\Error('MOVED 1970 127.0.0.1:6380');
  841. $rspSlotsArray = array(
  842. array(0, 8191, array('127.0.0.1', 6379)),
  843. array(8192, 16383, array('127.0.0.1', 6380)),
  844. );
  845. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379');
  846. $connection1->expects($this->once())
  847. ->method('executeCommand')
  848. ->with($cmdGET)
  849. ->will($this->returnValue($rspMOVED));
  850. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380');
  851. $connection2->expects($this->at(0))
  852. ->method('executeCommand')
  853. ->with($this->isRedisCommand('CLUSTER', array('SLOTS')))
  854. ->will($this->returnValue($rspSlotsArray));
  855. $connection2->expects($this->at(2))
  856. ->method('executeCommand')
  857. ->with($cmdGET)
  858. ->will($this->returnValue('foobar'));
  859. $factory = $this->getMock('Predis\Connection\Factory');
  860. $factory->expects($this->once())
  861. ->method('create')
  862. ->with(array('host' => '127.0.0.1', 'port' => '6380'))
  863. ->will($this->returnValue($connection2));
  864. $cluster = new RedisCluster($factory);
  865. $cluster->add($connection1);
  866. $this->assertSame('foobar', $cluster->executeCommand($cmdGET));
  867. $this->assertSame(2, count($cluster));
  868. }
  869. /**
  870. * @group disconnected
  871. * @expectedException \Predis\NotSupportedException
  872. * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
  873. */
  874. public function testThrowsExceptionOnNonSupportedCommand()
  875. {
  876. $ping = Profile\Factory::getDefault()->createCommand('ping');
  877. $cluster = new RedisCluster(new Connection\Factory());
  878. $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
  879. $cluster->getConnection($ping);
  880. }
  881. /**
  882. * @medium
  883. * @group disconnected
  884. */
  885. public function testCanBeSerialized()
  886. {
  887. $connection1 = $this->getMockConnection('tcp://127.0.0.1:6379?slots=0-1364');
  888. $connection2 = $this->getMockConnection('tcp://127.0.0.1:6380?slots=1365-2729');
  889. $connection3 = $this->getMockConnection('tcp://127.0.0.1:6381?slots=2730-4095');
  890. $cluster = new RedisCluster(new Connection\Factory());
  891. $cluster->add($connection1);
  892. $cluster->add($connection2);
  893. $cluster->add($connection3);
  894. $cluster->buildSlotsMap();
  895. $unserialized = unserialize(serialize($cluster));
  896. $this->assertEquals($cluster, $unserialized);
  897. }
  898. // ******************************************************************** //
  899. // ---- HELPER METHODS ------------------------------------------------ //
  900. // ******************************************************************** //
  901. /**
  902. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  903. *
  904. * @param mixed $parameters Optional parameters.
  905. *
  906. * @return mixed
  907. */
  908. protected function getMockConnection($parameters = null)
  909. {
  910. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  911. if ($parameters) {
  912. $parameters = Connection\Parameters::create($parameters);
  913. $hash = "{$parameters->host}:{$parameters->port}";
  914. $connection->expects($this->any())
  915. ->method('getParameters')
  916. ->will($this->returnValue($parameters));
  917. $connection->expects($this->any())
  918. ->method('__toString')
  919. ->will($this->returnValue($hash));
  920. }
  921. return $connection;
  922. }
  923. }