首页 > 其他分享 >KingbaseES 集群运维典型案例 03 --“双主” sys_rewind恢复机制

KingbaseES 集群运维典型案例 03 --“双主” sys_rewind恢复机制

时间:2024-07-26 11:07:59浏览次数:13  
标签:11 03 recovery 运维 -- 31 kingbase keepalives target

案例说明:
主库主机系统重启,触发failover切换,原主库系统启动后,数据库服务被启动,出现“双主”,使用“repmgr node rejoin --force-rewind”恢复集群。通过本案例了解,集群“双主”产生的原因及解决方案,并熟悉sys_rewind在集群恢复中的应用。

数据库版本:
KingbaseES V8R6

一、集群架构
如下所示,集群原节点状态:

[kingbase@node202 bin]$ ./repmgr cluster show
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | LSN_Lag | Connection string                                                                                                         
----+-------+---------+-----------+----------+----------+----------+----------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1  | node1 | primary | * running |          | default  | 100      | 17       |         | host=192.168.1.201 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3 tcp_user_timeout=9000
 2  | node2 | standby |   running | node1    | default  | 100      | 17       | 0 bytes | host=192.168.1.202 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3

二、主库系统重启
[root@node201 ~]# reboot

三、主库重启后集群状态
如下所示,原主库主机系统重启后,数据库服务自动启动,导致出现“双主”:

1、failover后集群节点状态

[kingbase@node202 bin]$ ./repmgr cluster show
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | LSN_Lag | Connection string                                                                                                         
----+-------+---------+-----------+----------+----------+----------+----------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1  | node1 | primary | - failed  | ?        | default  | 100      |          |         | host=192.168.1.201 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3 tcp_user_timeout=9000
 2  | node2 | primary | * running |          | default  | 100      | 18       |         | host=192.168.1.202 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3

[WARNING] following issues were detected
  - unable to connect to node "node1" (ID: 1)

2、查看节点上timeline变化

如下所示,新主库触发promote后,timeline为(18=1*16^1 + 2 * 16^0),原主库的timeline为(17):

新主库:
[kingbase@node202 bin]$ ls -lh ../data/sys_wal
-rw------- 1 kingbase kingbase 16M Jan 31 10:52 0000001100000000000000EB
-rw------- 1 kingbase kingbase 16M Jan 31 10:57 0000001100000000000000EC.partial
-rw------- 1 kingbase kingbase 694 Jan 26 09:41 00000011.history
-rw------- 1 kingbase kingbase 16M Jan 31 11:08 0000001200000000000000EC
-rw------- 1 kingbase kingbase 738 Jan 31 11:07 00000012.history

原主库:
[kingbase@node201 bin]$ ls -lh ../data/sys_wal
-rw------- 1 kingbase kingbase 16M Jan 31 10:52 0000001100000000000000EB
-rw------- 1 kingbase kingbase 16M Jan 31 11:06 0000001100000000000000EC
-rw------- 1 kingbase kingbase 16M Jan 31 11:06 0000001100000000000000ED
-rw------- 1 kingbase kingbase 694 Jan  3 16:27 00000011.history
drwx------ 2 kingbase kingbase 16K Jan 31 11:06 archive_status

四、模拟“双主”故障

1、触发failover切换

# 集群参数配置
[kingbase@node201 bin]$ cat ../etc/repmgr.conf |grep -E 'recovery|failover'
failover='automatic'
recovery='manual'

# 关闭主库数据库服务
[kingbase@node201 bin]$ ./sys_ctl stop -D ../data/
主库数据库服务被关闭后,将触发集群主备failover切换,在参数recovery=manual情况下,原主库将需要人工参与恢复为备库加入集群。

2、误启动原主库数据库服务
如下所示,如果需要人工将原主库恢复为新的备库加入到集群,需要在原主库data目录下创建standby.signal文件后,再启动数据库服务,数据库服务将启动到备库状态,如果在没有创建standby.signal标识文件下,直接启动原主库数据库服务,将直接进入主节点模式。

[kingbase@node201 bin]$ ./sys_ctl start -D ../data/

2、查看数据库服务进程
如下所示,原主库启动后,数据库服务进程启动为主库,集群出现“双主”。

[kingbase@node201 bin]$ ps -ef |grep kingbase

