PredisClusterHashStrategy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /* commands operating on hashes */
  131. 'HDEL' => $keyIsFirstArgument,
  132. 'HEXISTS' => $keyIsFirstArgument,
  133. 'HGET' => $keyIsFirstArgument,
  134. 'HGETALL' => $keyIsFirstArgument,
  135. 'HMGET' => $keyIsFirstArgument,
  136. 'HMSET' => $keyIsFirstArgument,
  137. 'HINCRBY' => $keyIsFirstArgument,
  138. 'HINCRBYFLOAT' => $keyIsFirstArgument,
  139. 'HKEYS' => $keyIsFirstArgument,
  140. 'HLEN' => $keyIsFirstArgument,
  141. 'HSET' => $keyIsFirstArgument,
  142. 'HSETNX' => $keyIsFirstArgument,
  143. 'HVALS' => $keyIsFirstArgument,
  144. 'HSCAN' => $keyIsFirstArgument,
  145. /* commands operating on HyperLogLog */
  146. 'PFADD' => $keyIsFirstArgument,
  147. 'PFCOUNT' => $keysAreAllArguments,
  148. 'PFMERGE' => $keysAreAllArguments,
  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
  167. * of type Predis\Command\CommandInterface.
  168. *
  169. * When the callback argument is omitted or NULL, the previously
  170. * associated handler for the specified command ID is removed.
  171. *
  172. * @param string $commandId The ID of the command to be handled.
  173. * @param mixed $callback A valid callable object or NULL.
  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("Callback must be a valid callable object or NULL");
  184. }
  185. $this->commands[$commandId] = $callback;
  186. }
  187. /**
  188. * Extracts the key from the first argument of a command instance.
  189. *
  190. * @param CommandInterface $command Command instance.
  191. * @return string
  192. */
  193. protected function getKeyFromFirstArgument(CommandInterface $command)
  194. {
  195. return $command->getArgument(0);
  196. }
  197. /**
  198. * Extracts the key from a command with multiple keys only when all keys
  199. * in the arguments array produce the same hash.
  200. *
  201. * @param CommandInterface $command Command instance.
  202. * @return string
  203. */
  204. protected function getKeyFromAllArguments(CommandInterface $command)
  205. {
  206. $arguments = $command->getArguments();
  207. if ($this->checkSameHashForKeys($arguments)) {
  208. return $arguments[0];
  209. }
  210. }
  211. /**
  212. * Extracts the key from a command with multiple keys only when all keys
  213. * in the arguments array produce the same hash.
  214. *
  215. * @param CommandInterface $command Command instance.
  216. * @return string
  217. */
  218. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  219. {
  220. $arguments = $command->getArguments();
  221. $keys = array();
  222. for ($i = 0; $i < count($arguments); $i += 2) {
  223. $keys[] = $arguments[$i];
  224. }
  225. if ($this->checkSameHashForKeys($keys)) {
  226. return $arguments[0];
  227. }
  228. }
  229. /**
  230. * Extracts the key from BLPOP and BRPOP commands.
  231. *
  232. * @param CommandInterface $command Command instance.
  233. * @return string
  234. */
  235. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  236. {
  237. $arguments = $command->getArguments();
  238. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  239. return $arguments[0];
  240. }
  241. }
  242. /**
  243. * Extracts the key from BITOP command.
  244. *
  245. * @param CommandInterface $command Command instance.
  246. * @return string
  247. */
  248. protected function getKeyFromBitOp(CommandInterface $command)
  249. {
  250. $arguments = $command->getArguments();
  251. if ($this->checkSameHashForKeys(array_slice($arguments, 1, count($arguments)))) {
  252. return $arguments[1];
  253. }
  254. }
  255. /**
  256. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  257. *
  258. * @param CommandInterface $command Command instance.
  259. * @return string
  260. */
  261. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  262. {
  263. $arguments = $command->getArguments();
  264. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  265. if ($this->checkSameHashForKeys($keys)) {
  266. return $arguments[0];
  267. }
  268. }
  269. /**
  270. * Extracts the key from EVAL and EVALSHA commands.
  271. *
  272. * @param CommandInterface $command Command instance.
  273. * @return string
  274. */
  275. protected function getKeyFromScriptingCommands(CommandInterface $command)
  276. {
  277. if ($command instanceof ScriptedCommand) {
  278. $keys = $command->getKeys();
  279. } else {
  280. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  281. }
  282. if ($keys && $this->checkSameHashForKeys($keys)) {
  283. return $keys[0];
  284. }
  285. }
  286. /**
  287. * {@inheritdoc}
  288. */
  289. public function getHash(CommandInterface $command)
  290. {
  291. $hash = $command->getHash();
  292. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  293. $key = call_user_func($this->commands[$cmdID], $command);
  294. if (isset($key)) {
  295. $hash = $this->getKeyHash($key);
  296. $command->setHash($hash);
  297. }
  298. }
  299. return $hash;
  300. }
  301. /**
  302. * {@inheritdoc}
  303. */
  304. public function getKeyHash($key)
  305. {
  306. $key = $this->extractKeyTag($key);
  307. $hash = $this->hashGenerator->hash($key);
  308. return $hash;
  309. }
  310. /**
  311. * Checks if the specified array of keys will generate the same hash.
  312. *
  313. * @param array $keys Array of keys.
  314. * @return bool
  315. */
  316. protected function checkSameHashForKeys(Array $keys)
  317. {
  318. if (!$count = count($keys)) {
  319. return false;
  320. }
  321. $currentKey = $this->extractKeyTag($keys[0]);
  322. for ($i = 1; $i < $count; $i++) {
  323. $nextKey = $this->extractKeyTag($keys[$i]);
  324. if ($currentKey !== $nextKey) {
  325. return false;
  326. }
  327. $currentKey = $nextKey;
  328. }
  329. return true;
  330. }
  331. /**
  332. * Returns only the hashable part of a key (delimited by "{...}"), or the
  333. * whole key if a key tag is not found in the string.
  334. *
  335. * @param string $key A key.
  336. * @return string
  337. */
  338. protected function extractKeyTag($key)
  339. {
  340. if (false !== $start = strpos($key, '{')) {
  341. if (false !== $end = strpos($key, '}', $start)) {
  342. $key = substr($key, ++$start, $end - $start);
  343. }
  344. }
  345. return $key;
  346. }
  347. }