InfluxDB meta文件解析
操作系统 : CentOS7.3.1611_x64
go语言版本:1.8.3 linux/amd64
InfluxDB版本:1.1.0
influxdb默认配置:
/etc/influxdb/influxdb.conf
meta默认配置:
[meta]
dir = "/var/lib/influxdb/meta"
retention-autocreate = true
logging-enabled = true
- dir
meta数据存放目录,默认值:/var/lib/influxdb/meta
meta数据文件默认路径:/var/lib/influxdb/meta/meta.db
- retention-autocreate
用于控制默认存储策略,数据库创建时,会自动生成autogen的存储策略,默认值:true
- logging-enabled
是否开启meta日志,默认值:true
meta文件的dump和load
源码路径: github.com/influxdata/influxdb/services/meta/client.go
meta文件dump
// snapshot will save the current meta data to disk
func snapshot(path string, data *Data) error {
file := filepath.Join(path, metaFile)
tmpFile := file + "tmp" f, err := os.Create(tmpFile)
if err != nil {
return err
}
defer f.Close() var d []byte
if b, err := data.MarshalBinary(); err != nil {
return err
} else {
d = b
} if _, err := f.Write(d); err != nil {
return err
} if err = f.Sync(); err != nil {
return err
} //close file handle before renaming to support Windows
if err = f.Close(); err != nil {
return err
} return renameFile(tmpFile, file)
}
snapshot可以通过以下两种方式触发:
1、当执行 Client.Open 函数时会进行snapshot操作;
2、执行meta文件更新时通过commit函数进行snapshot操作;
在InfluxDB中程序中,通过 NewServer 函数创建MetaClient变量(meta.NewClient),然后执行MetaClient.Open()进行初始化;
后续会通过Server.Open函数(run/server.go)启动各项服务,如果有meta文件的更新操作,通过commit函数进行snapshot操作;
meta文件load
// Load will save the current meta data from disk
func (c *Client) Load() error {
file := filepath.Join(c.path, metaFile) f, err := os.Open(file)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer f.Close() data, err := ioutil.ReadAll(f)
if err != nil {
return err
} if err := c.cacheData.UnmarshalBinary(data); err != nil {
return err
}
return nil
}
Client.Open()中会执行Load操作,NewServer时会自动加载。
meta文件内容编解码
源码路径: github.com/influxdata/influxdb/services/meta/data.go
meta数据encode:
// MarshalBinary encodes the metadata to a binary format.
func (data *Data) MarshalBinary() ([]byte, error) {
return proto.Marshal(data.marshal())
}
meta数据decode:
// UnmarshalBinary decodes the object from a binary format.
func (data *Data) UnmarshalBinary(buf []byte) error {
var pb internal.Data
if err := proto.Unmarshal(buf, &pb); err != nil {
return err
}
data.unmarshal(&pb)
return nil
}
proto路径 :github.com/gogo/protobuf/proto
meta文件结构定义
源码路径: github.com/influxdata/influxdb/services/meta/data.go
meta文件存储的就是 meta.Data 的数据,结构定义如下:
// Data represents the top level collection of all metadata.
type Data struct {
Term uint64 // associated raft term
Index uint64 // associated raft index
ClusterID uint64
Databases []DatabaseInfo
Users []UserInfo MaxShardGroupID uint64
MaxShardID uint64
}
Term :暂时不知道干什么用的。
Index :从源码看这个应该是类似版本号的东西,初始化为1,执行commit操作是会增加。如果为1,会立即执行持久化操作(在Open函数中操作)。
ClusterID : 是InfluxDB集群相关内容;
Databases :用于存储数据库信息;
Users :用于存储数据库用户信息;
DatabaseInfo 定义 :
// DatabaseInfo represents information about a database in the system.
type DatabaseInfo struct {
Name string
DefaultRetentionPolicy string
RetentionPolicies []RetentionPolicyInfo
ContinuousQueries []ContinuousQueryInfo
}
RetentionPolicyInfo 定义:
// RetentionPolicyInfo represents metadata about a retention policy.
type RetentionPolicyInfo struct {
Name string
ReplicaN int
Duration time.Duration
ShardGroupDuration time.Duration
ShardGroups []ShardGroupInfo
Subscriptions []SubscriptionInfo
}
ShardGroupInfo 定义:
// ShardGroupInfo represents metadata about a shard group. The DeletedAt field is important
// because it makes it clear that a ShardGroup has been marked as deleted, and allow the system
// to be sure that a ShardGroup is not simply missing. If the DeletedAt is set, the system can
// safely delete any associated shards.
type ShardGroupInfo struct {
ID uint64
StartTime time.Time
EndTime time.Time
DeletedAt time.Time
Shards []ShardInfo
TruncatedAt time.Time
}
ShardInfo 定义:
// ShardInfo represents metadata about a shard.
type ShardInfo struct {
ID uint64
Owners []ShardOwner
}
ShardOwner 定义:
// ShardOwner represents a node that owns a shard.
type ShardOwner struct {
NodeID uint64
}
ShardOwner主要用于集群,其中NodeId用于标识集群的节点ID,在InfluxDB 1.1社区版本中集群已经不支持了,该字段无效。
SubscriptionInfo 定义:
// SubscriptionInfo hold the subscription information
type SubscriptionInfo struct {
Name string
Mode string
Destinations []string
}
ContinuousQueryInfo 定义:
// ContinuousQueryInfo represents metadata about a continuous query.
type ContinuousQueryInfo struct {
Name string
Query string
}
UserInfo 定义:
// UserInfo represents metadata about a user in the system.
type UserInfo struct {
Name string
Hash string
Admin bool
Privileges map[string]influxql.Privilege
}
其它
meta文件解析示例代码:
package main import (
"os"
"fmt"
"io/ioutil"
"github.com/influxdata/influxdb/services/meta"
) func Load(metaFile string) error {
cacheData:= &meta.Data{
Index: ,
}
//file := filepath.Join(c.path, metaFile) f, err := os.Open(metaFile)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer f.Close() data, err := ioutil.ReadAll(f)
if err != nil {
return err
} if err := cacheData.UnmarshalBinary(data); err != nil {
return err
}
//fmt.Println(data)
//fmt.Println("=======================") fmt.Println("Term :",cacheData.Term)
fmt.Println("Index :",cacheData.Index)
fmt.Println("Databases :")
//fmt.Println(cacheData.Databases) for k,dbInfo := range cacheData.Databases {
//fmt.Println(k,dbInfo)
fmt.Println("k =",k)
fmt.Println(dbInfo.Name,dbInfo.DefaultRetentionPolicy)
for _,rPolicy := range dbInfo.RetentionPolicies {
//fmt.Println(rPolicy)
fmt.Println(rPolicy.Name,rPolicy.ReplicaN,rPolicy.Duration,rPolicy.ShardGroupDuration)
fmt.Println("-------------ShardGroups---------------")
//fmt.Println(rPolicy.ShardGroups)
for shardIdx,shardGroup := range rPolicy.ShardGroups {
//fmt.Println(shardGroup)
fmt.Println("shardIdx =",shardIdx)
fmt.Println("ID :",shardGroup.ID)
fmt.Println("StartTime :",shardGroup.StartTime)
fmt.Println("EndTime :",shardGroup.EndTime)
fmt.Println("DeletedAt :",shardGroup.DeletedAt)
//fmt.Println("Shards :",shardGroup.Shards)
fmt.Printf("Shards :")
for _,shard := range shardGroup.Shards {
fmt.Println(shard.ID,shard.Owners)
} fmt.Println("TruncatedAt :",shardGroup.TruncatedAt)
//fmt.Println(shardGroup.ID,shardGroup.StartTime,shardGroup.EndTime)
// DeletedAt,Shards , TruncatedAt
}
//fmt.Println(rPolicy.Subscriptions)
fmt.Println("--------------Subscriptions----------------")
for subsIdx,subInfo := range rPolicy.Subscriptions {
//fmt.Println(subInfo)
fmt.Println("subsIdx =",subsIdx)
fmt.Println("Name :",subInfo.Name)
fmt.Println("Mode :",subInfo.Mode)
fmt.Println("Destinations :",subInfo.Destinations)
} }
fmt.Println("=======================")
} fmt.Println("Users :")
fmt.Println(cacheData.Users)
fmt.Println(cacheData.MaxShardGroupID)
fmt.Println(cacheData.MaxShardID)
return nil
} func main() {
argsWithProg := os.Args
if(len(argsWithProg) < ) {
fmt.Println("usage : ",argsWithProg[]," configFile")
return
}
metaFile := os.Args[] fmt.Println(argsWithProg)
fmt.Println(metaFile) Load(metaFile)
}
好,就这些了,希望对你有帮助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180112_InfluxDB_meta文件解析.rst
欢迎补充
InfluxDB meta文件解析的更多相关文章
- 文件解析库doctotext源码分析
		doctotext中没有make install选项,make后生成可执行文件 在buile目录下面有.so动态库和头文件,需要的可以从这里面拷贝 build/doctotext就是可执行程序. ... 