kingbase  4137     1  0 11:14 ?        00:00:00 /home/kingbase/cluster/R6C8/HAC8/kingbase/bin/kingbase -D ../data
kingbase  4138  4137  0 11:14 ?        00:00:00 kingbase: logger
kingbase  4140  4137  0 11:14 ?        00:00:00 kingbase: checkpointer
kingbase  4141  4137  0 11:14 ?        00:00:00 kingbase: background writer
kingbase  4142  4137  0 11:14 ?        00:00:00 kingbase: walwriter
kingbase  4143  4137  0 11:14 ?        00:00:00 kingbase: autovacuum launcher
kingbase  4144  4137  0 11:14 ?        00:00:00 kingbase: archiver   last was 0000001100000000000000EC
kingbase  4145  4137  0 11:14 ?        00:00:00 kingbase: stats collector
kingbase  4146  4137  0 11:14 ?        00:00:00 kingbase: kwr collector
kingbase  4147  4137  0 11:14 ?        00:00:00 kingbase: ksh writer
kingbase  4148  4137  0 11:14 ?        00:00:00 kingbase: ksh collector
kingbase  4149  4137  0 11:14 ?        00:00:00 kingbase: logical replication launcher

# 数据库状态
test=# select sys_is_in_recovery();
 sys_is_in_recovery()
----------------------
 f
(1 row)

3、查看数据库日志

