Heka 使用的是 TOML 格式的配置文件, 有关 golang 加载 TOML 配置文件的技术请参看:  http://www.cnblogs.com/ghj1976/p/4082323.html 

Heka用的 读取 TOML 文件的Go库是: https://github.com/bbangert/toml 虽然跟上面文章的不是一个,但是基本差不多。

 

我们在hekad主进程的配置文件加载的逻辑如下:

  1. 在启动时,先读取hekad部分的配置内容,用于hekad的启动。
  2. 然后再去读其他配置节,把heka的管道启动起来。

github.com\mozilla-services\heka\cmd\hekad\main.go 文件中,我们摘要核心代码如下,就可以看到这个逻辑:

这里删除了一些跟这个逻辑过程无关的代码,方便阅读。

加载 hekad 部分配置

代码实现如下, 注意,这里处理了配置文件内置支持环境变量的功能。

 

对环境变量支持的文档如下:

Using Environment Variables

If you wish to use environmental variables in your config files as a way to configure values, you can simply use %ENV[VARIABLE_NAME] and the text will be replaced with the value of the environmental variable VARIABLE_NAME.

Example:

[AMQPInput]
url = "amqp://%ENV[USER]:%ENV[PASSWORD]@rabbitmq/"
exchange = "testout"
exchangeType = "fanout"

 

加载插件相关的配置逻辑

加载插件配置的逻辑主要是下面代码:

 

每个Heka的TOML配置节,对应的实体类如下:

// The TOML spec for plugin configuration options that will be pulled out by
// Heka itself before the config is passed to the Plugin.Init method. Not all
// options apply to all plugin types.
type PluginGlobals struct {
Typ string `toml:"type"`
Ticker uint `toml:"ticker_interval"`
Matcher string `toml:"message_matcher"` // Filter and Output only.
Signer string `toml:"message_signer"` // Filter and Output only.
Retries RetryOptions
Encoder string // Output only.
UseFraming *bool `toml:"use_framing"` // Output only.
CanExit *bool `toml:"can_exit"`
}

对应的解析时的逻辑如下:

 

func (self *PipelineConfig) loadPluginGlobals(section *ConfigSection) (err error) {

    // Set up default retry policy.

    pGlobals := new(PluginGlobals)

    pGlobals.Retries = RetryOptions{

        MaxDelay:   "30s",

        Delay:      "250ms",

        MaxRetries: -1,

    }

    if err = toml.PrimitiveDecode(section.tomlSection, pGlobals); err != nil {

        err = fmt.Errorf("Unable to decode config for plugin '%s': %s",

            section.name, err)

        return

    }

    if pGlobals.Typ == "" {

        pGlobals.Typ = section.name

    }

    if _, ok := AvailablePlugins[pGlobals.Typ]; !ok {

        err = fmt.Errorf("No registered plugin type: %s", pGlobals.Typ)

    } else {

        section.globals = pGlobals

    }

    return

}

补充说明如下:

 

插件的类型有5种,都是在名字或者type上可以看出来的, 对应的判断类型的代码如下:

var PluginTypeRegex = regexp.MustCompile("(Decoder|Encoder|Filter|Input|Output)$")

func getPluginCategory(pluginType string) string {

    pluginCats := PluginTypeRegex.FindStringSubmatch(pluginType)

    if len(pluginCats) < 2 {

        return ""

    }

    return pluginCats[1]

}

加载配置文件这里,代码在 LoadFromConfigFile 函数, 这里其实就主要做了上面两个事情,外加不同类型插件的特殊处理。

// Loads all plugin configuration from a TOML configuration file. The

// PipelineConfig should be already initialized via the Init function before

// this method is called.

