概述
========

本章节包含以下内容：

- 集群切换原理介绍_
- 参数含义_
- 切机时间构成公式_
- 同步转异步时间构成公式_


集群切换原理介绍
----------------------

切换流程及原理请参考： :ref:`故障自动转移`

参数含义
-----------


.. table:: 参数含义
   :widths: 4 4 2
   :class: longtable

   +----------------------------------+---------------------------------+-----------------------+
   | 参数名                           | 参数说明                        | 参数默认值            |
   +==================================+=================================+=======================+
   | monitor_interval_secs            | repmrd监控执行间隔              | 2 (秒)                |
   +----------------------------------+---------------------------------+-----------------------+
   | reconnect_attempts               | 连接失败或者ping信              | 10 (次)               |
   |                                  | 任网关失败后重试次数            |                       |
   +----------------------------------+---------------------------------+-----------------------+
   | sibling_nodes_disconnect_timeout | 检测是否所有备节点都已成功      | 30 (秒)               |
   |                                  | kill                            |                       |
   |                                  | wal_receiver进程时间            |                       |
   +----------------------------------+---------------------------------+-----------------------+
   | reconnect_interval               | 重试间隔时间，最小为3（秒）     | 6 (秒)                |
   +----------------------------------+---------------------------------+-----------------------+
   | connect_timeout                  | libkci连接超时                  | 10(秒)                |
   +----------------------------------+---------------------------------+-----------------------+
   | ConnectTimeout                   | scmd_options参数中              | 10(秒)                |
   |                                  | ConnectTimeout的                |                       |
   |                                  | 值为ssh命令超时时间             |                       |
   +----------------------------------+---------------------------------+-----------------------+
   | recovery                         | 恢复模式，可配置为，automatic-- | standby               |
   |                                  | 自动恢复故障主库或备库          |                       |
   |                                  | ，standby-- 自动恢复故障备库    |                       |
   |                                  | ，manual-- 手动恢复             |                       |
   +----------------------------------+---------------------------------+-----------------------+
   | vip_timeout                      | 升主过程中卸载旧主节点的        | 60(秒)                |
   |                                  | vip 过程超时时间                |                       |
   +----------------------------------+---------------------------------+-----------------------+
   | wal_sender_timeout               | 数据库配置参数                  | 30(秒)                |
   |                                  | ，wal_sender                    |                       |
   |                                  | 进程超时退出时间                |                       |
   +----------------------------------+---------------------------------+-----------------------+
   | T1                               | 判断网络是否正常耗时            | ping                  |
   |                                  |                                 | 第一个信任网关第      |
   |                                  |                                 | 一次就成功，目前默认  |
   |                                  |                                 | ping命令耗时2（秒）   |
   +----------------------------------+---------------------------------+-----------------------+
   | T2                               | 卸载原主节点vip，               | 根据是否配置vip       |
   |                                  | 加载新主节点vip耗时             | ,T2耗时0秒或者X(秒),  |
   |                                  |                                 | X具体值详见公式说明。 |
   +----------------------------------+---------------------------------+-----------------------+


切机时间构成公式
--------------------

.. figure:: images/cluster-switch-image5.png
   :width: 5.76806in
   :height: 3.93713in

每隔monitor_interval_secs秒，hamgrd进行一次监控检测。根据切换流程可以计算切换所需要的时间。

   最短切机时间为：0 + ((reconnect_attempts - 1) \* reconnect_interval) + 1 + T1 + T2

   最长切机时间为：monitor_interval_secs + ((reconnect_attempts - 1) \* reconnect_interval
   + reconnect_attempts \* connect_timeout) + 5 + sibling_nodes_disconnect_timeout
   + (reconnect_attempts - 1) \* 2 + T1 + T2

切换时间公式：

   (0 ~ monitor_interval_secs) + (((reconnect_attempts - 1) \*
   reconnect_interval) ~ ((reconnect_attempts - 1) \* reconnect_interval +
   reconnect_attempts \* connect_timeout)) +
   (1 ~ (5 + sibling_nodes_disconnect_timeout)) + (T1 ~ (reconnect_attempts - 1) \*
   2 + T1 + T2)