[kingbase@node201 sys_log]$ tail -1000 kingbase-2024-01-31_111429.csv
2024-01-31 11:14:29.129 CST,,,4137,,65b9bb15.1029,1,,2024-01-31 11:14:29 CST,,0,LOG,00000,"ending log output to stderr",,"Future log output will go to log destination ""csvlog"".",,,,,,,""
2024-01-31 11:14:29.149 CST,,,4139,,65b9bb15.102b,1,,2024-01-31 11:14:29 CST,,0,LOG,00000,"database system was shut down at 2024-01-31 11:06:32 CST",,,,,,,,,""
2024-01-31 11:14:29.164 CST,,,4137,,65b9bb15.1029,2,,2024-01-31 11:14:29 CST,,0,LOG,00000,"database system is ready to accept connections",,,,,,,,,""
2024-01-31 11:14:31.775 CST,"esrep","esrep",4156,"192.168.1.201:32776",65b9bb17.103c,1,"ALTER SYSTEM",2024-01-31 11:14:31 CST,4/21,0,LOG,XX000,"attention:user esrep is modifying synchronous_standby_names by ALTER SYSTEM SET statement",,,,,,"ALTER SYSTEM SET synchronous_standby_names = ''",,,"internal_rwcmgr"
2024-01-31 11:14:31.783 CST,,,4137,,65b9bb15.1029,3,,2024-01-31 11:14:29 CST,,0,LOG,00000,"received SIGHUP, reloading configuration files",,,,,,,,,""
2024-01-31 11:14:31.784 CST,,,4137,,65b9bb15.1029,4,,2024-01-31 11:14:29 CST,,0,LOG,00000,"parameter ""synchronous_standby_names"" changed to """"",,,,,,,,,""

4、集群节点状态
如下所示,集群出现“双主”状态:

[kingbase@node202 bin]$ ./repmgr cluster show
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | LSN_Lag | Connection string                                                                                                         
----+-------+---------+-----------+----------+----------+----------+----------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1  | node1 | primary | - failed  | ?        | default  | 100      |          |         | host=192.168.1.201 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3 tcp_user_timeout=9000
 2  | node2 | primary | * running |          | default  | 100      | 18       |         | host=192.168.1.202 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3

[WARNING] following issues were detected
  - unable to connect to node "node1" (ID: 1)

五、恢复集群
如下所示,将原主库通过node rejoin方式作为备库加入到集群,以下为日志信息:

1、执行‘repmgr node rejoin’

[kingbase@node201 bin]$ ./repmgr node rejoin  -h 192.168.1.202 -U esrep -d esrep --force-rewind
......
[NOTICE] rejoin target is node "node2" (ID: 2)
[DEBUG] connecting to: "user=esrep connect_timeout=10 dbname=esrep host=192.168.1.202 port=54321 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3 fallback_application_name=internal_rwcmgr options=-csearch_path="
[DEBUG] local tli: 17; local_xlogpos: 0/EE000028; follow_target_history->tli: 17; follow_target_history->end: 0/EC000FA0
[NOTICE] sys_rewind execution required for this node to attach to rejoin target node 2
[DETAIL] rejoin target server's timeline 18 forked off current database system timeline 17 before current recovery point 0/EE000028
[NOTICE] executing sys_rewind
[DETAIL] sys_rewind command is "/home/kingbase/cluster/R6C8/HAC8/kingbase/bin/sys_rewind -D '/home/kingbase/cluster/R6C8/HAC8/kingbase/data' --source-server='host=192.168.1.202 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3'"
sys_rewind: servers diverged at WAL location 0/EC000FA0 on timeline 17
sys_rewind: rewinding from last common checkpoint at 0/EB000088 on timeline 17
sys_rewind: find last common checkpoint start time from 2024-01-31 11:18:07.003968 CST to 2024-01-31 11:18:07.204522 CST, in "0.200554" seconds.
sys_rewind: collect the number of WAL files to be processed:3, start time from 2024-01-31 11:18:07.204754 CST to 2024-01-31 11:18:07.320593 CST, cost "0.115839" seconds.
sys_rewind: read WAL in target server from 0/EB000088 to 0/EE0000A0 (endpoint 0/EE0000A0)
sys_rewind: read the local Wal file information, start time from 2024-01-31 11:18:07.204754 CST to 2024-01-31 11:18:07.321418 CST, cost "0.000793" seconds.
sys_rewind: file replication start time from 2024-01-31 11:18:07.321453 CST to 2024-01-31 11:18:07.827427 CST, cost "0.505974" seconds.
sys_rewind: update the control file: minRecoveryPoint is '0/EC013BE0', minRecoveryPointTLI is '18', and database state is 'in archive recovery'
sys_rewind: we will remove the dir '/home/kingbase/cluster/R6C8/HAC8/kingbase/data/sys_replslot/repmgr_slot_2.rewind' and all the file/dir in it.
sys_rewind: rewind start wal location 0/EB000058 (file 0000001100000000000000EB), end wal location 0/EC013BE0 (file 0000001200000000000000EC). wal data increment:16462(kB). time from 2024-01-31 11:18:07.321453 CST to 2024-01-31 11:18:08.543478 CST, in "1.539510" seconds.
sys_rewind: Done!
[NOTICE] 0 files copied to /home/kingbase/cluster/R6C8/HAC8/kingbase/data
[INFO] creating replication slot as user "esrep"
[DEBUG] CreateSlotBySQL(): creating slot "repmgr_slot_1" on upstream
[NOTICE] setting node 1's upstream to node 2
[WARNING] unable to ping "host=192.168.1.201 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3 tcp_user_timeout=9000"
[DETAIL] KCIping() returned "KCIPING_NO_RESPONSE"
[NOTICE] begin to start server at 2024-01-31 11:18:08.556667
[NOTICE] starting server using "/home/kingbase/cluster/R6C8/HAC8/kingbase/bin/sys_ctl  -w -t 90 -D '/home/kingbase/cluster/R6C8/HAC8/kingbase/data' -l /home/kingbase/cluster/R6C8/HAC8/kingbase/bin/logfile start"
[NOTICE] start server finish at 2024-01-31 11:18:09.265062
[NOTICE] NODE REJOIN successful
[DETAIL] node 1 is now attached to node 2

2、rewind对比原主库和新主库history文件
如下所示,sys_rewind通过对比源和目标库的history文件,查找节点之间的分叉点。

原主库:
[kingbase@node201 sys_wal]$ cat 00000011.history
1       0/180000A0      no recovery target specified
2       0/190000A0      no recovery target specified
3       0/250000A0      no recovery target specified
4       0/260000A0      no recovery target specified
5       0/AB0000A0      no recovery target specified
6       0/AD0000A0      no recovery target specified 
7       0/B10000A0      no recovery target specified
8       0/B30000A0      no recovery target specified
9       0/B60000A0      no recovery target specified
10      0/B70000A0      no recovery target specified
11      0/BE0000A0      no recovery target specified
12      0/BF0000A0      no recovery target specified
13      0/C00000A0      no recovery target specified
14      0/C10000A0      no recovery target specified
15      0/C90000A0      no recovery target specified
16      0/CA0000A0      no recovery target specified

新主库:
[kingbase@node202 bin]$ cat ../data/sys_wal/00000012.history
1       0/180000A0      no recovery target specified
2       0/190000A0      no recovery target specified
3       0/250000A0      no recovery target specified
4       0/260000A0      no recovery target specified
5       0/AB0000A0      no recovery target specified
6       0/AD0000A0      no recovery target specified
7       0/B10000A0      no recovery target specified
8       0/B30000A0      no recovery target specified
9       0/B60000A0      no recovery target specified
10      0/B70000A0      no recovery target specified
11      0/BE0000A0      no recovery target specified
12      0/BF0000A0      no recovery target specified
13      0/C00000A0      no recovery target specified
14      0/C10000A0      no recovery target specified
15      0/C90000A0      no recovery target specified
16      0/CA0000A0      no recovery target specified
17      0/EC000FA0      no recovery target specified  --history分叉点

源库和目标库的分叉点:

如下所示,在rewind过程中,源库和目标库之间通过history文件获取分叉点,然后从分叉点之前最近的checkpoint开始对wal日志执行recovery(必须保证从checkpoint开始,源库上的wal日志必须存在,否则将会因为缺失wal日志,rewind失败。):

3、查看新主库数据库日志

2024-01-31 11:18:09.261 CST,"esrep","",13596,"192.168.1.201:32307",65b9bbf1.351c,1,"idle",2024-01-31 11:18:09 CST,4/0,0,LOG,00000,"received replication command: IDENTIFY_SYSTEM",,,,,,,,,"node1"
2024-01-31 11:18:09.261 CST,"esrep","",13596,"192.168.1.201:32307",65b9bbf1.351c,2,"idle",2024-01-31 11:18:09 CST,4/0,0,LOG,00000,"received replication command: START_REPLICATION SLOT ""repmgr_slot_1"" 0/EC000000 TIMELINE 18",,,,,,,,,"node1"
2024-01-31 11:18:11.237 CST,"esrep","esrep",3552,"192.168.1.202:49745",65b9b688.de0,3,"ALTER SYSTEM",2024-01-31 10:55:04 CST,2/4414,0,LOG,XX000,"attention:user esrep is modifying synchronous_standby_names by ALTER SYSTEM SET statement",,,,,,"ALTER SYSTEM SET synchronous_standby_names = 'ANY 1(node1)'",,,"internal_rwcmgr"
2024-01-31 11:18:11.239 CST,,,2823,,65b9b66f.b07,25,,2024-01-31 10:54:39 CST,,0,LOG,00000,"received SIGHUP, reloading configuration files",,,,,,,,,""
2024-01-31 11:18:11.241 CST,,,2823,,65b9b66f.b07,26,,2024-01-31 10:54:39 CST,,0,LOG,00000,"parameter ""synchronous_standby_names"" changed to ""ANY 1(node1)""",,,,,,,,,""
2024-01-31 11:18:11.402 CST,"esrep","",13596,"192.168.1.201:32307",65b9bbf1.351c,3,"streaming 0/EC015300",2024-01-31 11:18:09 CST,4/0,0,LOG,00000,"standby ""node1"" is now a candidate for quorum synchronous standby",,,,,,,,,"node1"

