PredisReplication.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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\Network;
  11. use Predis\Commands\ICommand;
  12. /**
  13. * Defines the standard virtual connection class that is used
  14. * by Predis to handle replication with a group of servers in
  15. * a master/slave configuration.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class PredisReplication implements IConnectionReplication
  20. {
  21. private $readonly = array();
  22. private $current = null;
  23. private $master = null;
  24. private $slaves = array();
  25. /**
  26. *
  27. */
  28. public function __construct()
  29. {
  30. $this->readonly = $this->getReadOnlyOperations();
  31. }
  32. /**
  33. * Returns if the specified command performs a read-only operation
  34. * against a key stored on Redis.
  35. *
  36. * @param ICommand $command Instance of Redis command.
  37. * @return Boolean
  38. */
  39. protected function isReadOperation(ICommand $command)
  40. {
  41. if (isset($this->readonly[$id = $command->getId()])) {
  42. if (true === $readonly = $this->readonly[$id]) {
  43. return true;
  44. }
  45. return $readonly($command);
  46. }
  47. return false;
  48. }
  49. /**
  50. * Checks if one master and at least one slave have been defined.
  51. */
  52. protected function check()
  53. {
  54. if (!isset($this->master) || !$this->slaves) {
  55. throw new \RuntimeException('Replication needs a master and at least one slave.');
  56. }
  57. }
  58. /**
  59. * Resets the connection state.
  60. */
  61. protected function reset()
  62. {
  63. $this->current = null;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function add(IConnectionSingle $connection)
  69. {
  70. $alias = $connection->getParameters()->alias;
  71. if ($alias === 'master') {
  72. $this->master = $connection;
  73. }
  74. else {
  75. $this->slaves[$alias ?: count($this->slaves)] = $connection;
  76. }
  77. $this->reset();
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function remove(IConnectionSingle $connection)
  83. {
  84. if ($connection->getParameters()->alias === 'master') {
  85. $this->master = null;
  86. $this->reset();
  87. }
  88. else {
  89. if (($id = array_search($connection, $this->slaves, true)) !== false) {
  90. unset($this->slaves[$id]);
  91. $this->reset();
  92. }
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getConnection(ICommand $command)
  99. {
  100. if ($this->current === null) {
  101. $this->check();
  102. $this->current = $this->isReadOperation($command) ? $this->pickSlave() : $this->master;
  103. return $this->current;
  104. }
  105. if ($this->current === $this->master) {
  106. return $this->current;
  107. }
  108. if (!$this->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 null;
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function switchTo($connection)
  130. {
  131. $this->check();
  132. if (!$connection instanceof IConnectionSingle) {
  133. $connection = $this->getConnectionById($connection);
  134. }
  135. if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
  136. throw new \InvalidArgumentException('The specified connection is not valid.');
  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 a random slave.
  163. *
  164. * @return IConnectionSingle
  165. */
  166. protected function pickSlave()
  167. {
  168. return $this->slaves[array_rand($this->slaves)];
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function isConnected()
  174. {
  175. return $this->current ? $this->current->isConnected() : false;
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function connect()
  181. {
  182. if ($this->current === null) {
  183. $this->check();
  184. $this->current = $this->pickSlave();
  185. }
  186. $this->current->connect();
  187. }
  188. /**
  189. * {@inheritdoc}
  190. */
  191. public function disconnect()
  192. {
  193. if ($this->master) {
  194. $this->master->disconnect();
  195. }
  196. foreach ($this->slaves as $connection) {
  197. $connection->disconnect();
  198. }
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function writeCommand(ICommand $command)
  204. {
  205. $this->getConnection($command)->writeCommand($command);
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function readResponse(ICommand $command)
  211. {
  212. return $this->getConnection($command)->readResponse($command);
  213. }
  214. /**
  215. * {@inheritdoc}
  216. */
  217. public function executeCommand(ICommand $command)
  218. {
  219. return $this->getConnection($command)->executeCommand($command);
  220. }
  221. /**
  222. * Returns a list of commands that perform read-only operations.
  223. *
  224. * @return array
  225. */
  226. protected function getReadOnlyOperations()
  227. {
  228. return array(
  229. 'EXISTS' => true,
  230. 'TYPE' => true,
  231. 'KEYS' => true,
  232. 'RANDOMKEY' => true,
  233. 'TTL' => true,
  234. 'GET' => true,
  235. 'MGET' => true,
  236. 'SUBSTR' => true,
  237. 'STRLEN' => true,
  238. 'GETRANGE' => true,
  239. 'GETBIT' => true,
  240. 'LLEN' => true,
  241. 'LRANGE' => true,
  242. 'LINDEX' => true,
  243. 'SCARD' => true,
  244. 'SISMEMBER' => true,
  245. 'SINTER' => true,
  246. 'SUNION' => true,
  247. 'SDIFF' => true,
  248. 'SMEMBERS' => true,
  249. 'SRANDMEMBER' => true,
  250. 'ZRANGE' => true,
  251. 'ZREVRANGE' => true,
  252. 'ZRANGEBYSCORE' => true,
  253. 'ZREVRANGEBYSCORE' => true,
  254. 'ZCARD' => true,
  255. 'ZSCORE' => true,
  256. 'ZCOUNT' => true,
  257. 'ZRANK' => true,
  258. 'ZREVRANK' => true,
  259. 'HGET' => true,
  260. 'HMGET' => true,
  261. 'HEXISTS' => true,
  262. 'HLEN' => true,
  263. 'HKEYS' => true,
  264. 'HVELS' => true,
  265. 'HGETALL' => true,
  266. 'PING' => true,
  267. 'AUTH' => true,
  268. 'SELECT' => true,
  269. 'ECHO' => true,
  270. 'QUIT' => true,
  271. 'INFO' => true,
  272. 'DBSIZE' => true,
  273. 'MONITOR' => true,
  274. 'LASTSAVE' => true,
  275. 'SHUTDOWN' => true,
  276. 'OBJECT' => true,
  277. 'SORT' => function(ICommand $command) {
  278. $arguments = $command->getArguments();
  279. return ($c = count($arguments)) === 1 ? true : $arguments[$c - 2] !== 'STORE';
  280. },
  281. );
  282. }
  283. }