فهرست منبع

Change how the hash of a node is retrieved in the hashring-based distributor.

Now classes that extend Predis\Distribution\HashRing can override how the hash
of a node is retrieved in order to implement different kind of distribution
strategies while reusing the base algorithm.
Daniele Alessandri 14 سال پیش
والد
کامیت
b30f495345
2فایلهای تغییر یافته به همراه6 افزوده شده و 2 حذف شده
  1. 5 1
      lib/Predis/Distribution/HashRing.php
  2. 1 1
      lib/Predis/Distribution/KetamaPureRing.php

+ 5 - 1
lib/Predis/Distribution/HashRing.php

@@ -80,7 +80,7 @@ class HashRing implements IDistributionStrategy {
 
     protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
         $nodeObject = $node['object'];
-        $nodeHash = (string) $nodeObject;
+        $nodeHash = $this->getNodeHash($nodeObject);
         $replicas = (int) round($weightRatio * $totalNodes * $replicas);
         for ($i = 0; $i < $replicas; $i++) {
             $key = crc32("$nodeHash:$i");
@@ -88,6 +88,10 @@ class HashRing implements IDistributionStrategy {
         }
     }
 
+    protected function getNodeHash($nodeObject) {
+        return (string) $nodeObject;
+    }
+
     public function generateKey($value) {
         return crc32($value);
     }

+ 1 - 1
lib/Predis/Distribution/KetamaPureRing.php

@@ -11,7 +11,7 @@ class KetamaPureRing extends HashRing {
 
     protected function addNodeToRing(&$ring, $node, $totalNodes, $replicas, $weightRatio) {
         $nodeObject = $node['object'];
-        $nodeHash = (string) $nodeObject;
+        $nodeHash = $this->getNodeHash($nodeObject);
         $replicas = (int) floor($weightRatio * $totalNodes * ($replicas / 4));
         for ($i = 0; $i < $replicas; $i++) {
             $unpackedDigest = unpack('V4', md5("$nodeHash-$i", true));