InfluxDB源码目录结构解析
操作系统 : CentOS7.3.1611_x64
go语言版本:1.8.3 linux/amd64
InfluxDB版本:1.1.0
influxdata主目录结构
[root@localhost influxdata]# pwd
/root/dev/golib/src/github.com/influxdata
[root@localhost influxdata]# tree -d -L 1
.
├── influxdb
├── influxql
├── usage-client
├── yamux
└── yarpc
目录解析说明:
-
influxdb
为源码的主目录
-
influxql
实现了InfluxDB查询语言的解析器(源码主目录里面引用的是influxdata/influxdb/influxql,没有该目录的相关引用)
-
usage-client
client lib V1版本
-
yamux
Yet another Multiplexer(又一个多路复用器)是Golang的多路复用库
-
yarpc
Yet Another RPC (又一个RPC)是Golang的RPC库
源码主目录结构
一级目录结构如下:
[root@localhost influxdb]# pwd
/root/dev/golib/src/github.com/influxdata/influxdb
[root@localhost influxdb]# tree -d -L 1
.
├── client
├── cmd
├── coordinator
├── etc
├── importer
├── influxql
├── internal
├── man
├── models
├── monitor
├── pkg
├── scripts
├── services
├── stress
├── tcp
├── tests
├── toml
├── tsdb
└── uuid
二级目录结构如下:
[root@localhost influxdb]# tree -d -L 2
.
├── client
│ └── v2
├── cmd
│ ├── influx
│ ├── influxd
│ ├── influx_inspect
│ ├── influx_stress
│ └── influx_tsm
├── coordinator
├── etc
│ └── burn-in
├── importer
│ └── v8
├── influxql
│ ├── internal
│ └── neldermead
├── internal
├── man
├── models
├── monitor
│ └── diagnostics
├── pkg
│ ├── deep
│ ├── escape
│ ├── limiter
│ ├── pool
│ └── slices
├── scripts
├── services
│ ├── admin
│ ├── collectd
│ ├── continuous_querier
│ ├── graphite
│ ├── httpd
│ ├── meta
│ ├── opentsdb
│ ├── precreator
│ ├── retention
│ ├── snapshotter
│ ├── subscriber
│ └── udp
├── stress
│ ├── stress_test_server
│ └── v2
├── tcp
├── tests
│ ├── siege
│ ├── tmux
│ └── urlgen
├── toml
├── tsdb
│ ├── engine
│ └── internal
└── uuid
目录解析说明:
-
client
client lib V2版本
-
cmd
InfluxDB相关程序所在目录。其中:
influxd目录为InfluxDB主程序代码;
influx为InfluxDB自带的控制台管理工具源码;
influx_inspect为InfluxDB数据查看工具源码;
influx_stress为InfluxDB压力测试工具源码;
influx_tsm为数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)源码
-
coordinator
协调器,负责数据的写入和一些创建语句的执行。
在InfluxDB的ChangeLog中显示在v1.0.0中使用coordinator替换cluster,感觉自建集群功能可以通过此模块实现。
coordinator是tsdb的一个封装,原本是提供分布式存储、保证数据一致性等功能,但是现在由于集群功能不再开源,其中 这部分逻辑也已经被移除,现在仅仅是tsdb的代理。
-
etc
存放默认配置
-
importer
版本向后兼容相关代码,在ReadMe中已经提到:Version 0.8.9 of InfluxDB adds support to export your data to a format that can be imported into 0.9.3 and later.
-
influxql
实现了InfluxDB查询语言的解析器
-
internal
主要实现了MetaClient接口
-
man
帮助手册
-
models
基础数据类型定义
-
monitor
monitor模块是influxd用来给用户提供一些统计、诊断数据的模块。influxd有相关文档 专门介绍了该项功能,需要给对外暴露相关信息的模块可以调用Monitor.RegisterDiagnosticsClient()方法来注册到该 模块中来。当相关接口被调用时,会读取注册模块的数据。
-
pkg
一些通用包的集合。
deep里面主要实现了deepValueEqual方法,用于深层次比较两个值是否相等;
escape里面主要实现了byte和string两种数据类型转义字符的相关操作;
limiter里面主要是一个基于channel实现的简单并发限制器Fixed;
pool里面主要实现了Bytes和Generic两种类型的Pool,在Pool中的对象不使用时不会被垃圾回收自动清理掉;
slices 里面主要实现了一些string数组的操作;
-
scripts
该目录存放的是一些关于InfluxDB的脚本。
-
services
该目录存放的是一些关于InfluxDB的服务。
admin 为InfluxDB内置的管理服务;
collectd 为collectd(https://collectd.org)对接服务,可以接收通过UDP发送过来的collectd格式数据;
graphite 为InfluxDB的graphite服务;
opentsdb 为InfluxDB的opentsdb服务,可用于替换opentsdb;
influxdb兼容了主流的多种监控数据接收协议有graphit/opentsdb/collectd。这些模块接收到相关数据后,将数据 解析成统一格式Point 然后通过coordinator.PointsWriter.WritePointsPrivileged() /coordinator.PointsWriter.WritePoints() /coordinator.PointsWriter.WritePointsInto() 写入tsdb。
continuous_querier 为InfluxDB的CQ服务;
httpd 是influxdb对外提供服务的主要协议,用户可以通过http协议查询数据、数据写入、修改database、增加用户等操作。对外 提供的了一些API 和/debug/pprof、/debug/expvar、/debug/requests接口。
meta 为InfluxDB的元数据服务,用于管理数据库的元数据相关内容;influxdb将meta数据库,包装成一个MetaClient对外提供数据,需要meta的模块都引用这个MetaCient。该meta.db 直接使用protobuf格式的数据作为持久化文件。meta加载持久化文件后,会将全部内容缓存在内存中。当有meta改写时, MetaClient会将更新后的数据序列化然后写入磁盘中。MetaClient一部分数据已slice的形式存储,很多api都会将该slice返回给调用方,从而脱离了其锁的保护,有数据并发竞争 访问的问题存在。meta.db中存储每个database的元数据(名称、过期策略、ContinuousQuery)和用户信息。
precreator 为InfluxDB的Shard预创建服务;influxd将时序数据按时间区间来切分文件,被切分的块称之为shard。当influxdb组成一个集群时,shard写时创建 可能会在触发切换文件的一瞬间影响进程的吞吐量,因此采用预先创建shard的方式,使得文件总是预先被创建的,写请求 不会被切换文件的动作阻塞。
precreator模块就是创建一个定时函数,每隔一定时间间隔,检查并按需预先创建一次shard。
retention 为InfluxDB的数据保留策略的强制执行服务,主要用于定时删除文件;influxdb通常存储一些监控、统计数据,通常来说,这些数据并不需要一直存储着,当数据超过一定期限时,这些数据 就失去了保留的意义。同时influxdb的存储空间也不是无限的。因此influxdb就内置了数据过期自动删除的逻辑。对于 influxdb上的每个database可以设置一些过期策略,满足这些策略时,influxdb就是删除满足条件的shard。
retention就是执行这些过期策略的模块。该模块定期检查每个database的过期策略,将满足过期条件的ShardGroup删除, 然后再将与该ShardGroup相关的shard删除。
snapshotter 为InfluxDB的快照服务;
subscriber 为InfluxDB的订阅服务;subscriber处理influxdb中SUBSCRIPTIONS部分的逻辑,将内部通过coordinator.PointsWriter写入的数据发送到 database中配置的SUBSCRIPTIONS指定的URI上,采用推的模式。有点类似Watch的机制。Kapacitor 就使用了此机制。各个数据接收模块都会使用到coordinator.PointsWriter。
udp 为InfluxDB的udp服务,可以通过该接口进行数据库的写入和查询等操作;
-
stress
该目录存放的是压力测试相关内容。
-
tcp
网络连接的多路复用。
-
tests
测试相关内容
-
toml
toml的解析器,和另一个toml解析器(github.com/BurntSushi/toml)不同,为独立的解析模块,主要是解析时间字符串和磁盘容量数据。
-
tsdb
tsdb目录主要是时序数据库的实现。
-
uuid
该目录里面主要存放uuid生成的相关代码。
在influxdb指定的存储目录下的文件结构为:
├── data # 配置中[data]下dir配置的路径
│ ├── _internal
│ │ └── monitor
│ │ ├── 73
│ │ │ └── 000000003-000000002.tsm
│ │ ├── 74
│ │ │ └── 000000003-000000002.tsm
│ │ └── 75
│ │ └── 000000001-000000001.tsm
│ └── testing
│ └── autogen
│ └── 2
│ └── 000000002-000000002.tsm
├── meta
│ └── meta.db
└── wal # 配置文件[data]下wal-dir配置
├── _internal
│ └── monitor
│ ├── 73
│ │ └── _00012.wal
│ ├── 74
│ │ └── _00012.wal
│ └── 75
│ ├── _00005.wal
│ ├── _00006.wal
│ └── _00007.wal
└── testing
└── autogen
└── 2
└── _00003.wal