.. table:: 切机时间构成公式说明
   :widths: 3 2 6
   :class: longtable

   +-------------------------+----------------+-------------------------------------------------------------------------------+
   | 监控耗时                |     公式       | (0~monitor_interval_secs)                                                     |
   |                         +----------------+-------------------------------------------------------------------------------+
   |                         |    耗时说明    | 取决于距离上一次repmrd执行监控检测多久。                                      |
   |                         |                |                                                                               |
   |                         |                | 最小值为：0                                                                   |
   |                         |                |                                                                               |
   |                         |                | 距离上次检测monitor_interval_secs                                             |
   |                         |                | 秒，立马开始下次检测。                                                        |
   |                         |                |                                                                               |
   |                         |                | 最大值为：monitor_interval_secs                                               |
   |                         |                |                                                                               |
   |                         |                | 距离上次检测0秒，需等待monitor_interval_secs秒才开始下次检测。                |
   +-------------------------+----------------+-------------------------------------------------------------------------------+
   | 判断上游节点故障耗时    |   公式         | ( ((reconnect_attempts - 1) \*                                                |
   |                         |                | reconnect_interval ) ~                                                        |
   |                         |                | ((reconnect_attempts - 1) \*                                                  |
   |                         |                | reconnect_interval +                                                          |
   |                         |                | reconnect_attempts \*                                                         |
   |                         |                | connect_timeout))                                                             |
   |                         +----------------+-------------------------------------------------------------------------------+
   |                         |  耗时说明      |                                                                               |
   |                         |                | 最小值为:                                                                     |
   |                         |                |                                                                               |
   |                         |                | (reconnect_attempts - 1) \*                                                   |
   |                         |                | reconnect_interval                                                            |
   |                         |                | 意为：每次上游节点都是立即返回错误                                            |
   |                         |                | ，重试次数完成后确认上游节点故障。                                            |
   |                         |                |                                                                               |
   |                         |                | 最大值为：                                                                    |
   |                         |                |                                                                               |
   |                         |                | ((reconnect_attempts - 1) \*                                                  |
   |                         |                | reconnect_interval +                                                          |
   |                         |                | reconnect_attempts \*                                                         |
   |                         |                | connect_timeout))                                                             |
   |                         |                |                                                                               |
   |                         |                | 第一次检测上游节点失败是因为连接超时，                                        |
   |                         |                | 并且之后的每次重试都是因为超时连接才失败的，                                  |
   |                         |                | 重试次数完成后确认上游节点故障。                                              |
   +-------------------------+----------------+-------------------------------------------------------------------------------+
   | 备节点杀掉wal_receiver  |    公式        | (1 ~ (5 + sibling_nodes_disconnect_timeout))                                  |
   | 进程耗时                |                |                                                                               |
   |                         +----------------+-------------------------------------------------------------------------------+
   |                         |   耗时说明     | 最小值为：1                                                                   |
   |                         |                |                                                                               |
   |                         |                | 在确认主节点故障，准备升主时，备节点会kill自己的wal_receiver进程，            |
   |                         |                | 同时去检测是否所有的备节点都已kill该进程，每秒检测一次，                      |
   |                         |                | 确认所有备节点都kill掉wal_receiver进程后即视为成功                            |
   |                         |                | ，所以最短为1秒，即第一次检测所有备节点都成功kill掉wal_receiver。             |
   |                         |                |                                                                               |
   |                         |                | 最大值为：                                                                    |
   |                         |                | 5 + sibling_nodes_disconnect_timeout                                          |
   |                         |                |                                                                               |
   |                         |                | 先睡眠5秒，然后kill掉自己的wal_receiver进程后，                               |
   |                         |                | 每隔1秒检测一次其他备节点是否kill掉wal_receiver进程，                         |
   |                         |                | 最多检测sibling_nodes_disconnect_timeout秒后，                                |
   |                         |                | 即会默认所有备节点已成功kill wal_receiver进程，                               |
   |                         |                | 然后开始下一阶段的操作。                                                      |
   +-------------------------+----------------+-------------------------------------------------------------------------------+
   | 判断网络是否正常        |  公式          | (T1 ~ (reconnect_attempts - 1) \* 2 + T1)                                     |
   |                         +----------------+-------------------------------------------------------------------------------+
   |                         |   耗时说明     |                                                                               |
   |                         |                | 最小值为： T1                                                                 |
   |                         |                |                                                                               |
   |                         |                | ping第一个信任网关第一次就成功，                                              |
   |                         |                | 目前默认ping命令耗时2秒。                                                     |
   |                         |                |                                                                               |
   |                         |                | 最大值为：(reconnect_attempts - 1) \* 2 + T1                                  |
   |                         |                |                                                                               |
   |                         |                | 重试reconnect_attempts - 1都失败了，每次耗时2                                 |
   |                         |                | 秒，最后一次ping命令成功，用时为T1。                                          |
   +-------------------------+----------------+-------------------------------------------------------------------------------+
   | 卸载加载vip耗时         |  公式          | T2                                                                            |
   |                         |                |                                                                               |
   |                         |                | 未配置vip：                                                                   |
   |                         |                |                                                                               |
   |                         |                | 0                                                                             |
   |                         |                |                                                                               |
   |                         |                | 配置vip：                                                                     |
   |                         |                |                                                                               |
   |                         |                | T1 ~ (T1 + scmd_option:ConnectTimeout + vip_timeout)                          |
   |                         +----------------+-------------------------------------------------------------------------------+
   |                         |  耗时说明      | 卸载原主节点                                                                  |
   |                         |                | vip，加载新主节点vip耗时，根据是否配置vip,T2耗时0秒或者X秒。                  |
   |                         |                |                                                                               |
   |                         |                | 0 表示没配置vip，所以无耗时。                                                 |
   |                         |                |                                                                               |
   |                         |                | X 表示已配置vip，最短为2秒。                                                  |
   |                         |                |                                                                               |
   |                         |                | 即ping vip 不通，直接执行在新主加载vip或者ping                                |
   |                         |                | vip通，但是vip存在新主上。                                                    |
   |                         |                |                                                                               |
   |                         |                | 最长为：                                                                      |
   |                         |                |                                                                               |
   |                         |                | T1 + scmd_option:ConnectTimeout + vip_timeout                                 |
   |                         |                |                                                                               |
   |                         |                | Ping vip通，且vip不在新主节点上                                               |
   |                         |                | ，执行远程卸载vip命令，远程命令执行超时，进行重试。                           |
   |                         |                |                                                                               |
   |                         |                | 重试最大超时时间                                                              |
   |                         |                | vip_timeout，在vip_timeout秒时执行远程卸载命令成功且ping                      |
   |                         |                | vip ping不通并加载vip成功。                                                   |
   +-------------------------+----------------+-------------------------------------------------------------------------------+


