浅谈一致性hash
2013-06-30 12:04 idkkk 阅读(954) 评论(0) 编辑 收藏 举报相信做过互联网应用的都知道,如何很好的做到横向扩展,其实是个蛮难的话题,缓存可横向扩展,如果采用简单的取模,余数方式的部署,基本是无法做到后期的扩展的,数据迁移及分布都是问题,举个例子:
假设采用取模的方式来实现的分布式缓存,缓存节点为10个,这时候所有的缓存分布在10个节点上,任意一个节点down掉都会导致其他的缓存需要重新分布,从而会让所有缓存失效,这种在互联网应用上基本上是绝不允许出现的,那么如何来解决这个问题呢?!
一般目前互联网上的很多开源应用都是在客户端采用一致性hash来实现分布的,一致性hash又称环状hash,任意一节点出现问题不会影响全局数据有效性,具体的原理可以参考这里:一致哈希
下面简单贴出一致性hash的java实现参考:
import java.nio.ByteBuffer; import java.nio.ByteOrder; /** * This is a very fast, non-cryptographic hash suitable for general hash-based * lookup. See http://murmurhash.googlepages.com/ for more details. * <p/> * <p> * The C version of MurmurHash 2.0 found at that site was ported to Java by * Andrzej Bialecki (ab at getopt org). * </p> */ public class MurmurHash implements Hashing { /** * Hashes bytes in an array. * * @param data * The bytes to hash. * @param seed * The seed for the hash. * @return The 32 bit hash of the bytes in question. */ public static int hash(byte[] data, int seed) { return hash(ByteBuffer.wrap(data), seed); } /** * Hashes bytes in part of an array. * * @param data * The data to hash. * @param offset * Where to start munging. * @param length * How many bytes to process. * @param seed * The seed to start with. * @return The 32-bit hash of the data in question. */ public static int hash(byte[] data, int offset, int length, int seed) { return hash(ByteBuffer.wrap(data, offset, length), seed); } /** * Hashes the bytes in a buffer from the current position to the limit. * * @param buf * The bytes to hash. * @param seed * The seed for the hash. * @return The 32 bit murmur hash of the bytes in the buffer. */ public static int hash(ByteBuffer buf, int seed) { // save byte order for later restoration ByteOrder byteOrder = buf.order(); buf.order(ByteOrder.LITTLE_ENDIAN); int m = 0x5bd1e995; int r = 24; int h = seed ^ buf.remaining(); int k; while (buf.remaining() >= 4) { k = buf.getInt(); k *= m; k ^= k >>> r; k *= m; h *= m; h ^= k; } if (buf.remaining() > 0) { ByteBuffer finish = ByteBuffer.allocate(4).order( ByteOrder.LITTLE_ENDIAN); // for big-endian version, use this first: // finish.position(4-buf.remaining()); finish.put(buf).rewind(); h ^= finish.getInt(); h *= m; } h ^= h >>> 13; h *= m; h ^= h >>> 15; buf.order(byteOrder); return h; } public static long hash64A(byte[] data, int seed) { return hash64A(ByteBuffer.wrap(data), seed); } public static long hash64A(byte[] data, int offset, int length, int seed) { return hash64A(ByteBuffer.wrap(data, offset, length), seed); } public static long hash64A(ByteBuffer buf, int seed) { 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 long hash(byte[] key) { return hash64A(key, 0x1234ABCD); } public long hash(String key) { return hash(SafeEncoder.encode(key)); } }
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public interface Hashing { public static final Hashing MURMUR_HASH = new MurmurHash(); public ThreadLocal<MessageDigest> md5Holder = new ThreadLocal<MessageDigest>(); public static final Hashing MD5 = new Hashing() { public long hash(String key) { return hash(SafeEncoder.encode(key)); } public long hash(byte[] key) { try { if (md5Holder.get() == null) { md5Holder.set(MessageDigest.getInstance("MD5")); } } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("++++ no md5 algorythm found"); } MessageDigest md5 = md5Holder.get(); md5.reset(); md5.update(key); byte[] bKey = md5.digest(); long res = ((long) (bKey[3] & 0xFF) << 24) | ((long) (bKey[2] & 0xFF) << 16) | ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF); return res; } }; public long hash(String key); public long hash(byte[] key); }
一致性Hash在分布式应用中使用的很多,memcached,redis等等。