-
当系统崩溃后可以通过
wal
文件恢复还没有写入tsm
文件的数据。 -
当系统重启,遍历所有
wal
文件,构造cache
。
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
组件在后台持续运行,每隔 1 秒会检查一次是否有需要压缩合并的数据。其主要有两种操作:
-
cache
中的数据大小达到阀值后,进行快照,之后转存到一个新的Tsm
文件中。 -
合并
Tsm
文件。将多个小的Tsm
文件合并成一个,使每一个文件尽量达到单个文件的最大值,减少Tsm
文件的数量,并且一些数据的删除操作也是在这个时候完成。
=====================================================================================
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
和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
可查看所有database
的shards
分布情况:
show shard groups
查看所有database
的shard group
。
==============================================================================
默认情况下,influxdb
数据存储在/var/lib/influxdb/
下:
-
/var/lib/influxdb/data
存储tsm
文件。 -
/var/lib/influxdb/wal
存储wal
文件。 -
/var/lib/influxdb/meta
存放元数据(meta.db
),包含系统状态的内部信息,用户信息、数据库/分片元数据、CQs、RPs和订阅等。
第一层是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
第一层是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
============================================================================
influxdb
中数据存储位置、文件大小等一些配置可在/etc/influxdb/influxdb.conf
中配置。如下摘取部分配置:
[meta]
存储meta数据
Where the metadata/raft database is stored
dir = “/var/lib/influxdb/meta”
惊喜
最后还准备了一套上面资料对应的面试题(有答案哦)和面试时的高频面试算法题(如果面试准备时间不够,那么集中把这些算法题做完即可,命中率高达85%+)
============================
influxdb
中数据存储位置、文件大小等一些配置可在/etc/influxdb/influxdb.conf
中配置。如下摘取部分配置:
[meta]
存储meta数据
Where the metadata/raft database is stored
dir = “/var/lib/influxdb/meta”
惊喜
最后还准备了一套上面资料对应的面试题(有答案哦)和面试时的高频面试算法题(如果面试准备时间不够,那么集中把这些算法题做完即可,命中率高达85%+)
[外链图片转存中…(img-ejXYgqeU-1714300673244)]
[外链图片转存中…(img-P2NibSZa-1714300673244)]