=====================
特殊场景恢复实践案例
=====================

上一章节介绍了通用的恢复场景，本章将举例说明恢复时可能遇到的问题，并解释特殊场景的恢复问题。

指定时间点恢复和指定备份集恢复的区别
---------------------------------------

指定时间点恢复和指定备份集恢复都可以将数据库恢复到同样的某个时刻，指定时间点恢复默认使用距离恢复点最近的备份集，因此二者使用的备份集可能不同。不同的备份集内的非表文件（比如kingbase.conf或sys_hba.conf等配置文件、日志文件）就会有差异，尤其是配置文件内参数的差异就会决定数据库恢复结束后的一些行为，比如最大连接数变了，某个IP段不允许访问数据库了。

无法恢复到最新增量数据
-------------------------

如下图所示，数据库在全量备份1之后创建了T1表， ``位置1`` 为创建T1表之后的时刻，然后又创建了T2表， ``位置2`` 为创建T2表之后的时刻，最后开始模拟恢复场景：

1. 首先恢复到 ``位置1`` ，并且执行了提升节点操作

2. 然后又尝试恢复到 ``位置2`` ，发现看不见T2表

原因是在步骤二恢复时，没有添加 ``--target-timeline`` 参数指定要恢复到时间线1上，因此默认恢复到了最新时间线2上，所以看不到T2表。

.. figure:: images/sys_rman-37.png

恢复后启动数据库报错
-----------------------

如下图所示，数据库在全量备份1之后又做了一次备份为全量备份2，然后开始模拟恢复场景：

1. 首先恢复到 ``位置1`` ，并且执行了提升节点操作

2. 然后，通过时间点恢复尝试恢复到时间线1上的全量备份2之后的 ``位置2`` ，启动数据库失败

原因是在步骤二的指定时间点恢复时没有指定时间线1，恢复过程是这样的：首先找到离 ``位置2`` 最近的备份集为 ``全量备份2`` ，然后恢复备份集，启动数据库后开始从备份集开始的位置 ``0/4000028`` 重放WAL日志，默认又使用最新时间线2，发现时间线2在时间线1上的分歧点 ``0/310DE0`` ， ``0/310DE0`` 到 ``0/4000028`` 这段的日志是不符合时间线2的预期的，因此数据库报错退出。

解决方式就是在步骤二指定时间点恢复时添加时间线1的设置。

.. figure:: images/sys_rman-38.png

恢复后发现查询卡住
---------------------

如下图所示，指定了删除表t1之前的时间点做恢复，恢复后启动数据库查询t1表数据卡住，ksql没有任何查询输出，但执行 ``sys_wal_replay_resume()`` 函数之后，就可以正常查询t1表了，数据也是正常的。

.. figure:: images/sys_rman-39.jpg

原因
~~~~~~

受限于KingbaseES PITR的机制，PITR选定的时间，一般都不是事务结束的时间，所以redo会继续往后走，将下一个事务的结束时间和设定的时间进行对比，如果设定时间小于，就停止了，就是目前数据库的状态：

1. 这时候会有1个或多个事务处于未提交状态，这些未提交事务可能持有锁（例如drop table 持有排他锁，本示例中查询卡住就是因为拿不到t1的共享锁），此时执行 ``sys_wal_replay_resume()`` 函数将这些事务回滚就可以了，对恢复无影响

2. 如果刚好卡在下一条事务是 drop database这种无法回滚的DDL操作，就会出现DDL语句被直接执行（kingbase不支持事务内执行drop database的操作，无法回滚，比如下一小节提到的问题）

.. figure:: images/sys_rman-40.png

解决方法
~~~~~~~~~~

1. 尝试更改指定时间点恢复的目标时刻，让恢复后的状态不卡在特殊SQL未提交前的状态。

2. 改为指定事务ID恢复，事务ID是唯一标志，不会往下继续查时间大小，比如本示例改为xid=928。

.. _复现和定位问题步骤:

复现和定位问题步骤
~~~~~~~~~~~~~~~~~~~~

