ClusterStrategy.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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' => $getKeyFromFirstArgument, // TODO
  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. 'GEOPOS' => $getKeyFromFirstArgument,
  158. 'GEODIST' => $getKeyFromFirstArgument,
  159. 'GEORADIUS' => array($this, 'getKeyFromGeoradiusCommands'),
  160. 'GEORADIUSBYMEMBER' => array($this, 'getKeyFromGeoradiusCommands'),
  161. );
  162. }
  163. /**
  164. * Returns the list of IDs for the supported commands.
  165. *
  166. * @return array
  167. */
  168. public function getSupportedCommands()
  169. {
  170. return array_keys($this->commands);
  171. }
  172. /**
  173. * Sets an handler for the specified command ID.
  174. *
  175. * The signature of the callback must have a single parameter of type
  176. * Predis\Command\CommandInterface.
  177. *
  178. * When the callback argument is omitted or NULL, the previously associated
  179. * handler for the specified command ID is removed.
  180. *
  181. * @param string $commandID Command ID.
  182. * @param mixed $callback A valid callable object, or NULL to unset the handler.
  183. *
  184. * @throws \InvalidArgumentException
  185. */
  186. public function setCommandHandler($commandID, $callback = null)
  187. {
  188. $commandID = strtoupper($commandID);
  189. if (!isset($callback)) {
  190. unset($this->commands[$commandID]);
  191. return;
  192. }
  193. if (!is_callable($callback)) {
  194. throw new \InvalidArgumentException(
  195. 'The argument must be a callable object or NULL.'
  196. );
  197. }
  198. $this->commands[$commandID] = $callback;
  199. }
  200. /**
  201. * Extracts the key from the first argument of a command instance.
  202. *
  203. * @param CommandInterface $command Command instance.
  204. *
  205. * @return string
  206. */
  207. protected function getKeyFromFirstArgument(CommandInterface $command)
  208. {
  209. return $command->getArgument(0);
  210. }
  211. /**
  212. * Extracts the key from a command with multiple keys only when all keys in
  213. * the arguments array produce the same hash.
  214. *
  215. * @param CommandInterface $command Command instance.
  216. *
  217. * @return string|null
  218. */
  219. protected function getKeyFromAllArguments(CommandInterface $command)
  220. {
  221. $arguments = $command->getArguments();
  222. if ($this->checkSameSlotForKeys($arguments)) {
  223. return $arguments[0];
  224. }
  225. }
  226. /**
  227. * Extracts the key from a command with multiple keys only when all keys in
  228. * the arguments array produce the same hash.
  229. *
  230. * @param CommandInterface $command Command instance.
  231. *
  232. * @return string|null
  233. */
  234. protected function getKeyFromInterleavedArguments(CommandInterface $command)
  235. {
  236. $arguments = $command->getArguments();
  237. $keys = array();
  238. for ($i = 0; $i < count($arguments); $i += 2) {
  239. $keys[] = $arguments[$i];
  240. }
  241. if ($this->checkSameSlotForKeys($keys)) {
  242. return $arguments[0];
  243. }
  244. }
  245. /**
  246. * Extracts the key from BLPOP and BRPOP commands.
  247. *
  248. * @param CommandInterface $command Command instance.
  249. *
  250. * @return string|null
  251. */
  252. protected function getKeyFromBlockingListCommands(CommandInterface $command)
  253. {
  254. $arguments = $command->getArguments();
  255. if ($this->checkSameSlotForKeys(array_slice($arguments, 0, count($arguments) - 1))) {
  256. return $arguments[0];
  257. }
  258. }
  259. /**
  260. * Extracts the key from BITOP command.
  261. *
  262. * @param CommandInterface $command Command instance.
  263. *
  264. * @return string|null
  265. */
  266. protected function getKeyFromBitOp(CommandInterface $command)
  267. {
  268. $arguments = $command->getArguments();
  269. if ($this->checkSameSlotForKeys(array_slice($arguments, 1, count($arguments)))) {
  270. return $arguments[1];
  271. }
  272. }
  273. /**
  274. * Extracts the key from GEORADIUS and GEORADIUSBYMEMBER commands.
  275. *
  276. * @param CommandInterface $command Command instance.
  277. *
  278. * @return string|null
  279. */
  280. protected function getKeyFromGeoradiusCommands(CommandInterface $command)
  281. {
  282. $arguments = $command->getArguments();
  283. $argc = count($arguments);
  284. $startIndex = $command->getId() === 'GEORADIUS' ? 5 : 4;
  285. if ($argc > $startIndex) {
  286. $keys = array($arguments[0]);
  287. for ($i = $startIndex; $i < $argc; $i++) {
  288. $argument = strtoupper($arguments[$i]);
  289. if ($argument === 'STORE' || $argument === 'STOREDIST') {
  290. $keys[] = $arguments[++$i];
  291. }
  292. }
  293. if ($this->checkSameSlotForKeys($keys)) {
  294. return $arguments[0];
  295. } else {
  296. return null;
  297. }
  298. }
  299. return $arguments[0];
  300. }
  301. /**
  302. * Extracts the key from ZINTERSTORE and ZUNIONSTORE commands.
  303. *
  304. * @param CommandInterface $command Command instance.
  305. *
  306. * @return string|null
  307. */
  308. protected function getKeyFromZsetAggregationCommands(CommandInterface $command)
  309. {
  310. $arguments = $command->getArguments();
  311. $keys = array_merge(array($arguments[0]), array_slice($arguments, 2, $arguments[1]));
  312. if ($this->checkSameSlotForKeys($keys)) {
  313. return $arguments[0];
  314. }
  315. }
  316. /**
  317. * Extracts the key from EVAL and EVALSHA commands.
  318. *
  319. * @param CommandInterface $command Command instance.
  320. *
  321. * @return string|null
  322. */
  323. protected function getKeyFromScriptingCommands(CommandInterface $command)
  324. {
  325. if ($command instanceof ScriptCommand) {
  326. $keys = $command->getKeys();
  327. } else {
  328. $keys = array_slice($args = $command->getArguments(), 2, $args[1]);
  329. }
  330. if ($keys && $this->checkSameSlotForKeys($keys)) {
  331. return $keys[0];
  332. }
  333. }
  334. /**
  335. * {@inheritdoc}
  336. */
  337. public function getSlot(CommandInterface $command)
  338. {
  339. $slot = $command->getSlot();
  340. if (!isset($slot) && isset($this->commands[$cmdID = $command->getId()])) {
  341. $key = call_user_func($this->commands[$cmdID], $command);
  342. if (isset($key)) {
  343. $slot = $this->getSlotByKey($key);
  344. $command->setSlot($slot);
  345. }
  346. }
  347. return $slot;
  348. }
  349. /**
  350. * Checks if the specified array of keys will generate the same hash.
  351. *
  352. * @param array $keys Array of keys.
  353. *
  354. * @return bool
  355. */
  356. protected function checkSameSlotForKeys(array $keys)
  357. {
  358. if (!$count = count($keys)) {
  359. return false;
  360. }
  361. $currentSlot = $this->getSlotByKey($keys[0]);
  362. for ($i = 1; $i < $count; ++$i) {
  363. $nextSlot = $this->getSlotByKey($keys[$i]);
  364. if ($currentSlot !== $nextSlot) {
  365. return false;
  366. }
  367. $currentSlot = $nextSlot;
  368. }
  369. return true;
  370. }
  371. /**
  372. * Returns only the hashable part of a key (delimited by "{...}"), or the
  373. * whole key if a key tag is not found in the string.
  374. *
  375. * @param string $key A key.
  376. *
  377. * @return string
  378. */
  379. protected function extractKeyTag($key)
  380. {
  381. if (false !== $start = strpos($key, '{')) {
  382. if (false !== ($end = strpos($key, '}', $start)) && $end !== ++$start) {
  383. $key = substr($key, $start, $end - $start);
  384. }
  385. }
  386. return $key;
  387. }
  388. }