美文网首页
CRC16 - redis集群槽计算

CRC16 - redis集群槽计算

作者: laod_wh | 来源:发表于2019-10-16 16:40 被阅读0次
package com.sumavision.omc.microservice.bo.cache.manager.util;


/**
 * 测试: 解决场景:把1亿的用户 存储在一个队列里,过大。用sharding 摸拟redis 集群 sharding Redis
 * 集群使用数据分片(sharding)而非一致性哈希(consistency hashing)来实现: 一个 Redis 集群包含 16384
 * 个哈希槽(hash slot), 数据库中的每个键都属于这 16384 个哈希槽的其中一个, 集群使用公式 CRC16(key) % 16384 来计算键
 * key 属于哪个槽, 其中 CRC16(key) 语句用于计算键 key 的 CRC16 校验和 。
 * <p>
 * Created by dailong on 2019/10/16.
 */
public class CRC16Util {
    
    public final static int maxInt = 1000;// 00000;//1亿
    public final static int USER_KEY_SLOT_COUNT = 20; // 定议分配存储用户的Slot位
    
    /******************************************************************************
     * Compilation: javac CRC16CCITT.java Execution: java CRC16CCITT s
     * Dependencies:
     *
     * Reads in a sequence of bytes and prints out its 16 bit Cylcic Redundancy
     * Check (CRC-CCIIT 0xFFFF).
     *
     * 1 + x + x^5 + x^12 + x^16 is irreducible polynomial.
     *
     * % java CRC16-CCITT 123456789 CRC16-CCITT = 29b1
     *
     ******************************************************************************/
    
    public static int crc16(final byte[] buffer) {
        
        int crc = 0xFFFF; // initial value 65535
        int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
        // byte[] testBytes = "123456789".getBytes("ASCII");
        for (byte b : buffer) {
            for (int i = 0; i < 8; i++) {
                boolean bit = ((b >> (7 - i) & 1) == 1);
                boolean c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit)
                    crc ^= polynomial;
            }
        }
        
        return crc &= 0xffff;
        
    }
    
    /**
     * @param buffer
     * @return
     */
    static int crc162(final byte[] buffer) {
        int crc = 0xFFFF;
        
        for (int j = 0; j < buffer.length; j++) {
            crc = ((crc >>> 8) | (crc << 8)) & 0xffff;
            crc ^= (buffer[j] & 0xff);// byte to int, trunc sign
            crc ^= ((crc & 0xff) >> 4);
            crc ^= (crc << 12) & 0xffff;
            crc ^= ((crc & 0xFF) << 5) & 0xffff;
        }
        crc &= 0xffff;
        return crc;
        
    }
    
    
    // ,如果存储有压力,可调大槽位
    public static void main(String[] args) {
        
        // int 不用crc16
        /*for (int i = 1; i < maxInt; i++) {
            // 根据玩家id 分布指定到Slot位
            int ranint = i % USER_KEY_SLOT_COUNT;
            String key = "key:" + ranint;
            System.out.println("key:" + key);
            // redisList.lpush(randomKey, String.valueOf(playerId));
        }*/
        
        /**
         * crc16 redis 集群也是用这种方式分配key
         */
        String a = "init_tager_temp";
        int solt = crc16(a.getBytes()) % 16384;
        
        String key = "key:" + solt;
        System.out.println("crc%solt=key:" + key);
        
        // redisList.lpush(randomKey, String.valueOf(playerId));
    }
    
}

相关文章

  • Redis主备集群配置

    redis3.0以上支持集群模式,集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽...

  • CRC16 - redis集群槽计算

  • 集群存储数据的工作原理

    Redis集群是如何实现数据的分布式存储的? 存储数据和查询数据时会调用集群算法CRC16 集群算法会计算出1个数...

  • redis集群数据迁移

    redis槽位 ​ redis集群搭建完后会自动在redis集群中内置16384个哈希槽(至于为啥是16384...

  • Redis-集群

    哈希槽Redis集群没有采用一致性hash,而是引入了哈希槽的概念。Redis集群有16384个哈希槽,每个key...

  • redis-cluster其他知识

    1.redis cluster,每个master都会计算这个key对应的CRC16值,然后对16384个hashs...

  • Redis集群

    集群 节点 一致性hash 哈希槽 异步复制 网络分区 Redis的集群方案 redis cluster...

  • Redis之旅--Redis集群(九)

    一、Redis Cluster模式 redis集群并没有使用一致性hash算法而引入了哈希槽概念,Redis 集群...

  • 为什么redis集群的最大槽数是16384个?

    Redis 集群并没有使用一致性hash,而是引入了哈希槽的概念。Redis 集群有16384个哈希槽,每个key...

  • Redis集群查找数据的过程

    在Redis集群中,每个节点都会保存槽信息,比如Redis集群默认有16384个槽,假设node0节点保存了0-4...

网友评论

      本文标题:CRC16 - redis集群槽计算

      本文链接:https://www.haomeiwen.com/subject/txyumctx.html