首页 > 其他分享 >关于集群节点timeline不一致的处理方式

关于集群节点timeline不一致的处理方式

时间:2024-03-30 11:57:14浏览次数:15  
标签:wal postgres timeline 集群 pg rewind 节点 history

关于集群节点 timeline 不一致的处理方式
本文出处:https://www.modb.pro/db/400223

在 PostgreSQL/MogDB/openGauss 数据库日常维护过程中,如果多次对数据库进行角色切换,可能会出现 timeline 不一致的情况,导致备库不能正常加入到数据库集群,现在以 PG 为例对这些可能发生的情况进行复现,并进行整理。

timeline 介绍
为了将基于时间点恢复后生成的 WAL 记录序列与初始数据库历史中产生的 WAL 记录序列区分开来,避免原来的 wal 文件被覆盖,同时也为了避免管理混乱,PostgreSQL 数据库引入了“时间线”的概念,使其可以通过备份恢复到任何之前的状态,包括早先被放弃的时间线分支中的状态。

当一次归档恢复完成,一个新的时间线被创建来标识恢复之后生成的 WAL 记录序列。时间线 ID 号是 WAL 段文件名的一部分,因此一个新的时间线不会重写由之前的时间线生成的 WAL 数据。

场景一
--主库日志
ERROR: requested starting point 0/8000000 on timeline 1 is not in this server's history
DETAIL: This server's history forked from timeline 1 at 0/6018D98.
STATEMENT: START_REPLICATION 0/8000000 TIMELINE 1

--备库日志
LOG: new timeline 2 forked off current database system timeline 1 before current recovery point 0/80000A0
FATAL: could not start WAL streaming: ERROR: requested starting point 0/8000000 on timeline 1 is not in this server's history
DETAIL: This server's history forked from timeline 1 at 0/6018D98.
发生场景

备库 promote 为主库,源主库以备库的方式重新加入集群
以备份的方式恢复为新主库,源主库以备库的方式加入集群
处理方式

重建备库,适用数据量较小的数据库
借助 pg_rewind 工具,推荐使用这种方式 pg_rewind 会把所有的配置文件都覆盖,建议提前做好备份 并在启动前添加 recovery.conf 或 standby.signal 文件
pg_rewind 相关报错

pg_rewind: fatal: target server needs to use either data checksums or "wal_log_hints = on"
即使数据库已经开启了wal_log_hints = on,依然报这个错,这时需要以primary的形式重启一下数据库。

pg_rewind: source and target cluster are on the same timeline
pg_rewind: no rewind required
主备时间线一致,无法直接使用,这时需要让目标节点先以备库的方式运行,然后通过promote提升为主节点,增加timeline,再次执行pg_rewind

pg_rewind: fatal: could not find common ancestor of the source and target cluster's timelines
建议直接重建备库
场景二
--备库启动失败
LOG: entering standby mode
FATAL: requested timeline 2 is not a child of this server's history
DETAIL: Latest checkpoint is at 0/8000028 on timeline 1, but in the history of the requested timeline, the server forked off from that timeline at 0/6018D98.
LOG: startup process (PID 1059) exited with exit code 1

发生场景

在场景一中启动数据库,会将新主库的 00000002.history 传输到备库本地
[postgres@bogon pg_wal]$ ls -l
total 49160
-rw-------. 1 postgres postgres 332 May 5 20:52 000000010000000000000004.00000028.backup
-rw-------. 1 postgres postgres 16777216 May 6 08:54 000000010000000000000008
-rw-------. 1 postgres postgres 16777216 May 6 08:49 000000010000000000000009
-rw-------. 1 postgres postgres 16777216 May 6 08:54 00000001000000000000000A
-rw-------. 1 postgres postgres 32 May 6 08:58 00000002.history
drwx------. 2 postgres postgres 88 May 6 08:58 archive_status

处理方式

将pg_wal、archive_status 和 归档目录 中的 00000002.history 删除即可
[postgres@bogon pg_wal]$ rm -f 00000002.history
[postgres@bogon pg_wal]$ cd archive_status/
[postgres@bogon archive_status]$ ls -l
total 0
-rw-------. 1 postgres postgres 0 May 5 20:52 000000010000000000000004.00000028.backup.done
-rw-------. 1 postgres postgres 0 May 6 08:58 00000002.history.done
[postgres@bogon archive_status]$ rm -rf *
[postgres@bogon archive_status]$

场景三
LOG: started streaming WAL from primary at 0/7000000 on timeline 2
FATAL: could not receive data from WAL stream: ERROR: requested starting point 0/7000000 is ahead of the WAL flush position of this server 0/601A5D8
cp: cannot stat ‘/data/pgarchive/00000003.history’: No such file or directory
cp: cannot stat ‘/data/pgarchive/000000020000000000000007’: No such file or directory

发生场景

备库以单机(未加入集群,以 primary 的角色)的方式启动过,虽然时间线没变,但是 wal 文件已经不一致
处理方式 此时由于备库的需要从 0/7000000 开始进行重放,已经比主库的 0/601A5D8 提前,说明此时数据库已经不一致。 尝试过修改通过 pg_resetwal 修改 timeline,也尝试过通过 pg_switch_wal()切换 wal 文件,依然无法通过 pg_rewind 进行处理,原因是 wal 不连续,只能选择重建

--修改timeline
postgres=# SELECT timeline_id,redo_wal_file FROM pg_control_checkpoint();
timeline_id | redo_wal_file
-------------+--------------------------
2 | 00000002000000000000000F
(1 row)

