典型场景示例
============

本章是根据上一章节“集群切换原理概述”中描述的原理以及相关时间计算公式，进行实际环境测试。测试不同配置、不同故障场景下的切换时间。

本章节包含以下内容：

- 演练环境介绍_
- 切换过程介绍_
- 进程故障_
- 设备重启_
- 电源故障_
- 网络故障_

演练环境介绍
-----------------

.. table:: 硬件配置说明
   :widths: 2 6
   :class: longtable

   +----------+----------------------------------------------------------+
   | 操作系统 | Linux 28-123-HA03 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 |
   |          | 23:39:32 UTC 2018 x86_64 GNU/Linux                       |
   +----------+----------------------------------------------------------+
   | CPU      | 12 CPUs x Intel(R) Xeon(R) Bronze 3104 CPU @ 1.70GHz     |
   +----------+----------------------------------------------------------+
   | 内存     | 239.47 GB                                                |
   +----------+----------------------------------------------------------+
   | 磁盘     | SATA盘：                                                 |
   |          |                                                          |
   |          | Vendor: VMware                                           |
   |          |                                                          |
   |          | Product: Virtual disk                                    |
   |          |                                                          |
   |          | Revision: 2.0                                            |
   |          |                                                          |
   |          | Compliance: SPC-4                                        |
   |          |                                                          |
   |          | User Capacity: 107,374,182,400 bytes： [107 GB]          |
   |          |                                                          |
   |          | Logical block size: 512 bytes                            |
   |          |                                                          |
   |          | LU is fully provisioned                                  |
   +----------+----------------------------------------------------------+
   | 网卡     | Supported ports: [ TP ]                                  |
   |          |                                                          |
   |          | Supported link modes:                                    |
   |          |                                                          |
   |          | 1000baseT/Full                                           |
   |          |                                                          |
   |          | 10000baseT/Full                                          |
   |          |                                                          |
   |          | Speed: 10000Mb/s                                         |
   |          |                                                          |
   |          | Duplex: Full                                             |
   |          |                                                          |
   |          | Port: Twisted Pair                                       |
   |          |                                                          |
   |          | PHYAD: 0                                                 |
   |          |                                                          |
   |          | Transceiver: internal                                    |
   |          |                                                          |
   |          | Auto-negotiation: off                                    |
   |          |                                                          |
   |          | Supports Wake-on: uag                                    |
   |          |                                                          |
   |          | Wake-on: d                                               |
   |          |                                                          |
   |          | Link detected: yes                                       |
   +----------+----------------------------------------------------------+


操作系统参数配置: 详见《 :ref:`金仓数据守护集群和读写分离集群使用手册` 》 配置文件说明 章节，系统参数检查下的系统参数配置表。



切换过程介绍
-----------------

本节将介绍KingbaseES在不同场景的故障情况下，在相同的切机流程中的不同判断逻辑。具体情况请参考下列表格：

