ClusterStrategy.php 14 KB

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