SentinelReplicationTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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\Replication;
  14. use Predis\Response;
  15. use PredisTestCase;
  16. /**
  17. *
  18. */
  19. class SentinelReplicationTest extends PredisTestCase
  20. {
  21. /**
  22. * @group disconnected
  23. * @expectedException Predis\ClientException
  24. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  25. */
  26. public function testMethodGetSentinelConnectionThrowsExceptionOnEmptySentinelsPool()
  27. {
  28. $replication = $this->getReplicationConnection('svc', array());
  29. $replication->getSentinelConnection();
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testMethodGetSentinelConnectionReturnsFirstAvailableSentinel()
  35. {
  36. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  37. $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
  38. $sentinel3 = $this->getMockSentinelConnection('tcp://127.0.0.1:5383?alias=sentinel3');
  39. $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2, $sentinel3));
  40. $this->assertSame($sentinel1, $replication->getSentinelConnection());
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testMethodAddAttachesMasterOrSlaveNodesToReplication()
  46. {
  47. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  48. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  49. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  50. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  51. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  52. $replication->add($master);
  53. $replication->add($slave1);
  54. $replication->add($slave2);
  55. $this->assertSame($master, $replication->getConnectionById('master'));
  56. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  57. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  58. $this->assertSame($master, $replication->getMaster());
  59. $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testMethodRemoveDismissesMasterOrSlaveNodesFromReplication()
  65. {
  66. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  67. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  68. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  69. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  70. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  71. $replication->add($master);
  72. $replication->add($slave1);
  73. $replication->add($slave2);
  74. $this->assertTrue($replication->remove($slave1));
  75. $this->assertFalse($replication->remove($sentinel1));
  76. $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
  77. $this->assertCount(1, $slaves = $replication->getSlaves());
  78. $this->assertSame('127.0.0.1:6383', (string) $slaves[0]);
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testMethodUpdateSentinelsFetchesSentinelNodes()
  84. {
  85. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  86. $sentinel1->expects($this->once())
  87. ->method('executeCommand')
  88. ->with($this->isRedisCommand(
  89. 'SENTINEL', array('sentinels', 'svc')
  90. ))
  91. ->will($this->returnValue(
  92. array(
  93. array(
  94. "name", "127.0.0.1:5382",
  95. "ip", "127.0.0.1",
  96. "port", "5382",
  97. "runid", "a113aa7a0d4870a85bb22b4b605fd26eb93ed40e",
  98. "flags", "sentinel",
  99. ),
  100. array(
  101. "name", "127.0.0.1:5383",
  102. "ip", "127.0.0.1",
  103. "port", "5383",
  104. "runid", "f53b52d281be5cdd4873700c94846af8dbe47209",
  105. "flags", "sentinel",
  106. ),
  107. )
  108. ));
  109. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  110. $replication->updateSentinels();
  111. // TODO: sorry for the smell...
  112. $reflection = new \ReflectionProperty($replication, 'sentinels');
  113. $reflection->setAccessible(true);
  114. $expected = array(
  115. array('host' => '127.0.0.1', 'port' => '5381'),
  116. array('host' => '127.0.0.1', 'port' => '5382'),
  117. array('host' => '127.0.0.1', 'port' => '5383'),
  118. );
  119. $this->assertSame($sentinel1, $replication->getSentinelConnection());
  120. $this->assertSame($expected, array_intersect_key($expected, $reflection->getValue($replication)));
  121. }
  122. /**
  123. * @group disconnected
  124. */
  125. public function testMethodUpdateSentinelsRemovesCurrentSentinelAndRetriesNextOneOnFailure()
  126. {
  127. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  128. $sentinel1->expects($this->once())
  129. ->method('executeCommand')
  130. ->with($this->isRedisCommand(
  131. 'SENTINEL', array('sentinels', 'svc')
  132. ))
  133. ->will($this->throwException(
  134. new Connection\ConnectionException($sentinel1, "Unknown connection error [127.0.0.1:5381]")
  135. ));
  136. $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
  137. $sentinel2->expects($this->once())
  138. ->method('executeCommand')
  139. ->with($this->isRedisCommand(
  140. 'SENTINEL', array('sentinels', 'svc')
  141. ))
  142. ->will($this->returnValue(
  143. array(
  144. array(
  145. "name", "127.0.0.1:5383",
  146. "ip", "127.0.0.1",
  147. "port", "5383",
  148. "runid", "f53b52d281be5cdd4873700c94846af8dbe47209",
  149. "flags", "sentinel",
  150. ),
  151. )
  152. ));
  153. $replication = $this->getReplicationConnection('svc', array($sentinel1, $sentinel2));
  154. $replication->updateSentinels();
  155. // TODO: sorry for the smell...
  156. $reflection = new \ReflectionProperty($replication, 'sentinels');
  157. $reflection->setAccessible(true);
  158. $expected = array(
  159. array('host' => '127.0.0.1', 'port' => '5382'),
  160. array('host' => '127.0.0.1', 'port' => '5383'),
  161. );
  162. $this->assertSame($sentinel2, $replication->getSentinelConnection());
  163. $this->assertSame($expected, array_intersect_key($expected, $reflection->getValue($replication)));
  164. }
  165. /**
  166. * @group disconnected
  167. * @expectedException Predis\ClientException
  168. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  169. */
  170. public function testMethodUpdateSentinelsThrowsExceptionOnNoAvailableSentinel()
  171. {
  172. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  173. $sentinel1->expects($this->once())
  174. ->method('executeCommand')
  175. ->with($this->isRedisCommand(
  176. 'SENTINEL', array('sentinels', 'svc')
  177. ))
  178. ->will($this->throwException(
  179. new Connection\ConnectionException($sentinel1, "Unknown connection error [127.0.0.1:5381]")
  180. ));
  181. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  182. $replication->updateSentinels();
  183. }
  184. /**
  185. * @group disconnected
  186. */
  187. public function testMethodQuerySentinelFetchesMasterNodeSlaveNodesAndSentinelNodes()
  188. {
  189. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  190. $sentinel1->expects($this->exactly(3))
  191. ->method('executeCommand')
  192. ->withConsecutive(
  193. $this->isRedisCommand('SENTINEL', array('sentinels', 'svc')),
  194. $this->isRedisCommand('SENTINEL', array('get-master-addr-by-name', 'svc')),
  195. $this->isRedisCommand('SENTINEL', array('slaves', 'svc'))
  196. )
  197. ->will($this->onConsecutiveCalls(
  198. // SENTINEL sentinels svc
  199. array(
  200. array(
  201. "name", "127.0.0.1:5382",
  202. "ip", "127.0.0.1",
  203. "port", "5382",
  204. "runid", "a113aa7a0d4870a85bb22b4b605fd26eb93ed40e",
  205. "flags", "sentinel",
  206. ),
  207. ),
  208. // SENTINEL get-master-addr-by-name svc
  209. array('127.0.0.1', '6381'),
  210. // SENTINEL slaves svc
  211. array(
  212. array(
  213. "name", "127.0.0.1:6382",
  214. "ip", "127.0.0.1",
  215. "port", "6382",
  216. "runid", "112cdebd22924a7d962be496f3a1c4c7c9bad93f",
  217. "flags", "slave",
  218. "master-host", "127.0.0.1",
  219. "master-port", "6381"
  220. ),
  221. array(
  222. "name", "127.0.0.1:6383",
  223. "ip", "127.0.0.1",
  224. "port", "6383",
  225. "runid", "1c0bf1291797fbc5608c07a17da394147dc62817",
  226. "flags", "slave",
  227. "master-host", "127.0.0.1",
  228. "master-port", "6381"
  229. ),
  230. )
  231. ));
  232. $sentinel2 = $this->getMockSentinelConnection('tcp://127.0.0.1:5382?alias=sentinel2');
  233. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  234. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  235. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  236. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  237. $replication->querySentinel();
  238. // TODO: sorry for the smell...
  239. $reflection = new \ReflectionProperty($replication, 'sentinels');
  240. $reflection->setAccessible(true);
  241. $sentinels = array(
  242. array('host' => '127.0.0.1', 'port' => '5381'),
  243. array('host' => '127.0.0.1', 'port' => '5382'),
  244. );
  245. $this->assertSame($sentinel1, $replication->getSentinelConnection());
  246. $this->assertSame($sentinels, array_intersect_key($sentinels, $reflection->getValue($replication)));
  247. $master = $replication->getMaster();
  248. $slaves = $replication->getSlaves();
  249. $this->assertSame('127.0.0.1:6381', (string) $master);
  250. $this->assertCount(2, $slaves);
  251. $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
  252. $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
  253. }
  254. /**
  255. * @group disconnected
  256. */
  257. public function testMethodGetMasterAsksSentinelForMasterOnMasterNotSet()
  258. {
  259. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  260. $sentinel1->expects($this->at(0))
  261. ->method('executeCommand')
  262. ->with($this->isRedisCommand(
  263. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  264. ))
  265. ->will($this->returnValue(
  266. array('127.0.0.1', '6381')
  267. ));
  268. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  269. $this->assertSame('127.0.0.1:6381', (string) $replication->getMaster());
  270. }
  271. /**
  272. * @group disconnected
  273. * @expectedException Predis\ClientException
  274. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  275. */
  276. public function testMethodGetMasterThrowsExceptionOnNoAvailableSentinels()
  277. {
  278. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  279. $sentinel1->expects($this->any())
  280. ->method('executeCommand')
  281. ->with($this->isRedisCommand(
  282. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  283. ))
  284. ->will($this->throwException(
  285. new Connection\ConnectionException($sentinel1, "Unknown connection error [127.0.0.1:5381]")
  286. ));
  287. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  288. $replication->getMaster();
  289. }
  290. /**
  291. * @group disconnected
  292. */
  293. public function testMethodGetSlavesOnEmptySlavePoolAsksSentinelForSlaves()
  294. {
  295. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  296. $sentinel1->expects($this->at(0))
  297. ->method('executeCommand')
  298. ->with($this->isRedisCommand(
  299. 'SENTINEL', array('slaves', 'svc')
  300. ))
  301. ->will($this->returnValue(
  302. array(
  303. array(
  304. "name", "127.0.0.1:6382",
  305. "ip", "127.0.0.1",
  306. "port", "6382",
  307. "runid", "112cdebd22924a7d962be496f3a1c4c7c9bad93f",
  308. "flags", "slave",
  309. "master-host", "127.0.0.1",
  310. "master-port", "6381"
  311. ),
  312. array(
  313. "name", "127.0.0.1:6383",
  314. "ip", "127.0.0.1",
  315. "port", "6383",
  316. "runid", "1c0bf1291797fbc5608c07a17da394147dc62817",
  317. "flags", "slave",
  318. "master-host", "127.0.0.1",
  319. "master-port", "6381"
  320. ),
  321. )
  322. ));
  323. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  324. $slaves = $replication->getSlaves();
  325. $this->assertSame('127.0.0.1:6382', (string) $slaves[0]);
  326. $this->assertSame('127.0.0.1:6383', (string) $slaves[1]);
  327. }
  328. /**
  329. * @group disconnected
  330. * @expectedException Predis\ClientException
  331. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  332. */
  333. public function testMethodGetSlavesThrowsExceptionOnNoAvailableSentinels()
  334. {
  335. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  336. $sentinel1->expects($this->any())
  337. ->method('executeCommand')
  338. ->with($this->isRedisCommand(
  339. 'SENTINEL', array('slaves', 'svc')
  340. ))
  341. ->will($this->throwException(
  342. new Connection\ConnectionException($sentinel1, "Unknown connection error [127.0.0.1:5381]")
  343. ));
  344. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  345. $replication->getSlaves();
  346. }
  347. /**
  348. * @group disconnected
  349. * @expectedException Predis\ClientException
  350. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  351. */
  352. public function testMethodConnectThrowsExceptionOnConnectWithEmptySentinelsPool()
  353. {
  354. $replication = $this->getReplicationConnection('svc', array());
  355. $replication->connect();
  356. }
  357. /**
  358. * @group disconnected
  359. */
  360. public function testMethodConnectForcesConnectionToSlave()
  361. {
  362. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  363. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  364. $master->expects($this->never())
  365. ->method('connect');
  366. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  367. $slave1->expects($this->once())
  368. ->method('connect');
  369. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  370. $replication->add($master);
  371. $replication->add($slave1);
  372. $replication->connect();
  373. }
  374. /**
  375. * @group disconnected
  376. */
  377. public function testMethodConnectOnEmptySlavePoolAsksSentinelForSlavesAndForcesConnectionToSlave()
  378. {
  379. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  380. $sentinel1->expects($this->any())
  381. ->method('executeCommand')
  382. ->with($this->isRedisCommand(
  383. 'SENTINEL', array('slaves', 'svc')
  384. ))
  385. ->will($this->returnValue(
  386. array(
  387. array(
  388. "name", "127.0.0.1:6382",
  389. "ip", "127.0.0.1",
  390. "port", "6382",
  391. "runid", "112cdebd22924a7d962be496f3a1c4c7c9bad93f",
  392. "flags", "slave",
  393. "master-host", "127.0.0.1",
  394. "master-port", "6381",
  395. ),
  396. )
  397. ));
  398. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  399. $master->expects($this->never())
  400. ->method('connect');
  401. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  402. $slave1->expects($this->once())
  403. ->method('connect');
  404. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  405. $factory->expects($this->once())
  406. ->method('create')
  407. ->with(array(
  408. 'host' => '127.0.0.1',
  409. 'port' => '6382',
  410. 'alias' => 'slave-127.0.0.1:6382',
  411. ))
  412. ->will($this->returnValue($slave1));
  413. $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
  414. $replication->add($master);
  415. $replication->connect();
  416. }
  417. /**
  418. * @group disconnected
  419. */
  420. public function testMethodDisconnectForcesDisconnectionOnAllConnectionsInPool()
  421. {
  422. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  423. $sentinel1->expects($this->never())->method('disconnect');
  424. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  425. $master->expects($this->once())->method('disconnect');
  426. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  427. $slave1->expects($this->once())->method('disconnect');
  428. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  429. $slave2->expects($this->once())->method('disconnect');
  430. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  431. $replication->add($master);
  432. $replication->add($slave1);
  433. $replication->add($slave2);
  434. $replication->disconnect();
  435. }
  436. /**
  437. * @group disconnected
  438. */
  439. public function testMethodIsConnectedReturnConnectionStatusOfCurrentConnection()
  440. {
  441. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  442. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  443. $slave1->expects($this->exactly(2))
  444. ->method('isConnected')
  445. ->will($this->onConsecutiveCalls(true, false));
  446. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  447. $replication->add($slave1);
  448. $this->assertFalse($replication->isConnected());
  449. $replication->connect();
  450. $this->assertTrue($replication->isConnected());
  451. $replication->getConnectionById('slave1')->disconnect();
  452. $this->assertFalse($replication->isConnected());
  453. }
  454. /**
  455. * @group disconnected
  456. */
  457. public function testMethodGetConnectionByIdReturnsConnectionWhenFound()
  458. {
  459. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  460. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  461. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  462. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  463. $replication->add($master);
  464. $replication->add($slave1);
  465. $this->assertSame($master, $replication->getConnectionById('master'));
  466. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  467. $this->assertNull($replication->getConnectionById('unknown'));
  468. }
  469. /**
  470. * @group disconnected
  471. */
  472. public function testMethodSwitchToSelectsCurrentConnectionByConnectionAlias()
  473. {
  474. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  475. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  476. $master->expects($this->once())->method('connect');
  477. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  478. $slave1->expects($this->never())->method('connect');
  479. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave2');
  480. $slave2->expects($this->once())->method('connect');
  481. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  482. $replication->add($master);
  483. $replication->add($slave1);
  484. $replication->add($slave2);
  485. $replication->switchTo('master');
  486. $this->assertSame($master, $replication->getCurrent());
  487. $replication->switchTo('slave2');
  488. $this->assertSame($slave2, $replication->getCurrent());
  489. }
  490. /**
  491. * @group disconnected
  492. * @expectedException InvalidArgumentException
  493. * @expectedExceptionMessage Invalid connection or connection not found.
  494. */
  495. public function testMethodSwitchToThrowsExceptionOnConnectionNotFound()
  496. {
  497. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  498. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  499. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  500. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  501. $replication->add($master);
  502. $replication->add($slave1);
  503. $replication->switchTo('unknown');
  504. }
  505. /**
  506. * @group disconnected
  507. */
  508. public function testMethodSwitchToMasterSelectsCurrentConnectionToMaster()
  509. {
  510. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  511. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  512. $master->expects($this->once())->method('connect');
  513. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  514. $slave1->expects($this->never())->method('connect');
  515. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  516. $replication->add($master);
  517. $replication->add($slave1);
  518. $replication->switchToMaster();
  519. $this->assertSame($master, $replication->getCurrent());
  520. }
  521. /**
  522. * @group disconnected
  523. */
  524. public function testMethodSwitchToSlaveSelectsCurrentConnectionToRandomSlave()
  525. {
  526. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  527. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  528. $master->expects($this->never())->method('connect');
  529. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  530. $slave1->expects($this->once())->method('connect');
  531. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  532. $replication->add($master);
  533. $replication->add($slave1);
  534. $replication->switchToSlave();
  535. $this->assertSame($slave1, $replication->getCurrent());
  536. }
  537. /**
  538. * @group disconnected
  539. */
  540. public function testGetConnectionReturnsMasterForWriteCommands()
  541. {
  542. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  543. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  544. $master->expects($this->exactly(2))
  545. ->method('isConnected')
  546. ->will($this->onConsecutiveCalls(false, true));
  547. $master->expects($this->at(2))
  548. ->method('executeCommand')
  549. ->with($this->isRedisCommand('ROLE'))
  550. ->will($this->returnValue(array(
  551. "master", 3129659, array( array("127.0.0.1", 6382, 3129242) ),
  552. )));
  553. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  554. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  555. $replication->add($master);
  556. $replication->add($slave1);
  557. $this->assertSame($master, $replication->getConnection(
  558. Command\RawCommand::create('set', 'key', 'value')
  559. ));
  560. $this->assertSame($master, $replication->getConnection(
  561. Command\RawCommand::create('del', 'key')
  562. ));
  563. }
  564. /**
  565. * @group disconnected
  566. */
  567. public function testGetConnectionReturnsSlaveForReadOnlyCommands()
  568. {
  569. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  570. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  571. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  572. $slave1->expects($this->exactly(2))
  573. ->method('isConnected')
  574. ->will($this->onConsecutiveCalls(false, true));
  575. $slave1->expects($this->at(2))
  576. ->method('executeCommand')
  577. ->with($this->isRedisCommand('ROLE'))
  578. ->will($this->returnValue(array(
  579. "slave", "127.0.0.1", 9000, "connected", 3167038
  580. )));
  581. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  582. $replication->add($master);
  583. $replication->add($slave1);
  584. $this->assertSame($slave1, $replication->getConnection(
  585. Command\RawCommand::create('get', 'key')
  586. ));
  587. $this->assertSame($slave1, $replication->getConnection(
  588. Command\RawCommand::create('exists', 'key')
  589. ));
  590. }
  591. /**
  592. * @group disconnected
  593. */
  594. public function testGetConnectionSwitchesToMasterAfterWriteCommand()
  595. {
  596. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  597. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  598. $master->expects($this->exactly(2))
  599. ->method('isConnected')
  600. ->will($this->onConsecutiveCalls(false, true));
  601. $master->expects($this->at(2))
  602. ->method('executeCommand')
  603. ->with($this->isRedisCommand('ROLE'))
  604. ->will($this->returnValue(array(
  605. "master", 3129659, array( array("127.0.0.1", 6382, 3129242) ),
  606. )));
  607. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  608. $slave1->expects($this->exactly(1))
  609. ->method('isConnected')
  610. ->will($this->onConsecutiveCalls(false));
  611. $slave1->expects($this->at(2))
  612. ->method('executeCommand')
  613. ->with($this->isRedisCommand('ROLE'))
  614. ->will($this->returnValue(array(
  615. "slave", "127.0.0.1", 9000, "connected", 3167038
  616. )));
  617. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  618. $replication->add($master);
  619. $replication->add($slave1);
  620. $this->assertSame($slave1, $replication->getConnection(
  621. Command\RawCommand::create('exists', 'key')
  622. ));
  623. $this->assertSame($master, $replication->getConnection(
  624. Command\RawCommand::create('set', 'key', 'value')
  625. ));
  626. $this->assertSame($master, $replication->getConnection(
  627. Command\RawCommand::create('get', 'key')
  628. ));
  629. }
  630. /**
  631. * @group disconnected
  632. * @expectedException Predis\Replication\RoleException
  633. * @expectedExceptionMessage Expected master but got slave [127.0.0.1:6381]
  634. */
  635. public function testGetConnectionThrowsExceptionOnNodeRoleMismatch()
  636. {
  637. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  638. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  639. $master->expects($this->once())
  640. ->method('isConnected')
  641. ->will($this->returnValue(false));
  642. $master->expects($this->at(2))
  643. ->method('executeCommand')
  644. ->with($this->isRedisCommand('ROLE'))
  645. ->will($this->returnValue(array(
  646. "slave", "127.0.0.1", 9000, "connected", 3167038
  647. )));
  648. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  649. $replication->add($master);
  650. $replication->getConnection(Command\RawCommand::create('del', 'key'));
  651. }
  652. /**
  653. * @group disconnected
  654. */
  655. public function testMethodExecuteCommandSendsCommandToNodeAndReturnsResponse()
  656. {
  657. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  658. $cmdGet = Command\RawCommand::create('get', 'key');
  659. $cmdGetResponse = 'value';
  660. $cmdSet = Command\RawCommand::create('set', 'key', 'value');
  661. $cmdSetResponse = Response\Status::get('OK');
  662. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  663. $master->expects($this->any())
  664. ->method('isConnected')
  665. ->will($this->returnValue(true));
  666. $master->expects($this->at(2))
  667. ->method('executeCommand')
  668. ->with($this->isRedisCommand('SET', array('key', $cmdGetResponse)))
  669. ->will($this->returnValue($cmdSetResponse));
  670. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  671. $slave1->expects($this->any())
  672. ->method('isConnected')
  673. ->will($this->returnValue(true));
  674. $slave1->expects($this->at(2))
  675. ->method('executeCommand')
  676. ->with($this->isRedisCommand('GET', array('key')))
  677. ->will($this->returnValue($cmdGetResponse));
  678. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  679. $replication->add($master);
  680. $replication->add($slave1);
  681. $this->assertSame($cmdGetResponse, $replication->executeCommand($cmdGet));
  682. $this->assertSame($cmdSetResponse, $replication->executeCommand($cmdSet));
  683. }
  684. /**
  685. * @group disconnected
  686. */
  687. public function testMethodExecuteCommandRetriesReadOnlyCommandOnNextSlaveOnFailure()
  688. {
  689. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  690. $sentinel1->expects($this->any())
  691. ->method('executeCommand')
  692. ->with($this->isRedisCommand(
  693. 'SENTINEL', array('slaves', 'svc')
  694. ))
  695. ->will($this->returnValue(
  696. array(
  697. array(
  698. "name", "127.0.0.1:6383",
  699. "ip", "127.0.0.1",
  700. "port", "6383",
  701. "runid", "1c0bf1291797fbc5608c07a17da394147dc62817",
  702. "flags", "slave",
  703. "master-host", "127.0.0.1",
  704. "master-port", "6381"
  705. ),
  706. )
  707. ));
  708. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  709. $master->expects($this->any())
  710. ->method('isConnected')
  711. ->will($this->returnValue(true));
  712. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  713. $slave1->expects($this->any())
  714. ->method('isConnected')
  715. ->will($this->returnValue(true));
  716. $slave1->expects($this->at(2))
  717. ->method('executeCommand')
  718. ->with($this->isRedisCommand('GET', array('key')))
  719. ->will($this->throwException(
  720. new Connection\ConnectionException($slave1, "Unknown connection error [127.0.0.1:6382]")
  721. ));
  722. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  723. $slave2->expects($this->any())
  724. ->method('isConnected')
  725. ->will($this->returnValue(true));
  726. $slave2->expects($this->at(2))
  727. ->method('executeCommand')
  728. ->with($this->isRedisCommand('GET', array('key')))
  729. ->will($this->returnValue('value'));
  730. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  731. $factory->expects($this->once())
  732. ->method('create')
  733. ->with(array(
  734. 'host' => '127.0.0.1',
  735. 'port' => '6383',
  736. 'alias' => 'slave-127.0.0.1:6383',
  737. ))
  738. ->will($this->returnValue($slave2));
  739. $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
  740. $replication->add($master);
  741. $replication->add($slave1);
  742. $this->assertSame('value', $replication->executeCommand(
  743. Command\RawCommand::create('get', 'key')
  744. ));
  745. }
  746. /**
  747. * @group disconnected
  748. */
  749. public function testMethodExecuteCommandRetriesWriteCommandOnNewMasterOnFailure()
  750. {
  751. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  752. $sentinel1->expects($this->any())
  753. ->method('executeCommand')
  754. ->with($this->isRedisCommand(
  755. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  756. ))
  757. ->will($this->returnValue(
  758. array('127.0.0.1', '6391')
  759. ));
  760. $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  761. $masterOld->expects($this->any())
  762. ->method('isConnected')
  763. ->will($this->returnValue(true));
  764. $masterOld->expects($this->at(2))
  765. ->method('executeCommand')
  766. ->with($this->isRedisCommand('DEL', array('key')))
  767. ->will($this->throwException(
  768. new Connection\ConnectionException($masterOld, "Unknown connection error [127.0.0.1:6381]")
  769. ));
  770. $masterNew = $this->getMockConnection('tcp://127.0.0.1:6391?alias=master');
  771. $masterNew->expects($this->any())
  772. ->method('isConnected')
  773. ->will($this->returnValue(true));
  774. $masterNew->expects($this->at(2))
  775. ->method('executeCommand')
  776. ->with($this->isRedisCommand('DEL', array('key')))
  777. ->will($this->returnValue(1));
  778. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  779. $factory->expects($this->once())
  780. ->method('create')
  781. ->with(array(
  782. 'host' => '127.0.0.1',
  783. 'port' => '6391',
  784. 'alias' => 'master',
  785. ))
  786. ->will($this->returnValue($masterNew));
  787. $replication = $this->getReplicationConnection('svc', array($sentinel1), $factory);
  788. $replication->add($masterOld);
  789. $this->assertSame(1, $replication->executeCommand(
  790. Command\RawCommand::create('del', 'key')
  791. ));
  792. }
  793. /**
  794. * @group disconnected
  795. * @expectedException Predis\Response\ServerException
  796. * @expectedExceptionMessage ERR No such master with that name
  797. */
  798. public function testMethodExecuteCommandThrowsExceptionOnUnknownServiceName()
  799. {
  800. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  801. $sentinel1->expects($this->any())
  802. ->method('executeCommand')
  803. ->with($this->isRedisCommand(
  804. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  805. ))
  806. ->will($this->returnValue(null));
  807. $masterOld = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  808. $masterOld->expects($this->any())
  809. ->method('isConnected')
  810. ->will($this->returnValue(true));
  811. $masterOld->expects($this->at(2))
  812. ->method('executeCommand')
  813. ->with($this->isRedisCommand('DEL', array('key')))
  814. ->will($this->throwException(
  815. new Connection\ConnectionException($masterOld, "Unknown connection error [127.0.0.1:6381]")
  816. ));
  817. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  818. $replication->add($masterOld);
  819. $replication->executeCommand(
  820. Command\RawCommand::create('del', 'key')
  821. );
  822. }
  823. /**
  824. * @group disconnected
  825. * @expectedException Predis\ClientException
  826. * @expectedExceptionMessage No sentinel server available for autodiscovery.
  827. */
  828. public function testMethodExecuteCommandThrowsExceptionOnConnectionFailureAndNoAvailableSentinels()
  829. {
  830. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  831. $sentinel1->expects($this->any())
  832. ->method('executeCommand')
  833. ->with($this->isRedisCommand(
  834. 'SENTINEL', array('get-master-addr-by-name', 'svc')
  835. ))
  836. ->will($this->throwException(
  837. new Connection\ConnectionException($sentinel1, "Unknown connection error [127.0.0.1:5381]")
  838. ));
  839. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  840. $master->expects($this->any())
  841. ->method('isConnected')
  842. ->will($this->returnValue(true));
  843. $master->expects($this->at(2))
  844. ->method('executeCommand')
  845. ->with($this->isRedisCommand('DEL', array('key')))
  846. ->will($this->throwException(
  847. new Connection\ConnectionException($master, "Unknown connection error [127.0.0.1:6381]")
  848. ));
  849. $replication = $this->getReplicationConnection('svc', array($sentinel1));
  850. $replication->add($master);
  851. $replication->executeCommand(
  852. Command\RawCommand::create('del', 'key')
  853. );
  854. }
  855. /**
  856. * @group disconnected
  857. */
  858. public function testMethodGetReplicationStrategyReturnsInstance()
  859. {
  860. $strategy = new Replication\ReplicationStrategy();
  861. $factory = new Connection\Factory();
  862. $replication = new SentinelReplication(
  863. 'svc', array('tcp://127.0.0.1:5381?alias=sentinel1'), $factory, $strategy
  864. );
  865. $this->assertSame($strategy, $replication->getReplicationStrategy());
  866. }
  867. /**
  868. * @group disconnected
  869. */
  870. public function testMethodSerializeCanSerializeWholeObject()
  871. {
  872. $sentinel1 = $this->getMockSentinelConnection('tcp://127.0.0.1:5381?alias=sentinel1');
  873. $master = $this->getMockConnection('tcp://127.0.0.1:6381?alias=master');
  874. $slave1 = $this->getMockConnection('tcp://127.0.0.1:6382?alias=slave1');
  875. $slave2 = $this->getMockConnection('tcp://127.0.0.1:6383?alias=slave2');
  876. $strategy = new Replication\ReplicationStrategy();
  877. $factory = new Connection\Factory();
  878. $replication = new SentinelReplication('svc', array($sentinel1), $factory, $strategy);
  879. $replication->add($master);
  880. $replication->add($slave1);
  881. $replication->add($slave2);
  882. $unserialized = unserialize(serialize($replication));
  883. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  884. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  885. $this->assertEquals($master, $unserialized->getConnectionById('slave2'));
  886. $this->assertEquals($strategy, $unserialized->getReplicationStrategy());
  887. }
  888. // ******************************************************************** //
  889. // ---- HELPER METHODS ------------------------------------------------ //
  890. // ******************************************************************** //
  891. /**
  892. * Creates a new instance of replication connection.
  893. *
  894. * @param string $service Name of the service
  895. * @param array $sentinels Array of sentinels
  896. * @param ConnectionFactoryInterface|null $factory Optional connection factory instance.
  897. *
  898. * @return SentinelReplication
  899. */
  900. protected function getReplicationConnection($service, $sentinels, Connection\FactoryInterface $factory = null)
  901. {
  902. $factory = $factory ?: new Connection\Factory();
  903. $replication = new SentinelReplication($service, $sentinels, $factory);
  904. $replication->setRetryWait(0);
  905. return $replication;
  906. }
  907. /**
  908. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  909. *
  910. * @param mixed $parameters Optional parameters.
  911. *
  912. * @return mixed
  913. */
  914. protected function getMockSentinelConnection($parameters = null)
  915. {
  916. $connection = $this->getMockConnection($parameters);
  917. return $connection;
  918. }
  919. /**
  920. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  921. *
  922. * @param mixed $parameters Optional parameters.
  923. *
  924. * @return mixed
  925. */
  926. protected function getMockConnection($parameters = null)
  927. {
  928. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  929. if ($parameters) {
  930. $parameters = Connection\Parameters::create($parameters);
  931. $hash = "{$parameters->host}:{$parameters->port}";
  932. $connection->expects($this->any())
  933. ->method('getParameters')
  934. ->will($this->returnValue($parameters));
  935. $connection->expects($this->any())
  936. ->method('__toString')
  937. ->will($this->returnValue($hash));
  938. }
  939. return $connection;
  940. }
  941. }