PredisClusterHashStrategyTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 PredisTestCase;
  12. use Predis\Cluster\Distribution\HashRing;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class PredisClusterHashStrategyTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testSupportsKeyTags()
  23. {
  24. // NOTE: 32 and 64 bits PHP runtimes can produce different hash values.
  25. $expected = PHP_INT_SIZE == 4 ? -1938594527 : 2356372769;
  26. $strategy = $this->getHashStrategy();
  27. $this->assertSame($expected, $strategy->getKeyHash('{foo}'));
  28. $this->assertSame($expected, $strategy->getKeyHash('{foo}:bar'));
  29. $this->assertSame($expected, $strategy->getKeyHash('{foo}:baz'));
  30. $this->assertSame($expected, $strategy->getKeyHash('bar:{foo}:bar'));
  31. $this->assertSame(0, $strategy->getKeyHash(''));
  32. $this->assertSame(0, $strategy->getKeyHash('{}'));
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testSupportedCommands()
  38. {
  39. $strategy = $this->getHashStrategy();
  40. $this->assertSame($this->getExpectedCommands(), $strategy->getSupportedCommands());
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testReturnsNullOnUnsupportedCommand()
  46. {
  47. $strategy = $this->getHashStrategy();
  48. $command = ServerProfile::getDevelopment()->createCommand('ping');
  49. $this->assertNull($strategy->getHash($command));
  50. }
  51. /**
  52. * @group disconnected
  53. */
  54. public function testFirstKeyCommands()
  55. {
  56. $strategy = $this->getHashStrategy();
  57. $profile = ServerProfile::getDevelopment();
  58. $arguments = array('key');
  59. foreach ($this->getExpectedCommands('keys-first') as $commandID) {
  60. $command = $profile->createCommand($commandID, $arguments);
  61. $this->assertNotNull($strategy->getHash($command), $commandID);
  62. }
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testAllKeysCommands()
  68. {
  69. $strategy = $this->getHashStrategy();
  70. $profile = ServerProfile::getDevelopment();
  71. $arguments = array('{key}:1', '{key}:2', '{key}:3', '{key}:4');
  72. foreach ($this->getExpectedCommands('keys-all') as $commandID) {
  73. $command = $profile->createCommand($commandID, $arguments);
  74. $this->assertNotNull($strategy->getHash($command), $commandID);
  75. }
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testInterleavedKeysCommands()
  81. {
  82. $strategy = $this->getHashStrategy();
  83. $profile = ServerProfile::getDevelopment();
  84. $arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
  85. foreach ($this->getExpectedCommands('keys-interleaved') as $commandID) {
  86. $command = $profile->createCommand($commandID, $arguments);
  87. $this->assertNotNull($strategy->getHash($command), $commandID);
  88. }
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testKeysForBlockingListCommands()
  94. {
  95. $strategy = $this->getHashStrategy();
  96. $profile = ServerProfile::getDevelopment();
  97. $arguments = array('{key}:1', '{key}:2', 10);
  98. foreach ($this->getExpectedCommands('keys-blockinglist') as $commandID) {
  99. $command = $profile->createCommand($commandID, $arguments);
  100. $this->assertNotNull($strategy->getHash($command), $commandID);
  101. }
  102. }
  103. /**
  104. * @group disconnected
  105. */
  106. public function testKeysForZsetAggregationCommands()
  107. {
  108. $strategy = $this->getHashStrategy();
  109. $profile = ServerProfile::getDevelopment();
  110. $arguments = array('{key}:destination', 2, '{key}:1', '{key}:1', array('aggregate' => 'SUM'));
  111. foreach ($this->getExpectedCommands('keys-zaggregated') as $commandID) {
  112. $command = $profile->createCommand($commandID, $arguments);
  113. $this->assertNotNull($strategy->getHash($command), $commandID);
  114. }
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testKeysForBitOpCommand()
  120. {
  121. $strategy = $this->getHashStrategy();
  122. $profile = ServerProfile::getDevelopment();
  123. $arguments = array('AND', '{key}:destination', '{key}:src:1', '{key}:src:2');
  124. foreach ($this->getExpectedCommands('keys-bitop') as $commandID) {
  125. $command = $profile->createCommand($commandID, $arguments);
  126. $this->assertNotNull($strategy->getHash($command), $commandID);
  127. }
  128. }
  129. /**
  130. * @group disconnected
  131. */
  132. public function testKeysForScriptCommand()
  133. {
  134. $strategy = $this->getHashStrategy();
  135. $profile = ServerProfile::getDevelopment();
  136. $arguments = array('%SCRIPT%', 2, '{key}:1', '{key}:2', 'value1', 'value2');
  137. foreach ($this->getExpectedCommands('keys-script') as $commandID) {
  138. $command = $profile->createCommand($commandID, $arguments);
  139. $this->assertNotNull($strategy->getHash($command), $commandID);
  140. }
  141. }
  142. /**
  143. * @group disconnected
  144. */
  145. public function testKeysForScriptedCommand()
  146. {
  147. $strategy = $this->getHashStrategy();
  148. $arguments = array('{key}:1', '{key}:2', 'value1', 'value2');
  149. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
  150. $command->expects($this->once())
  151. ->method('getScript')
  152. ->will($this->returnValue('return true'));
  153. $command->expects($this->exactly(2))
  154. ->method('getKeysCount')
  155. ->will($this->returnValue(2));
  156. $command->setArguments($arguments);
  157. $this->assertNotNull($strategy->getHash($command), "Scripted Command [{$command->getId()}]");
  158. }
  159. /**
  160. * @group disconnected
  161. */
  162. public function testUnsettingCommandHandler()
  163. {
  164. $strategy = $this->getHashStrategy();
  165. $profile = ServerProfile::getDevelopment();
  166. $strategy->setCommandHandler('set');
  167. $strategy->setCommandHandler('get', null);
  168. $command = $profile->createCommand('set', array('key', 'value'));
  169. $this->assertNull($strategy->getHash($command));
  170. $command = $profile->createCommand('get', array('key'));
  171. $this->assertNull($strategy->getHash($command));
  172. }
  173. /**
  174. * @group disconnected
  175. */
  176. public function testSettingCustomCommandHandler()
  177. {
  178. $strategy = $this->getHashStrategy();
  179. $profile = ServerProfile::getDevelopment();
  180. $callable = $this->getMock('stdClass', array('__invoke'));
  181. $callable->expects($this->once())
  182. ->method('__invoke')
  183. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  184. ->will($this->returnValue('key'));
  185. $strategy->setCommandHandler('get', $callable);
  186. $command = $profile->createCommand('get', array('key'));
  187. $this->assertNotNull($strategy->getHash($command));
  188. }
  189. // ******************************************************************** //
  190. // ---- HELPER METHODS ------------------------------------------------ //
  191. // ******************************************************************** //
  192. /**
  193. * Creates the default hash strategy object.
  194. *
  195. * @return CommandHashStrategyInterface
  196. */
  197. protected function getHashStrategy()
  198. {
  199. $distributor = new HashRing();
  200. $hashGenerator = $distributor->getHashGenerator();
  201. $strategy = new PredisClusterHashStrategy($hashGenerator);
  202. return $strategy;
  203. }
  204. /**
  205. * Returns the list of expected supported commands.
  206. *
  207. * @param string $type Optional type of command (based on its keys)
  208. * @return array
  209. */
  210. protected function getExpectedCommands($type = null)
  211. {
  212. $commands = array(
  213. /* commands operating on the key space */
  214. 'EXISTS' => 'keys-first',
  215. 'DEL' => 'keys-all',
  216. 'TYPE' => 'keys-first',
  217. 'EXPIRE' => 'keys-first',
  218. 'EXPIREAT' => 'keys-first',
  219. 'PERSIST' => 'keys-first',
  220. 'PEXPIRE' => 'keys-first',
  221. 'PEXPIREAT' => 'keys-first',
  222. 'TTL' => 'keys-first',
  223. 'PTTL' => 'keys-first',
  224. 'SORT' => 'keys-first', // TODO
  225. 'DUMP' => 'keys-first',
  226. 'RESTORE' => 'keys-first',
  227. /* commands operating on string values */
  228. 'APPEND' => 'keys-first',
  229. 'DECR' => 'keys-first',
  230. 'DECRBY' => 'keys-first',
  231. 'GET' => 'keys-first',
  232. 'GETBIT' => 'keys-first',
  233. 'MGET' => 'keys-all',
  234. 'SET' => 'keys-first',
  235. 'GETRANGE' => 'keys-first',
  236. 'GETSET' => 'keys-first',
  237. 'INCR' => 'keys-first',
  238. 'INCRBY' => 'keys-first',
  239. 'INCRBYFLOAT' => 'keys-first',
  240. 'SETBIT' => 'keys-first',
  241. 'SETEX' => 'keys-first',
  242. 'MSET' => 'keys-interleaved',
  243. 'MSETNX' => 'keys-interleaved',
  244. 'SETNX' => 'keys-first',
  245. 'SETRANGE' => 'keys-first',
  246. 'STRLEN' => 'keys-first',
  247. 'SUBSTR' => 'keys-first',
  248. 'BITOP' => 'keys-bitop',
  249. 'BITCOUNT' => 'keys-first',
  250. /* commands operating on lists */
  251. 'LINSERT' => 'keys-first',
  252. 'LINDEX' => 'keys-first',
  253. 'LLEN' => 'keys-first',
  254. 'LPOP' => 'keys-first',
  255. 'RPOP' => 'keys-first',
  256. 'RPOPLPUSH' => 'keys-all',
  257. 'BLPOP' => 'keys-blockinglist',
  258. 'BRPOP' => 'keys-blockinglist',
  259. 'BRPOPLPUSH' => 'keys-blockinglist',
  260. 'LPUSH' => 'keys-first',
  261. 'LPUSHX' => 'keys-first',
  262. 'RPUSH' => 'keys-first',
  263. 'RPUSHX' => 'keys-first',
  264. 'LRANGE' => 'keys-first',
  265. 'LREM' => 'keys-first',
  266. 'LSET' => 'keys-first',
  267. 'LTRIM' => 'keys-first',
  268. /* commands operating on sets */
  269. 'SADD' => 'keys-first',
  270. 'SCARD' => 'keys-first',
  271. 'SDIFF' => 'keys-all',
  272. 'SDIFFSTORE' => 'keys-all',
  273. 'SINTER' => 'keys-all',
  274. 'SINTERSTORE' => 'keys-all',
  275. 'SUNION' => 'keys-all',
  276. 'SUNIONSTORE' => 'keys-all',
  277. 'SISMEMBER' => 'keys-first',
  278. 'SMEMBERS' => 'keys-first',
  279. 'SSCAN' => 'keys-first',
  280. 'SPOP' => 'keys-first',
  281. 'SRANDMEMBER' => 'keys-first',
  282. 'SREM' => 'keys-first',
  283. /* commands operating on sorted sets */
  284. 'ZADD' => 'keys-first',
  285. 'ZCARD' => 'keys-first',
  286. 'ZCOUNT' => 'keys-first',
  287. 'ZINCRBY' => 'keys-first',
  288. 'ZINTERSTORE' => 'keys-zaggregated',
  289. 'ZRANGE' => 'keys-first',
  290. 'ZRANGEBYSCORE' => 'keys-first',
  291. 'ZRANK' => 'keys-first',
  292. 'ZREM' => 'keys-first',
  293. 'ZREMRANGEBYRANK' => 'keys-first',
  294. 'ZREMRANGEBYSCORE' => 'keys-first',
  295. 'ZREVRANGE' => 'keys-first',
  296. 'ZREVRANGEBYSCORE' => 'keys-first',
  297. 'ZREVRANK' => 'keys-first',
  298. 'ZSCORE' => 'keys-first',
  299. 'ZUNIONSTORE' => 'keys-zaggregated',
  300. 'ZSCAN' => 'keys-first',
  301. 'ZLEXCOUNT' => 'keys-first',
  302. 'ZRANGEBYLEX' => 'keys-first',
  303. 'ZREMRANGEBYLEX' => 'keys-first',
  304. /* commands operating on hashes */
  305. 'HDEL' => 'keys-first',
  306. 'HEXISTS' => 'keys-first',
  307. 'HGET' => 'keys-first',
  308. 'HGETALL' => 'keys-first',
  309. 'HMGET' => 'keys-first',
  310. 'HMSET' => 'keys-first',
  311. 'HINCRBY' => 'keys-first',
  312. 'HINCRBYFLOAT' => 'keys-first',
  313. 'HKEYS' => 'keys-first',
  314. 'HLEN' => 'keys-first',
  315. 'HSET' => 'keys-first',
  316. 'HSETNX' => 'keys-first',
  317. 'HVALS' => 'keys-first',
  318. 'HSCAN' => 'keys-first',
  319. /* commands operating on HyperLogLog */
  320. 'PFADD' => 'keys-first',
  321. 'PFCOUNT' => 'keys-all',
  322. 'PFMERGE' => 'keys-all',
  323. /* scripting */
  324. 'EVAL' => 'keys-script',
  325. 'EVALSHA' => 'keys-script',
  326. );
  327. if (isset($type)) {
  328. $commands = array_filter($commands, function ($expectedType) use ($type) {
  329. return $expectedType === $type;
  330. });
  331. }
  332. return array_keys($commands);
  333. }
  334. }