venus
venus copied to clipboard
venus-market需要实现一种自动迁移配置文件的方案
概述
venus-market每次配置文件需要发生变化, 都是变成了一场灾难. 所以, 我们需要实现一种venus-market配置文件变化后可以实现自动迁移的方案.
方案描述
对于配置相关的模块, 增加以下变量, 用于配合迁移:
type ConfigVersion uint64 // 配置类型版本号
const ConfigVersionV0 = 0
.....
const ConfigVersionVn = n
// Config的迁移接口.
type MigratableConfig interface {
NextVersion() (MigratableConfig, error)
}
type ConfigV0 { Header, ....}
type ConfigV1 { Header, ...}
type ConfigVn { Header, ...}
// 配置类型的header, 主要用于识别版本信息, 也可以带有其它额外的信息
type ConfigHeader struct {
Version ConfigVersion
}
// Version和配置类型的映射信息
var ConfigurationSchedules = map[ConfigVersion]Reflect.Type {
ConfigVersionV0 : reflect.Typeof(ConfigV0),
....
ConfigVersionVn : reflect.Typeof(ConfigVn),
}
var Config = ConfigV0
迁移逻辑
venus-market启动时, 会加载配置文件, 所有逻辑都是在加载配置文件中实现.
- 加载配置文件时, 先加载
ConfigHeader, 用于获取配置文件的版本. - 由于
ConfigurationSchedules中保存了版本信息与配置文件类型的信息, 可以通过反射构造出当前配置文件对应的类型.
config := reflect.New(ConfigurationSchedules[version])
- 通过2步得到的类型, 去加载配置信息.
- 由于所有的ConfigVx都实现了
NextVersion接口, 通过循环调用NextVersion, 可以将config自动迁移为最新的版本:
var next, error = config.NextVersion()
var changed bool
for ;next!=nil && e==nil; next, error = config.NextVersion() {
config = next
changed = true
}
- 最终根据第4步得到的
changed和config,如果changed为true, 则将config写入到配置文件, 此时 完成了配置的自动迁移.
应该遵守原则
- 最新版本的
configVx.NextVersion()返回值为nil - 配置文件的迁移, 必须
venus-market上一个版本的配置的行为完全一致. - 每次实现新的迁移都应该提供完善的单元测试
其它组件也会有类型配置迁移的问题,考虑一个统一的迁移方案,多个组件能共用
close as not planned.