首先，创建测试表t1灌入一些数据，执行全量备份，随便更新一条t1表的数据，查询当前时间为 ``2022-05-20 17:47:06.674379+08`` ，删掉t1表。

.. figure:: images/sys_rman-41.png

.. figure:: images/sys_rman-42.png

然后，停止数据库，重命名data目录，使用上一步中获得的时间执行指定时间点恢复。

.. code::

  /opt/Kingbase/ES/V8/Server/bin/sys_rman --config /home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase restore --type=time --target='2022-05-20 17:47:06.674379+08'

.. code:: 

   [kingbase@localhost V8]$ /opt/Kingbase/ES/V8/Server/bin/sys_rman --config /home/kingbase/kbbr_repo/sys_rman.conf --stanza=kingbase restore --type=time --target='2022-05-20 17:47:06.674379+08'
   2022-05-20 17:54:23.682 P00 INFO: restore command begin 2.27: --band-width=0 --cmd-ssh=/opt/Kingbase/ES/V8/Server/bin/sys_securecmd --config=/home/kingbase/kbbr_repo/sys_rman.conf --exec-id=3378-40222f80 --link-all --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --non-archived-space=1024 --kb1-path=/opt/Kingbase/ES/V8/data --process-max=4 --repol-path=/home/kingbase/kbbr_repo --stanza=kingbase --target="2022-05-20 17:47:06.674379+08" --type=time
   2022-05-20 17:54:23.708 P00 INFO: repol: restore backup set 20220520-174531F, recovery will start at 2022-05-20 17:45:31 
   2022-05-20 17:54:23.814 P00 INFO: Restore Process: FILE: 1/1756 0% SIZE: 1073152 bytes / 69729678 bytes 1MB / 66.5MB 1%
   2022-05-20 17:54:23.839 P00 INFO: Restore Process: FILE: 2/1756 0% SIZE: 2146304 bytes / 69729678 bytes 2MB / 66.5MB 3%
   2022-05-20 17:54:23.964 P00 INFO: Restore Process: FILE: 3/1756 0% SIZE: 3219456 bytes / 69729678 bytes 3MB / 66.5MB 4%
   2022-05-20 17:54:23.965 P00 INFO: Restore Process. FILE: 4/1756 0% SIZE. 1292608 bytes / 60779678 bytes 4MB / 66.5MB 5%

.. code:: 

   2022-05-20 17:54:39.493 P00 INFO: Restore Process: FILE: 1754 / 1756 99% SIZE: 69729678 bytes / 69729678 bytes 66.5MB / 66.5M 100%
   2022-05-20 17:54:39.694 P00 INFO: Restore Process: FILE: 1755 / 1756 99% SIZE: 69729678 bytes / 69729678 bytes 66.5MB / 66.5M 100%
   2022-05-20 17:54:39.694 P00 INFO: Restore Process: FILE: 1756 / 1756 100% SZIE: 69729678 bytes / 69729678 bytes 66.5MB / 66.5M 100% 
   2022-05-20 17:54:39.695 P00 INF0: write updated /opt/Kingbase/ES/V8/data/kingbase.auto.conf
   2022-05-20 17:54:39.713 P00 INFO: restore global/sys_control (performed last to ensure aborted restores cannot be started)
   2022-05-20 17:54:39.714 P00 INFO: restore size = 66.5MB, file total = 1756
   2022-05-20 17:54:39.714 P00 INFO: restore command end: completed successfully (16043ms)

再然后，启动数据库，查询t1表数据，发现卡住。

.. code:: 

    [kingbasedlocalhost V8]$ /opt/Kingbase/ES/V8/Server/bin/ksql -d test -Usystem
    ksqL (V8.0)
    Type "help" for help.
    WARNING:License file will expire in 29 days.
    test=# select * from t1 limit 10;

然后，获取查询语句对应的KingbaseES后台进程ID为 ``3809`` ，使用pstack命令获取该进程的调用栈，发现是在等待锁。

