PredisReplication.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 $readonlySHA1 = array();
  23. private $current = null;
  24. private $master = null;
  25. private $slaves = array();
  26. /**
  27. *
  28. */
  29. public function __construct()
  30. {
  31. $this->readonly = $this->getReadOnlyOperations();
  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(IConnectionSingle $connection)
  53. {
  54. $alias = $connection->getParameters()->alias;
  55. if ($alias === 'master') {
  56. $this->master = $connection;
  57. }
  58. else {
  59. $this->slaves[$alias ?: count($this->slaves)] = $connection;
  60. }
  61. $this->reset();
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function remove(IConnectionSingle $connection)
  67. {
  68. if ($connection->getParameters()->alias === 'master') {
  69. $this->master = null;
  70. $this->reset();
  71. }
  72. else {
  73. if (($id = array_search($connection, $this->slaves, true)) !== false) {
  74. unset($this->slaves[$id]);
  75. $this->reset();
  76. }
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getConnection(ICommand $command)
  83. {
  84. if ($this->current === null) {
  85. $this->check();
  86. $this->current = $this->isReadOperation($command) ? $this->pickSlave() : $this->master;
  87. return $this->current;
  88. }
  89. if ($this->current === $this->master) {
  90. return $this->current;
  91. }
  92. if (!$this->isReadOperation($command)) {
  93. $this->current = $this->master;
  94. }
  95. return $this->current;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getConnectionById($connectionId)
  101. {
  102. if ($connectionId === 'master') {
  103. return $this->master;
  104. }
  105. if (isset($this->slaves[$connectionId])) {
  106. return $this->slaves[$connectionId];
  107. }
  108. return null;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function switchTo($connection)
  114. {
  115. $this->check();
  116. if (!$connection instanceof IConnectionSingle) {
  117. $connection = $this->getConnectionById($connection);
  118. }
  119. if ($connection !== $this->master && !in_array($connection, $this->slaves, true)) {
  120. throw new \InvalidArgumentException('The specified connection is not valid.');
  121. }
  122. $this->current = $connection;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getCurrent()
  128. {
  129. return $this->current;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getMaster()
  135. {
  136. return $this->master;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getSlaves()
  142. {
  143. return array_values($this->slaves);
  144. }
  145. /**
  146. * Returns a random slave.
  147. *
  148. * @return IConnectionSingle
  149. */
  150. protected function pickSlave()
  151. {
  152. return $this->slaves[array_rand($this->slaves)];
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function isConnected()
  158. {
  159. return $this->current ? $this->current->isConnected() : false;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function connect()
  165. {
  166. if ($this->current === null) {
  167. $this->check();
  168. $this->current = $this->pickSlave();
  169. }
  170. $this->current->connect();
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function disconnect()
  176. {
  177. if ($this->master) {
  178. $this->master->disconnect();
  179. }
  180. foreach ($this->slaves as $connection) {
  181. $connection->disconnect();
  182. }
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function writeCommand(ICommand $command)
  188. {
  189. $this->getConnection($command)->writeCommand($command);
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function readResponse(ICommand $command)
  195. {
  196. return $this->getConnection($command)->readResponse($command);
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function executeCommand(ICommand $command)
  202. {
  203. return $this->getConnection($command)->executeCommand($command);
  204. }
  205. /**
  206. * Returns if the specified command performs a read-only operation
  207. * against a key stored on Redis.
  208. *
  209. * @param ICommand $command Instance of Redis command.
  210. * @return Boolean
  211. */
  212. protected function isReadOperation(ICommand $command)
  213. {
  214. if (isset($this->readonly[$id = $command->getId()])) {
  215. if (true === $readonly = $this->readonly[$id]) {
  216. return true;
  217. }
  218. return $readonly($command);
  219. }
  220. if (($eval = $id === 'EVAL') || $id === 'EVALSHA') {
  221. $sha1 = $eval ? sha1($command->getArgument(0)) : $command->getArgument(0);
  222. if (isset($this->readonlySHA1[$sha1])) {
  223. if (true === $readonly = $this->readonlySHA1[$sha1]) {
  224. return true;
  225. }
  226. return $readonly($command);
  227. }
  228. }
  229. return false;
  230. }
  231. /**
  232. * Marks a command as a read-only operation. When the behaviour of a
  233. * command can be decided only at runtime depending on its arguments,
  234. * a callable object can be provided to dinamically check if the passed
  235. * instance of a command performs write operations or not.
  236. *
  237. * @param string $commandID ID of the command.
  238. * @param mixed $readonly A boolean or a callable object.
  239. */
  240. public function setCommandReadOnly($commandID, $readonly = true)
  241. {
  242. $commandID = strtoupper($commandID);
  243. if ($readonly) {
  244. $this->readonly[$commandID] = $readonly;
  245. }
  246. else {
  247. unset($this->readonly[$commandID]);
  248. }
  249. }
  250. /**
  251. * Marks a Lua script for EVAL and EVALSHA as a read-only operation. When
  252. * the behaviour of a script can be decided only at runtime depending on
  253. * its arguments, a callable object can be provided to dinamically check
  254. * if the passed instance of EVAL or EVALSHA performs write operations or
  255. * not.
  256. *
  257. * @param string $script Body of the Lua script.
  258. * @param mixed $readonly A boolean or a callable object.
  259. */
  260. public function setScriptReadOnly($script, $readonly = true)
  261. {
  262. $sha1 = sha1($script);
  263. if ($readonly) {
  264. $this->readonlySHA1[$sha1] = $readonly;
  265. }
  266. else {
  267. unset($this->readonlySHA1[$sha1]);
  268. }
  269. }
  270. /**
  271. * Returns the default list of commands performing read-only operations.
  272. *
  273. * @return array
  274. */
  275. protected function getReadOnlyOperations()
  276. {
  277. return array(
  278. 'EXISTS' => true,
  279. 'TYPE' => true,
  280. 'KEYS' => true,
  281. 'RANDOMKEY' => true,
  282. 'TTL' => true,
  283. 'GET' => true,
  284. 'MGET' => true,
  285. 'SUBSTR' => true,
  286. 'STRLEN' => true,
  287. 'GETRANGE' => true,
  288. 'GETBIT' => true,
  289. 'LLEN' => true,
  290. 'LRANGE' => true,
  291. 'LINDEX' => true,
  292. 'SCARD' => true,
  293. 'SISMEMBER' => true,
  294. 'SINTER' => true,
  295. 'SUNION' => true,
  296. 'SDIFF' => true,
  297. 'SMEMBERS' => true,
  298. 'SRANDMEMBER' => true,
  299. 'ZRANGE' => true,
  300. 'ZREVRANGE' => true,
  301. 'ZRANGEBYSCORE' => true,
  302. 'ZREVRANGEBYSCORE' => true,
  303. 'ZCARD' => true,
  304. 'ZSCORE' => true,
  305. 'ZCOUNT' => true,
  306. 'ZRANK' => true,
  307. 'ZREVRANK' => true,
  308. 'HGET' => true,
  309. 'HMGET' => true,
  310. 'HEXISTS' => true,
  311. 'HLEN' => true,
  312. 'HKEYS' => true,
  313. 'HVELS' => true,
  314. 'HGETALL' => true,
  315. 'PING' => true,
  316. 'AUTH' => true,
  317. 'SELECT' => true,
  318. 'ECHO' => true,
  319. 'QUIT' => true,
  320. 'INFO' => true,
  321. 'DBSIZE' => true,
  322. 'MONITOR' => true,
  323. 'LASTSAVE' => true,
  324. 'SHUTDOWN' => true,
  325. 'OBJECT' => true,
  326. 'SORT' => function(ICommand $command) {
  327. $arguments = $command->getArguments();
  328. return ($c = count($arguments)) === 1 ? true : $arguments[$c - 2] !== 'STORE';
  329. },
  330. );
  331. }
  332. }