4、集群恢复完成

[kingbase@node202 bin]$ ./repmgr cluster show


 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | LSN_Lag | Connection string                                                                                                         
----+-------+---------+-----------+----------+----------+----------+----------+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1  | node1 | standby |   running | node2    | default  | 100      | 17       | 0 bytes | host=192.168.1.201 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3 tcp_user_timeout=9000
 2  | node2 | primary | * running |          | default  | 100      | 18       |         | host=192.168.1.202 user=esrep dbname=esrep port=54321 connect_timeout=10 keepalives=1 keepalives_idle=10 keepalives_interval=1 keepalives_count=3

六、总结
sys_rewind检查源和目标集群的时间线历史以确定它们的分叉点,从分叉点之前最近的checkpoint位置开始解析WAL,解析出变动的数据页,然后将变动的数据页拷贝过来,并从分叉点最近的checkpoint开始应用wal日志,最终保证源库和目标库数据一致。
sys_rewind 的使用不限于故障转移,例如,可以提升备用服务器主库,运行一些写入事务,然后重新回滚再次成为备用服务器。

标签:11,03,recovery,运维,--,31,kingbase,keepalives,target
From: https://www.cnblogs.com/kingbase/p/18280369

相关文章

  • JDK、JRE和JVM
    目录1.JDK(JavaDevelopmentKit)定义:功能:重要性:2.JRE(JavaRuntimeEnvironment)定义:功能:重要性:3.JVM(JavaVirtualMachine)定义:功能:重要性:4.三者之间的关系1.JDK(JavaDevelopmentKit)定义:JDK是Java开发工具包的缩写,是Java软件开发的核心,为程序开发者提供了一整套用于开发Jav......
  • DevOps - DevOps随想杂谈
    1-趋势与本义随着技术的发展,基础设施和应用程序之间的界限会变得越来越模糊,"服务"管理也将变得更加全面和简单。通过实施DevOps可以便捷地搭建包含交付流水线的研发协作平台,可以快速实现商业价值。在这一过程中,反对将DevOps绝对理论化、模型化,而是坚持DevOps的实践性和......
  • 聊一聊PostgreSQL数据库,以及PostgreSQL认证体系
    PostgreSQL数据库简介PostgreSQL是一种强大的开源关系型数据库管理系统(RDBMS)。它具有以下显著特点和优势:强大的功能:支持丰富的数据类型,包括数组、JSON、XML等复杂数据类型。提供完善的事务处理机制,确保数据的一致性和可靠性。高度的可扩展性:可以通过自定义函数、存储过程和......
  • SqlServer 导入 MySql
    1、将sqlserver的数据导入到mysql中本文演示:将sqlserver中的数据库表结构和数据导入到mysql中2、sqlserver库3、navicat逆向表到模型4、转换模型为...5、选择mysql6、建模转换后导出sql7、导出sql后,在mysql所在的数据库中,运行该sql文件(该sql为表结构)8、运行完毕后,......
  • 【jmeter】记一次服务带宽的流量模型测试
    一、场景   服务在进行压力测试的过程中,出现里流量带宽被打满的情况,导致接口请求耗时增加,所以需要评估并发上去之后,流量的走势,预估带宽的占用  二、场景设置   使用jmeter进行场景设计   每次递增10个业务,业务执行300秒,直到100个业务对象  三、执行压......
  • Django 将现有字段更改为外键
    我有一个Django模型,以前看起来像这样:classCar(models.Model):manufacturer_id=models.IntegerField()还有另一个模型,称为Manufacturer,id字段引用了该模型。但是,我意识到使用Django的内置外键功能会很有用,因此我将模型更改为:classCar(mode......
  • 多任务 bert 用于 5 个类别的多标签分类
    我构建并微调了5个基于BioClinicalBERT的模型(微调bert)来预测以下类别的医疗记录标签:specialties=["aud","den","oph","oto","psy","tbi"]clinical_summaries=["consultation","hospital_discharge",&q......
  • 为什么我似乎无法通过编程证明从 jpeg 创建的 png 对于任何给定像素都是相同的?
    因此png是无损的-这意味着它们以某种方式压缩,因此它们代表的数据不会丢失。因此,假设颜色深度和yaddayadda相同,从jpeg创建的png应该是像素相同的。果然,这很容易证明,只需打开Krita或GIMP,然后在像素级别手动检查jpeg与从jpeg创建的png,读取相同任意像素的单独RGB......
  • conda 中的“torch”和“pytorch”版本不匹配
    我在环境中安装了以下内容:cudatoolkit=11.3pytorch=1.11.0torchvision=0.12.0但是当询问torch/cuda版本时,我得到了这个:$python3-c"importtorch;print(torch.__version__)"1.12.1+cu102这不是我想要的(1.12.1就可以了,但是cu102太旧了)。进一步看,我看到这......
  • 无法在 Jupyter Notebook 中导入 Pytorch 模块
    我在激活虚拟环境时使用conda命令安装了pytorch。但是,当我在JupyterNotebook中导入torch模块时出现一些问题。我在提示符和JupyterNotebook中检查了sys.path。|||嗯..在提示符中,的结果是sys.path,并且导入torch模块时没有错误。['','/home/u......