MasterSlaveReplication.php 5.6 KB

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