redis cluster topology handshake问题

2024-01-01 10:38

本文主要是介绍redis cluster topology handshake问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题背景:

2021-01-29 由于55.6物理机可用内存较少,担心内存在高峰期吃紧对社区帖子服务的comment集群进行了节点的迁移(在迁移之前帖子服务redis4的client连接已经构建,只是没有读写流量

1.下线了2个实例 10.10.55.6:26962和10.10.55.6:26966,其中一个是master,另外一个是slave

2.上线了2个实例 10.10.164.15:26962和10.10.164.15:26966,对上一步的两个节点进行替换

 

2021-02-20 帖子服务打开了redis4的读写流量,此时发现error报错

错误内容显示帖子服务还是一直访问的10.10.55.6:26962这个已经被下掉替换的节点(预期的访问是10.10.164.15:26962这个节点)

 

 

问题复盘:

case1:

1.集群3主3从,分别是:A-A1、B-B1、C-C1(此时client连接建立,无读写)

2.将A节点替换成新的节点D,新的集群是:D-A1、B-B1、C-C1

3.替换之后打开读写

结果:client能够获取到server的拓扑信息,不能复现

case2:

 

 

1.集群3主3从,分别是:A、B、C和A1、B1、C1(此时client连接建立,无读写)

2.在B节点将A节点从集群forget,然后在A节点执行shutdown(此时模拟问题场景)

3.操作后的集群是:A(handshake)-A1、B-B1、C-C1

4.之后打开读写

结果:client访问报错,问题复现

原因分析:

其中在node的添加和摘除操作中涉及到:

CLUSTER MEET ip port
CLUSTER FORGET node-id

这两个命令,在MEET操作中,会进行socket通信的建立和协议的交换,其中有个中间过程叫做handshake

在redis手册中我们能够看到:

https://redis.io/commands/cluster-meet

An already known node sends a list of nodes in the gossip section that we are not aware of. If the receiving node trusts the sending node as a known node, it will process the gossip section and send an handshake to the nodes that are still not known.

简述:在meet操作时,会有handshake动作,具体实现见:clusterStartHandshake函数

https://redis.io/commands/cluster-forget

Details on why the ban-list is needed

In the following example we'll show why the command must not just remove a given node from the nodes table, but also prevent it for being re-inserted again for some time.

Let's assume we have four nodes, A, B, C and D. In order to end with just a three nodes cluster A, B, C we may follow these steps:

  1. Reshard all the hash slots from D to nodes A, B, C.
  2. D is now empty, but still listed in the nodes table of A, B and C.
  3. We contact A, and send CLUSTER FORGET D.
  4. B sends node A a heartbeat packet, where node D is listed.
  5. A does no longer known node D (see step 3), so it starts an handshake with D.
  6. D ends re-added in the nodes table of A.

简述:其中在进行forget之后,其他的节点gossip还在进行,会将forget掉的那个节点信息传播进来,这时候执行forget命令的节点就会发生handshake动作,尝试连接这个被forget掉的节点(注意:这个时候要是那个forget的节点已经shutdown了,那么当前的handshake将会如何处理?

Special conditions not allowing the command execution

The command does not succeed and returns an error in the following cases:

  1. The specified node ID is not found in the nodes table.
  2. The node receiving the command is a replica, and the specified node ID identifies its current master.
  3. The node ID identifies the same node we are sending the command to.

简述:其中列了三个case会导致forget执行失败(nodeID不存在,nodeID是自己,nodeID是自己的master)

在redis的issue里面我们能够找到:

https://github.com/antirez/redis/issues/2965

There are only two ways this can happen:

  1. You fail to send CLUSTER FORGET to all the nodes in the cluster. So eventually there are nodes that still has a clue about this other node, and it will inform the other nodes via gossip. Make sure to send CLUSTER FORGET to every single node in the cluster.
  2. Or alternatively, there is an instance running in 10.15.107.150 but you said there is not.

在进行forget的时候,没有确认所有的node都成功执行了forget命令,导致gossip下次广播的时候将下掉的节点信息带到了forget的节点上(注意:已经下掉的node收到这个gossip就会尝试进行meet,那么就会出现handshake动作

至于为什么不下掉,引自下面资料源码解释:

/* If it's not in NOADDR state and we don't have it, we* start a handshake process against this IP/PORT pairs.** Note that we require that the sender of this gossip message* is a well known node in our cluster, otherwise we risk* joining another cluster. */if (sender &&!(flags & CLUSTER_NODE_NOADDR) &&!clusterBlacklistExists(g->nodename)){clusterStartHandshake(g->ip,ntohs(g->port));}
搜索代码中改变nodeid定位到createClusterNode(),定位到其调用过程clusterStartHandshake()->createClusterNode()->getRandomHexChars(node->name, CLUSTER_NAMELEN)随机生成nodeid,进入handshake状态. (如果下线的节点重新上线了,与该节点成功建立连接,并在收到该节点报文后更新其nodeid为节点真正的nodeid)
结合forget过程中强调的nodeID不一致会导致下不掉就不难分析出原因

改进措施:

既然handshake状态是由于收到fail状态信息导致的,那么只用把fail状态forget掉就可以,而且fail状态的节点A nodeid是一直不变的.
在集群的每个节点,执行cluster forget其节点包含fail状态节点的nodeid, 之后handshake状态信息也不见了,再次执行数据扩容操作,成功完成.

issue提供的解决脚本:

#echo "usage: host port"
nodes_addrs=$(redis-cli -h $1 -p $2 cluster nodes|grep -v handshake| awk '{print $2}')
echo $nodes_addrs
for addr in ${nodes_addrs[@]}; dohost=${addr%:*}port=${addr#*:}del_nodeids=$(redis-cli -h $host -p $port cluster nodes|grep -E 'handshake|fail'| awk '{print $1}')for nodeid in ${del_nodeids[@]}; doecho $host $port $nodeidredis-cli -h $host -p $port cluster forget $nodeiddone
done

添加过程中程序会遍历集群各节点执行cluster info命令检查cluster_known_nodes是否达到预期结果

参考资料:

handshake源码讲解:https://www.cnblogs.com/gqtcgq/p/7247044.html

handshake例子解析:https://githubmota.github.io/2018/06/15/TODO/

redis cluster的集群通信是通过gossip进行的,关于gossip的介绍:

https://singgel.blog.csdn.net/article/details/107489947

https://singgel.blog.csdn.net/article/details/107490037

这篇关于redis cluster topology handshake问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/558999

相关文章

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

解决升级JDK报错:module java.base does not“opens java.lang.reflect“to unnamed module问题

《解决升级JDK报错:modulejava.basedoesnot“opensjava.lang.reflect“tounnamedmodule问题》SpringBoot启动错误源于Jav... 目录问题描述原因分析解决方案总结问题描述启动sprintboot时报以下错误原因分析编程异js常是由Ja

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Redis实现分布式锁全过程

《Redis实现分布式锁全过程》文章介绍Redis实现分布式锁的方法,包括使用SETNX和EXPIRE命令确保互斥性与防死锁,Redisson客户端提供的便捷接口,以及Redlock算法通过多节点共识... 目录Redis实现分布式锁1. 分布式锁的基本原理2. 使用 Redis 实现分布式锁2.1 获取锁

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

SysMain服务可以关吗? 解决SysMain服务导致的高CPU使用率问题

《SysMain服务可以关吗?解决SysMain服务导致的高CPU使用率问题》SysMain服务是超级预读取,该服务会记录您打开应用程序的模式,并预先将它们加载到内存中以节省时间,但它可能占用大量... 在使用电脑的过程中,CPU使用率居高不下是许多用户都遇到过的问题,其中名为SysMain的服务往往是罪魁