@Override public RedisClient route(String key) { if (this.ketamaClients == null || this.ketamaClients.size() == 0) { return null; } long hash = HashAlgorithm.KETAMA_HASH.hash(key); RedisClient client = this.getClientByHash(hash); int tries = 0; while ((client == null || client.isAvailable()) && tries++ < MAX_TRIES) { hash = this.nextHash(hash, key, tries); client = this.getClientByHash(hash); } return client; }
public final long nextHash(long hashVal, String key, int tries) { long tmpKey = HashAlgorithm.KETAMA_HASH.hash(tries + key); hashVal += (int) (tmpKey ^ tmpKey >>> 32); hashVal &= 0xffffffffL; /* truncate to 32-bits */ return hashVal; }