$pg_resetwal -l 000000030000000000000010 /data/pgdata14/
Write-ahead log reset

--修改时间线
postgres=# SELECT timeline_id,redo_wal_file FROM pg_control_checkpoint();
timeline_id | redo_wal_file
-------------+--------------------------
3 | 000000030000000000000012
(1 row)

--切换wal
postgres=# select pg_switch_wal();
$ pg_ctl promote -D /data/pgdata14

总结
备库在运行过程中,以 promote 的方式提升为主,即使有数据写入,只要 wal 完整,也可以使用 pg_rewind 回退. 在 pg_rewind 完成后启动,注意修改参数文件、hba 文件、清理归档日志及添加 standby.signal/recovery.conf
备库在运行过程中,以主库的方式重启过,即使没有任何操作,也没有办法回退,只能重建 只要中间以主库运行过,wal 就没有办法连续了

标签:wal,postgres,timeline,集群,pg,rewind,节点,history
From: https://www.cnblogs.com/helloopenGauss/p/18105293

相关文章

  • openGauss 基于流复制的资源池化主备双集群容灾
    基于流复制的资源池化主备双集群容灾可获得性本特性自openGauss6.0.0版本开始引入,仅适用于资源池化架构。特性简介本特性采用流复制能力来实现主备双集群的xlog日志同步,保证主备双集群的xlog一致性,从而增强主备双集群的容灾能力,降低存储空间,并保证主备集群内节点切换、主备集......
  • openGauss 基于Dorado存储同步复制的主备双集群容灾
    基于Dorado存储同步复制的主备双集群容灾可获得性本特性自openGauss5.1.0版本开始引入,仅适用于资源池化架构。特性简介本特性采用Dorado存储的同步复制能力来实现主备双集群的xlog日志同步,保证主备双集群xlog日志实时一致性,从而提升主备双集群的事务性能,降低存储空间,并保证主......
  • 使用Docker搭建Redis Cluster集群
    Cluster模式是Redis的一种高级集群模式,它通过数据分片和分布式存储实现了负载均衡和高可用性。在Cluster模式下,Redis将所有的键值对数据分散在多个节点上。每个节点负责一部分数据,称为槽位。通过对数据的分片,Cluster模式可以突破单节点的内存限制,实现更大规模的数据存储。Redis......
  • 代码随想录第22天 | 235. 二叉搜索树的最近公共祖先 701.二叉搜索树中的插入操作 450.
    235. 二叉搜索树的最近公共祖先 235.二叉搜索树的最近公共祖先-力扣(LeetCode)代码随想录(programmercarl.com)二叉搜索树找祖先就有点不一样了!|235.二叉搜索树的最近公共祖先_哔哩哔哩_bilibili给定一个二叉搜索树,找到该树中两个指定节点的最近公共祖先。百度百......
  • Consul服务注册发现集群搭建
    Consul是一种用于服务发现、配置和分布式一致性的开源工具和平台。它由HashiCorp公司开发和维护,旨在简化构建和维护分布式系统的任务。Consul提供了许多功能,包括:服务发现:Consul允许服务注册和发现。当服务启动时,它可以向Consul注册自己的位置和元数据。其他服务可以通过Co......
  • C++项目——集群聊天服务器项目(七)Model层设计、注册业务实现
    在前几节的研究中,我们已经实现网络层与业务层分离,本节实现数据层与业务层分离,降低各层之间的耦合性,同时实现用户注册业务。网络层专注于处理网络通信与读写事件业务层专注于处理读写事件到来时所需求的各项业务数据层专注于与底层数据库间进行增删改查。数据库中有User、Fr......
  • KingbaseES V8R3集群运维案例之---failover切换后新主库启动过程
    案例说明:KingbaseESV8R3集群failover切换后,在生产环境中,新主库启动过程中可能会有业务访问,出现‘系统只读’的问题。如下图所示:适用版本:KingbaseESV8R3一、问题分析1、如下所示,failover切换过程:1)在master节点执行failover_stream.sh脚本执行failover切换。2)ping网关地......
  • KingbaseES V8R6集群运维案例之---PGPASSWORD变量导致esrep用户连接主库失败
    案例说明:KingbaseESV8R6集群,在备库执行clone时,esrep用户认证失败,导致clone失败。适用版本:KingbaseESV8R6一、问题现象如下所示,在执行备库clone是,esrep认证失败:备库sys_log日志:(esrep用户认证失败)二、问题分析对于KingbaseESV8R6集群,esrep的用户通过~/.encpwd建立认证(......
  • KingbaseES V8R6集群运维案例之---备库register故障
    案例说明:据现场实施人员说,备库执行了clone,启动数据库服务,执行'repmgrstandbyregister'后,无法将备库register到集群。适用版本:KingbaseESV8R6一、问题现象如下图所示,执行'repmgrstandbyregister',register失败:二、问题分析1、repmgrstandbyregister分析如下图所示:......
  • KingbaseES V8R3集群运维案例之---集群启动“DATA_SIZE_DIFF 16 (MB)”故障
    案例说明:为保证集群数据的一致性安全,在主备库的数据相差“DATA_SIZE_DIFF>=16M"以上时,该备库不能参与主备切换,并且通过kingbase_monitor.sh启动集群时,集群将无法启动;本案例对此种故障做了复现,并测试了解决方法。适用版本:KingbaseESV8R3适用版本:KingbaseESV8R3一、案例......