MasterSlaveReplicationTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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\Connection;
  12. use Predis\Profile;
  13. use Predis\Replication\ReplicationStrategy;
  14. use PredisTestCase;
  15. /**
  16. *
  17. */
  18. class MasterSlaveReplicationTest extends PredisTestCase
  19. {
  20. /**
  21. * @group disconnected
  22. */
  23. public function testAddingConnectionsToReplication()
  24. {
  25. $master = $this->getMockConnection('tcp://host1?alias=master');
  26. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  27. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  28. $replication = new MasterSlaveReplication();
  29. $replication->add($master);
  30. $replication->add($slave1);
  31. $replication->add($slave2);
  32. $this->assertSame($master, $replication->getConnectionById('master'));
  33. $this->assertSame($slave1, $replication->getConnectionById('slave1'));
  34. $this->assertSame($slave2, $replication->getConnectionById('slave2'));
  35. $this->assertSame($master, $replication->getMaster());
  36. $this->assertSame(array($slave1, $slave2), $replication->getSlaves());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testRemovingConnectionsFromReplication()
  42. {
  43. $master = $this->getMockConnection('tcp://host1?alias=master');
  44. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  45. $slave2 = $this->getMockConnection('tcp://host3?alias=slave2');
  46. $replication = new MasterSlaveReplication();
  47. $replication->add($master);
  48. $replication->add($slave1);
  49. $this->assertTrue($replication->remove($slave1));
  50. $this->assertFalse($replication->remove($slave2));
  51. $this->assertSame($master, $replication->getMaster());
  52. $this->assertSame(array(), $replication->getSlaves());
  53. }
  54. /**
  55. * @group disconnected
  56. * @expectedException \RuntimeException
  57. * @expectedExceptionMessage Replication needs one master and at least one slave.
  58. */
  59. public function testThrowsExceptionOnEmptyReplication()
  60. {
  61. $replication = new MasterSlaveReplication();
  62. $replication->connect();
  63. }
  64. /**
  65. * @group disconnected
  66. * @expectedException \RuntimeException
  67. * @expectedExceptionMessage Replication needs one master and at least one slave.
  68. */
  69. public function testThrowsExceptionOnMissingMaster()
  70. {
  71. $replication = new MasterSlaveReplication();
  72. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  73. $replication->connect();
  74. }
  75. /**
  76. * @group disconnected
  77. * @expectedException \RuntimeException
  78. * @expectedExceptionMessage Replication needs one master and at least one slave.
  79. */
  80. public function testThrowsExceptionOnMissingSlave()
  81. {
  82. $replication = new MasterSlaveReplication();
  83. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  84. $replication->connect();
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testConnectForcesConnectionToOneOfSlaves()
  90. {
  91. $master = $this->getMockConnection('tcp://host1?alias=master');
  92. $master->expects($this->never())->method('connect');
  93. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  94. $slave->expects($this->once())->method('connect');
  95. $replication = new MasterSlaveReplication();
  96. $replication->add($master);
  97. $replication->add($slave);
  98. $replication->connect();
  99. }
  100. /**
  101. * @group disconnected
  102. */
  103. public function testIsConnectedReturnsTrueIfAtLeastOneConnectionIsOpen()
  104. {
  105. $master = $this->getMockConnection('tcp://host1?alias=master');
  106. $master->expects($this->never())->method('isConnected')->will($this->returnValue(false));
  107. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  108. $slave->expects($this->once())->method('isConnected')->will($this->returnValue(true));
  109. $replication = new MasterSlaveReplication();
  110. $replication->add($master);
  111. $replication->add($slave);
  112. $replication->connect();
  113. $this->assertTrue($replication->isConnected());
  114. }
  115. /**
  116. * @group disconnected
  117. */
  118. public function testIsConnectedReturnsFalseIfAllConnectionsAreClosed()
  119. {
  120. $master = $this->getMockConnection('tcp://host1?alias=master');
  121. $master->expects($this->any())->method('isConnected')->will($this->returnValue(false));
  122. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  123. $slave->expects($this->any())->method('isConnected')->will($this->returnValue(false));
  124. $replication = new MasterSlaveReplication();
  125. $replication->add($master);
  126. $replication->add($slave);
  127. $this->assertFalse($replication->isConnected());
  128. $replication->connect();
  129. $replication->disconnect();
  130. $this->assertFalse($replication->isConnected());
  131. }
  132. /**
  133. * @group disconnected
  134. */
  135. public function testDisconnectForcesCurrentConnectionToDisconnect()
  136. {
  137. $master = $this->getMockConnection('tcp://host1?alias=master');
  138. $master->expects($this->once())->method('disconnect');
  139. $slave = $this->getMockConnection('tcp://host2?alias=slave1');
  140. $slave->expects($this->once())->method('disconnect');
  141. $replication = new MasterSlaveReplication();
  142. $replication->add($master);
  143. $replication->add($slave);
  144. $replication->disconnect();
  145. }
  146. /**
  147. * @group disconnected
  148. */
  149. public function testCanSwitchConnectionByAlias()
  150. {
  151. $master = $this->getMockConnection('tcp://host1?alias=master');
  152. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  153. $replication = new MasterSlaveReplication();
  154. $replication->add($master);
  155. $replication->add($slave1);
  156. $this->assertNull($replication->getCurrent());
  157. $replication->switchTo('master');
  158. $this->assertSame($master, $replication->getCurrent());
  159. $replication->switchTo('slave1');
  160. $this->assertSame($slave1, $replication->getCurrent());
  161. }
  162. /**
  163. * @group disconnected
  164. * @expectedException \InvalidArgumentException
  165. * @expectedExceptionMessage Invalid connection or connection not found.
  166. */
  167. public function testThrowsErrorWhenSwitchingToUnknownConnection()
  168. {
  169. $replication = new MasterSlaveReplication();
  170. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  171. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  172. $replication->switchTo('unknown');
  173. }
  174. /**
  175. * @group disconnected
  176. */
  177. public function testUsesSlavesOnReadOnlyCommands()
  178. {
  179. $profile = Profile\Factory::getDefault();
  180. $master = $this->getMockConnection('tcp://host1?alias=master');
  181. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  182. $replication = new MasterSlaveReplication();
  183. $replication->add($master);
  184. $replication->add($slave1);
  185. $cmd = $profile->createCommand('exists', array('foo'));
  186. $this->assertSame($slave1, $replication->getConnection($cmd));
  187. $cmd = $profile->createCommand('get', array('foo'));
  188. $this->assertSame($slave1, $replication->getConnection($cmd));
  189. }
  190. /**
  191. * @group disconnected
  192. */
  193. public function testUsesMasterOnWriteRequests()
  194. {
  195. $profile = Profile\Factory::getDefault();
  196. $master = $this->getMockConnection('tcp://host1?alias=master');
  197. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  198. $replication = new MasterSlaveReplication();
  199. $replication->add($master);
  200. $replication->add($slave1);
  201. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  202. $this->assertSame($master, $replication->getConnection($cmd));
  203. $cmd = $profile->createCommand('get', array('foo'));
  204. $this->assertSame($master, $replication->getConnection($cmd));
  205. }
  206. /**
  207. * @group disconnected
  208. */
  209. public function testUsesMasterOnReadRequestsWhenNoSlavesAvailable()
  210. {
  211. $profile = Profile\Factory::getDefault();
  212. $master = $this->getMockConnection('tcp://host1?alias=master');
  213. $replication = new MasterSlaveReplication();
  214. $replication->add($master);
  215. $cmd = $profile->createCommand('exists', array('foo'));
  216. $this->assertSame($master, $replication->getConnection($cmd));
  217. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  218. $this->assertSame($master, $replication->getConnection($cmd));
  219. }
  220. /**
  221. * @group disconnected
  222. */
  223. public function testSwitchesFromSlaveToMasterOnWriteRequestss()
  224. {
  225. $profile = Profile\Factory::getDefault();
  226. $master = $this->getMockConnection('tcp://host1?alias=master');
  227. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  228. $replication = new MasterSlaveReplication();
  229. $replication->add($master);
  230. $replication->add($slave1);
  231. $cmd = $profile->createCommand('exists', array('foo'));
  232. $this->assertSame($slave1, $replication->getConnection($cmd));
  233. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  234. $this->assertSame($master, $replication->getConnection($cmd));
  235. $cmd = $profile->createCommand('exists', array('foo'));
  236. $this->assertSame($master, $replication->getConnection($cmd));
  237. }
  238. /**
  239. * @group disconnected
  240. */
  241. public function testWritesCommandToCorrectConnection()
  242. {
  243. $profile = Profile\Factory::getDefault();
  244. $cmdExists = $profile->createCommand('exists', array('foo'));
  245. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  246. $master = $this->getMockConnection('tcp://host1?alias=master');
  247. $master->expects($this->once())->method('writeRequest')->with($cmdSet);
  248. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  249. $slave1->expects($this->once())->method('writeRequest')->with($cmdExists);
  250. $replication = new MasterSlaveReplication();
  251. $replication->add($master);
  252. $replication->add($slave1);
  253. $replication->writeRequest($cmdExists);
  254. $replication->writeRequest($cmdSet);
  255. }
  256. /**
  257. * @group disconnected
  258. */
  259. public function testReadsCommandFromCorrectConnection()
  260. {
  261. $profile = Profile\Factory::getDefault();
  262. $cmdExists = $profile->createCommand('exists', array('foo'));
  263. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  264. $master = $this->getMockConnection('tcp://host1?alias=master');
  265. $master->expects($this->once())->method('readResponse')->with($cmdSet);
  266. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  267. $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
  268. $replication = new MasterSlaveReplication();
  269. $replication->add($master);
  270. $replication->add($slave1);
  271. $replication->readResponse($cmdExists);
  272. $replication->readResponse($cmdSet);
  273. }
  274. /**
  275. * @group disconnected
  276. */
  277. public function testExecutesCommandOnCorrectConnection()
  278. {
  279. $profile = Profile\Factory::getDefault();
  280. $cmdExists = $profile->createCommand('exists', array('foo'));
  281. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  282. $master = $this->getMockConnection('tcp://host1?alias=master');
  283. $master->expects($this->once())->method('executeCommand')->with($cmdSet);
  284. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  285. $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
  286. $replication = new MasterSlaveReplication();
  287. $replication->add($master);
  288. $replication->add($slave1);
  289. $replication->executeCommand($cmdExists);
  290. $replication->executeCommand($cmdSet);
  291. }
  292. /**
  293. * @group disconnected
  294. */
  295. public function testWatchTriggersSwitchToMasterConnection()
  296. {
  297. $profile = Profile\Factory::getDefault();
  298. $cmdWatch = $profile->createCommand('watch', array('foo'));
  299. $master = $this->getMockConnection('tcp://host1?alias=master');
  300. $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
  301. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  302. $slave1->expects($this->never())->method('executeCommand');
  303. $replication = new MasterSlaveReplication();
  304. $replication->add($master);
  305. $replication->add($slave1);
  306. $replication->executeCommand($cmdWatch);
  307. }
  308. /**
  309. * @group disconnected
  310. */
  311. public function testMultiTriggersSwitchToMasterConnection()
  312. {
  313. $profile = Profile\Factory::getDefault();
  314. $cmdMulti = $profile->createCommand('multi');
  315. $master = $this->getMockConnection('tcp://host1?alias=master');
  316. $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
  317. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  318. $slave1->expects($this->never())->method('executeCommand');
  319. $replication = new MasterSlaveReplication();
  320. $replication->add($master);
  321. $replication->add($slave1);
  322. $replication->executeCommand($cmdMulti);
  323. }
  324. /**
  325. * @group disconnected
  326. */
  327. public function testEvalTriggersSwitchToMasterConnection()
  328. {
  329. $profile = Profile\Factory::get('dev');
  330. $cmdEval = $profile->createCommand('eval', array("return redis.call('info')"));
  331. $master = $this->getMockConnection('tcp://host1?alias=master');
  332. $master->expects($this->once())->method('executeCommand')->with($cmdEval);
  333. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  334. $slave1->expects($this->never())->method('executeCommand');
  335. $replication = new MasterSlaveReplication();
  336. $replication->add($master);
  337. $replication->add($slave1);
  338. $replication->executeCommand($cmdEval);
  339. }
  340. /**
  341. * @group disconnected
  342. */
  343. public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
  344. {
  345. $profile = Profile\Factory::get('dev');
  346. $cmdSortNormal = $profile->createCommand('sort', array('key'));
  347. $cmdSortStore = $profile->createCommand('sort', array('key', array('store' => 'key:store')));
  348. $master = $this->getMockConnection('tcp://host1?alias=master');
  349. $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
  350. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  351. $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
  352. $replication = new MasterSlaveReplication();
  353. $replication->add($master);
  354. $replication->add($slave1);
  355. $replication->executeCommand($cmdSortNormal);
  356. $replication->executeCommand($cmdSortStore);
  357. }
  358. /**
  359. * @group disconnected
  360. * @expectedException \Predis\NotSupportedException
  361. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  362. */
  363. public function testThrowsExceptionOnNonSupportedCommand()
  364. {
  365. $cmd = Profile\Factory::getDefault()->createCommand('info');
  366. $replication = new MasterSlaveReplication();
  367. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  368. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  369. $replication->getConnection($cmd);
  370. }
  371. /**
  372. * @group disconnected
  373. */
  374. public function testCanOverrideReadOnlyFlagForCommands()
  375. {
  376. $profile = Profile\Factory::getDefault();
  377. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  378. $cmdGet = $profile->createCommand('get', array('foo'));
  379. $master = $this->getMockConnection('tcp://host1?alias=master');
  380. $master->expects($this->once())->method('executeCommand')->with($cmdGet);
  381. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  382. $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
  383. $replication = new MasterSlaveReplication();
  384. $replication->add($master);
  385. $replication->add($slave1);
  386. $replication->getReplicationStrategy()->setCommandReadOnly($cmdSet->getId(), true);
  387. $replication->getReplicationStrategy()->setCommandReadOnly($cmdGet->getId(), false);
  388. $replication->executeCommand($cmdSet);
  389. $replication->executeCommand($cmdGet);
  390. }
  391. /**
  392. * @group disconnected
  393. */
  394. public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
  395. {
  396. $profile = Profile\Factory::getDefault();
  397. $cmdExistsFoo = $profile->createCommand('exists', array('foo'));
  398. $cmdExistsBar = $profile->createCommand('exists', array('bar'));
  399. $master = $this->getMockConnection('tcp://host1?alias=master');
  400. $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
  401. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  402. $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
  403. $replication = new MasterSlaveReplication();
  404. $replication->add($master);
  405. $replication->add($slave1);
  406. $replication->getReplicationStrategy()->setCommandReadOnly('exists', function ($cmd) {
  407. list($arg1) = $cmd->getArguments();
  408. return $arg1 === 'foo';
  409. });
  410. $replication->executeCommand($cmdExistsFoo);
  411. $replication->executeCommand($cmdExistsBar);
  412. }
  413. /**
  414. * @group disconnected
  415. */
  416. public function testCanSetReadOnlyFlagForEvalScripts()
  417. {
  418. $profile = Profile\Factory::get('dev');
  419. $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
  420. $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
  421. $master = $this->getMockConnection('tcp://host1?alias=master');
  422. $master->expects($this->never())->method('executeCommand');
  423. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  424. $slave1->expects($this->exactly(2))
  425. ->method('executeCommand')
  426. ->with($this->logicalOr($cmdEval, $cmdEvalSha));
  427. $replication = new MasterSlaveReplication();
  428. $replication->add($master);
  429. $replication->add($slave1);
  430. $replication->getReplicationStrategy()->setScriptReadOnly($script);
  431. $replication->executeCommand($cmdEval);
  432. $replication->executeCommand($cmdEvalSha);
  433. }
  434. /**
  435. * @group disconnected
  436. */
  437. public function testExposesReplicationStrategy()
  438. {
  439. $replication = new MasterSlaveReplication();
  440. $this->assertInstanceOf('Predis\Replication\ReplicationStrategy', $replication->getReplicationStrategy());
  441. $strategy = new ReplicationStrategy();
  442. $replication = new MasterSlaveReplication($strategy);
  443. $this->assertSame($strategy, $replication->getReplicationStrategy());
  444. }
  445. /**
  446. * @group disconnected
  447. */
  448. public function testCanBeSerialized()
  449. {
  450. $master = $this->getMockConnection('tcp://host1?alias=master');
  451. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  452. $replication = new MasterSlaveReplication();
  453. $replication->add($master);
  454. $replication->add($slave1);
  455. $unserialized = unserialize(serialize($replication));
  456. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  457. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  458. }
  459. // ******************************************************************** //
  460. // ---- HELPER METHODS ------------------------------------------------ //
  461. // ******************************************************************** //
  462. /**
  463. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  464. *
  465. * @param mixed $parameters Optional parameters.
  466. *
  467. * @return mixed
  468. */
  469. protected function getMockConnection($parameters = null)
  470. {
  471. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  472. if ($parameters) {
  473. $parameters = Connection\Parameters::create($parameters);
  474. $hash = "{$parameters->host}:{$parameters->port}";
  475. $connection->expects($this->any())
  476. ->method('getParameters')
  477. ->will($this->returnValue($parameters));
  478. $connection->expects($this->any())
  479. ->method('__toString')
  480. ->will($this->returnValue($hash));
  481. }
  482. return $connection;
  483. }
  484. }