MasterSlaveReplication.php 5.6 KB

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