MasterSlaveReplication.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 InvalidArgumentException;
  12. use RuntimeException;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Connection\NodeConnectionInterface;
  15. use Predis\Replication\ReplicationStrategy;
  16. /**
  17. * Aggregate connection handling replication of Redis nodes configured in a
  18. * single master / multiple slaves setup.
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class MasterSlaveReplication implements ReplicationInterface
  23. {
  24. protected $strategy;
  25. protected $master;
  26. protected $slaves;
  27. protected $current;
  28. /**
  29. *
  30. */
  31. public function __construct(ReplicationStrategy $strategy = null)
  32. {
  33. $this->slaves = array();
  34. $this->strategy = $strategy ?: new ReplicationStrategy();
  35. }
  36. /**
  37. * Checks if one master and at least one slave have been defined.
  38. */
  39. protected function check()
  40. {
  41. if (!isset($this->master) || !$this->slaves) {
  42. throw new RuntimeException('Replication needs one master and at least one slave.');
  43. }
  44. }
  45. /**
  46. * Resets the connection state.
  47. */
  48. protected function reset()
  49. {
  50. $this->current = null;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function add(NodeConnectionInterface $connection)
  56. {
  57. $alias = $connection->getParameters()->alias;
  58. if ($alias === 'master') {
  59. $this->master = $connection;
  60. } else {
  61. $this->slaves[$alias ?: count($this->slaves)] = $connection;
  62. }
  63. $this->reset();
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function remove(NodeConnectionInterface $connection)
  69. {
  70. if ($connection->getParameters()->alias === 'master') {
  71. $this->master = null;
  72. $this->reset();
  73. return true;
  74. } else {
  75. if (($id = array_search($connection, $this->slaves, true)) !== false) {
  76. unset($this->slaves[$id]);
  77. $this->reset();
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getConnection(CommandInterface $command)
  87. {
  88. if ($this->current === null) {
  89. $this->check();
  90. $this->current = $this->strategy->isReadOperation($command)
  91. ? $this->pickSlave()
  92. : $this->master;
  93. return $this->current;
  94. }
  95. if ($this->current === $this->master) {
  96. return $this->current;
  97. }
  98. if (!$this->strategy->isReadOperation($command)) {
  99. $this->current = $this->master;
  100. }
  101. return $this->current;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getConnectionById($connectionId)
  107. {
  108. if ($connectionId === 'master') {
  109. return $this->master;
  110. }
  111. if (isset($this->slaves[$connectionId])) {
  112. return $this->slaves[$connectionId];
  113. }
  114. return null;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function switchTo($connection)
  120. {
  121. $this->check();
  122. if (!$connection instanceof NodeConnectionInterface) {
  123. $connection = $this->getConnectionById($connection);
  124. }
  125. if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
  126. throw new InvalidArgumentException('Invalid connection or connection not found.');
  127. }
  128. $this->current = $connection;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function getCurrent()
  134. {
  135. return $this->current;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getMaster()
  141. {
  142. return $this->master;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getSlaves()
  148. {
  149. return array_values($this->slaves);
  150. }
  151. /**
  152. * Returns the underlying replication strategy.
  153. *
  154. * @return ReplicationStrategy
  155. */
  156. public function getReplicationStrategy()
  157. {
  158. return $this->strategy;
  159. }
  160. /**
  161. * Returns a random slave.
  162. *
  163. * @return NodeConnectionInterface
  164. */
  165. protected function pickSlave()
  166. {
  167. return $this->slaves[array_rand($this->slaves)];
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function isConnected()
  173. {
  174. return $this->current ? $this->current->isConnected() : false;
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function connect()
  180. {
  181. if ($this->current === null) {
  182. $this->check();
  183. $this->current = $this->pickSlave();
  184. }
  185. $this->current->connect();
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function disconnect()
  191. {
  192. if ($this->master) {
  193. $this->master->disconnect();
  194. }
  195. foreach ($this->slaves as $connection) {
  196. $connection->disconnect();
  197. }
  198. }
  199. /**
  200. * {@inheritdoc}
  201. */
  202. public function writeRequest(CommandInterface $command)
  203. {
  204. $this->getConnection($command)->writeRequest($command);
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function readResponse(CommandInterface $command)
  210. {
  211. return $this->getConnection($command)->readResponse($command);
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function executeCommand(CommandInterface $command)
  217. {
  218. return $this->getConnection($command)->executeCommand($command);
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function __sleep()
  224. {
  225. return array('master', 'slaves', 'strategy');
  226. }
  227. }