java 一致性雜湊源碼 轉

來源:互聯網
上載者:User

標籤:

package hash;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class Shard {
    
    private TreeMap<Long, String> nodes; // 虛擬節點   
    private List<String> shards; // 真實機器節點   
    private final int NODE_NUM = 100; // 每個機器節點關聯的虛擬節點個數   
 
    public Shard(List<String> shards) {  
        super();  
        this.shards = shards;  
        init();  
    }  
 
    private void init() { // 初始化一致性hash環   
        nodes = new TreeMap<Long, String>();  
        for (int i = 0; i != shards.size(); ++i) { // 每個真實機器節點都需要關聯虛擬節點   
            final String shardInfo = shards.get(i);  
 
            for (int n = 0; n < NODE_NUM; n++)  
                // 一個真實機器節點關聯NODE_NUM個虛擬節點   
                nodes.put(hash("SHARD-" + i + "-NODE-" + n), shardInfo);  
 
        }  
    }  
 
    public String getShardInfo(String key) {  
        SortedMap<Long, String> tail = nodes.tailMap(hash(key)); // 沿環的順時針找到一個虛擬節點   
        if (tail.size() == 0) {  
            return nodes.get(nodes.firstKey());  
        }  
        return tail.get(tail.firstKey()); // 返回該虛擬節點對應的真實機器節點的資訊   
    }  
 
    /**
     *  MurMurHash演算法,是非加密HASH演算法,效能很高,
     *  比傳統的CRC32,MD5,SHA-1(這兩個演算法都是加密HASH演算法,複雜度本身就很高,帶來的效能上的損害也不可避免)
     *  等HASH演算法要快很多,而且據說這個演算法的碰撞率很低.
     *  http://murmurhash.googlepages.com/
     */  
    private Long hash(String key) {  
          
        ByteBuffer buf = ByteBuffer.wrap(key.getBytes());  
        int seed = 0x1234ABCD;  
          
        ByteOrder byteOrder = buf.order();  
        buf.order(ByteOrder.LITTLE_ENDIAN);  
 
        long m = 0xc6a4a7935bd1e995L;  
        int r = 47;  
 
        long h = seed ^ (buf.remaining() * m);  
 
        long k;  
        while (buf.remaining() >= 8) {  
            k = buf.getLong();  
 
            k *= m;  
            k ^= k >>> r;  
            k *= m;  
 
            h ^= k;  
            h *= m;  
        }  
 
        if (buf.remaining() > 0) {  
            ByteBuffer finish = ByteBuffer.allocate(8).order(  
                    ByteOrder.LITTLE_ENDIAN);  
            // for big-endian version, do this first:   
            // finish.position(8-buf.remaining());   
            finish.put(buf).rewind();  
            h ^= finish.getLong();  
            h *= m;  
        }  
 
        h ^= h >>> r;  
        h *= m;  
        h ^= h >>> r;  
 
        buf.order(byteOrder);  
        return h;  
    }  
 
}  

 

 

public class Hash {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<String> popIDs = new ArrayList<String>();
        popIDs.add("111");
        popIDs.add("222");
        popIDs.add("333");
        popIDs.add("444");
        popIDs.add("555");
        Shard s = new Shard(popIDs);
        
        for(int i=0;i<4;i++)
        {
            System.out.println(s.getShardInfo("100000000000000001"+i));
        }

    }

 

java 一致性雜湊源碼 轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.