瀏覽代碼

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 13 年之前
父節點
當前提交
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));