SentinelReplication.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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\Replication;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Command\RawCommand;
  13. use Predis\CommunicationException;
  14. use Predis\Connection\ConnectionException;
  15. use Predis\Connection\FactoryInterface as ConnectionFactoryInterface;
  16. use Predis\Connection\NodeConnectionInterface;
  17. use Predis\Connection\Parameters;
  18. use Predis\Replication\ReplicationStrategy;
  19. use Predis\Replication\RoleException;
  20. use Predis\Response\ErrorInterface as ErrorResponseInterface;
  21. use Predis\Response\ServerException;
  22. /**
  23. * @author Daniele Alessandri <suppakilla@gmail.com>
  24. * @author Ville Mattila <ville@eventio.fi>
  25. */
  26. class SentinelReplication implements ReplicationInterface
  27. {
  28. /**
  29. * @var NodeConnectionInterface
  30. */
  31. protected $master;
  32. /**
  33. * @var NodeConnectionInterface[]
  34. */
  35. protected $slaves = array();
  36. /**
  37. * @var NodeConnectionInterface
  38. */
  39. protected $current;
  40. /**
  41. * @var string
  42. */
  43. protected $service;
  44. /**
  45. * @var ConnectionFactoryInterface
  46. */
  47. protected $connectionFactory;
  48. /**
  49. * @var ReplicationStrategy
  50. */
  51. protected $strategy;
  52. /**
  53. * @var NodeConnectionInterface[]
  54. */
  55. protected $sentinels = array();
  56. /**
  57. * @var NodeConnectionInterface
  58. */
  59. protected $sentinelConnection;
  60. /**
  61. * @var float
  62. */
  63. protected $sentinelTimeout = 0.100;
  64. /**
  65. * Max number of automatic retries of commands upon server failure.
  66. *
  67. * -1 = unlimited retry attempts
  68. * 0 = no retry attempts (fails immediatly)
  69. * n = fail only after n retry attempts
  70. *
  71. * @var int
  72. */
  73. protected $retryLimit = 20;
  74. /**
  75. * Time to wait in milliseconds before fetching a new configuration from one
  76. * of the sentinel servers.
  77. *
  78. * @var int
  79. */
  80. protected $retryWait = 1000;
  81. /**
  82. * Flag for automatic fetching of available sentinels.
  83. *
  84. * @var bool
  85. */
  86. protected $updateSentinels = false;
  87. /**
  88. * @param string $service Name of the service for autodiscovery.
  89. * @param array $sentinels Sentinel servers connection parameters.
  90. * @param ConnectionFactoryInterface $connectionFactory Connection factory instance.
  91. * @param ReplicationStrategy $strategy Replication strategy instance.
  92. */
  93. public function __construct(
  94. $service,
  95. array $sentinels,
  96. ConnectionFactoryInterface $connectionFactory,
  97. ReplicationStrategy $strategy = null
  98. ) {
  99. $this->sentinels = $sentinels;
  100. $this->service = $service;
  101. $this->connectionFactory = $connectionFactory;
  102. $this->strategy = $strategy ?: new ReplicationStrategy();
  103. }
  104. /**
  105. * Sets a default timeout for connections to sentinels.
  106. *
  107. * When "timeout" is present in the connection parameters of sentinels, its
  108. * value overrides the default sentinel timeout.
  109. *
  110. * @param float $timeout Timeout value.
  111. */
  112. public function setSentinelTimeout($timeout)
  113. {
  114. $this->sentinelTimeout = (float) $timeout;
  115. }
  116. /**
  117. * Sets the maximum number of retries for commands upon server failure.
  118. *
  119. * -1 = unlimited retry attempts
  120. * 0 = no retry attempts (fails immediatly)
  121. * n = fail only after n retry attempts
  122. *
  123. * @param int $retry Number of retry attempts.
  124. */
  125. public function setRetryLimit($retry)
  126. {
  127. $this->retryLimit = (int) $retry;
  128. }
  129. /**
  130. * Sets the time to wait (in seconds) before fetching a new configuration
  131. * from one of the sentinels.
  132. *
  133. * @param float $seconds Time to wait before the next attempt.
  134. */
  135. public function setRetryWait($seconds)
  136. {
  137. $this->retryWait = (float) $seconds;
  138. }
  139. /**
  140. * Set automatic fetching of available sentinels.
  141. *
  142. * @param bool $update Enable or disable automatic updates.
  143. */
  144. public function setUpdateSentinels($update)
  145. {
  146. $this->updateSentinels = (bool) $update;
  147. }
  148. /**
  149. * Resets the current connection.
  150. */
  151. protected function reset()
  152. {
  153. $this->current = null;
  154. }
  155. /**
  156. * Wipes the current list of master and slaves nodes.
  157. */
  158. protected function wipeServerList()
  159. {
  160. $this->reset();
  161. $this->master = null;
  162. $this->slaves = array();
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function add(NodeConnectionInterface $connection)
  168. {
  169. $alias = $connection->getParameters()->alias;
  170. if ($alias === 'master') {
  171. $this->master = $connection;
  172. } else {
  173. $this->slaves[$alias ?: count($this->slaves)] = $connection;
  174. }
  175. $this->reset();
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function remove(NodeConnectionInterface $connection)
  181. {
  182. if ($connection === $this->master) {
  183. $this->master = null;
  184. $this->reset();
  185. return true;
  186. }
  187. if (false !== $id = array_search($connection, $this->slaves, true)) {
  188. unset($this->slaves[$id]);
  189. $this->reset();
  190. return true;
  191. }
  192. return false;
  193. }
  194. /**
  195. * Creates a new connection to a sentinel server.
  196. *
  197. * @return NodeConnectionInterface
  198. */
  199. protected function createSentinelConnection($parameters)
  200. {
  201. if ($parameters instanceof NodeConnectionInterface) {
  202. return $parameters;
  203. }
  204. if (is_string($parameters)) {
  205. $parameters = Parameters::parse($parameters);
  206. }
  207. if (is_array($parameters)) {
  208. // We explicitly set "database" and "password" to null,
  209. // so that no AUTH and SELECT command is send to the sentinels.
  210. $parameters['database'] = null;
  211. $parameters['password'] = null;
  212. if (!isset($parameters['timeout'])) {
  213. $parameters['timeout'] = $this->sentinelTimeout;
  214. }
  215. }
  216. $connection = $this->connectionFactory->create($parameters);
  217. return $connection;
  218. }
  219. /**
  220. * Returns the current sentinel connection.
  221. *
  222. * If there is no active sentinel connection, a new connection is created.
  223. *
  224. * @return NodeConnectionInterface
  225. */
  226. public function getSentinelConnection()
  227. {
  228. if (!$this->sentinelConnection) {
  229. if (!$this->sentinels) {
  230. throw new \Predis\ClientException('No sentinel server available for autodiscovery.');
  231. }
  232. $sentinel = array_shift($this->sentinels);
  233. $this->sentinelConnection = $this->createSentinelConnection($sentinel);
  234. }
  235. return $this->sentinelConnection;
  236. }
  237. /**
  238. * Fetches an updated list of sentinels from a sentinel.
  239. */
  240. public function updateSentinels()
  241. {
  242. SENTINEL_QUERY: {
  243. $sentinel = $this->getSentinelConnection();
  244. try {
  245. $payload = $sentinel->executeCommand(
  246. RawCommand::create('SENTINEL', 'sentinels', $this->service)
  247. );
  248. $this->sentinels = array();
  249. // NOTE: sentinel server does not return itself, so we add it back.
  250. $this->sentinels[] = $sentinel->getParameters()->toArray();
  251. foreach ($payload as $sentinel) {
  252. $this->sentinels[] = array(
  253. 'host' => $sentinel[3],
  254. 'port' => $sentinel[5],
  255. );
  256. }
  257. } catch (ConnectionException $exception) {
  258. $this->sentinelConnection = null;
  259. goto SENTINEL_QUERY;
  260. }
  261. }
  262. }
  263. /**
  264. * Fetches the details for the master and slave servers from a sentinel.
  265. */
  266. public function querySentinel()
  267. {
  268. $this->wipeServerList();
  269. $this->updateSentinels();
  270. $this->getMaster();
  271. $this->getSlaves();
  272. }
  273. /**
  274. * Handles error responses returned by redis-sentinel.
  275. *
  276. * @param NodeConnectionInterface $sentinel Connection to a sentinel server.
  277. * @param ErrorResponseInterface $error Error response.
  278. */
  279. private function handleSentinelErrorResponse(NodeConnectionInterface $sentinel, ErrorResponseInterface $error)
  280. {
  281. if ($error->getErrorType() === 'IDONTKNOW') {
  282. throw new ConnectionException($sentinel, $error->getMessage());
  283. } else {
  284. throw new ServerException($error->getMessage());
  285. }
  286. }
  287. /**
  288. * Fetches the details for the master server from a sentinel.
  289. *
  290. * @param NodeConnectionInterface $sentinel Connection to a sentinel server.
  291. * @param string $service Name of the service.
  292. *
  293. * @return array
  294. */
  295. protected function querySentinelForMaster(NodeConnectionInterface $sentinel, $service)
  296. {
  297. $payload = $sentinel->executeCommand(
  298. RawCommand::create('SENTINEL', 'get-master-addr-by-name', $service)
  299. );
  300. if ($payload === null) {
  301. throw new ServerException('ERR No such master with that name');
  302. }
  303. if ($payload instanceof ErrorResponseInterface) {
  304. $this->handleSentinelErrorResponse($sentinel, $payload);
  305. }
  306. return array(
  307. 'host' => $payload[0],
  308. 'port' => $payload[1],
  309. 'alias' => 'master',
  310. );
  311. }
  312. /**
  313. * Fetches the details for the slave servers from a sentinel.
  314. *
  315. * @param NodeConnectionInterface $sentinel Connection to a sentinel server.
  316. * @param string $service Name of the service.
  317. *
  318. * @return array
  319. */
  320. protected function querySentinelForSlaves(NodeConnectionInterface $sentinel, $service)
  321. {
  322. $slaves = array();
  323. $payload = $sentinel->executeCommand(
  324. RawCommand::create('SENTINEL', 'slaves', $service)
  325. );
  326. if ($payload instanceof ErrorResponseInterface) {
  327. $this->handleSentinelErrorResponse($sentinel, $payload);
  328. }
  329. foreach ($payload as $slave) {
  330. $flags = explode(',', $slave[9]);
  331. if (array_intersect($flags, array('s_down', 'o_down', 'disconnected'))) {
  332. continue;
  333. }
  334. $slaves[] = array(
  335. 'host' => $slave[3],
  336. 'port' => $slave[5],
  337. 'alias' => "slave-$slave[1]",
  338. );
  339. }
  340. return $slaves;
  341. }
  342. /**
  343. * {@inheritdoc}
  344. */
  345. public function getCurrent()
  346. {
  347. return $this->current;
  348. }
  349. /**
  350. * {@inheritdoc}
  351. */
  352. public function getMaster()
  353. {
  354. if ($this->master) {
  355. return $this->master;
  356. }
  357. if ($this->updateSentinels) {
  358. $this->updateSentinels();
  359. }
  360. SENTINEL_QUERY: {
  361. $sentinel = $this->getSentinelConnection();
  362. try {
  363. $masterParameters = $this->querySentinelForMaster($sentinel, $this->service);
  364. $masterConnection = $this->connectionFactory->create($masterParameters);
  365. $this->add($masterConnection);
  366. } catch (ConnectionException $exception) {
  367. $this->sentinelConnection = null;
  368. goto SENTINEL_QUERY;
  369. }
  370. }
  371. return $masterConnection;
  372. }
  373. /**
  374. * {@inheritdoc}
  375. */
  376. public function getSlaves()
  377. {
  378. if ($this->slaves) {
  379. return array_values($this->slaves);
  380. }
  381. if ($this->updateSentinels) {
  382. $this->updateSentinels();
  383. }
  384. SENTINEL_QUERY: {
  385. $sentinel = $this->getSentinelConnection();
  386. try {
  387. $slavesParameters = $this->querySentinelForSlaves($sentinel, $this->service);
  388. foreach ($slavesParameters as $slaveParameters) {
  389. $this->add($this->connectionFactory->create($slaveParameters));
  390. }
  391. } catch (ConnectionException $exception) {
  392. $this->sentinelConnection = null;
  393. goto SENTINEL_QUERY;
  394. }
  395. }
  396. return array_values($this->slaves ?: array());
  397. }
  398. /**
  399. * Returns a random slave.
  400. *
  401. * @return NodeConnectionInterface
  402. */
  403. protected function pickSlave()
  404. {
  405. if ($slaves = $this->getSlaves()) {
  406. return $slaves[rand(1, count($slaves)) - 1];
  407. }
  408. }
  409. /**
  410. * Returns the connection instance in charge for the given command.
  411. *
  412. * @param CommandInterface $command Command instance.
  413. *
  414. * @return NodeConnectionInterface
  415. */
  416. private function getConnectionInternal(CommandInterface $command)
  417. {
  418. if (!$this->current) {
  419. if ($this->strategy->isReadOperation($command) && $slave = $this->pickSlave()) {
  420. $this->current = $slave;
  421. } else {
  422. $this->current = $this->getMaster();
  423. }
  424. return $this->current;
  425. }
  426. if ($this->current === $this->master) {
  427. return $this->current;
  428. }
  429. if (!$this->strategy->isReadOperation($command)) {
  430. $this->current = $this->getMaster();
  431. }
  432. return $this->current;
  433. }
  434. /**
  435. * Asserts that the specified connection matches an expected role.
  436. *
  437. * @param NodeConnectionInterface $sentinel Connection to a redis server.
  438. * @param string $role Expected role of the server ("master", "slave" or "sentinel").
  439. */
  440. protected function assertConnectionRole(NodeConnectionInterface $connection, $role)
  441. {
  442. $role = strtolower($role);
  443. $actualRole = $connection->executeCommand(RawCommand::create('ROLE'));
  444. if ($role !== $actualRole[0]) {
  445. throw new RoleException($connection, "Expected $role but got $actualRole[0] [$connection]");
  446. }
  447. }
  448. /**
  449. * {@inheritdoc}
  450. */
  451. public function getConnection(CommandInterface $command)
  452. {
  453. $connection = $this->getConnectionInternal($command);
  454. if (!$connection->isConnected()) {
  455. // When we do not have any available slave in the pool we can expect
  456. // read-only operations to hit the master server.
  457. $expectedRole = $this->strategy->isReadOperation($command) && $this->slaves ? 'slave' : 'master';
  458. $this->assertConnectionRole($connection, $expectedRole);
  459. }
  460. return $connection;
  461. }
  462. /**
  463. * {@inheritdoc}
  464. */
  465. public function getConnectionById($connectionId)
  466. {
  467. if ($connectionId === 'master') {
  468. return $this->getMaster();
  469. }
  470. $this->getSlaves();
  471. if (isset($this->slaves[$connectionId])) {
  472. return $this->slaves[$connectionId];
  473. }
  474. }
  475. /**
  476. * {@inheritdoc}
  477. */
  478. public function switchTo($connection)
  479. {
  480. if (!$connection instanceof NodeConnectionInterface) {
  481. $connection = $this->getConnectionById($connection);
  482. }
  483. if ($connection && $connection === $this->current) {
  484. return;
  485. }
  486. if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
  487. throw new \InvalidArgumentException('Invalid connection or connection not found.');
  488. }
  489. $connection->connect();
  490. if ($this->current) {
  491. $this->current->disconnect();
  492. }
  493. $this->current = $connection;
  494. }
  495. /**
  496. * {@inheritdoc}
  497. */
  498. public function switchToMaster()
  499. {
  500. $this->switchTo('master');
  501. }
  502. /**
  503. * {@inheritdoc}
  504. */
  505. public function switchToSlave()
  506. {
  507. $connection = $this->pickSlave();
  508. $this->switchTo($connection);
  509. }
  510. /**
  511. * {@inheritdoc}
  512. */
  513. public function isConnected()
  514. {
  515. return $this->current ? $this->current->isConnected() : false;
  516. }
  517. /**
  518. * {@inheritdoc}
  519. */
  520. public function connect()
  521. {
  522. if (!$this->current) {
  523. if (!$this->current = $this->pickSlave()) {
  524. $this->current = $this->getMaster();
  525. }
  526. }
  527. $this->current->connect();
  528. }
  529. /**
  530. * {@inheritdoc}
  531. */
  532. public function disconnect()
  533. {
  534. if ($this->master) {
  535. $this->master->disconnect();
  536. }
  537. foreach ($this->slaves as $connection) {
  538. $connection->disconnect();
  539. }
  540. }
  541. /**
  542. * Retries the execution of a command upon server failure after asking a new
  543. * configuration to one of the sentinels.
  544. *
  545. * @param CommandInterface $command Command instance.
  546. * @param string $method Actual method.
  547. *
  548. * @return mixed
  549. */
  550. private function retryCommandOnFailure(CommandInterface $command, $method)
  551. {
  552. $retries = 0;
  553. SENTINEL_RETRY: {
  554. try {
  555. $response = $this->getConnection($command)->$method($command);
  556. } catch (CommunicationException $exception) {
  557. $this->wipeServerList();
  558. $exception->getConnection()->disconnect();
  559. if ($retries == $this->retryLimit) {
  560. throw $exception;
  561. }
  562. usleep($this->retryWait * 1000);
  563. ++$retries;
  564. goto SENTINEL_RETRY;
  565. }
  566. }
  567. return $response;
  568. }
  569. /**
  570. * {@inheritdoc}
  571. */
  572. public function writeRequest(CommandInterface $command)
  573. {
  574. $this->retryCommandOnFailure($command, __FUNCTION__);
  575. }
  576. /**
  577. * {@inheritdoc}
  578. */
  579. public function readResponse(CommandInterface $command)
  580. {
  581. return $this->retryCommandOnFailure($command, __FUNCTION__);
  582. }
  583. /**
  584. * {@inheritdoc}
  585. */
  586. public function executeCommand(CommandInterface $command)
  587. {
  588. return $this->retryCommandOnFailure($command, __FUNCTION__);
  589. }
  590. /**
  591. * Returns the underlying replication strategy.
  592. *
  593. * @return ReplicationStrategy
  594. */
  595. public function getReplicationStrategy()
  596. {
  597. return $this->strategy;
  598. }
  599. /**
  600. * {@inheritdoc}
  601. */
  602. public function __sleep()
  603. {
  604. return array(
  605. 'master', 'slaves', 'service', 'sentinels', 'connectionFactory', 'strategy',
  606. );
  607. }
  608. }