func (self *PipelineConfig) LoadFromConfigFile(filename string) (err error) {

    var configFile ConfigFile

    // 更新配置文件中,自定义变量(环境变量)

    contents, err := ReplaceEnvsFile(filename)

    if err != nil {

        return err

    }

    // TOML 解析成 configFile

    if _, err = toml.Decode(contents, &configFile); err != nil {

        return fmt.Errorf("Error decoding config file: %s", err)

    }

    var (

        errcnt              uint

        protobufDRegistered bool

        protobufERegistered bool

    )

    sectionsByCategory := make(map[string][]*ConfigSection)

    // Load all the plugin globals and file them by category.

    for name, conf := range configFile {

        if name == HEKA_DAEMON {

            continue

        }

        log.Printf("Pre-loading: [%s]\n", name)

        section := &ConfigSection{

            name:        name,

            tomlSection: conf,

        }

        // 加载插件配置文件, 这里面做了插件注册的检查

        if err = self.loadPluginGlobals(section); err != nil {

            self.log(err.Error())

            errcnt++

            continue

        }

        // 获取插件的类型

        category := getPluginCategory(section.globals.Typ)

        if category == "" {

            self.log(fmt.Sprintf("Type doesn't contain valid plugin name: %s\n",

                section.globals.Typ))

            errcnt++

            continue

        }

        // 特殊插件类型的处理

        section.category = category

        if section.globals.Typ == "MultiDecoder" {

            // Special case MultiDecoders so we can make sure they get

            // registered *after* all possible subdecoders.

            sectionsByCategory["MultiDecoder"] = append(sectionsByCategory["MultiDecoder"],

                section)

        } else {

            sectionsByCategory[category] = append(sectionsByCategory[category], section)

        }

        if name == "ProtobufDecoder" {

            protobufDRegistered = true

        }

        if name == "ProtobufEncoder" {

            protobufERegistered = true

        }

    }

    // Make sure ProtobufDecoder is registered.

    if !protobufDRegistered {

        var configDefault ConfigFile

        toml.Decode(protobufDecoderToml, &configDefault)

        log.Println("Pre-loading: [ProtobufDecoder]")

        section := &ConfigSection{

            name:        "ProtobufDecoder",

            category:    "Decoder",

            tomlSection: configDefault["ProtobufDecoder"],

        }

        if err = self.loadPluginGlobals(section); err != nil {

            // This really shouldn't happen.

            self.log(err.Error())

            errcnt++

        } else {

            sectionsByCategory["Decoder"] = append(sectionsByCategory["Decoder"],

                section)

        }

    }

    // Make sure ProtobufEncoder is registered.

    if !protobufERegistered {

        var configDefault ConfigFile

        toml.Decode(protobufEncoderToml, &configDefault)

        log.Println("Pre-loading: [ProtobufEncoder]")

        section := &ConfigSection{

            name:        "ProtobufEncoder",

            category:    "Encoder",

            tomlSection: configDefault["ProtobufEncoder"],

        }

        if err = self.loadPluginGlobals(section); err != nil {

            // This really shouldn't happen.

            self.log(err.Error())

            errcnt++

        } else {

            sectionsByCategory["Encoder"] = append(sectionsByCategory["Encoder"],

                section)

        }

    }

    multiDecoders := make([]multiDecoderNode, len(sectionsByCategory["MultiDecoder"]))

    multiConfigs := make(map[string]*ConfigSection)

    for i, section := range sectionsByCategory["MultiDecoder"] {

        multiConfigs[section.name] = section

        multiDecoders[i] = newMultiDecoderNode(section.name, subsFromSection(section.tomlSection))

    }

    multiDecoders, err = orderDependencies(multiDecoders)

    if err != nil {

        return err

    }

    for i, d := range multiDecoders {

        sectionsByCategory["MultiDecoder"][i] = multiConfigs[d.name]

    }

    // Append MultiDecoders to the end of the Decoders list.

    sectionsByCategory["Decoder"] = append(sectionsByCategory["Decoder"],

        sectionsByCategory["MultiDecoder"]...)

    // Force decoders and encoders to be registered before the other plugin

    // types are initialized so we know they'll be there for inputs and

    // outputs to use during initialization.

    order := []string{"Decoder", "Encoder", "Input", "Filter", "Output"}

    for _, category := range order {

        for _, section := range sectionsByCategory[category] {

            log.Printf("Loading: [%s]\n", section.name)

            if err = self.loadSection(section); err != nil {

                self.log(err.Error())

                errcnt++

            }

        }

    }

    if errcnt != 0 {

        return fmt.Errorf("%d errors loading plugins", errcnt)

    }

    return

}

