No.171#Redis集群Gosisp协议与节点通信

引言

Redis集群模式被广泛用于生产环境,本文找几个点捋一下,主要内容:

一、数据分片与分配算法

二、Gosisp协议类型与格式

三、节点选择与通信流程

一、数据分片与分配算法

为了应对流量并发瓶颈,以及方便数据迁移与扩容,数据分片方式是常用的解决方式。

Kafka的分区(partition)、RocketMQ的队列(Queue)、Elasticsearch的主分片/副本(shard)、数据库的分库分表等,均采用数据分片思想应对高并发流量。

Redis的集群模式也不例外,采用虚拟槽slot实现数据分片。

Redis的槽位范围0~16383,共16384个槽位。

Redis Cluster中每个节点负责一部分槽数量,分配算法:slot=CRC16(key)&16383。

槽位分配与选择示意图如下:

1febc29113b848c55b67861d7e5adc40.png


二、Gosisp协议类型与格式

1、Gosisp协议类型

节点通信使用Gosisp协议,消息类型有:ping消息、pong消息、meet消息、fail消息。

226ab6d157d283a1765cb0e2e16026a1.png

  • MEET消息:当新节点加入时握手使用。

  • PING消息:节点之间周期性地发送ping消息、交换状态。

  • PONG消息:收到meet、ping消息的响应、并封装自身状态消息。

  • FAIL消息:当节点下线时,像集群广播一个fail消息,其他节点收到会更新该节点的状态。

通信端口=节点端口+10000

每个节点周期性的选择几个节点发送ping消息

2、消息头格式

消息头的结构在clusterMsg中,具体属性如下:

字段说明简述
char sig[4]Signature "RCmb" (Redis Cluster message bus).信号签名
uint32_t totlenTotal length of this message消息长度
uint16_t verProtocol version, currently set to 1协议版本
uint16_t portTCP base port number端口信息
uint16_t typeMessage type消息类型,ping、meet、pong等
uint16_t countOnly used for some kind of messages消息体包含的节点数量
uint64_t currentEpochThe epoch accordingly to the sending node发送节点的纪元(epoch)配置
uint64_t configEpochThe config epoch if it's a master,
or the last epoch advertised by its master if it is a slave
主从节点中,主节点的纪元配置
uint64_t offsetMaster replication offset if node is a master
or processed replication offset if node is a slave
复制偏移量
char sender[CLUSTER_NAMELEN]Name of the sender node发送节点的nodeId信息
unsigned char myslots[CLUSTER_SLOTS/8]myslots info发送节点负责的槽位信息
char slaveof[CLUSTER_NAMELEN]
从节点的nodeId信息
char myip[NET_IP_STR_LEN]Sender IP, if not all zeroed发送者IP
uint16_t extensionsNumber of extensions sent along with this packet扩展信息
char notused1[30]30 bytes reserved for future usage保留30个字节扩展供未来使用
uint16_t pportSender TCP plaintext port, if base port is TLS如果基础端口为TLS,TCP的明文端口
uint16_t cportSender TCP cluster bus port发送者TCP集群总线端口
uint16_t flagsSender node flags发送节点标识,区分主从以及是否下线
unsigned char stateCluster state from the POV of the sender发送者角度的集群状态
unsigned char mflags[3]Message flags: CLUSTERMSG_FLAG[012]_...消息标识
union clusterMsgData data
消息体正文

3、消息体格式

消息体clusterMsgData结构如下:

union clusterMsgData {
    /* PING, MEET and PONG */
    struct {
        /* Array of N clusterMsgDataGossip structures */
        clusterMsgDataGossip gossip[1];
        /* Extension data that can optionally be sent for ping/meet/pong
         * messages. We can't explicitly define them here though, since
         * the gossip array isn't the real length of the gossip data. */
    } ping;

    /* FAIL */
    struct {
        clusterMsgDataFail about;
    } fail;

    /* PUBLISH */
    struct {
        clusterMsgDataPublish msg;
    } publish;

    /* UPDATE */
    struct {
        clusterMsgDataUpdate nodecfg;
    } update;

    /* MODULE */
    struct {
        clusterMsgModule msg;
    } module;
};

备注:clusterMsgDataGossip:PING, MEET and PONG采用的消息结构体,详细如下。

typedef struct {
    char nodename[CLUSTER_NAMELEN];
    uint32_t ping_sent;
    uint32_t pong_received;
    char ip[NET_IP_STR_LEN];  /* IP address last time it was seen */
    uint16_t port;              /* base port last time it was seen */
    uint16_t cport;             /* cluster port last time it was seen */
    uint16_t flags;             /* node->flags copy */
    uint16_t pport;             /* plaintext-port, when base port is TLS */
    uint16_t notused1;
} clusterMsgDataGossip;
  • nodename:节点NodeId

  • ping_sent:最后一次向该节点发送ping消息时间

  • pong_received:最后一次接受该节点pong消息时间

  • ip/port/cport/flags/pport:IP端口以及节点标识


三、节点选择与通信流程

1、节点通信流程

两个节点之间发送MEET/PING消息,回复PONG消息的流程如下。

2fc0c5c5d7c62ed6b1fbade369aa2c75.png


2、通信节点选择

Gosisp协议PING/PONG通信时,具体选择哪个节点发起通信?

每秒从本地实例列表选择5个节点,在这5个节点中选择最久没有通信的实例,向该实例发送PING消息。

避免一些实例节点一直选不到,会有一个定时任务扫描兜底措施。

集群内部每秒10次的固定频率扫描本地缓存节点列表,也就是每100ms一次。

如果节点:PONG更新时间>(cluster-node-timeout/2)立即向该节点发送PING消息。

cluster-node-timeout是判定实例故障的心跳超时时间,默认15秒。

86b7e27271db6db7361f3ca3a07396fe.png


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值