背景

Alluxio 的 Master 以及 Job Master 服务和 Ozone 的 Ozone Manager 以及 Storage Container Manager 服务等,都是基于 Apache Ratis 实现的 HA 高可用服务。对于这些高可用服务,在某些场景,都需要获取 Leader 信息、commit 信息等 Ratis group 内部的信息,也会用在更换机器、滚动升级等场景,需要切换 leader 等必要的功能。目前 Alluxio 和 Ozone 正在分别实现这些功能,而这些功能的实现逻辑相似,两边都在消耗人力在开发以及代码维护上。

因此,经过尝试,把这些 Ratis 控制相关的部分抽离出来,单独实现,而 Alluxio 和 Ozone 只需要使用这一公共的控制软件即可。

整体架构

如上图所示,Alluxio 和 Ozone 两个使用 RATIS 的项目,都是利用 Ratis 实现了 HA 高可用,每个服务都内嵌了 Ratis Server,原本 Ratis 的获取信息以及控制命令,需要由 Alluxio 的 Client 发起请求到 Alluxio 的 Master ,以及由 Ozone 的 Client 发起请求到 Ozone 的 OM/SCM 服务。

抽象后,可以成为最下边的图形所示,只需要共建一个 ratis-shell 软件作为访问 ratis server 的 client 端 shell 封装即可。这样除了 Alluxio、Ozone 之外的任意基于 ratis 的软件,都可以使用 ratis shell 进行 ratis 信息获取以及设置 ratis 的状态了。

支持功能以及使用场景

  • 灰度滚动升级 HA 组件,需要先升级其中一个 Follower 角色的服务,然后让其成为 Leader,测试功能无误后,再升级其它的服务。此时,需要 Leader 切换能力。

  • 当调试 HA 服务组件中的 Leader 时,不希望选取导致正在被调试的 Leader 倒台。此时,需要暂停选取,而调试完后,再恢复选举。

  • 当 HA 服务组件需要替换节点时,需要新增 Peer 和 移除 Peer 的功能。

  • 当监控 HA 服务组件状态时,需要获取 Peer 信息,leader 信息,commit 信息。

  • 当 HA 服务组件需要迁移节点时,需要按需触发生成 snapshot 的功能。

Apache Ratis 中实现 Ratis-shell

目前,ratis-shell 在 Ratis 项目中,以一个单独模块的形式存在:

ratis-shell 与 ratis server 交互的 API 为 ratis-client 导出的用户接口,主要为以下接口 API:

主入口为 bin/ratis 脚本,但是所有的命令后端都是调用 Java 主类 RatisShell,实现所有功能。

RatisShell 利用反射的方式注册同 java 包下的所有实现了 Command 接口的命令。

而这些 Ratis-shell 命令的实现类也基本上都包含子命令,比如 ElectionCommand 的子命令,位于 election 包中。

使用 Apache Ratis-shell

  • 获取帮助
# 获取帮助$ bin/ratis shUsage: ratis sh [generic options] [election [resume] [stepDown] [transfer] [pause]]          [group [list] [info]]                                      [peer [add] [setPriority] [remove]]                        [snapshot [create]]
  • 获取 group 信息
# 获取每一个 ratis group 的信息$ bin/ratis sh group info -peers localhost:19200,localhost:19201,localhost:19202group id: 02511d47-d67c-49a3-9011-abb3109a44c1leader info: localhost_19200(localhost:19200)[server {  id: "localhost_19200"  address: "localhost:19200"}commitIndex: 18, server {  id: "localhost_19202"  address: "localhost:19202"}commitIndex: 18, server {  id: "localhost_19201"  address: "localhost:19201"}commitIndex: 18]
  • 触发 leader 选举相关
# 触发 leader 切换到指定 peer$ ratis sh election transfer -peers localhost:19200,localhost:19201,localhost:19202 -address localhost:19202# 触发 leader 下台$ ratis sh election stepDown -peers localhost:19200,localhost:19201,localhost:19202# 设置指定 server 暂停/恢复选举$ ratis sh election pause -peers localhost:19200,localhost:19201,localhost:19202 -address localhost:19202 $ ratis sh election resume -peers localhost:19200,localhost:19201,localhost:19202 -address localhost:19202 
  • Peer 相关操作
# 增加 peer$ ratis sh peer add -peers localhost:19200,localhost:19201,localhost:19202 -address localhost:19203,localhost:19204# 删除 peer$ ratis sh peer remove -peers localhost:19200,localhost:19201,localhost:19202 -address localhost:19200 # 设置 peer 的优先级$ ratis sh peer setPriority -peers localhost:19200,localhost:19201,localhost:19202 -addressPriority localhost:19200|2

  • snapshot 相关
# 触发指定 peer 产生 snapshot$ ratis sh snapshot create -peers localhost:19200,localhost:19201,localhost:19202 -peerId localhost_19200

更多 ratis-shell 用法,请关注下方 ratis-shell 文档

Ratis-shell 的获取

  • 从 Apache Ratis 源码编译,软件包位于 ratis-assembly/target/apache-ratis--shell.tar.gz。
  • 新版本 ratis 2.3.1 之后的二进制包会包含 ratis-shell 二进制包。
  • 腾讯 Alluxio(DOP=Data Orchestration Platform 数据编排平台) 已经集成 ratis-shell。开源版本 Alluxio 和 Apache Ozone 集成 ratis-shell 敬请期待。

开源贡献

总结

ratis-shell 是一个对多个软件的公共部分进行抽象分离,从而实现减少冗余代码的思想的实现。

目前 ratis-shell 已经是 Apache ratis 项目的一个模块,也已经集成到腾讯 Alluxio OTeam 的版本软件包之中。

文章来源于腾讯云开发者社区,点击查看原文