MasterSlaveReplicationTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 testSwitchesFromSlaveToMasterOnWriteRequestss()
  210. {
  211. $profile = Profile\Factory::getDefault();
  212. $master = $this->getMockConnection('tcp://host1?alias=master');
  213. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  214. $replication = new MasterSlaveReplication();
  215. $replication->add($master);
  216. $replication->add($slave1);
  217. $cmd = $profile->createCommand('exists', array('foo'));
  218. $this->assertSame($slave1, $replication->getConnection($cmd));
  219. $cmd = $profile->createCommand('set', array('foo', 'bar'));
  220. $this->assertSame($master, $replication->getConnection($cmd));
  221. $cmd = $profile->createCommand('exists', array('foo'));
  222. $this->assertSame($master, $replication->getConnection($cmd));
  223. }
  224. /**
  225. * @group disconnected
  226. */
  227. public function testWritesCommandToCorrectConnection()
  228. {
  229. $profile = Profile\Factory::getDefault();
  230. $cmdExists = $profile->createCommand('exists', array('foo'));
  231. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  232. $master = $this->getMockConnection('tcp://host1?alias=master');
  233. $master->expects($this->once())->method('writeRequest')->with($cmdSet);
  234. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  235. $slave1->expects($this->once())->method('writeRequest')->with($cmdExists);
  236. $replication = new MasterSlaveReplication();
  237. $replication->add($master);
  238. $replication->add($slave1);
  239. $replication->writeRequest($cmdExists);
  240. $replication->writeRequest($cmdSet);
  241. }
  242. /**
  243. * @group disconnected
  244. */
  245. public function testReadsCommandFromCorrectConnection()
  246. {
  247. $profile = Profile\Factory::getDefault();
  248. $cmdExists = $profile->createCommand('exists', array('foo'));
  249. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  250. $master = $this->getMockConnection('tcp://host1?alias=master');
  251. $master->expects($this->once())->method('readResponse')->with($cmdSet);
  252. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  253. $slave1->expects($this->once())->method('readResponse')->with($cmdExists);
  254. $replication = new MasterSlaveReplication();
  255. $replication->add($master);
  256. $replication->add($slave1);
  257. $replication->readResponse($cmdExists);
  258. $replication->readResponse($cmdSet);
  259. }
  260. /**
  261. * @group disconnected
  262. */
  263. public function testExecutesCommandOnCorrectConnection()
  264. {
  265. $profile = Profile\Factory::getDefault();
  266. $cmdExists = $profile->createCommand('exists', array('foo'));
  267. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  268. $master = $this->getMockConnection('tcp://host1?alias=master');
  269. $master->expects($this->once())->method('executeCommand')->with($cmdSet);
  270. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  271. $slave1->expects($this->once())->method('executeCommand')->with($cmdExists);
  272. $replication = new MasterSlaveReplication();
  273. $replication->add($master);
  274. $replication->add($slave1);
  275. $replication->executeCommand($cmdExists);
  276. $replication->executeCommand($cmdSet);
  277. }
  278. /**
  279. * @group disconnected
  280. */
  281. public function testWatchTriggersSwitchToMasterConnection()
  282. {
  283. $profile = Profile\Factory::getDefault();
  284. $cmdWatch = $profile->createCommand('watch', array('foo'));
  285. $master = $this->getMockConnection('tcp://host1?alias=master');
  286. $master->expects($this->once())->method('executeCommand')->with($cmdWatch);
  287. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  288. $slave1->expects($this->never())->method('executeCommand');
  289. $replication = new MasterSlaveReplication();
  290. $replication->add($master);
  291. $replication->add($slave1);
  292. $replication->executeCommand($cmdWatch);
  293. }
  294. /**
  295. * @group disconnected
  296. */
  297. public function testMultiTriggersSwitchToMasterConnection()
  298. {
  299. $profile = Profile\Factory::getDefault();
  300. $cmdMulti = $profile->createCommand('multi');
  301. $master = $this->getMockConnection('tcp://host1?alias=master');
  302. $master->expects($this->once())->method('executeCommand')->with($cmdMulti);
  303. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  304. $slave1->expects($this->never())->method('executeCommand');
  305. $replication = new MasterSlaveReplication();
  306. $replication->add($master);
  307. $replication->add($slave1);
  308. $replication->executeCommand($cmdMulti);
  309. }
  310. /**
  311. * @group disconnected
  312. */
  313. public function testEvalTriggersSwitchToMasterConnection()
  314. {
  315. $profile = Profile\Factory::get('dev');
  316. $cmdEval = $profile->createCommand('eval', array("return redis.call('info')"));
  317. $master = $this->getMockConnection('tcp://host1?alias=master');
  318. $master->expects($this->once())->method('executeCommand')->with($cmdEval);
  319. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  320. $slave1->expects($this->never())->method('executeCommand');
  321. $replication = new MasterSlaveReplication();
  322. $replication->add($master);
  323. $replication->add($slave1);
  324. $replication->executeCommand($cmdEval);
  325. }
  326. /**
  327. * @group disconnected
  328. */
  329. public function testSortTriggersSwitchToMasterConnectionOnStoreModifier()
  330. {
  331. $profile = Profile\Factory::get('dev');
  332. $cmdSortNormal = $profile->createCommand('sort', array('key'));
  333. $cmdSortStore = $profile->createCommand('sort', array('key', array('store' => 'key:store')));
  334. $master = $this->getMockConnection('tcp://host1?alias=master');
  335. $master->expects($this->once())->method('executeCommand')->with($cmdSortStore);
  336. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  337. $slave1->expects($this->once())->method('executeCommand')->with($cmdSortNormal);
  338. $replication = new MasterSlaveReplication();
  339. $replication->add($master);
  340. $replication->add($slave1);
  341. $replication->executeCommand($cmdSortNormal);
  342. $replication->executeCommand($cmdSortStore);
  343. }
  344. /**
  345. * @group disconnected
  346. * @expectedException \Predis\NotSupportedException
  347. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  348. */
  349. public function testThrowsExceptionOnNonSupportedCommand()
  350. {
  351. $cmd = Profile\Factory::getDefault()->createCommand('info');
  352. $replication = new MasterSlaveReplication();
  353. $replication->add($this->getMockConnection('tcp://host1?alias=master'));
  354. $replication->add($this->getMockConnection('tcp://host2?alias=slave1'));
  355. $replication->getConnection($cmd);
  356. }
  357. /**
  358. * @group disconnected
  359. */
  360. public function testCanOverrideReadOnlyFlagForCommands()
  361. {
  362. $profile = Profile\Factory::getDefault();
  363. $cmdSet = $profile->createCommand('set', array('foo', 'bar'));
  364. $cmdGet = $profile->createCommand('get', array('foo'));
  365. $master = $this->getMockConnection('tcp://host1?alias=master');
  366. $master->expects($this->once())->method('executeCommand')->with($cmdGet);
  367. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  368. $slave1->expects($this->once())->method('executeCommand')->with($cmdSet);
  369. $replication = new MasterSlaveReplication();
  370. $replication->add($master);
  371. $replication->add($slave1);
  372. $replication->getReplicationStrategy()->setCommandReadOnly($cmdSet->getId(), true);
  373. $replication->getReplicationStrategy()->setCommandReadOnly($cmdGet->getId(), false);
  374. $replication->executeCommand($cmdSet);
  375. $replication->executeCommand($cmdGet);
  376. }
  377. /**
  378. * @group disconnected
  379. */
  380. public function testAcceptsCallableToOverrideReadOnlyFlagForCommands()
  381. {
  382. $profile = Profile\Factory::getDefault();
  383. $cmdExistsFoo = $profile->createCommand('exists', array('foo'));
  384. $cmdExistsBar = $profile->createCommand('exists', array('bar'));
  385. $master = $this->getMockConnection('tcp://host1?alias=master');
  386. $master->expects($this->once())->method('executeCommand')->with($cmdExistsBar);
  387. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  388. $slave1->expects($this->once())->method('executeCommand')->with($cmdExistsFoo);
  389. $replication = new MasterSlaveReplication();
  390. $replication->add($master);
  391. $replication->add($slave1);
  392. $replication->getReplicationStrategy()->setCommandReadOnly('exists', function ($cmd) {
  393. list($arg1) = $cmd->getArguments();
  394. return $arg1 === 'foo';
  395. });
  396. $replication->executeCommand($cmdExistsFoo);
  397. $replication->executeCommand($cmdExistsBar);
  398. }
  399. /**
  400. * @group disconnected
  401. */
  402. public function testCanSetReadOnlyFlagForEvalScripts()
  403. {
  404. $profile = Profile\Factory::get('dev');
  405. $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
  406. $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
  407. $master = $this->getMockConnection('tcp://host1?alias=master');
  408. $master->expects($this->never())->method('executeCommand');
  409. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  410. $slave1->expects($this->exactly(2))
  411. ->method('executeCommand')
  412. ->with($this->logicalOr($cmdEval, $cmdEvalSha));
  413. $replication = new MasterSlaveReplication();
  414. $replication->add($master);
  415. $replication->add($slave1);
  416. $replication->getReplicationStrategy()->setScriptReadOnly($script);
  417. $replication->executeCommand($cmdEval);
  418. $replication->executeCommand($cmdEvalSha);
  419. }
  420. /**
  421. * @group disconnected
  422. */
  423. public function testExposesReplicationStrategy()
  424. {
  425. $replication = new MasterSlaveReplication();
  426. $this->assertInstanceOf('Predis\Replication\ReplicationStrategy', $replication->getReplicationStrategy());
  427. $strategy = new ReplicationStrategy();
  428. $replication = new MasterSlaveReplication($strategy);
  429. $this->assertSame($strategy, $replication->getReplicationStrategy());
  430. }
  431. /**
  432. * @group disconnected
  433. */
  434. public function testCanBeSerialized()
  435. {
  436. $master = $this->getMockConnection('tcp://host1?alias=master');
  437. $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
  438. $replication = new MasterSlaveReplication();
  439. $replication->add($master);
  440. $replication->add($slave1);
  441. $unserialized = unserialize(serialize($replication));
  442. $this->assertEquals($master, $unserialized->getConnectionById('master'));
  443. $this->assertEquals($slave1, $unserialized->getConnectionById('slave1'));
  444. }
  445. // ******************************************************************** //
  446. // ---- HELPER METHODS ------------------------------------------------ //
  447. // ******************************************************************** //
  448. /**
  449. * Returns a base mocked connection from Predis\Connection\NodeConnectionInterface.
  450. *
  451. * @param mixed $parameters Optional parameters.
  452. *
  453. * @return mixed
  454. */
  455. protected function getMockConnection($parameters = null)
  456. {
  457. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  458. if ($parameters) {
  459. $parameters = Connection\Parameters::create($parameters);
  460. $hash = "{$parameters->host}:{$parameters->port}";
  461. $connection->expects($this->any())
  462. ->method('getParameters')
  463. ->will($this->returnValue($parameters));
  464. $connection->expects($this->any())
  465. ->method('__toString')
  466. ->will($this->returnValue($hash));
  467. }
  468. return $connection;
  469. }
  470. }