Heka 的配置文件加载逻辑的更多相关文章

  1. Spring Boot 2.4.0正式发布,全新的配置文件加载机制(不向下兼容)

    千里之行,始于足下.关注公众号[BAT的乌托邦],有Spring技术栈.MyBatis.JVM.中间件等小而美的原创专栏供以免费学习.分享.成长,拒绝浅尝辄止.本文已被 https://www.you ...

  2. Spring Boot源码分析-配置文件加载原理

    在Spring Boot源码分析-启动过程中我们进行了启动源码的分析,大致了解了整个Spring Boot的启动过程,具体细节这里不再赘述,感兴趣的同学可以自行阅读.今天让我们继续阅读源码,了解配置文 ...

  3. Spring使用环境变量控制配置文件加载

    项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...

  4. struts几个配置文件加载顺序_2015.01.04

    struts几个配置文件加载顺序: 01:struts-default.xml 02:struts-plugin.xml 03:struts.xml 04:struts.properties 05:w ...

  5. asp.netcore 深入了解配置文件加载过程

    前言     配置文件中程序运行中,担当着不可或缺的角色:通常情况下,使用 visual studio 进行创建项目过程中,项目配置文件会自动生成在项目根目录下,如 appsettings.json, ...

  6. bash 的配置文件加载顺序

    bash配置文件的加载顺序和登陆方式有关,下面先介绍下登陆方式. 1 登陆方式有2种 登陆式SHELL: su - oracle    su -l oracle 正常从终端登陆 非登录式SHELL: ...

  7. 解决eclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题

    解决eclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题. 有时候是src/main/resources下面的,有时候是sr ...

  8. node 加载逻辑

    [node 加载逻辑] require(X) from module at path Y . If X is a core module, a. return the core module b. S ...

  9. springboot的yaml基础语法与取值,配置类,配置文件加载优先级

    1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...

随机推荐

  1. Python的并发编程

    我们将一个正在运行的程序称为进程.每个进程都有它自己的系统状态,包含内存状态.打开文件列表.追踪指令执行情况的程序指针以及一个保存局部变量的调用栈.通常情况下,一个进程依照一个单序列控制流顺序执行,这 ...

  2. P4173 残缺的字符串

    题目链接 题意分析 啥 ? ? ? \(FFT\)做字符串匹配 可是就是这样 我们定义匹配函数 我们定义\(A\)是匹配串 \(B\)是被匹配串 我们当前到达\(B\)串的\(x\)位置 \[P(x) ...

  3. 由import javax.persistence.*;引用引发问题的思考(SpringBoot)

    在学习SpringBoot的 JPA时候增加一个实体类的时候发现import包的时候一直报错. 对比一下,发现自己的项目里面是少了 spring-boot-starter-data-jpa.jar包的 ...

  4. 大数据-hive安装

    1.下载Hive需要的版本 我们选用的是hive-3.1.0 将下载下来的hive压缩文件放到/opt/workspace/下 2.解压hive-3.1.0.tar.gz文件 [root@master ...

  5. [Alpha]Scrum Meeting#6

    github 本次会议项目由PM召开,时间为4月8日晚上10点30分 时长25分钟 任务表格 人员 昨日工作 下一步工作 木鬼 整理开会记录 撰写并发布之前因为清明耽误的博客 SiMrua 寻找方法捕 ...

  6. Java的输出方式

    System.out.println("...." + elements + "..."); 格式化输出: 我们知道输出格式化数字可以使用 printf() 和 ...

  7. P1001 A+B Problem (树链剖分)

    这题考验我们构造模型的能力. 考虑构造一棵树,树上有3个节点,节点1和节点2连一条权值为a的边,节点1和节点3连一条权值为b的边,显然答案就是节点2到节点3的最短路径. 但这样还不够.考虑加法的性质, ...

  8. alpha-beta搜索算法

    alpha-beta搜索(min-max搜索): 简称mfs,用来解决双方最优决策博弈问题. 核心思想:在搜索树中,下一层越小,对当前层越有利,由于取max,一旦下一层出现了比其他孩子结果更大的值,那 ...

  9. 原来部署好的WCF(可以调用),因为部署.net core,而安装了DotNetCore.2.0.5-WindowsHosting,导致现在WCF站点不可以。

    报错如下: 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面. 解决方法: 如果出现如下结果,则证明可以啦.

  10. Excel2007使用SQL语句

    Excel2007使用SQL语句 假如金三导出表格如下:[入库查询dddd.xls] 第1步 第2步 第3步 找到[入库查询dddd.xls] 比如 SELECT 纳税人名称, sum(实缴金额) F ...