custom_cluster_distributor.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. require __DIR__.'/shared.php';
  11. // Developers can implement Predis\Distribution\DistributorInterface to create
  12. // their own distributors used by the client to distribute keys among a cluster
  13. // of servers.
  14. use Predis\Cluster\Distributor\DistributorInterface;
  15. use Predis\Cluster\Hash\HashGeneratorInterface;
  16. use Predis\Cluster\PredisStrategy;
  17. use Predis\Connection\Aggregate\PredisCluster;
  18. class NaiveDistributor implements DistributorInterface, HashGeneratorInterface
  19. {
  20. private $nodes;
  21. private $nodesCount;
  22. public function __construct()
  23. {
  24. $this->nodes = array();
  25. $this->nodesCount = 0;
  26. }
  27. public function add($node, $weight = null)
  28. {
  29. $this->nodes[] = $node;
  30. ++$this->nodesCount;
  31. }
  32. public function remove($node)
  33. {
  34. $this->nodes = array_filter($this->nodes, function ($n) use ($node) {
  35. return $n !== $node;
  36. });
  37. $this->nodesCount = count($this->nodes);
  38. }
  39. public function getSlot($hash)
  40. {
  41. return $this->nodesCount > 1 ? abs($hash % $this->nodesCount) : 0;
  42. }
  43. public function getBySlot($slot)
  44. {
  45. return isset($this->nodes[$slot]) ? $this->nodes[$slot] : null;
  46. }
  47. public function getByHash($hash)
  48. {
  49. if (!$this->nodesCount) {
  50. throw new RuntimeException('No connections.');
  51. }
  52. $slot = $this->getSlot($hash);
  53. $node = $this->getBySlot($slot);
  54. return $node;
  55. }
  56. public function get($value)
  57. {
  58. $hash = $this->hash($value);
  59. $node = $this->getByHash($hash);
  60. return $node;
  61. }
  62. public function hash($value)
  63. {
  64. return crc32($value);
  65. }
  66. public function getHashGenerator()
  67. {
  68. return $this;
  69. }
  70. }
  71. $options = array(
  72. 'cluster' => function () {
  73. $distributor = new NaiveDistributor();
  74. $strategy = new PredisStrategy($distributor);
  75. $cluster = new PredisCluster($strategy);
  76. return $cluster;
  77. },
  78. );
  79. $client = new Predis\Client($multiple_servers, $options);
  80. for ($i = 0; $i < 100; ++$i) {
  81. $client->set("key:$i", str_pad($i, 4, '0', 0));
  82. $client->get("key:$i");
  83. }
  84. $server1 = $client->getClientFor('first')->info();
  85. $server2 = $client->getClientFor('second')->info();
  86. if (isset($server1['Keyspace'], $server2['Keyspace'])) {
  87. $server1 = $server1['Keyspace'];
  88. $server2 = $server2['Keyspace'];
  89. }
  90. printf("Server '%s' has %d keys while server '%s' has %d keys.\n",
  91. 'first', $server1['db15']['keys'], 'second', $server2['db15']['keys']
  92. );