PredisClusterHashStrategy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. 'INCRBYFLOAT' => $keyIsFirstArgument,
  69. 'SETBIT' => $keyIsFirstArgument,
  70. 'SETEX' => $keyIsFirstArgument,
  71. 'MSET' => array($this, 'getKeyFromInterleavedArguments'),
  72. 'MSETNX' => array($this, 'getKeyFromInterleavedArguments'),
  73. 'SETNX' => $keyIsFirstArgument,
  74. 'SETRANGE' => $keyIsFirstArgument,
  75. 'STRLEN' => $keyIsFirstArgument,
  76. 'SUBSTR' => $keyIsFirstArgument,
  77. 'BITOP' => array($this, 'getKeyFromBitOp'),
  78. 'BITCOUNT' => $keyIsFirstArgument,
  79. /* commands operating on lists */
  80. 'LINSERT' => $keyIsFirstArgument,
  81. 'LINDEX' => $keyIsFirstArgument,
  82. 'LLEN' => $keyIsFirstArgument,
  83. 'LPOP' => $keyIsFirstArgument,
  84. 'RPOP' => $keyIsFirstArgument,
  85. 'RPOPLPUSH' => $keysAreAllArguments,
  86. 'BLPOP' => array($this, 'getKeyFromBlockingListCommands'),
  87. 'BRPOP' => array($this, 'getKeyFromBlockingListCommands'),
  88. 'BRPOPLPUSH' => array($this, 'getKeyFromBlockingListCommands'),
  89. 'LPUSH' => $keyIsFirstArgument,
  90. 'LPUSHX' => $keyIsFirstArgument,
  91. 'RPUSH' => $keyIsFirstArgument,
  92. 'RPUSHX' => $keyIsFirstArgument,
  93. 'LRANGE' => $keyIsFirstArgument,
  94. 'LREM' => $keyIsFirstArgument,
  95. 'LSET' => $keyIsFirstArgument,
  96. 'LTRIM' => $keyIsFirstArgument,
  97. /* commands operating on sets */
  98. 'SADD' => $keyIsFirstArgument,
  99. 'SCARD' => $keyIsFirstArgument,
  100. 'SDIFF' => $keysAreAllArguments,
  101. 'SDIFFSTORE' => $keysAreAllArguments,
  102. 'SINTER' => $keysAreAllArguments,
  103. 'SINTERSTORE' => $keysAreAllArguments,
  104. 'SUNION' => $keysAreAllArguments,
  105. 'SUNIONSTORE' => $keysAreAllArguments,
  106. 'SISMEMBER' => $keyIsFirstArgument,
  107. 'SMEMBERS' => $keyIsFirstArgument,
  108. 'SSCAN' => $keyIsFirstArgument,
  109. 'SPOP' => $keyIsFirstArgument,
  110. 'SRANDMEMBER' => $keyIsFirstArgument,
  111. 'SREM' => $keyIsFirstArgument,
  112. /* commands operating on sorted sets */
  113. 'ZADD' => $keyIsFirstArgument,
  114. 'ZCARD' => $keyIsFirstArgument,
  115. 'ZCOUNT' => $keyIsFirstArgument,
  116. 'ZINCRBY' => $keyIsFirstArgument,
  117. 'ZINTERSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  118. 'ZRANGE' => $keyIsFirstArgument,
  119. 'ZRANGEBYSCORE' => $keyIsFirstArgument,
  120. 'ZRANK' => $keyIsFirstArgument,
  121. 'ZREM' => $keyIsFirstArgument,
  122. 'ZREMRANGEBYRANK' => $keyIsFirstArgument,
  123. 'ZREMRANGEBYSCORE' => $keyIsFirstArgument,
  124. 'ZREVRANGE' => $keyIsFirstArgument,
  125. 'ZREVRANGEBYSCORE' => $keyIsFirstArgument,
  126. 'ZREVRANK' => $keyIsFirstArgument,
  127. 'ZSCORE' => $keyIsFirstArgument,
  128. 'ZUNIONSTORE' => array($this, 'getKeyFromZsetAggregationCommands'),
  129. 'ZSCAN' => $keyIsFirstArgument,
  130. 'ZLEXCOUNT' => $keyIsFirstArgument,
  131. /* commands operating on hashes */
  132. 'HDEL' => $keyIsFirstArgument,
  133. 'HEXISTS' => $keyIsFirstArgument,
  134. 'HGET' => $keyIsFirstArgument,
  135. 'HGETALL' => $keyIsFirstArgument,
  136. 'HMGET' => $keyIsFirstArgument,
  137. 'HMSET' => $keyIsFirstArgument,
  138. 'HINCRBY' => $keyIsFirstArgument,
  139. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  140. 'HKEYS' => $keyIsFirstArgument,
  141. 'HLEN' => $keyIsFirstArgument,
  142. 'HSET' => $keyIsFirstArgument,
  143. 'HSETNX' => $keyIsFirstArgument,
  144. 'HVALS' => $keyIsFirstArgument,
  145. 'HSCAN' => $keyIsFirstArgument,
  146. /* commands operating on HyperLogLog */
  147. 'PFADD' => $keyIsFirstArgument,
  148. 'PFCOUNT' => $keysAreAllArguments,
  149. 'PFMERGE' => $keysAreAllArguments,
  150. /* scripting */
  151. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  152. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  153. );
  154. }
  155. /**
  156. * Returns the list of IDs for the supported commands.
  157. *
  158. * @return array
  159. */
  160. public function getSupportedCommands()
  161. {
  162. return array_keys($this->commands);
  163. }
  164. /**
  165. * Sets an handler for the specified command ID.
  166. *
  167. * The signature of the callback must have a single parameter
  168. * of type Predis\Command\CommandInterface.
  169. *
  170. * When the callback argument is omitted or NULL, the previously
  171. * associated handler for the specified command ID is removed.
  172. *
  173. * @param string $commandId The ID of the command to be handled.
  174. * @param mixed $callback A valid callable object or NULL.
  175. */
  176. public function setCommandHandler($commandId, $callback = null)
  177. {
  178. $commandId = strtoupper($commandId);
  179. if (!isset($callback)) {
  180. unset($this->commands[$commandId]);
  181. return;
  182. }
  183. if (!is_callable($callback)) {
  184. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  185. }
  186. $this->commands[$commandId] = $callback;
  187. }
  188. /**
  189. * Extracts the key from the first argument of a command instance.
  190. *
  191. * @param CommandInterface $command Command instance.
  192. * @return string
  193. */
  194. protected function getKeyFromFirstArgument(CommandInterface $command)
  195. {
  196. return $command->getArgument(0);
  197. }
  198. /**
  199. * Extracts the key from a command with multiple keys only when all keys
  200. * in the arguments array produce the same hash.
  201. *
  202. * @param CommandInterface $command Command instance.
  203. * @return string
  204. */
  205. protected function getKeyFromAllArguments(CommandInterface $command)
  206. {
  207. $arguments = $command->getArguments();
  208. if ($this->checkSameHashForKeys($arguments)) {
  209. return $arguments[0];
  210. }
  211. }
  212. /**
  213. * Extracts the key from a command with multiple keys only when all keys
  214. * in the arguments array produce the same hash.
  215. *
  216. * @param CommandInterface $command Command instance.
  217. * @return string
  218. */
  219. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  220. {
  221. $arguments = $command->getArguments();
  222. $keys = array();
  223. for ($i = 0; $i < count($arguments); $i += 2) {
  224. $keys[] = $arguments[$i];
  225. }
  226. if ($this->checkSameHashForKeys($keys)) {
  227. return $arguments[0];
  228. }
  229. }
  230. /**
  231. * Extracts the key from BLPOP and BRPOP commands.
  232. *
  233. * @param CommandInterface $command Command instance.
  234. * @return string
  235. */
  236. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  237. {
  238. $arguments = $command->getArguments();
  239. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  240. return $arguments[0];
  241. }
  242. }
  243. /**
  244. * Extracts the key from BITOP command.
  245. *
  246. * @param CommandInterface $command Command instance.
  247. * @return string
  248. */
  249. protected function getKeyFromBitOp(CommandInterface $command)
  250. {
  251. $arguments = $command->getArguments();
  252. if ($this->checkSameHashForKeys(array_slice($arguments, 1, count($arguments)))) {
  253. return $arguments[1];
  254. }
  255. }
  256. /**
  257. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  258. *
  259. * @param CommandInterface $command Command instance.
  260. * @return string
  261. */
  262. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  263. {
  264. $arguments = $command->getArguments();
  265. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  266. if ($this->checkSameHashForKeys($keys)) {
  267. return $arguments[0];
  268. }
  269. }
  270. /**
  271. * Extracts the key from EVAL and EVALSHA commands.
  272. *
  273. * @param CommandInterface $command Command instance.
  274. * @return string
  275. */
  276. protected function getKeyFromScriptingCommands(CommandInterface $command)
  277. {
  278. if ($command instanceof ScriptedCommand) {
  279. $keys = $command->getKeys();
  280. } else {
  281. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  282. }
  283. if ($keys && $this->checkSameHashForKeys($keys)) {
  284. return $keys[0];
  285. }
  286. }
  287. /**
  288. * {@inheritdoc}
  289. */
  290. public function getHash(CommandInterface $command)
  291. {
  292. $hash = $command->getHash();
  293. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  294. $key = call_user_func($this->commands[$cmdID], $command);
  295. if (isset($key)) {
  296. $hash = $this->getKeyHash($key);
  297. $command->setHash($hash);
  298. }
  299. }
  300. return $hash;
  301. }
  302. /**
  303. * {@inheritdoc}
  304. */
  305. public function getKeyHash($key)
  306. {
  307. $key = $this->extractKeyTag($key);
  308. $hash = $this->hashGenerator->hash($key);
  309. return $hash;
  310. }
  311. /**
  312. * Checks if the specified array of keys will generate the same hash.
  313. *
  314. * @param array $keys Array of keys.
  315. * @return bool
  316. */
  317. protected function checkSameHashForKeys(Array $keys)
  318. {
  319. if (!$count = count($keys)) {
  320. return false;
  321. }
  322. $currentKey = $this->extractKeyTag($keys[0]);
  323. for ($i = 1; $i < $count; $i++) {
  324. $nextKey = $this->extractKeyTag($keys[$i]);
  325. if ($currentKey !== $nextKey) {
  326. return false;
  327. }
  328. $currentKey = $nextKey;
  329. }
  330. return true;
  331. }
  332. /**
  333. * Returns only the hashable part of a key (delimited by "{...}"), or the
  334. * whole key if a key tag is not found in the string.
  335. *
  336. * @param string $key A key.
  337. * @return string
  338. */
  339. protected function extractKeyTag($key)
  340. {
  341. if (false !== $start = strpos($key, '{')) {
  342. if (false !== $end = strpos($key, '}', $start)) {
  343. $key = substr($key, ++$start, $end - $start);
  344. }
  345. }
  346. return $key;
  347. }
  348. }