.. code:: 

    [kingbasealocalhost ~]$ ps -elf | grep "kingbase: system test"
    1 S kingbase 3809 3789 0 80 157782 ep_pol 17:58 ?     00:00:00 kingbase: system test [Local] SELECT waiting 

.. code:: 

    [kingbasealocalhost ~]$ pstack 3809
    #0 0x00007f182f773163 in epoll_wait_nocancel () from /lib64/libc.so.6
    #1 0x00000000007cc983 in WaitEventSetWait ()
    #2 0x00000000007cd264 in WaitLatchOrSocket ()
    #3 0x00000000007da535 in ProcSleep ()
    #4 0x00000000007d7d92 in WaitOnLock ()
    #5 0x00000000007d952a in LockAcquireExtended ()
    #6 0x00000000007d57d7 in LockRelation0id ()
    #7 0x000000000055d81a in RangeVarGetRelidExtended ()
    #8 0x00000000004b44a0 in relation_openrv_extended ()
    #9 0x000000000051c499 in table_openrv_extended ()
    #10 0x00000000005c44e4 in parserOpenTable ()
    #11 0x00000000005c5fb4 in addRangeTableEntry ()
    #12 0x00000000005a726c in transformTabLeEntry ()
    #13 0x00000000005a9bac in transformFromClauseItem ()
    #14 0x00000000005ab6c6 in transformFromClause ()
    #15 0x000000000058658e in transformSelectStmt ()
    #16 0x0000000000587b89 in transformStmt ()
    #17 0x000000000058a7aa in transformTopLevelStmt ()
    #18 0x000000000058a99a in parse_analyze ()
    #19 0x00000000007ed2d7 in pg_analyze_and_rewrite ()
    #20 0x00000000007edd99 in exec_simple_query ()
    #21 0x00000000007f0c60 in PostgresMain ()
    #22 0x000000000077805a in PostmasterMain ()
    #23 0x00000000006c98af in main ()

其次，查看数据库日志，发现恢复停在 ``929`` 事务（结束时间为 ``2022-05-20 17:47:14.538044+08`` ）提交之前，最后使用了 ``000000010000000000000004`` 做的恢复。

.. code:: 

   2022-05-20 17:58:07.710 CST [3792] LOG:database system was interrupted; last known up at 2022-05-20 17:45:32 CST
   2022-05-20 17:58:07.767 P00 INFO: archive-get command begin 2.27: 0000002.history, sys_ wal/RECOVERYHISTORY] --archive-timeout=600 --cmd-ssh=/opt/Kingbase/ES/V8/Server/bin/sys_securecmd --config=/home/kingbase/kbbr_repo/sys_rman.conf --exec-id=3793-cdbd404d --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --kb1-path=/opt/Kingbase/ES/V8/data --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase
   2022-05-20 17:58:07.770 P00 INFO: unable to find 00000002.history in the archive
   2022-05-20 17:58:07.770 P00 INFO: archive-get command end: completed successfully (11ms)
   2022-05-20 17:58:07.771 CST [3792] LOG: starting point-in-time recovery to 2022-05-20 17:47:06.674379+08
   2022-05-20 17:58:07.780 P00 INFO: archive-get command begin 2.27: [00000000000000000000003, sys_wal/RECOVERYXL0G] --archive-timeout=600 --cmd-ssh=/opt/Kingbase/ES/V8/Server/bin/sys_securecmd --config=/home/kingbase/kbbr_repo/sys_rman.conf --exec-id=3796-347d02c1 --1og-level-console=info --log-Level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --kb1-path=/opt/Kingbase/ES/V8/data --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase
   2022-05-20 17:58:07.801 P00 INFO: found 000000000000000000003 in the repol: 12-1 archive
   2022-05-20 17:58:07.801 P00 INFO: archive-get command end: completed successfully (26ms)
   2022-05-20 17:58:07.803 CST [3792] LOG: restored log file "0000000000000000000003" from archive 
   2022-05-20 17:58:08.051 CST [3792] LOG: redo starts at 0/3000028
   2022-05-20 17:58:08.051 CST [3792] LOG:redo wal segment count 1
   2022-05-20 17:58:08.052 CST [3792] LOG:consistent recovery state reached at 0/3000128 
   2022-05-20 17:58:08.054 CST [3789] LOGdatabase system is ready to accept read only connections 
   2022-05-20 17:58:08.064 P00 INFO: archive-get command begin 2.27: [000000010000000000000004, sys_wal/RECOVERYXL0G] --archive-timeout=600 --cmd-ssh=/opt/Kingbase/ES/V8/Server/bin/sys_securecmd --config=/home/kingbase/kbbr_repo/sys_rman.conf --exec-id=3801-ebe65106 --log-level-console=info --log-level-file=info --log-path=/opt/Kingbase/ES/V8/Server/log --log-subprocess --kb1-path=/opt/Kingbase/ES/V8/data --process-max=4 --repo1-path=/home/kingbase/kbbr_repo --stanza=kingbase
   2022-05-20 17:58:08.084 P00 INFO: found 0000000 0000000000000004 in the repol: 12-1 archive 
   2022-05-20 17:58:08.084 P00 INFO: archive-get command end: completed successfully (26ms)
   2022-05-20 17:58:08.085 CST [3792] LOG:  restored log file "0000000000000000000004" from archive
   2022-05-20 17:58:08.247 CST [3792] LOG:  process: 1/1
   2022-05-20 17:58:08.249 CST [3792] LOG:  recovery stopping before commit of transaction 929, time 2022-05-20 17:47:14.538044+08
   2022-05-20 17:58:08.249 CST [3792] LOG: recovery has paused
   2022-05-20 17:58:08.249 CST [3792] HINT: Execute pg_wal_replay_resume() to continue.
   2022-05-20 18:10:58.835 CST [3809] ERROR: canceling statement due to user request
   2022-05-20 18:10:58.835 CST [3809] STATEMENT: select * from t1 limit 10;

