天天看点

influxdb基础(三)——influxdb按时间分片存储数据(shard和shard group)前言shard分片shard group 分片逻辑分组shard durationinfluxdb客户端查看shard和shard groupshard物理目录结构shard相关配置总结

文章目录

  • 前言
  • 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

存储结构关系图:

influxdb基础(三)——influxdb按时间分片存储数据(shard和shard group)前言shard分片shard group 分片逻辑分组shard durationinfluxdb客户端查看shard和shard groupshard物理目录结构shard相关配置总结

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)存放预写日志,持久化在磁盘中。其主要作用如下:

  1. 当系统崩溃后可以通过

    wal

    文件恢复还没有写入

    tsm

    文件的数据。
  2. 当系统重启,遍历所有

    wal

    文件,构造

    cache

Tsm file真正持久化存储数据

Tsm file

.tsm

)真正用于数据持久化存储。文件格式主要由为4 部分组成:

Header

Blocks

Index

Footer

  1. Header

    由两部分组成

    MagicNumber

    Version

    MagicNumber

    用于描述文件存储引擎,

    Version

    记录引擎版本。
  2. Blocks

    内部是一些连续的

    Block

    Block

    influxdb

    中的最小读取对象,每次读取操作都会读取一个

    Block

    。每一个

    Block

    分为

    CRC32

    值和

    Data

    两部分,

    CRC32

    值用于校验

    Data

    的内容是否有问题。
  3. Index

    存放的是

    Blocks

    里内容的索引。

    influxdb

    在做查询操作时,可以根据

    Index

    的信息快速定位到

    Tsm file

    中需要查询的

    block

    的位置。
  4. Footer

    位于

    Tsm file

    的最后8字节,存放了

    Index

    的起始位置在

    Tsm file

    中的偏移量,方便将索引信息加载到内存中。

Compactor 合并压缩线程

Compactor

组件在后台持续运行,每隔 1 秒会检查一次是否有需要压缩合并的数据。其主要有两种操作:

  1. cache

    中的数据大小达到阀值后,进行快照,之后转存到一个新的

    Tsm

    文件中。
  2. 合并

    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个:

  1. 一定程度上缓解数据写入热点问题。
  2. 批量清理过期数据变得简单高效。

    Compactor

    通过判断每一个

    shard group

    是否过期,过期的就按

    shard group

    批量删除数据。
  3. 提高数据按时间维度查找的性能。一般情况查询都会带一个时间范围条件,但是如果这个时间范围很大,跨度了几个

    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

分布情况:

influxdb基础(三)——influxdb按时间分片存储数据(shard和shard group)前言shard分片shard group 分片逻辑分组shard durationinfluxdb客户端查看shard和shard groupshard物理目录结构shard相关配置总结

show shard groups

show shard groups

查看所有

database

shard group

influxdb基础(三)——influxdb按时间分片存储数据(shard和shard group)前言shard分片shard group 分片逻辑分组shard durationinfluxdb客户端查看shard和shard groupshard物理目录结构shard相关配置总结

shard物理目录结构

默认情况下,

influxdb

数据存储在

/var/lib/influxdb/

下:

influxdb基础(三)——influxdb按时间分片存储数据(shard和shard group)前言shard分片shard group 分片逻辑分组shard durationinfluxdb客户端查看shard和shard groupshard物理目录结构shard相关配置总结
  • /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

[[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"
           

总结

  1. TSM

    树是专门为

    influxdb

    构建的数据存储格式。与现有的

    B+ tree

    LSM tree

    实现相比,

    TSM tree

    具有更好的压缩和更高的读写吞吐量。
  2. shard

    TSM

    存储引擎的具体实现。
  3. 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

继续阅读