Featuregate 介绍

    在 MOSN 中,存在一些功能需要在启动时决定是否开启的,为了满足这种需求,MOSN 推出了 featuregate(功能开关)的能力。

    Featuregate 描述了一组 MOSN 中需要开启 / 关闭的 feature 状态,每个 feature 都有自己默认的状态,每个 MOSN 版本支持的 feature、feature 默认的版本都有所不同;featuregate 的描述用一个字符串表示,按照的方式,用逗号进行分割:

    Featuregate 不仅仅是提供了一种功能切换的能力,同时也提供了一种可扩展的开发机制,基于 MOSN 进行二次开发时,可以使用 featuregate 做到如下的功能:

    • 功能切换的能力,可以控制某个 feature 的开启 / 关闭
    • feature 之间的依赖关系管理,包括 feature 之间的启动顺序依赖、开启 / 关闭状态的依赖等
      • 举例说明,基于 MOSN 实现两个 feature,分别为 A 和 B,需要在 A 初始化完成以后,B 会使用 A 初始化的结果进行初始化,这就是 B 依赖 A,当 feature A 处于 Disable 状态时,B 显然也会处于 Disable 或者需要作出对应的“降级”; feature gate 框架提供了一种简单的方式,可以更加专注于 feature 的开发,而不用去管理对应的启动与依赖

    基于 featuregate 进行开发

    首先,我们来看一下,featuregate 框架提供了哪些接口:

    1. // 返回一个 Feature 当前的状态,true 表示 enable,false 表示 disable
    2. func Enabled(key Feature) bool
    3. // “订阅”一个 Feature,并且返回其订阅完成以后的状态。
    4. // 当订阅的 Feature 初始化完成以后,会返回其是否 Enable。
    5. // 如果订阅的 Feature 是 Disable 的,会直接返回 false;如果在订阅的 timeout 期间,Feature 依然没有
    6. // 初始化完成,那么会返回订阅超时的错误,如果 timeout 小于等于 0,则没有订阅超时
    7. func Subscribe(key Feature, timeout time.Duration) (bool, error)
    8. // 设置 feature gates 的状态,value 为一个完整的 feature gates 描述
    9. func Set(value string) error
    10. // 设置 feature gates 的状态,其中 map 的 key 为 feature 的 key,value 是期望设置的 feature 状态
    11. func SetFromMap(m map[string]bool) error
    12. // 新注册一个 feature 到 feature gate 中
    13. func AddFeatureSpec(key Feature, spec FeatureSpec) error
    14. // 设置一个 feature 的状态
    15. func SetFeatureState(key Feature, enable bool) error
    16. // 开启初始化 feature
    17. func StartInit()
    18. // 等待所有的 feature 初始化结束
    19. func WaitInitFinsh() error

    这其中,StartInit 和 WaitInitFinsh 是由 MOSN 框架进行调用,基于 MOSN 进行二次开发时无须关注和调用;通常情况下,Set 和 SetFromMap 也无须关注。所有的上述接口,都是由框架下默认的一个不可导出的全局 featuregate 对象暴露,在没有极为特殊需求的场景下(如编写单元测试),不需要额外生成 FeatureGate 对象,使用默认的即可。

    接下来,我们看一下 featuregate 的实现:

    1. type knownFeatureSpec struct {
    2. FeatureSpec
    3. once sync.Once
    4. channel chan struct{}
    5. }
    6. type Feature string
    7. type FeatureGate struct {
    8. lock sync.Mutex
    9. known map[Feature]*knownFeatureSpec
    10. // inited is set to true when StartInit is called.
    11. inited bool
    12. wg sync.WaitGroup
    13. once sync.Once
    14. }
    • prerelease 是不可导出的定义,有三个约定的导出变量可以使用,相当于传统语言的 Enum 类型,用于描述 feature 的信息,没有明确的作用
    • FeatureSpec可以自行实现,同时多数情况下可以用框架实现的BaseFeatureSpec,或者基于BaseFeatureSpec进行封装;如注释描述,通常情况下只需要额外封装实现一个InitFunc函数即可
    1. // BaseFeatureSpec is a basic implementation of FeatureSpec.
    2. // Usually, a feature spec just need an init func.
    3. type BaseFeatureSpec struct {
    4. // 默认状态
    5. DefaultValue bool
    6. // 是否可修改状态,如果为 true,说明这个 feature 只能保持默认状态
    7. // 一般情况下设置这个为 true 的时候,default 也是 true
    8. // 这种 feature 主要会用于做为其他 feature 的“基础依赖”
    9. IsLockedDefault bool
    10. PreReleaseValue prerelease
    11. stateValue bool // stateValue shoule be setted by SetState
    12. inited int32 // inited cannot be setted
    13. }

    了解了 featuregate 的基本实现,就可以考虑使用 featuregate 进行基本的编程扩展了。下面会介绍几种 featuregate 的使用场景,以及如何编写 feature。

    1. 基本的“全局”开关

    对于 feature 切换最基本的使用场景,就是使用一个类似“全局变量”进行控制,通过if条件判断执行不同的逻辑。使用 featuregate 框架实现这种能力,可以把控制 feature 切换的参数全部统一到启动参数中。

    1. var featureName featuregate.Feature = "simple_feature"
    2. func init() {
    3. fs := &featuregate.BaseFeatureSpec{
    4. DefaultValue: true
    5. }
    6. featuregate.AddFeatureSpec(featureName,fs)
    7. }
    8. func myfunc() {
    9. if featuregate.Enable(featureName) {
    10. } else {
    11. dosth2()
    12. }
    13. }

    2. 需要进行“初始化”操作

    通过封装扩展 InitFunc 函数,让相关的初始化工作在 MOSN 启动时统一完成,如果 feature 处于 disable 状态,那么 InitFunc 不会执行。

    3. Feature 之间存在依赖关系

    • 假设我们存在四个独立的组件(feature),分别是 A、B、C,D
    • 如果 A 没有启动,B 就不能启动,而 C 存在一种降级方案,依然可以继续工作
    • 四个 feature 在 featuregate 框架下可各自实现,如下
    1. var FeatureA featuregate.Feature = "A"
    2. func init() {
    3. fs := &featuregate.BaseFeatureSpec{
    4. DefaultValue: true
    5. }
    6. featuregate.AddFeatureSpec(FeatureA,fs)
    7. }
    1. var FeatureB featuregate.Feature = "B"
    2. type FB struct {
    3. *BaseFeatureSpec
    4. }
    5. func (f *FB) InitFunc() {
    6. enabled, err := featuregate.Subscribe(FeatureA, 5 * time.Second)
    7. if err != nil || !enabled {
    8. f.SetState(false) // 如果 FeatureA 没有开启,则 FeatureB 也不开启
    9. }
    10. }
    1. type FeatureD featuregate.Feature = "D"
    2. type FD struct {
    3. *BaseFeatureSpec
    4. }
    5. func (f *FD) InitFunc() {
    6. enabled, err := featuregate.Subscribe(FeatureB, 0) // 不超时,一定要等待 B 结束
    7. if err != nil || !enabled {
    8. return
    9. }
    10. f.Start()
    11. }
    12. }

    为什么不使用配置的方式,而要使用 featuregate?

    • 有的 feature 需要判断的时机,比配置文件解析要早,甚至可能影响配置解析的逻辑