- CocosStudio文件解析工具CsdAnalysis
		起因 因为工作需要,所以需要使用CocosStudio来制作界面动画什么的.做完了发现需要找里边对象的时候会有很长一串代码,感觉不是很爽.之前写OC代码的时候可以吧程序中的对象指针跟编辑器中的对象相对 ... 
- 通过正则表达式实现简单xml文件解析
		这是我通过正则表达式实现的xml文件解析工具,有些XHTML文件中包含特殊符号,暂时还无法正常使用. 设计思路:常见的xml文件都是单根树结构,工具的目的是通过递归的方式将整个文档树装载进一个Node ... 
- unity文件解析以及版本控制
		刚开始使用unity做开发时,拿到一个范例工程先上传SVN,之后再自己做一些修改后,发现有非常多文件都有变化,这才知道有很多本地生成的文件,是不用上传的,但是不知道哪些才是需要共用的.之后又困扰于修改 ... 
- 八、Android学习第七天——XML文件解析方法(转)
		(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 八.Android学习第七天——XML文件解析方法 XML文件:exten ... 
- phpcms V9 首页模板文件解析
		在了解了<phpcms V9 URL访问解析>之后,我们已经知道首页最终执行的是content模块下index控制器的init方法. 下面, 我们逐步分析过程如下: 第一.首页默认执行的是 ... 
- (转)AVI文件格式解析+AVI文件解析工具
		AVI文件解析工具下载地址:http://download.csdn.net/detail/zjq634359531/7556659 AVI(Audio Video Interleaved的缩写)是一 ... 
- itextSharp 附pdf文件解析
		一.PdfObject: pdf对象 ,有9种,对象是按照对象内涵来分的,如果按照对象的使用规则来说,对象又分为间接对象和直接对象.间接对象是PDF中最常用的对象,如前面对象集合里面的,所有对象都是间 ... 
- 《热血传奇2》wix、wil文件解析Java实现
		在百度上搜索java+wil只有iteye上一篇有丁点儿内容,不过他说的是错的!或者说是不完整的,我个人认为我对于热血传奇客户端解析还是有一定研究的,请移步: <JMir——Java版热血传奇2 ... 
随机推荐
- HashMap实现原理简析及实现的demo(一看就明白)
			HashMap底层就是一个数组结构,数组中的每一项又是一个链表. jdk源码: transient Node<K,V>[] table; static class Node<K,V& ... 
- mycql 多表联合查询
			egon笔记: 1 单表查询 select distinct 字段1,字段2,字段3 from 表 where 约束条件 group by 分组字段 having 过滤条件 order by 排序字段 ... 
- STC15W408AS简单使用教程-简单的光强检测!
			第一步:搭建开发环境 安装最新版本的STC_ISP程序烧录软件,链接:http://pan.baidu.com/s/1slLPnOD 密码:6bov 安装keil C51的51系列单片机集成IDE软件 ... 
- Lock为线程上锁,防止数据混乱
			用法: 先实例化 lock = threading.Lock() 1. lock.acquire() 上锁 需上锁代码 lock.release() 解锁 2. with lock: 上下两种方式都 ... 
- TF:TF之Tensorboard实践:将神经网络Tensorboard形式得到events.out.tfevents文件+dos内运行该文件本地服务器输出到网页可视化—Jason niu
			import tensorflow as tf import numpy as np def add_layer(inputs, in_size, out_size, n_layer, activat ... 
- SVM:随机产生100个点,建立模型,找出超平面方程——Jaosn niu
			import numpy as np import pylab as pl from sklearn import svm # we create 40 separable points #np.ra ... 
- SNMP弱口令漏洞的使用
			如果能获取只读(RO)或读/写(RW)权限的团体字符串,将对你从设备中提取信息发挥重要作用,snmp v1 v2天生存在安全缺陷,snmp v3中添加了加密功能提供了更好的检查机制,增强了安全性为了获 ... 
- linux 学习笔记 APACHE安装总结
			#cd /usr/local #mkdir APACHE #tar zxvf /usr/etc/DEV/httpd-2.2.9.tar.gz #mv httpd-2.2.9/* . #rm -rf h ... 
- 使用Log4j日志处理
			Springboot日志默认使用的是logback,本文将介绍将springboot项目日志修改为log4j. 首先要将默认的日志依赖排除,然后引用log4j,pom文件代码如下: <?xml ... 
- Python流程控制if判断以及whlie循环
			一.基本运算符补充 1. 算术运算 print(10 / 3) print(10 // 3) print(10 ** 2) 2. 赋值运算 ... 