.. table:: 不同场景节点切换过程说明
   :widths: 1 2 2 2 2
   :class: longtable

   +--------------------+-------------------------------------------------------------------------------------+
   |                    |                                    **故障场景**                                     |
   |                    |                                                                                     |
   |    **切机流程**    +--------------------+--------------------+---------------------+---------------------+
   |                    |      进程故障      |      设备重启      |      电源故障       |      网卡故障       |
   |                    |                    |                    |                     |                     |
   +====================+====================+====================+=====================+=====================+
   |repmgrd监控判断     |检测到上游节点连接错误，时长为（0~monitor_interval_secs)。                           |
   |                    |                                                                                     |
   +--------------------+--------------------+--------------------+---------------------+---------------------+
   |主节点故障判断      |备节点通过libkci判断|备节点通过libkci判断|当连接超时时长达到或 |当连接超时时长达到或 |
   |                    |主库故障，瞬时返回  |主库故障时长，取决于|超过(((reconnect\ _  |超过(((reconnect\ _  |
   |                    |结果，确认主库故障。|服务重启时间，最长  |attempts-1)*reconnect|attempts-1)*reconnect|
   |                    |时长为(reconnect    |时 长为(((reconnect |_interval)+((        |_interval)+((        |
   |                    |_attempts-1)*       |_attempts-1)*       |reconnect_attempts-1)|reconnect_attempts-1)|
   |                    |reconnect_interval。|reconnect_interval)+|* connect_timeout))，|*connect_timeout))， |
   |                    |                    |((reconnect_attempts|可判断主节点故障。   |可判断主节点故障。   |
   |                    |                    |-1）*connect_       |                     |                     |
   |                    |                    |timeout))。         |                     |                     |
   |                    |                    |                    |                     |                     |
   +--------------------+--------------------+--------------------+---------------------+---------------------+
   |备升主操作          |所有备库终止wal_receiver进程，备节点选举出新主，进行升主操作，时长为                 |
   |                    |(1~(5+sibling_nodes_disconnect_timeout))。                                           |
   |                    |                                                                                     |
   +--------------------+-------------------------------------------------------------------------------------+
   |网关连通性测试      |检测信任网关,时长为（T1~(reconnect_attempts-1)*2+T1)，默认ping命令耗时为2s，         |
   |                    |T1为第一次成功的时间。                                                               |
   +--------------------+--------------------+--------------------+---------------------+---------------------+
   |VIP管理             |存在VIP时，主节点   |此故障场景中，可能  |此故障场景中，可能   |因为断网，原主节点上 |
   |                    |卸载VIP，新主加载   |存在VIP能ping通但   |存在VIP能ping通但    |的VIP无需卸载，      |
   |                    |VIP，两个过程串行执 |无法卸载的情况。    |无法卸载的情况。     |新主节点可以直接     |
   |                    |行，过程时长为二者  |所以，此步骤的时    |所以，此步骤的时     |加载成功，时长为加   |
   |                    |时间的叠加。若无VIP |间也是范围值(T1~    |间也是范围值(T1~     |载VIP的时长。        |
   |                    |，则跳过该步骤。    |(T1+scmd_option:    |(T1+scmd_option:     |                     |
   |                    |                    |ConnectTimeout+     |ConnectTimeout+      |                     |
   |                    |                    |vip_timeout))。     |vip_timeout))。      |                     |
   |                    |                    |                    |                     |                     |
   +--------------------+--------------------+--------------------+---------------------+---------------------+



进程故障
---------

本节聚焦于KingbaseES服务进程故障的实际场景。通过采取主动终止数据库服务进程的方式，模拟进程故障，以进行集群主节点切换的实战演练。

.. _场景参数设置及预期-1:

场景参数设置及预期
^^^^^^^^^^^^^^^^^^^

.. table:: 进程故障参数配置示例

   =========================================== ===========================================
   **参数配置1(单位：秒)**                     **参数配置2(单位：秒)**
   =========================================== ===========================================
   monitor_interval_secs=1                     monitor_interval_secs=2
                                       
   reconnect_attempts=2                        reconnect_attempts=6
                                       
   reconnect_interval=3                        reconnect_interval=10
                                       
   sibling_nodes_disconnect_timeout=30         sibling_nodes_disconnect_timeout=60
                                       
   connect_timeout=10                          connect_timeout=10
                                       
   ConnectTimeout=10                           ConnectTimeout=10
                                       
   recovery=automatic                          recovery=automatic
                                       
   vip_timeout=30                              vip_timeout=60
                                       
   wal_sender_timeout=60                       wal_sender_timeout=60
   =========================================== ===========================================



根据示例参数，预期时间如下：

**例如：参数配置1 已配置vip 已配置1个信任网关**

.. table:: 进程故障参数配置1理论耗时

   ============================================ ========================
   **流程**                                     **耗时(单位：秒)**
   ============================================ ========================
   监控检测间隔(monitor_interval_secs)          0~1
   连接上游节点失败，重试连接                   3
   检测是否所有备节点已杀掉wal_receiver进程     1~35
   卸载原主节点vip，加载新主节点vip耗时         2
   ping 信任网关                                2~4
   总计                                         6~45
   ============================================ ========================

**例如：参数配置2 已配置vip 已配置1个信任网关**

.. table:: 进程故障参数配置2理论耗时

   ================================================ =======================
   **流程**                                         **耗时(单位：秒)**
   ================================================ =======================
   监控检测间隔(monitor_interval_secs)              0~2
   连接上游节点失败，重试连接                       50
   检测是否所有备节点已杀掉wal_receiver进程         1~65
   卸载原主节点vip，加载新主节点vip耗时             2
   ping 信任网关                                    2~12
   总计                                             53~126
   ================================================ =======================

.. _切机时间-1:

切机时间
^^^^^^^^^^^^

.. table:: 进程故障业务恢复时间
   :widths: 2 2 1 1 3 3   
   :class: longtable

   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | **参数配置** | **流复制配置** | **是否配置Vip** | **配置信任网关 | **杀掉           | **杀掉             |
   |              | **同步/异步**  |                 | 个数**         | 主节点db**       | 备节点db**         |
   |              |                |                 |                | **业务恢复时间** | **业务恢复时间**   |
   |              |                |                 |                | （会发生切机）   | （不会发生切机，   |
   |              |                |                 |                |                  | 业务仅等待同步     |
   |              |                |                 |                |                  | 转异步时间后恢复） |
   +==============+================+=================+================+==================+====================+
   | 参数配置1    | 同步           | 否              | 1              | 7                | 1                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 否              | 1              | 6                |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 否              | 1              | 55               | 2                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 否              | 1              | 53               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 同步           | 是              | 1              | 10               | 1                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 是              | 1              | 9                |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 是              | 1              | 57               | 2                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 是              | 1              | 56               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+



设备重启
--------

本节聚焦于KingbaseES服务设备重启故障的实际场景。通过reboot命令重启操作系统，模拟数据库设备重启故障，以进行集群主节点切换的实战演练。

.. _场景参数设置及预期-2:

场景参数设置及预期
^^^^^^^^^^^^^^^^^^^^^^^

.. table:: 重启故障参数配置示例

   =================================== ===================================
   **参数配置1(单位：秒)**             **参数配置2(单位：秒)**
   =================================== ===================================
   monitor_interval_secs=1             monitor_interval_secs=2
                                       
   reconnect_attempts=2                reconnect_attempts=6
                                       
   reconnect_interval=3                reconnect_interval=10
                                       
   sibling_nodes_disconnect_timeout=30 sibling_nodes_disconnect_timeout=60
                                       
   connect_timeout=10                  connect_timeout=10
                                       
   ConnectTimeout=10                   ConnectTimeout=10
                                       
   recovery=automatic                  recovery=automatic
                                       
   vip_timeout=30                      vip_timeout=60
                                       
   wal_sender_timeout=60               wal_sender_timeout=60
   =================================== ===================================

根据示例参数，预期时间如下：

**例如：参数配置1 已配置vip 已配置1个信任网关**

.. table:: 重启故障参数配置1理论耗时

   ================================================ ========================
   **流程**                                         **耗时(单位：秒)**
   ================================================ ========================
   监控检测间隔(monitor_interval_secs)              0~1
   连接上游节点失败，重试连接                       3~23
   检测是否所有备节点已杀掉wal_receiver进程         1~35
   卸载原主节点vip，加载新主节点vip耗时             2~42
   ping 信任网关                                    2~4
   总计                                             6~105
   ================================================ ========================

**例如：参数配置2 已配置vip 已配置1个信任网关**

.. table:: 重启故障参数配置2理论耗时
   
   ===================================================== ========================
   **流程**                                              **耗时(单位：秒)**
   ===================================================== ========================
   监控检测间隔(monitor_interval_secs)                    0~2
   连接上游节点失败，重试连接                             50~110
   检测是否所有备节点已杀掉wal_receiver进程               1~65
   卸载原主节点vip，加载新主节点vip耗时                   2~72
   ping 信任网关                                          2~12
   总计                                                   53~256
   ===================================================== ========================

特别说明：

在杀掉wal_receiver进程时，会首先去检测是否存在wal_receiver进程，如果存在则会睡眠5秒之后才执行真正的kill命令，当reconnect_attempts和reconnect_interval参数被配置成为2和3时，此时wal_receiver进程还存在，所以会睡眠5秒，但当参数配置成6和10时，wal_receiver进程已经不存在了，则不会睡眠5秒。

