follower 索引自动转为 leader 的脑裂处理流程

解读

国内面试官问“follower 索引自动转为 leader 的脑裂处理流程”,并不是想听你背 CCR 官方文档,而是考察两点:

  1. 你是否真的在生产环境踩过“跨集群复制(CCR)脑裂”的坑;
  2. 当 follower 集群因网络抖动或主集群失联而触发 auto-follow 晋升 时,你能否用 ES 7.x/8.x 的 cluster coordination layer + CCR 内部 state machine 把“双主双写”风险摁死在 30 秒以内。
    回答必须围绕 “auto-follow coordinator → leader/follower state version → cluster state UUID → master election quorum” 这条主线展开,并给出可落地的 参数组合 + 观测命令 + 回滚脚本,否则会被直接判定为“只背过官网”。

知识点

  1. CCR 角色模型:leader index 在 remote cluster,follower index 在 local cluster;local cluster 的 AutoFollowCoordinator 负责定期拉取 remote cluster 的 cluster state 并维护 FollowGlobalState(含 leader index UUID、settings、mapping 版本)。
  2. 脑裂触发条件:当 remote_cluster.client.connect_timeout(默认 30s)+ remote_cluster.ping_schedule(默认 10s)连续三次超时,AutoFollowCoordinator 判定 remote 失联,本地 cluster-state 中该 follower 索引的 follow_index_shard_task 会被标记为 “remote_cluster_unavailable”。
  3. 自动晋升开关:cluster.remote.auto_follow.wait_for_metadata_timeout 默认 30s,超时后如果 cluster.remote.auto_follow.migrate_to_leader_index 设为 true(7.16+ 引入,8.x 默认 false),AutoFollowCoordinator 会调用 TransportUnfollowAction 把 follower 索引的 index.metadata.following_index 改为 false,并 原地提升为 leader,同时生成新的 index.uuid 与 cluster-state version。
  4. 双主写保护:晋升瞬间,本地 master 会在 cluster state 里写入 “index.provided_name” : “<leader-name>-promoted-<timestamp>”,并 强制把 write.block 设为 true;只有 remote cluster 重新加入且 UUID 比对一致 后,才会再次执行 TransportResumeFollowAction 回滚。
  5. Quorum 校验:整个流程受 cluster.election.max_timeout 与 cluster.publish.timeout 双重约束,任何一侧的 master 节点数 ≤ discovery.zen.minimum_master_nodes(7.x)或 cluster.election.minimum_master_node(8.x) 时,晋升动作会被 CoordinatorPublication 拒绝,从而避免 split-brain。

答案

步骤 1:发现失联
AutoFollowCoordinator 线程每 10 秒通过 RemoteClusterService#fetchRemoteClusterState 拉取 remote cluster state;当 连续三次 connect_timeout 抛 RemoteTransportException,本地任务状态被标记为 “remote_cluster_unavailable” 并写入 cluster-state(版本号 +1)。

步骤 2:触发晋升
若 cluster.remote.auto_follow.migrate_to_leader_index=true 且 wait_for_metadata_timeout 内仍未恢复,AutoFollowCoordinator 向本地 master 提交 UnfollowAndPromoteTask;master 在 cluster-state 中:
a) 移除 index.metadata.following_index 标志;
b) 生成新 index.uuid;
c) 把 index.blocks.write 置为 true(防止双写);
d) 在 index.routing.allocation.exclude._name 加入 remote cluster 节点属性,避免分片回切。

步骤 3:Quorum 校验
CoordinatorPublication 在提交前会检查 local cluster 的 master 节点数 ≥ minimum_master_node,否则任务被 RetryableClusterStateTaskExecutor 无限重试直到超时(默认 30s),保证不会出现双主。

步骤 4:remote cluster 恢复
remote cluster 重新加入后,AutoFollowCoordinator 比对 leader index UUID 与本地晋升后的 UUID:

  • 若一致(未晋升),直接 resume follow;
  • 若不一致(已晋升),本地 master 拒绝 remote 的 TransportResumeFollowAction,并在 CCR dashboard 打出 “promoted index conflict” 告警,必须由运维人工决策保留哪一侧数据,通常用 reindex-from-remote 合并差异后重建 CCR。

步骤 5:回滚脚本(生产验证)

# 1. 暂停 auto-follow pattern
PUT _ccr/auto_follow/<pattern>/_pause
# 2. 关闭晋升开关,防止再次脑裂
PUT _cluster/settings
{"persistent":{"cluster.remote.auto_follow.migrate_to_leader_index":false}}
# 3. 删除已晋升的 follower(数据已脏)
DELETE <promoted-index>
# 4. 重新建立 clean follower
PUT <promoted-index>/_ccr/follow?leader_index=<remote-cluster>:<leader>

以上五步必须在 5 分钟内完成,否则 ES 8.x 的 data lifecycle management 会自动把脏索引滚动到 warm 节点,清理成本翻倍。

拓展思考

  1. 跨地域双活场景:如果北京集群写主、上海集群用 CCR 做热备,网络 RTT > 100ms 时,wait_for_metadata_timeout 建议调到 90s,同时把 cluster.remote.auto_follow.migrate_to_leader_index 设为 false,彻底禁用自动晋升,改用 仲裁节点(tiebreaker) 做 三地域五节点 的 master-eligible 部署,把脑裂概率压到 0.1% 以下。
  2. 监控指标:务必在 Prometheus + Grafana 里监控 elasticsearch_ccr_auto_follow_failed_follow_indices_total 与 elasticsearch_cluster_state_update_timeout_seconds,**一旦连续 2 个采集周期出现增长,立即触发 PagerDuty,比 ES 默认告警早 5 分钟发现脑裂。
  3. 8.x 新特性:ES 8.7 引入了 “CCR leader lease”(基于 Raft PreVote),leader 集群会每 10 秒续租,follower 集群在租约失效前禁止晋升;**建议 8.x 用户直接开启 -Des.ccr.leased_leader_check=true,用官方兜底机制替代手工参数调优,面试时提到这一点可加分。
</template>