influxdb基础(三) 您所在的位置:网站首页 influxdb分组查询 influxdb基础(三)

influxdb基础(三)

2023-07-30 21:30| 来源: 网络整理| 查看: 265

文章目录 前言shard分片Cache缓存Wal存储预写日志Tsm file真正持久化存储数据Compactor 合并压缩线程 shard group 分片逻辑分组shard durationinfluxdb客户端查看shard和shard groupshow shardsshow 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存储结构关系图: 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位于Tsm file 的最后8字节,存放了 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 durationshardGroupDuration< 2 days1 h>= 2 days and 6 months7 days influxdb客户端查看shard和shard group show shards

show shards 可查看所有database的shards分布情况:

show shards

show shard groups

show shard groups 查看所有database的shard group。

show shard groups

shard物理目录结构

默认情况下,influxdb数据存储在/var/lib/influxdb/下:

/var/lib/influxdb/

/var/lib/influxdb/data存储tsm文件。/var/lib/influxdb/wal存储wal文件。/var/lib/influxdb/meta存放元数据(meta.db),包含系统状态的内部信息,用户信息、数据库/分片元数据、CQs、RPs和订阅等。 /var/lib/influxdb/data目录结构

第一层是database目录,第二层是retention policy目录以及_series,rp目录下shard group id目录,shard group id目录下是.tsm文件和fields.idx。

[root@xfyLinux ~]# 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文件。

[root@xfyLinux 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.htmlhttps://docs.influxdata.com/influxdb/v1.8/concepts/glossary/#shard-durationhttp://www.linuxdaxue.com/influxdb-principle.html


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有