PredisClusterHashStrategy.php 13 KB

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