PredisClusterHashStrategy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /* scripting */
  146. 'EVAL' => array($this, 'getKeyFromScriptingCommands'),
  147. 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'),
  148. );
  149. }
  150. /**
  151. * Returns the list of IDs for the supported commands.
  152. *
  153. * @return array
  154. */
  155. public function getSupportedCommands()
  156. {
  157. return array_keys($this->commands);
  158. }
  159. /**
  160. * Sets an handler for the specified command ID.
  161. *
  162. * The signature of the callback must have a single parameter
  163. * of type Predis\Command\CommandInterface.
  164. *
  165. * When the callback argument is omitted or NULL, the previously
  166. * associated handler for the specified command ID is removed.
  167. *
  168. * @param string $commandId The ID of the command to be handled.
  169. * @param mixed $callback A valid callable object or NULL.
  170. */
  171. public function setCommandHandler($commandId, $callback = null)
  172. {
  173. $commandId = strtoupper($commandId);
  174. if (!isset($callback)) {
  175. unset($this->commands[$commandId]);
  176. return;
  177. }
  178. if (!is_callable($callback)) {
  179. throw new \InvalidArgumentException("Callback must be a valid callable object or NULL");
  180. }
  181. $this->commands[$commandId] = $callback;
  182. }
  183. /**
  184. * Extracts the key from the first argument of a command instance.
  185. *
  186. * @param CommandInterface $command Command instance.
  187. * @return string
  188. */
  189. protected function getKeyFromFirstArgument(CommandInterface $command)
  190. {
  191. return $command->getArgument(0);
  192. }
  193. /**
  194. * Extracts the key from a command with multiple keys only when all keys
  195. * in the arguments array produce the same hash.
  196. *
  197. * @param CommandInterface $command Command instance.
  198. * @return string
  199. */
  200. protected function getKeyFromAllArguments(CommandInterface $command)
  201. {
  202. $arguments = $command->getArguments();
  203. if ($this->checkSameHashForKeys($arguments)) {
  204. return $arguments[0];
  205. }
  206. }
  207. /**
  208. * Extracts the key from a command with multiple keys only when all keys
  209. * in the arguments array produce the same hash.
  210. *
  211. * @param CommandInterface $command Command instance.
  212. * @return string
  213. */
  214. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  215. {
  216. $arguments = $command->getArguments();
  217. $keys = array();
  218. for ($i = 0; $i < count($arguments); $i += 2) {
  219. $keys[] = $arguments[$i];
  220. }
  221. if ($this->checkSameHashForKeys($keys)) {
  222. return $arguments[0];
  223. }
  224. }
  225. /**
  226. * Extracts the key from BLPOP and BRPOP commands.
  227. *
  228. * @param CommandInterface $command Command instance.
  229. * @return string
  230. */
  231. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  232. {
  233. $arguments = $command->getArguments();
  234. if ($this->checkSameHashForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  235. return $arguments[0];
  236. }
  237. }
  238. /**
  239. * Extracts the key from BITOP command.
  240. *
  241. * @param CommandInterface $command Command instance.
  242. * @return string
  243. */
  244. protected function getKeyFromBitOp(CommandInterface $command)
  245. {
  246. $arguments = $command->getArguments();
  247. if ($this->checkSameHashForKeys(array_slice($arguments, 1, count($arguments)))) {
  248. return $arguments[1];
  249. }
  250. }
  251. /**
  252. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  253. *
  254. * @param CommandInterface $command Command instance.
  255. * @return string
  256. */
  257. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  258. {
  259. $arguments = $command->getArguments();
  260. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  261. if ($this->checkSameHashForKeys($keys)) {
  262. return $arguments[0];
  263. }
  264. }
  265. /**
  266. * Extracts the key from EVAL and EVALSHA commands.
  267. *
  268. * @param CommandInterface $command Command instance.
  269. * @return string
  270. */
  271. protected function getKeyFromScriptingCommands(CommandInterface $command)
  272. {
  273. if ($command instanceof ScriptedCommand) {
  274. $keys = $command->getKeys();
  275. } else {
  276. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  277. }
  278. if ($keys && $this->checkSameHashForKeys($keys)) {
  279. return $keys[0];
  280. }
  281. }
  282. /**
  283. * {@inheritdoc}
  284. */
  285. public function getHash(CommandInterface $command)
  286. {
  287. $hash = $command->getHash();
  288. if (!isset($hash) && isset($this->commands[$cmdID = $command->getId()])) {
  289. $key = call_user_func($this->commands[$cmdID], $command);
  290. if (isset($key)) {
  291. $hash = $this->getKeyHash($key);
  292. $command->setHash($hash);
  293. }
  294. }
  295. return $hash;
  296. }
  297. /**
  298. * {@inheritdoc}
  299. */
  300. public function getKeyHash($key)
  301. {
  302. $key = $this->extractKeyTag($key);
  303. $hash = $this->hashGenerator->hash($key);
  304. return $hash;
  305. }
  306. /**
  307. * Checks if the specified array of keys will generate the same hash.
  308. *
  309. * @param array $keys Array of keys.
  310. * @return bool
  311. */
  312. protected function checkSameHashForKeys(Array $keys)
  313. {
  314. if (!$count = count($keys)) {
  315. return false;
  316. }
  317. $currentKey = $this->extractKeyTag($keys[0]);
  318. for ($i = 1; $i < $count; $i++) {
  319. $nextKey = $this->extractKeyTag($keys[$i]);
  320. if ($currentKey !== $nextKey) {
  321. return false;
  322. }
  323. $currentKey = $nextKey;
  324. }
  325. return true;
  326. }
  327. /**
  328. * Returns only the hashable part of a key (delimited by "{...}"), or the
  329. * whole key if a key tag is not found in the string.
  330. *
  331. * @param string $key A key.
  332. * @return string
  333. */
  334. protected function extractKeyTag($key)
  335. {
  336. if (false !== $start = strpos($key, '{')) {
  337. if (false !== $end = strpos($key, '}', $start)) {
  338. $key = substr($key, ++$start, $end - $start);
  339. }
  340. }
  341. return $key;
  342. }
  343. }