综上所述：

.. table:: 切机时间构成公式综合说明
   :widths: 3 2 9
   :class: longtable


   +-------------------------+-----------------+-------------------------------------------------------------------------------+
   | 切机最短时间            |   公式          | 0 + (((reconnect_attempts - 1) \* reconnect_interval) + 1 + T1 + T2           |
   |                         +-----------------+-------------------------------------------------------------------------------+
   |                         |  耗时说明       | 立即进行监控检测，等待0秒；                                                   |
   |                         |                 |                                                                               |
   |                         |                 | 每次数据库直接返回连接错误，判断上游节点故障耗时                              |
   |                         |                 |                                                                               |
   |                         |                 | ( ((reconnect_attempts - 1) \* reconnect_interval )；                         |
   |                         |                 |                                                                               |
   |                         |                 | 检测是否所有备节点均kill wal_receiver进程，耗时1秒；                          |
   |                         |                 |                                                                               |
   |                         |                 | 备节点升主时ping信任网关一次成功耗时                                          |
   |                         |                 | T1，目前默认ping ip一次耗时2秒；                                              |
   |                         |                 |                                                                               |
   |                         |                 | 未配置vip，T2=0 耗时0秒。                                                     |
   +-------------------------+-----------------+-------------------------------------------------------------------------------+
   | 切机最长时间            |  公式           | monitor_interval_secs + ((reconnect_attempts - 1) \*                          |
   |                         |                 | reconnect_interval + reconnect_attempts \*                                    |
   |                         |                 | connect_timeout)) + 5 + sibling_nodes_disconnect_timeout                      |
   |                         |                 | +reconnect_attempts \* 2 + T1 + T2                                            |
   |                         +-----------------+-------------------------------------------------------------------------------+
   |                         | 耗时说明        | 上一轮检测结束后，等待monitor_interval_secs秒进入下一次监控检测。             |
   |                         |                 |                                                                               |
   |                         |                 | 每次连接数据库都通过超时来判断数据库故障，                                    |
   |                         |                 | 判断上游节点故障耗时：((reconnect_attempt s- 1) \* reconnect_interval +       |
   |                         |                 | reconnect_attempts * connect_timeout))                                        |
   |                         |                 |                                                                               |
   |                         |                 | 备节点升主时，ping最后一个信任网关的最后一次重试才成功耗时：                  |
   |                         |                 |                                                                               |
   |                         |                 | (reconnect_attempts - 1) \* reconnect_interval + T1                           |
   |                         |                 |                                                                               |
   |                         |                 | 已配置vip，耗时：T2 = 2 + scmd_options:ConnectTimeout + vip_timeout秒。       |
   |                         |                 |                                                                               |
   +-------------------------+-----------------+-------------------------------------------------------------------------------+




