从源码分析 MGR 的流控机制( 四 )

代码的逻辑看上去有点复杂 。
接下来 , 我们通过一个具体的示例看看 flow_control_step 函数的实现逻辑 。
基于案例定量分析测试集群有三个节点组成:127.0.0.1:33061 , 127.0.0.1:33071 和 127.0.0.1:33081 。
运行在多主模式下 。
使用 sysbench 对 127.0.0.1:33061 进行插入测试(oltp_insert) 。
为了更容易触发流控 , 这里将 127.0.0.1:33061 节点的 group_replication_flow_control_applier_threshold 设置为了 10 。
【从源码分析 MGR 的流控机制】以下是触发流控时 127.0.0.1:33061 的日志信息 。
[Note] [MY-011726] [Repl] Plugin group_replication reported: 'Flow control - update member stats: 127.0.0.1:33061 stats certifier_queue 0, applier_queue 0 certified 7841 (177), applied 0 (0), local 7851 (177), quota 146 (156) mode=1'[Note] [MY-011726] [Repl] Plugin group_replication reported: 'Flow control - update member stats: 127.0.0.1:33071 stats certifier_queue 0, applier_queue 0 certified 7997 (186), applied 8000 (218), local 0 (0), quota 146 (156) mode=1'[Note] [MY-011726] [Repl] Plugin group_replication reported: 'Flow control - update member stats: 127.0.0.1:33081 stats certifier_queue 0, applier_queue 15 certified 7911 (177), applied 7897 (195), local 0 (0), quota 146 (156) mode=1'[Note] [MY-011727] [Repl] Plugin group_replication reported: 'Flow control: throttling to 149 commits per 1 sec, with 1 writing and 1 non-recovering members, min capacity 177, lim throttle 0'以 127.0.0.1:33081 的状态数据为例 , 我们看看输出中各项的具体含义:

  • certifier_queue 0:认证队列的长度 。
  • applier_queue 15:应用队列的长度 。
  • certified 7911 (177):7911 是已经认证的总事务数 , 177 是上一周期进行认证的事务数(m_delta_transactions_certified) 。
  • applied 7897 (195):7897 是已经应用的总事务数 , 195 是上一周期应用的事务数(m_delta_transactions_applied) 。
  • local 0 (0):本地事务数 。括号中的 0 是上一周期的本地事务数(m_delta_transactions_local) 。
  • quota 146 (156):146 是上一周期的 quota_size , 156 是上一周期的 quota_used 。
  • mode=1:mode 等于 1 是开启流控 。
因为 127.0.0.1:33081 中 applier_queue 的长度(15)超过 127.0.0.1:33061 中的 group_replication_flow_control_applier_threshold 的设置(10) , 所以会触发流控 。
触发流控后 , 会调用 flow_control_step 计算下一周期的 m_quota_size 。
1. 循环遍历各节点的状态信息 。集群的吞吐量(min_capacity)取各个节点 m_delta_transactions_certified 和 m_delta_transactions_applied 的最小值 。具体在本例中 ,  min_capacity = min(177, 186, 218, 177, 195) = 177 。
2. min_capacity 不能太小 , 不能低于 lim_throttle 。im_throttle 的取值逻辑如下: