RedisClusterTest.php 43 KB

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