同步转异步时间构成公式
----------------------------

同步转异步模式耗时时间公式：

.. code::

   (0 ~ monitor_interval_secs) + (0 ~ wal_sender_timeout)



.. table:: 同步转异步时间构成公式说明
   :widths: 3 2 6
   :class: longtable

   +-------------------------+-----------------+-------------------------------------------------------------------------------+
   | 监控时间                |   公式          | (0 ~ monitor_interval_secs)                                                   |
   |                         +-----------------+-------------------------------------------------------------------------------+
   |                         |  耗时说明       | 具体耗时取决于距离上一次repmrd执行监控检测多久。                              |
   |                         |                 |                                                                               |
   |                         |                 | 最小值：0                                                                     |
   |                         |                 |                                                                               |
   |                         |                 | 距离上次检测monitor_interval_secs 秒，立马开始下次检测。                      |
   |                         |                 |                                                                               |
   |                         |                 | 最大值：monitor_interval_secs                                                 |
   |                         |                 |                                                                               |
   |                         |                 | 距离上次检测0秒，需等待monitor_interval_secs秒才开始下次检测。                |
   |                         |                 |                                                                               |
   +-------------------------+-----------------+-------------------------------------------------------------------------------+
   | 备节点故障检测时间      |  公式           |  (0 ~ wal_sender_timeout)                                                     |
   |                         +-----------------+-------------------------------------------------------------------------------+
   |                         | 耗时说明        | 上一轮检测结束后，等待monitor_interval_secs秒进入下一次监控检测。             |
   |                         |                 |                                                                               |
   |                         |                 |                                                                               |
   +-------------------------+-----------------+-------------------------------------------------------------------------------+


综上所述：

.. table:: 同步转异步时间构成公式综合说明
   :widths: 3 2 6
   :class: longtable

   +----------------+-----------+----------------------------------------------------+
   | 同步转异步     |    公式   | 0+0                                                |
   | 最短时间       |           |                                                    |
   |                |           |                                                    |
   |                +-----------+----------------------------------------------------+
   |                |           |  马上进行监控检测，等待0秒                         |
   |                |  耗时说明 |  sys_stat_replication视图在备节点故障后立即更新，  |
   |                |           |  耗时0秒。                                         |
   +----------------+-----------+----------------------------------------------------+
   | 同步转异步     |   公式    | monitor_interval_secs+wal_sender_timeout           |
   | 最长时间       |           |                                                    |
   |                |           |                                                    |
   |                |           |                                                    |
   |                +-----------+----------------------------------------------------+
   |                |           | 上一轮检测结束，等待monitor_interval_secs秒后，    |
   |                | 耗时说明  | 进入下一次监控检测wal_sender                       |
   |                |           | 进程在备节点故障后通过超时的方式判断备节点故障     |
   |                |           | ，然后再更新sys_stat_replication视图。             |
   |                |           |                                                    |
   |                |           |                                                    |
   +----------------+-----------+----------------------------------------------------+
               
