memcached - Distributed Consistent Hash Algorithm Exercise

interface hash{
     function _hash( $key ); // Calculate the 32-bit value corresponding to the key name 
}
 interface distribution{
     function lookup( $node ); // View the placement 
}

class Consistent implements hash,distribution{
    protected $_nodes = array();

    public function _hash($key)
    {
        return sprintf("%u",crc32($key));
    }

    public function lookup($key)
    {
       $point = $this ->_hash( $key );
         $node = current ( $this ->_nodes); // Pay the value corresponding to the smallest key to node 
       foreach ( $this ->_nodes as  $k => $v ){
            if ( $k >= $point ){
                 $node = $v ;
                 break ;
           }
       }
       return $node;
    }

    public function addNodes($node)
    {
        // Record server node 
        $this ->_nodes[ $this ->_hash( $node )] = $node ;
         $this -> sortNodes();
    }

    public  function sortNodes ()
    {
        ksort($this->_nodes,SORT_REGULAR);
    }
    public function getNodes()
    {
        return $this->_nodes;
    }
}

$con = new Consistent(); 
 $con ->addNodes('a' );
 $con ->addNodes('b' );
 $con ->addNodes('c' );
 echo 'The existing server nodes are:' ;
 print_r ( $con -> getNodes());
 echo 'name   should be written in '. $ 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 shortcomings are obvious. In the distributed implementation with this implementation, when one of the servers is down, the server behind this server needs to undertake the full workload of this server. So, the following is strengthened.

interface hash{
     function _hash( $key ); // Calculate the 32-bit value corresponding to the key name 
}
 interface distribution{
     function lookup( $node ); // View the placement 
}

class Consistent implements hash,distribution{
    
    protected $_positions = array();
    protected $_nodes = array();
    protected $_mul = 64;
    public function _hash($key)
    {
        return sprintf("%u",crc32($key));
    }

    public function lookup($key)
    {
       $point = $this ->_hash( $key );
         $node = current ( $this ->_positions); // Pay the value corresponding to the smallest key to node 
       foreach ( $this ->_positions as  $k => $v ){
            if ( $k >= $point ){
                 $node = $v ;
                 break ;
           }
       }
       return $node;
    }

    public function addPos($node)
    {
        // Record server node 
        for ( $i =0; $i < $this ->_mul; $i ++ ){
             $pos = $this ->_hash( $node .'-'. $i );
             $this - >_positions[ $pos ] = $node ;
        }
        $this->sortPos();
    }

    public  function sortPos()
    {
        ksort($this->_positions,SORT_REGULAR);
    }
    public function getPos()
    {
        return $this->_positions;
    }
}

$con = new Consistent(); 
 $con ->addPos('a' );
 $con ->addPos('b' );
 $con ->addPos('c' );
 echo 'The existing server nodes have:' ;
 print_r ( $con -> getPos());
 echo 'name   should be written in '. $ con ->_hash('titleasdas') . "<br />" ;
 echo ' should be written in '. $con -> lookup('titleasdas')."On the server";

Each server will spawn 64 different nodes. Then sort. In this way, multiple servers are distributed in different node locations in a balanced and disordered manner. When one of the servers goes down, there will be no situation where one of the servers bears the pressure of that server.

 

 

There is still a lack of understanding of distributed algorithms. If there are any mistakes in the above views, I hope the seniors will give me some advice, thank you very much.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063388&siteId=291194637