文章目录
- 前言
- shard分片
-
- Cache缓存
- Wal存储预写日志
- Tsm file真正持久化存储数据
- Compactor 合并压缩线程
- shard group 分片逻辑分组
- shard duration
- influxdb客户端查看shard和shard group
-
- show shards
- show shard groups
- shard物理目录结构
-
- /var/lib/influxdb/data目录结构
- /var/lib/influxdb/wal目录结构
- shard相关配置
- 总结
前言
influxdb的存储结构是怎样的?数据如何持有化,以什么形式存储?
shard
是
influxdb
存储引擎
TSM
的具体实现。
TSM TREE
是专门为
influxdb
构建的数据存储格式。与现有的
B+ tree
或
LSM tree
实现相比,
TSM tree
具有更好的压缩和更高的读写吞吐量。
shard group
是存储
shard
的逻辑容器,每一个
shard group
都有一个不重叠的时间跨度,可根据保留策略
retention policy
的
duration
换算而得。数据根据不同的时间跨度存储在不同的
shard group
中。
如图是本人根据自己的理解画的
influxdb
存储结构关系图:
shard分片
A shard contains the actual encoded and compressed data, and is represented by a TSM file on disk.
Every shard belongs to one and only one shard group. Multiple shards may exist in a single shard group.
Each shard contains a specific set of series.
All points falling on a given series in a given shard group will be stored in the same shard (TSM file) on disk.
shard
主要由4部分组成
Cache
、
Wal
、
Tsm file
、
Compactor
。
Cache缓存
cache
缓存,为提高读写效率必定会有缓存,在内存中
cache
是一个
key-value
的形式,
key
是
seriesKey + 分隔符 + filedName
,
value
是按时间排序存放的数据。在写数据时,同时向
cache
和
wal
写入,可以认为
cache
是
wal
文件中的数据在内存中的缓存。
cache
默认大小是
25m
,可配置,如果数据到达阈值,会进行一次快照,清空
cache
,并将快照刷新到
TSM
文件中。
Wal存储预写日志
wal
和
cache
的联系就是存储的文件内容一样,但是cache到达阈值后会创建快照并清除,而
wal
不会清除,它以文件的形式(.wal)存放预写日志,持久化在磁盘中。其主要作用如下:
- 当系统崩溃后可以通过
文件恢复还没有写入wal
文件的数据。tsm
- 当系统重启,遍历所有
文件,构造wal
。cache
Tsm file真正持久化存储数据
Tsm file
(
.tsm
)真正用于数据持久化存储。文件格式主要由为4 部分组成:
Header
、
Blocks
、
Index
、
Footer
。
-
由两部分组成Header
和MagicNumber
,Version
用于描述文件存储引擎,MagicNumber
记录引擎版本。Version
-
内部是一些连续的Blocks
,Block
是Block
中的最小读取对象,每次读取操作都会读取一个influxdb
。每一个Block
分为Block
值和CRC32
两部分,Data
值用于校验CRC32
的内容是否有问题。Data
-
存放的是Index
里内容的索引。Blocks
在做查询操作时,可以根据influxdb
的信息快速定位到Index
中需要查询的Tsm file
的位置。block
-
位于Footer
的最后8字节,存放了Tsm file
的起始位置在Index
中的偏移量,方便将索引信息加载到内存中。Tsm file
Compactor 合并压缩线程
Compactor
组件在后台持续运行,每隔 1 秒会检查一次是否有需要压缩合并的数据。其主要有两种操作:
-
中的数据大小达到阀值后,进行快照,之后转存到一个新的cache
文件中。Tsm
- 合并
文件。将多个小的Tsm
文件合并成一个,使每一个文件尽量达到单个文件的最大值,减少Tsm
文件的数量,并且一些数据的删除操作也是在这个时候完成。Tsm
shard group 分片逻辑分组
Shard groups are logical containers for shards. Shard groups are organized by time and retention policy.
Every retention policy that contains data has at least one associated shard group.
A given shard group contains all shards with data for the interval covered by the shard group.
The interval spanned by each shard group is the shard duration.
每一个
retention policy
至少有一个
shard group
,每一个
shard group
至少有一个
shard
。数据都会带一个
time
时间戳,当写入数据时,先通过时间判断(
Range Sharding
)应该写入哪个
shard group
,然后根据
series
进行
hash
计算(
Hash Sharding
),决定写入当前
shard group
下的哪个
shard
。其如此设计的目的主要有3个:
- 一定程度上缓解数据写入热点问题。
- 批量清理过期数据变得简单高效。
通过判断每一个Compactor
是否过期,过期的就按shard group
批量删除数据。shard group
- 提高数据按时间维度查找的性能。一般情况查询都会带一个时间范围条件,但是如果这个时间范围很大,跨度了几个
,查询性能也会有所下降。shard group
shard duration
shard duration
和
shardGroupDuration
基本是一致的。
shardGroupDuration
是根据
retention policy
的
duration
计算得来,不过也可以在创建
retention policy
指定。
The shard duration determines how much time each shard group spans.
The specific interval is determined by the SHARD DURATION of the retention policy.
See Retention Policy management for more information.
For example, given a retention policy with SHARD DURATION set to 1w, each shard group will span a single week and contain all points with timestamps in that week.
retention policy's duration
和
shardGroupDuration
的计算关系如下:
retention policy’s duration | shardGroupDuration |
---|---|
< 2 days | 1 h |
>= 2 days and <= 6 months | 1 day |
> 6 months | 7 days |
influxdb客户端查看shard和shard group
show shards
show shards
可查看所有
database
的
shards
分布情况:
show shard groups
show shard groups
查看所有
database
的
shard group
。
shard物理目录结构
默认情况下,
influxdb
数据存储在
/var/lib/influxdb/
下:
-
存储/var/lib/influxdb/data
文件。tsm
-
存储/var/lib/influxdb/wal
文件。wal
-
存放元数据(/var/lib/influxdb/meta
),包含系统状态的内部信息,用户信息、数据库/分片元数据、CQs、RPs和订阅等。meta.db
/var/lib/influxdb/data目录结构
第一层是
database
目录,第二层是
retention policy
目录以及
_series
,
rp
目录下
shard group id
目录,
shard group id
目录下是
.tsm
文件和
fields.idx
。
[[email protected] ~]# tree /var/lib/influxdb/data
/var/lib/influxdb/data
├── _internal
│ ├── monitor
│ │ ├── 11
│ │ │ ├── 000000003-000000002.tsm
│ │ │ └── fields.idx
│ │ ├── 12
│ │ │ ├── 000000004-000000002.tsm
│ │ │ └── fields.idx
│ └── _series
│ ├── 00
│ │ └── 0000
│ ├── 01
│ │ └── 0000
│ ├── 02
│ │ └── 0000
│ ├── 03
│ │ └── 0000
│ ├── 04
│ │ └── 0000
│ ├── 05
│ │ └── 0000
│ ├── 06
│ │ └── 0000
│ └── 07
│ └── 0000
└── mytest
├── 30d
│ ├── 15
│ │ ├── 000000003-000000001.tsm
│ │ └── fields.idx
│ ├── 17
│ │ ├── 000000003-000000002.tsm
│ │ └── fields.idx
│ └── 19
│ ├── 000000004-000000004.tsm
│ └── fields.idx
├── autogen
│ └── 3
└── _series
├── 00
│ └── 0000
├── 01
│ └── 0000
├── 02
│ └── 0000
├── 03
│ └── 0000
├── 04
│ └── 0000
├── 05
│ └── 0000
├── 06
│ └── 0000
└── 07
└── 0000
/var/lib/influxdb/wal目录结构
第一层是
database
目录,第二层是
retention policy
目录,
rp
目录下是
shard group id
目录,
shard group id
目录下是
.wal
文件。
[[email protected] 00]# tree /var/lib/influxdb/wal
/var/lib/influxdb/wal
├── _internal
│ └── monitor
│ ├── 11
│ ├── 12
│ │ └── _00013.wal
│ ├── 14
│ │ └── _00014.wal
│ ├── 16
│ │ └── _00015.wal
│ ├── 18
│ │ └── _00016.wal
│ ├── 20
│ │ └── _00016.wal
│ ├── 22
│ │ └── _00016.wal
│ └── 24
│ ├── _00005.wal
│ └── _00006.wal
└── mytest
├── 30d
│ ├── 15
│ │ └── _00004.wal
│ ├── 17
│ │ └── _00004.wal
│ └── 19
│ └── _00005.wal
└── autogen
└── 3
shard相关配置
influxdb
中数据存储位置、文件大小等一些配置可在
/etc/influxdb/influxdb.conf
中配置。如下摘取部分配置:
[meta]
# 存储meta数据
# Where the metadata/raft database is stored
dir = "/var/lib/influxdb/meta"
# Automatically create a default retention policy when creating a database.
# 用于控制默认存储策略,数据库创建时,会自动生成存储策略。
# retention-autocreate = true
# If log messages are printed for the meta service
# mete服务日志记录
# logging-enabled = true
###
### [data]
[data]
# The directory where the TSM storage engine stores TSM files.
# TSM文件存储路径
dir = "/var/lib/influxdb/data"
# The directory where the TSM storage engine stores WAL files.
# wal文件存储路径
wal-dir = "/var/lib/influxdb/wal"
# The amount of time that a write will wait before fsyncing. A duration
# 一般设置为0 同步写入wal文件和cache文件
# wal-fsync-delay = "0s"
# The type of shard index to use for new shards. The default is an in-memory index that is
# recreated at startup. A value of "tsi1" will use a disk based index that supports higher
# cardinality datasets.
# index-version = "inmem"
# Whether queries should be logged before execution. Very useful for troubleshooting, but will
# log any sensitive data contained within a query.
# 是否开启 请求日志,debug 环境开启,线上环境关闭
# query-log-enabled = true
# CacheMaxMemorySize is the maximum size a shard's cache can
# reach before it starts rejecting writes.
# Valid size suffixes are k, m, or g (case insensitive, 1024 = 1k).
# Values without a size suffix are in bytes.
# shard的最大内存 1g,超过1g后拒绝写入
# cache-max-memory-size = "1g"
# CacheSnapshotMemorySize is the size at which the engine will
# snapshot the cache and write it to a TSM file, freeing up memory
# Valid size suffixes are k, m, or g (case insensitive, 1024 = 1k).
# Values without a size suffix are in bytes.
# 设置cache最大阈值,达到阈值就生成快照 并刷新数据到tsm文件
# cache-snapshot-memory-size = "25m"
# CacheSnapshotWriteColdDuration is the length of time at
# which the engine will snapshot the cache and write it to
# a new TSM file if the shard hasn't received writes or deletes
# cache 快照 刷新到tsm文件的延迟 10分钟
# cache-snapshot-write-cold-duration = "10m"
# CompactFullWriteColdDuration is the duration at which the engine
# will compact all TSM files in a shard if it hasn't received a
# write or delete
# 单个tsm文件存在的时间4h,时间后合并为一个大的TSM文件
# compact-full-write-cold-duration = "4h"
总结
-
树是专门为TSM
构建的数据存储格式。与现有的influxdb
或B+ tree
实现相比,LSM tree
具有更好的压缩和更高的读写吞吐量。TSM tree
-
是shard
存储引擎的具体实现。TSM
-
有一个逻辑容器shard
,其按时间段分组,使得查询性能和批量清理数据变得简单高效。shard group
注:
influxdb
的存储结构知识点还是比较偏底层的,如要深入了解,需要阅读大量的源码,学习成本就陡然增加了。前期就粗浅的了解下,对以后保留策略的学习和简单运维有一定的帮助。
PS: 如若文章中有错误理解,欢迎批评指正,同时非常期待你的评论、点赞和收藏。我是徐同学,愿与你共同进步!
参考:
- http://blog.fatedier.com/2016/08/05/detailed-in-influxdb-tsm-storage-engine-one/
- https://www.cnblogs.com/zouhao/p/10997984.html
- https://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#shard-duration
- http://www.linuxdaxue.com/influxdb-principle.html