Interfacehash{function_hash ($key);//calculates the 32-bit value corresponding to the key name}Interfacedistribution{functionLookup$node);//View Landing Point}classConsistentImplementsHashdistribution{protected $_nodes=Array(); Public function_hash ($key) { return sprintf("%u",CRC32($key)); } Public functionLookup$key) { $point=$this->_hash ($key); $node= Current($this->_nodes);//The value corresponding to the smallest key is paid to node foreach($this->_nodes as $k=$v){ if($k>=$point){ $node=$v; Break; } } return $node; } Public functionAddNodes ($node) { //Record server node $this->_nodes[$this->_hash ($node)] =$node; $this-sortnodes (); } Public functionsortnodes () {Ksort($this->_nodes,sort_regular); } Public functiongetnodes () {return $this-_nodes; }}$con=Newconsistent ();$con->addnodes (' a ');$con->addnodes (' B ');$con->addnodes (' C ');Echo' Existing server nodes are: ';Print_r($con-getnodes ());Echo' The landing point for ' name '.$con->_hash (' Titleasdas '). "<br/>";Echo' should be written in '.$con->lookup (' Titleasdas '). " On the server ";
The above code simply implements the distribution of data to different servers. But the disadvantage is obvious. With this implementation of the distribution when the server is down, the server that is behind this server needs to assume the full workload of the server. So, the following strengthened a bit.
Interfacehash{function_hash ($key);//calculates the 32-bit value corresponding to the key name}Interfacedistribution{functionLookup$node);//View Landing Point}classConsistentImplementsHashdistribution{protected $_positions=Array(); protected $_nodes=Array(); protected $_mul= 64; Public function_hash ($key) { return sprintf("%u",CRC32($key)); } Public functionLookup$key) { $point=$this->_hash ($key); $node= Current($this->_positions);//The value corresponding to the smallest key is paid to node foreach($this->_positions as $k=$v){ if($k>=$point){ $node=$v; Break; } } return $node; } Public functionAddpos ($node) { //Record server node for($i= 0;$i<$this->_mul;$i++){ $pos=$this->_hash ($node.‘ -‘.$i); $this->_positions[$pos] =$node; } $this-Sortpos (); } Public functionSortpos () {Ksort($this->_positions,sort_regular); } Public functionGetPos () {return $this-_positions; }}$con=Newconsistent ();$con->addpos (' a ');$con->addpos (' B ');$con->addpos (' C ');Echo' Existing server nodes are: ';Print_r($con-GetPos ());Echo' The landing point for ' name '.$con->_hash (' Titleasdas '). "<br/>";Echo' should be written in '.$con->lookup (' Titleasdas '). " On the server ";
Generate 64 different nodes for each server. And then sort them. This allows multiple servers to be evenly distributed across different node locations. When one of the servers down also does not occur when one of the servers all assume the pressure of that server.
For the understanding of the distributed algorithm is still insufficient, the above views if there are errors in the hope that the predecessors feel free, thank you very much.
memcached-distributed consistent hashing algorithm practice