层级结构设计
在程序设计的过程中,切记贪大;要一个一个小功能去设计,才能完成整个工程。就像堆积木一层又一层,最后才有其形。在软件工程中,唯一有效的整体架构设计就是层级结构了,和积木一个道理;所以目前有很多机构,对小朋友先教积木再教编程,似乎有一定的道理。
Ela 的层级结构
Ela 的设计的层级结构如下:
ES 接口类
Ela 的设计是基于 Elasticsearch Golang SDK 而设计,该 SDK 实现了从 V5 到 V8 的基础 API 实现;尽管 ES 的各个版本有些微的差别,API 的模式大体是一致的。就因为 ES 的各个版本有些许的不一样,因此需要用一个 ES 的 接口类去屏蔽这些差异点,这样上层就不需要去感知各个版本的差异性了。
这样还有一个好处,就是可扩展性;未来 ES9 出现了,我只需要实现 ES9 的接口就可以轻松实现 ES9 与其他版本 ES 的数据迁移。
Migrator 类
Migrator 类实现了单个索引的数据全量迁移、数据比对、以及数据增量迁移。用户只需要输入源 ES 和 目标 ES 的账号和密码, Migrator 获取源和目标 ES 的版本号,选择对应的 ES 实现类,并调用对应的实现类的方法去完成对应的功能。
BulkMigrator 类
Migratory 实现了单个索引的功能,BulkMigrator 实现了多个索引的功能;在 Migrator 的基础上,它增加了多个索引对的支持以及正则表达式的同名索引对支持。因为涉及到多个索引,为了加快数据同步速度,支持多协程去并行对不同的索引做操作。
Task 类
因为配置参数太多,所以用配置文件去设置这些参数;Task 类基于 BulkMigrator 类去做的设计,支持 yaml 去配置任务。配置参数如下:
name: task1
source_es: es5
target_es: es8
index_pairs:
-
source_index: "sample_hello"
target_index: "sample_hello"
-
source_index: "sample_hello2"
target_index: "sample_hello2"
task_action: sync
force: true
TaskMgr 类
因为每次只运行一个任务是不够的,需要运行多个任务;所以 TaskMgr 基于 Task 类,实现了多个 Task 的顺序执行。
tasks:
- name: task1
source_es: es5
target_es: es8
index_pairs:
-
source_index: "sample_hello"
target_index: "sample_hello"
task_action: sync
force: true
- name: task2
source_es: es5
target_es: es8
index_pairs:
- source_index: "sample_hello"
target_index: "sample_hello"
task_action: compare
force: true
- name: task3
source_es: es5
target_es: es6
index_pairs:
-
source_index: "sample_hello"
target_index: "sample_hello"
task_action: sync
force: true
- name: task3
source_es: es5
target_es: es6
index_pairs:
-
source_index: "sample_hello"
target_index: "sample_hello"
task_action: sync_diff
force: true
- name: task4
source_es: es5
target_es: es6
index_pairs:
-
source_index: "sample_hello"
target_index: "sample_hello"
task_action: compare
force: true
- name: task5
source_es: es5
target_es: es6
index_pairs:
- source_index: "sample_hello"
target_index: "sample_hello1"
task_action: sync
force: true