然后从sys_rman归档目录中拿到数据库恢复的最后一个wal日志文件 ``000000010000000000000004`` ，并使用sys_waldump输出该文件记录的wal record，发现了事务ID为 ``929`` 的事务是DROP表操作（已经做了排他锁表操作，因此查表时无法拿到该表的共享锁）。

.. code:: 

   [kingbasealocalhost ~]$ mkdir tmp
   [kingbase@localhost ~]$ cd tmp
   [kingbase@localhost tmp]$
   [kingbase@localhost tmp]$
   [kingbase@localhost tmp]$ cp ~/kbbr_repo/archive/kingbase/12-1/000000000000/000000010000000000000004-a36704d0dfc04e0e84c9dbd863c97c2791e18285 .
   [kingbase@localhost tmp]$ mv 000000010000000000000004-a36704d0dfc04e0e84c9dbd863c97c2791e182850000000 000000010000000000000004
   [kingbase@localhost tmp]$ /opt/Kingbase/ES/V8/Server/bin/sys_waldump 000000010000000000000004
   rmgr: Standby     len (rec/tot):    42/    42, tx:      0, lsn: 0/04000028, prev 0/03000128, desc: RUNNING_XACTS nextXid 929 latestCompletedXid 50331944 oldestRunningXid 929
   rmgr: Standby     len (rec/tot):    42/    42, tx:    929, lsn: 0/04000058, prev 0/04000028, desc: LOCK xid 929 db 16173 rel 16384
   rmgr: Standby     len (rec/tot):    42/    42, tx:    929, lsn: 0/04000088, prev 0/04000058, desc: LOCK xid 929 db 16173 rel 16387
   rmgr: Standby     len (rec/tot):    42/    42, tx:    929, lsn: 0/040000B8, prev 0/04000088, desc: LOCK xid 929 db 16173 rel 16389
   rmgr: Heap        len    (rec/tot):    59/  5823, tx:    929, lsn: 0/040000E8, prev 0/040000B8, desc: DELETE off 15 flags 0x00 KEYS_UPDATED，blkref #0: rel 1663/16173/2610 blk 4 FPW 
   rmgr: Heap        len    (rec/tot):    59/  7035, tx:    929, lsn: 0/040017A8, prev 0/040000E8, desc: DELETE off 25 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/ 16173/ 1249 blk 119 FPW


