MasterSlaveReplication.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * Aggregated connection class used by to handle replication with a
  17. * group of servers in a master/slave configuration.
  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 a 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) ? $this->pickSlave() : $this->master;
  90. return $this->current;
  91. }
  92. if ($this->current === $this->master) {
  93. return $this->current;
  94. }
  95. if (!$this->strategy->isReadOperation($command)) {
  96. $this->current = $this->master;
  97. }
  98. return $this->current;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getConnectionById($connectionId)
  104. {
  105. if ($connectionId === 'master') {
  106. return $this->master;
  107. }
  108. if (isset($this->slaves[$connectionId])) {
  109. return $this->slaves[$connectionId];
  110. }
  111. return null;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function switchTo($connection)
  117. {
  118. $this->check();
  119. if (!$connection instanceof SingleConnectionInterface) {
  120. $connection = $this->getConnectionById($connection);
  121. }
  122. if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
  123. throw new InvalidArgumentException('The specified connection is not valid.');
  124. }
  125. $this->current = $connection;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function getCurrent()
  131. {
  132. return $this->current;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getMaster()
  138. {
  139. return $this->master;
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function getSlaves()
  145. {
  146. return array_values($this->slaves);
  147. }
  148. /**
  149. * Returns the underlying replication strategy.
  150. *
  151. * @return ReplicationStrategy
  152. */
  153. public function getReplicationStrategy()
  154. {
  155. return $this->strategy;
  156. }
  157. /**
  158. * Returns a random slave.
  159. *
  160. * @return SingleConnectionInterface
  161. */
  162. protected function pickSlave()
  163. {
  164. return $this->slaves[array_rand($this->slaves)];
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function isConnected()
  170. {
  171. return $this->current ? $this->current->isConnected() : false;
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function connect()
  177. {
  178. if ($this->current === null) {
  179. $this->check();
  180. $this->current = $this->pickSlave();
  181. }
  182. $this->current->connect();
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function disconnect()
  188. {
  189. if ($this->master) {
  190. $this->master->disconnect();
  191. }
  192. foreach ($this->slaves as $connection) {
  193. $connection->disconnect();
  194. }
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function writeRequest(CommandInterface $command)
  200. {
  201. $this->getConnection($command)->writeRequest($command);
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function readResponse(CommandInterface $command)
  207. {
  208. return $this->getConnection($command)->readResponse($command);
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function executeCommand(CommandInterface $command)
  214. {
  215. return $this->getConnection($command)->executeCommand($command);
  216. }
  217. /**
  218. * {@inheritdoc}
  219. */
  220. public function __sleep()
  221. {
  222. return array('master', 'slaves', 'strategy');
  223. }
  224. }