PredisClusterHashStrategy.php 14 KB

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