.. code:: 

   rmgr: Heap        len    (rec/tot):    54/    54, tx:    929, lsn: 0/0400DEDO, prev 0/0400DE98, desc: DELETE off 14 flags 0x00 KEYS_UPDATED，blkref #0: rel 1663/16173/1249 blk 119
   rmgr: Heap        len    (rec/tot):    54/    54, tx:    929, lsn: 0/0400DF08, prev 0/0400DEDO, desc: DELETE off 15 flags 0x00 KEYS_UPDATED, blkref #0: rel 1663/16173/1249 blk 119
   rmgr: Heap        len    (rec/tot):    54/    54, tx:    929, lsn: 0/0400DF40, prev 0/0400DF08, desc: DELETE off 5 flags 0x00 KEYS_UPDATED , blkref #0: rel 1663/16173/1259 blk 0  
   rmgr: Heap        len    (rec/tot):    54/    54, tx:    929, lsn: 0/0400DF78, prev 0/0400DF40, desc: DELETE off 95 flags 0x00 KEYS_UPDATED, blkref #0: rel 1663/16173/2608 blk 58
   rmgr: Transaction len    (rec/tot):  1133/  1133, tx:    929, lsn: 0/0400DFB0, prev 0/0400DF78, desc: COMMIT 2022-05-20 17:47:14.53 8044 CST; rels: base/16173/16384 base/16173/16387 base/16173/ 16389; inval msgs: catcache 44 catcache 76 catcache 76 catcache 7 catcac he 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 62 catcache 61 catcache 105 catcache 104 catcache 105 catcache 104 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 7 catcache 6 catcache 62 catcache 61 catcache 105 catcache 104 catcache 39 catcache 7 catcache 6 catcache 7 catcache 6 catcache 62 catcache 61 catcache 44 relcache 16384 snapshot 2608 snapshot 2608 snapshot 2608 relcache 16387 snapshot 2608 snapshot 2608 elcache 16389 relcache 16387 snapshot 2608 

最后，执行 ``sys_wal_replay_resume()`` 函数， ``929`` 事务回滚，排他锁释放，查询t1表正常了。

.. code:: 

   [kingbase@localhost V8]$ /opt/Kingbase/ES/V8/Server/bin/ksql -d test -Usystem
   ksql (V8.0)
   Type "help" for help.
   WARNING:License file will expire in 29 days.
   test=# select pg_wal_replay_resume();
   pg_wal_replay_resume
   (1 row)
   test=# select * from t1 limit 10;
    a |  b
    1 | 2022-05-19 23:26:14.340512+08
    2 | 2022-05-19 23:26:14.340512+08
    3 | 2022-05-19 23:26:14.340512+08
    4 | 2022-05-19 23:26:14.340512+08
    5 | 2022-05-19 23:26:14.340512+08
    6 | 2022-05-19 23:26:14.340512+08
    7 | 2022-05-19 23:26:14.340512+08
    8 | 2022-05-19 23:26:14.340512+08
    9 | 2022-05-19 23:26:14.340512+08 
   10 | 2022-05-19 23:26:14.340512+08
   (10 rows)
   test=#


恢复后发现database被删除
----------------------------

将上一节中的drop table操作改为drop database后，指定drop database操作之前的时刻做恢复，会发现该database异常，无法正常切换到该database做任何操作。因为drop database这种操作无法回滚，所以该database异常，具体复现和分析步骤不再赘述，可参考上一小节自行做验证。

集群备库如何恢复
-------------------

恢复好主库，主库启动后查询数据符合预期，执行 ``sys_wal_replay_resume()`` 提升节点，清空 ``kingbase.auto.conf`` 内的PITR参数设定，再通过 :ref:`repmgr` 重做备机，具体可参考《 :ref:`金仓数据守护集群和读写分离集群使用手册` 》。

