ClusterStrategy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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\Cluster;
  11. use InvalidArgumentException;
  12. use Predis\Command\CommandInterface;
  13. use Predis\Command\ScriptCommand;
  14. /**
  15. * Common class implementing the logic needed to support clustering strategies.
  16. *
  17. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. abstract class ClusterStrategy implements StrategyInterface
  20. {
  21. protected $commands;
  22. /**
  23. *
  24. */
  25. public function __construct()
  26. {
  27. $this->commands = $this->getDefaultCommands();
  28. }
  29. /**
  30. * Returns the default map of supported commands with their handlers.
  31. *
  32. * @return array
  33. */
  34. protected function getDefaultCommands()
  35. {
  36. $getKeyFromFirstArgument = array($this, 'getKeyFromFirstArgument');
  37. $getKeyFromAllArguments = array($this, 'getKeyFromAllArguments');
  38. return array(
  39. /* commands operating on the key space */
  40. 'EXISTS' => $getKeyFromFirstArgument,
  41. 'DEL' => $getKeyFromAllArguments,
  42. 'TYPE' => $getKeyFromFirstArgument,
  43. 'EXPIRE' => $getKeyFromFirstArgument,
  44. 'EXPIREAT' => $getKeyFromFirstArgument,
  45. 'PERSIST' => $getKeyFromFirstArgument,
  46. 'PEXPIRE' => $getKeyFromFirstArgument,
  47. 'PEXPIREAT' => $getKeyFromFirstArgument,
  48. 'TTL' => $getKeyFromFirstArgument,
  49. 'PTTL' => $getKeyFromFirstArgument,
  50. 'SORT' => $getKeyFromFirstArgument, // TODO
  51. 'DUMP' => $getKeyFromFirstArgument,
  52. 'RESTORE' => $getKeyFromFirstArgument,
  53. /* commands operating on string values */
  54. 'APPEND' => $getKeyFromFirstArgument,
  55. 'DECR' => $getKeyFromFirstArgument,
  56. 'DECRBY' => $getKeyFromFirstArgument,
  57. 'GET' => $getKeyFromFirstArgument,
  58. 'GETBIT' => $getKeyFromFirstArgument,
  59. 'MGET' => $getKeyFromAllArguments,
  60. 'SET' => $getKeyFromFirstArgument,
  61. 'GETRANGE' => $getKeyFromFirstArgument,
  62. 'GETSET' => $getKeyFromFirstArgument,
  63. 'INCR' => $getKeyFromFirstArgument,
  64. 'INCRBY' => $getKeyFromFirstArgument,
  65. 'INCRBYFLOAT' => $getKeyFromFirstArgument,
  66. 'SETBIT' => $getKeyFromFirstArgument,
  67. 'SETEX' => $getKeyFromFirstArgument,
  68. 'MSET' => array($this, 'getKeyFromInterleavedArguments'),
  69. 'MSETNX' => array($this, 'getKeyFromInterleavedArguments'),
  70. 'SETNX' => $getKeyFromFirstArgument,
  71. 'SETRANGE' => $getKeyFromFirstArgument,
  72. 'STRLEN' => $getKeyFromFirstArgument,
  73. 'SUBSTR' => $getKeyFromFirstArgument,
  74. 'BITOP' => array($this, 'getKeyFromBitOp'),
  75. 'BITCOUNT' => $getKeyFromFirstArgument,
  76. /* commands operating on lists */
  77. 'LINSERT' => $getKeyFromFirstArgument,
  78. 'LINDEX' => $getKeyFromFirstArgument,
  79. 'LLEN' => $getKeyFromFirstArgument,
  80. 'LPOP' => $getKeyFromFirstArgument,
  81. 'RPOP' => $getKeyFromFirstArgument,
  82. 'RPOPLPUSH' => $getKeyFromAllArguments,
  83. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  84. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  85. 'BRPOPLPUSH' => array($this, 'getKeyFromBlockingListCommands'),
  86. 'LPUSH' => $getKeyFromFirstArgument,
  87. 'LPUSHX' => $getKeyFromFirstArgument,
  88. 'RPUSH' => $getKeyFromFirstArgument,
  89. 'RPUSHX' => $getKeyFromFirstArgument,
  90. 'LRANGE' => $getKeyFromFirstArgument,
  91. 'LREM' => $getKeyFromFirstArgument,
  92. 'LSET' => $getKeyFromFirstArgument,
  93. 'LTRIM' => $getKeyFromFirstArgument,
  94. /* commands operating on sets */
  95. 'SADD' => $getKeyFromFirstArgument,
  96. 'SCARD' => $getKeyFromFirstArgument,
  97. 'SDIFF' => $getKeyFromAllArguments,
  98. 'SDIFFSTORE' => $getKeyFromAllArguments,
  99. 'SINTER' => $getKeyFromAllArguments,
  100. 'SINTERSTORE' => $getKeyFromAllArguments,
  101. 'SUNION' => $getKeyFromAllArguments,
  102. 'SUNIONSTORE' => $getKeyFromAllArguments,
  103. 'SISMEMBER' => $getKeyFromFirstArgument,
  104. 'SMEMBERS' => $getKeyFromFirstArgument,
  105. 'SSCAN' => $getKeyFromFirstArgument,
  106. 'SPOP' => $getKeyFromFirstArgument,
  107. 'SRANDMEMBER' => $getKeyFromFirstArgument,
  108. 'SREM' => $getKeyFromFirstArgument,
  109. /* commands operating on sorted sets */
  110. 'ZADD' => $getKeyFromFirstArgument,
  111. 'ZCARD' => $getKeyFromFirstArgument,
  112. 'ZCOUNT' => $getKeyFromFirstArgument,
  113. 'ZINCRBY' => $getKeyFromFirstArgument,
  114. 'ZINTERSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  115. 'ZRANGE' => $getKeyFromFirstArgument,
  116. 'ZRANGEBYSCORE' => $getKeyFromFirstArgument,
  117. 'ZRANK' => $getKeyFromFirstArgument,
  118. 'ZREM' => $getKeyFromFirstArgument,
  119. 'ZREMRANGEBYRANK' => $getKeyFromFirstArgument,
  120. 'ZREMRANGEBYSCORE' => $getKeyFromFirstArgument,
  121. 'ZREVRANGE' => $getKeyFromFirstArgument,
  122. 'ZREVRANGEBYSCORE' => $getKeyFromFirstArgument,
  123. 'ZREVRANK' => $getKeyFromFirstArgument,
  124. 'ZSCORE' => $getKeyFromFirstArgument,
  125. 'ZUNIONSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  126. 'ZSCAN' => $getKeyFromFirstArgument,
  127. 'ZLEXCOUNT' => $getKeyFromFirstArgument,
  128. 'ZRANGEBYLEX' => $getKeyFromFirstArgument,
  129. 'ZREMRANGEBYLEX' => $getKeyFromFirstArgument,
  130. /* commands operating on hashes */
  131. 'HDEL' => $getKeyFromFirstArgument,
  132. 'HEXISTS' => $getKeyFromFirstArgument,
  133. 'HGET' => $getKeyFromFirstArgument,
  134. 'HGETALL' => $getKeyFromFirstArgument,
  135. 'HMGET' => $getKeyFromFirstArgument,
  136. 'HMSET' => $getKeyFromFirstArgument,
  137. 'HINCRBY' => $getKeyFromFirstArgument,
  138. 'HINCRBYFLOAT' => $getKeyFromFirstArgument,
  139. 'HKEYS' => $getKeyFromFirstArgument,
  140. 'HLEN' => $getKeyFromFirstArgument,
  141. 'HSET' => $getKeyFromFirstArgument,
  142. 'HSETNX' => $getKeyFromFirstArgument,
  143. 'HVALS' => $getKeyFromFirstArgument,
  144. 'HSCAN' => $getKeyFromFirstArgument,
  145. /* commands operating on HyperLogLog */
  146. 'PFADD' => $getKeyFromFirstArgument,
  147. 'PFCOUNT' => $getKeyFromAllArguments,
  148. 'PFMERGE' => $getKeyFromAllArguments,
  149. /* scripting */
  150. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  151. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  152. );
  153. }
  154. /**
  155. * Returns the list of IDs for the supported commands.
  156. *
  157. * @return array
  158. */
  159. public function getSupportedCommands()
  160. {
  161. return array_keys($this->commands);
  162. }
  163. /**
  164. * Sets an handler for the specified command ID.
  165. *
  166. * The signature of the callback must have a single parameter of type
  167. * Predis\Command\CommandInterface.
  168. *
  169. * When the callback argument is omitted or NULL, the previously associated
  170. * handler for the specified command ID is removed.
  171. *
  172. * @param string $commandID Command ID.
  173. * @param mixed $callback A valid callable object, or NULL to unset the handler.
  174. */
  175. public function setCommandHandler($commandID, $callback = null)
  176. {
  177. $commandID = strtoupper($commandID);
  178. if (!isset($callback)) {
  179. unset($this->commands[$commandID]);
  180. return;
  181. }
  182. if (!is_callable($callback)) {
  183. throw new InvalidArgumentException(
  184. "The argument must be a callable object or NULL."
  185. );
  186. }
  187. $this->commands[$commandID] = $callback;
  188. }
  189. /**
  190. * Extracts the key from the first argument of a command instance.
  191. *
  192. * @param CommandInterface $command Command instance.
  193. *
  194. * @return string
  195. */
  196. protected function getKeyFromFirstArgument(CommandInterface $command)
  197. {
  198. return $command->getArgument(0);
  199. }
  200. /**
  201. * Extracts the key from a command with multiple keys only when all keys in
  202. * the arguments array produce the same hash.
  203. *
  204. * @param CommandInterface $command Command instance.
  205. *
  206. * @return string|null
  207. */
  208. protected function getKeyFromAllArguments(CommandInterface $command)
  209. {
  210. $arguments = $command->getArguments();
  211. if ($this->checkSameSlotForKeys($arguments)) {
  212. return $arguments[0];
  213. }
  214. }
  215. /**
  216. * Extracts the key from a command with multiple keys only when all keys in
  217. * the arguments array produce the same hash.
  218. *
  219. * @param CommandInterface $command Command instance.
  220. *
  221. * @return string|null
  222. */
  223. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  224. {
  225. $arguments = $command->getArguments();
  226. $keys = array();
  227. for ($i = 0; $i < count($arguments); $i += 2) {
  228. $keys[] = $arguments[$i];
  229. }
  230. if ($this->checkSameSlotForKeys($keys)) {
  231. return $arguments[0];
  232. }
  233. }
  234. /**
  235. * Extracts the key from BLPOP and BRPOP commands.
  236. *
  237. * @param CommandInterface $command Command instance.
  238. *
  239. * @return string|null
  240. */
  241. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  242. {
  243. $arguments = $command->getArguments();
  244. if ($this->checkSameSlotForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  245. return $arguments[0];
  246. }
  247. }
  248. /**
  249. * Extracts the key from BITOP command.
  250. *
  251. * @param CommandInterface $command Command instance.
  252. *
  253. * @return string|null
  254. */
  255. protected function getKeyFromBitOp(CommandInterface $command)
  256. {
  257. $arguments = $command->getArguments();
  258. if ($this->checkSameSlotForKeys(array_slice($arguments, 1, count($arguments)))) {
  259. return $arguments[1];
  260. }
  261. }
  262. /**
  263. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  264. *
  265. * @param CommandInterface $command Command instance.
  266. *
  267. * @return string|null
  268. */
  269. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  270. {
  271. $arguments = $command->getArguments();
  272. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  273. if ($this->checkSameSlotForKeys($keys)) {
  274. return $arguments[0];
  275. }
  276. }
  277. /**
  278. * Extracts the key from EVAL and EVALSHA commands.
  279. *
  280. * @param CommandInterface $command Command instance.
  281. *
  282. * @return string|null
  283. */
  284. protected function getKeyFromScriptingCommands(CommandInterface $command)
  285. {
  286. if ($command instanceof ScriptCommand) {
  287. $keys = $command->getKeys();
  288. } else {
  289. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  290. }
  291. if ($keys && $this->checkSameSlotForKeys($keys)) {
  292. return $keys[0];
  293. }
  294. }
  295. /**
  296. * {@inheritdoc}
  297. */
  298. public function getSlot(CommandInterface $command)
  299. {
  300. $slot = $command->getSlot();
  301. if (!isset($slot) && isset($this->commands[$cmdID = $command->getId()])) {
  302. $key = call_user_func($this->commands[$cmdID], $command);
  303. if (isset($key)) {
  304. $slot = $this->getSlotByKey($key);
  305. $command->setSlot($slot);
  306. }
  307. }
  308. return $slot;
  309. }
  310. /**
  311. * {@inheritdoc}
  312. */
  313. abstract public function getSlotByKey($key);
  314. /**
  315. * Checks if the specified array of keys will generate the same hash.
  316. *
  317. * @param array $keys Array of keys.
  318. *
  319. * @return bool
  320. */
  321. protected function checkSameSlotForKeys(array $keys)
  322. {
  323. if (!$count = count($keys)) {
  324. return false;
  325. }
  326. $currentSlot = $this->getSlotByKey($keys[0]);
  327. for ($i = 1; $i < $count; $i++) {
  328. $nextSlot = $this->getSlotByKey($keys[$i]);
  329. if ($currentSlot !== $nextSlot) {
  330. return false;
  331. }
  332. $currentSlot = $nextSlot;
  333. }
  334. return true;
  335. }
  336. /**
  337. * Returns only the hashable part of a key (delimited by "{...}"), or the
  338. * whole key if a key tag is not found in the string.
  339. *
  340. * @param string $key A key.
  341. *
  342. * @return string
  343. */
  344. protected function extractKeyTag($key)
  345. {
  346. if (false !== $start = strpos($key, '{')) {
  347. if (false !== ($end = strpos($key, '}', $start)) && $end !== ++$start) {
  348. $key = substr($key, $start, $end - $start);
  349. }
  350. }
  351. return $key;
  352. }
  353. /**
  354. * {@inheritdoc}
  355. */
  356. abstract public function getDistributor();
  357. }