.. _切机时间-2:

切机时间
^^^^^^^^^^^^^^^^^^^^^^^

.. table:: 重启故障业务恢复时间
   :widths: 2 2 1 1 3 3   
   :class: longtable

   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | **参数配置** | **流复制配置** | **是否配置Vip** | **配置信任网关 | **reboot         | **reboot           |
   |              | **同步/异步**  |                 | 个数**         | 主节点db所在OS** | 备节点db所在OS**   |
   |              |                |                 |                | **业务恢复时间** | **业务恢复时间**   |
   |              |                |                 |                | （会发生切机）   | （不会发生切机，   |
   |              |                |                 |                |                  | 业务仅等待同步     |
   |              |                |                 |                |                  | 转异步时间后恢复） |
   +==============+================+=================+================+==================+====================+
   | 参数配置1    | 同步           | 否              | 1              | 9                | 1                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 否              | 1              | 8                |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 否              | 1              | 16               | 2                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 否              | 1              | 15               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 同步           | 是              | 1              | 41               | 1                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 是              | 1              | 40               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 是              | 1              | 78               | 2                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 是              | 1              | 77               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+



电源故障
--------

本节聚焦于KingbaseES服务设备电源故障的实际场景。通过shutdown命令关闭操作系统，模拟数据库设备电源故障，以进行集群主节点切换的实战演练。

.. _场景参数设置及预期-3:

场景参数设置及预期
^^^^^^^^^^^^^^^^^^^^

.. table:: 电源故障参数配置示例

   =================================== ===================================
   **参数配置1(单位：秒)**             **参数配置2(单位：秒)**
   =================================== ===================================
   monitor_interval_secs=1             monitor_interval_secs=2
                                       
   reconnect_attempts=2                reconnect_attempts=6
                                       
   reconnect_interval=3                reconnect_interval=10
                                       
   sibling_nodes_disconnect_timeout=30 sibling_nodes_disconnect_timeout=60
                                       
   connect_timeout=10                  connect_timeout=10
                                       
   ConnectTimeout=10                   ConnectTimeout=10
                                       
   recovery=automatic                  recovery=automatic
                                       
   vip_timeout=30                      vip_timeout=60
                                       
   wal_sender_timeout=60               wal_sender_timeout=60
   =================================== ===================================

根据示例参数，预期时间如下：

**例如：参数配置1 已配置vip 已配置1个信任网关**

.. table:: 电源故障参数配置1理论耗时

   ======================================================== ========================
   **流程**                                                 **耗时(单位：秒)**
   ======================================================== ========================
   监控检测间隔(monitor_interval_secs)                      0~1
   连接上游节点失败，重试连接                               3~23
   检测是否所有备节点已杀掉wal_receiver进程                 1~35
   卸载原主节点vip，加载新主节点vip耗时                     2~42
   ping 信任网关                                            2~4
   总计                                                     6~105
   ======================================================== ========================

**例如：参数配置2 已配置vip 已配置1个信任网关**

.. table:: 电源故障参数配置2理论耗时

   ============================================ ========================
   **流程**                                     **耗时(单位：秒)**
   ============================================ ========================
   监控检测间隔(monitor_interval_secs)          0~2
   连接上游节点失败，重试连接                   110
   检测是否所有备节点已杀掉wal_receiver进程     1~60
   卸载原主节点vip，加载新主节点vip耗时         2~72
   ping 信任网关                                2~12
   总计                                         113~256
   ============================================ ========================

.. _切机时间-3:

切机时间
^^^^^^^^^^^^^^^^^^^^

