MasterSlaveReplication.php 5.8 KB

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