.. table:: 电源故障业务恢复时间
   :widths: 2 2 1 1 3 3   
   :class: longtable   

   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | **参数配置** | **流复制配置** | **是否配置Vip** | **配置信任网关 | **shutdown       | **shutdown         |
   |              | **同步/异步**  |                 | 个数**         | 主节点db所在OS** | 备节点db所在OS**   |
   |              |                |                 |                | **业务恢复时间** | **业务恢复时间**   |
   |              |                |                 |                | （会发生切机）   | （不会发生切机，   |
   |              |                |                 |                |                  | 业务仅等待同步     |
   |              |                |                 |                |                  | 转异步时间后恢复） |
   +==============+================+=================+================+==================+====================+
   | 参数配置1    | 同步           | 否              | 1              | 8                | 1                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 否              | 1              | 6                |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 否              | 1              | 55               | 2                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 否              | 1              | 53               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 同步           | 是              | 1              | 44               | 1                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 是              | 1              | 41               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 是              | 1              | 131              | 2                  |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 是              | 1              | 134              |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+



网络故障
--------

本节聚焦于KingbaseES服务设备网络故障的实际场景。通过断网工具，模拟数据库设备网络故障，以进行集群主节点切换的实战演练。

.. _场景参数设置及预期-4:

场景参数设置及预期
^^^^^^^^^^^^^^^^^^^^

.. table:: 网络故障参数配置示例

   =================================== ===================================
   **参数配置1(单位：秒)**                       **参数配置2(单位：秒)**
   =================================== ===================================
   monitor_interval_secs=1             monitor_interval_secs=2
                                       
   reconnect_attempts=2                reconnect_attempts=6
                                       
   reconnect_interval=3                reconnect_interval=10
                                       
   sibling_nodes_disconnect_timeout=30 sibling_nodes_disconnect_timeout=60
                                       
   connect_timeout=10                  connect_timeout=10
                                       
   ConnectTimeout=10                   ConnectTimeout=10
                                       
   recovery=automatic                  recovery=automatic
                                       
   vip_timeout=30                      vip_timeout=60
                                       
   wal_sender_timeout=60               wal_sender_timeout=60
   =================================== ===================================

根据示例参数，预期时间如下：

**例如：参数配置1 已配置vip 已配置1个信任网关**

.. table:: 网络故障参数配置1理论耗时

   ============================================ ========================
   **流程**                                     **耗时(单位：秒)**
   ============================================ ========================
   监控检测间隔(monitor_interval_secs)          0~1
   连接上游节点失败，重试连接                   3~23
   检测是否所有备节点已杀掉wal_receiver进程     1~35
   卸载原主节点vip，加载新主节点vip耗时         2
   ping 信任网关                                2~4
   总计                                         28~65
   ============================================ ========================

**例如：参数配置2 已配置vip 已配置1个信任网关**

.. table:: 网络故障参数配置2理论耗时

   ============================================ ========================
   **流程**                                     **耗时(单位：秒)**
   ============================================ ========================
   监控检测间隔(monitor_interval_secs)          0~2
   连接上游节点失败，重试连接                   110
   检测是否所有备节点已杀掉wal_receiver进程     1~60
   卸载原主节点vip，加载新主节点vip耗时         2
   ping 信任网关                                2~12
   总计                                         115~186
   ============================================ ========================

.. _切机时间-4:

切机时间
^^^^^^^^^^^^^^^^^^^^

.. table:: 网络故障业务恢复时间
   :widths: 2 2 1 1 3 3   
   :class: longtable
   
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | **参数配置** | **流复制配置** | **是否配置Vip** | **配置信任网关 | **主节点db       | **备节点db         |
   |              | **同步/异步**  |                 | 个数**         | 所在OS网络断开** | 所在OS网络断开**   |
   |              |                |                 |                | **业务恢复时间** | **业务恢复时间**   |
   |              |                |                 |                | （会发生切机）   | （不会发生切机，   |
   |              |                |                 |                |                  | 业务仅等待同步     |
   |              |                |                 |                |                  | 转异步时间后恢复） |
   +==============+================+=================+================+==================+====================+
   | 参数配置1    | 同步           | 否              | 1              | 55               | 61                 |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 否              | 1              | 53               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 否              | 1              | 94               | 62                 |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 否              | 1              | 95               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 同步           | 是              | 1              | 51               | 61                 |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置1    | 异步           | 是              | 1              | 49               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 同步           | 是              | 1              | 96               | 62                 |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+
   | 参数配置2    | 异步           | 是              | 1              | 98               |                    |
   +--------------+----------------+-----------------+----------------+------------------+--------------------+

   