Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically clean up expired schema data #33

Open
shilinlee opened this issue Apr 7, 2023 · 0 comments
Open

Automatically clean up expired schema data #33

shilinlee opened this issue Apr 7, 2023 · 0 comments

Comments

@shilinlee
Copy link
Owner

shilinlee commented Apr 7, 2023

元数据老化设计方案

问题

问题1:

由于一些业务,Tag 和 Field 会持续增加,导致元数据膨胀,而很多Tag和Field可能随着业务调整,不会再有新数据写入。导致这些元数据一直存在meta中,浪费空间,可能会影响性能。故需要设计一个安全删除过期元数据的方案。

问题2:

Tag 和 Field 不支持同名字段,需要兼容吗? Influxdb支持。

问题3:

某个表数据已经全部过期,是否需要删除整个measurement元数据。--- RP过期是否会删除?

本方案目标:

  • 清理过期元数据,主要是Tag和Field
  • Tag 和 Field 支持同名字段吗 // TODO:Thinking
  • 如果measurement没有任何shardkey,IndexRelation,则可以删除。

Tag Field支持同名

将Tag和Field拆成2个map

type MeasurementInfo struct {
	Name          string
  ......
  
  // 删除
  Schema map[string]int32
  
  // 新增
  Tags        map[string]int32
  Fields      map[string]int32
}

IndexGroup 引用 schema id

基于引用计数的清理。

每一个表的tag, field会被分配一个全局唯一的ID,按顺序递增1,2,3......,schema id就是上面说的ID。

  • tag, field中保存有IndexGroup的ref数量
  • IndexGroup中记录所有的scheme id

当其中一个IndexGroup过期后,对应的所有tag/field的引用计数 ref - 1,当其中的tag/field引用计数ref减为0了,则可以清理了。

// TODO:写互斥

meta元数据存在如下更新

IndexGroupInfo

type IndexGroupInfo struct {
	ID        uint64
	StartTime time.Time
	EndTime   time.Time
	Indexes   []IndexInfo
	DeletedAt time.Time
  
	// 新增
	SchemaBitmap uint64
}

MeasurementInfo

type KeyInfo struct {
	ID    uint64 // unique key id
  Ref   int    // IndexGroupInfo ref count
	Type  int32  // data type
}


type MeasurementInfo struct {
	Name          string
  ......
  
  // 删除
  Schema map[string]int32
  
  // 新增
	Tags        map[string]KeyInfo
  Fields      map[string]KeyInfo
}


"MeasurementInfo": {
   "name": "mst_0001",
   "Schema": 
           {
            "tag1": {
              "id": 1,
              "ref": 1,
              "typ": String
            },
            "tag2": {
              "id": 2,
              "ref": 2,
              "typ": String
            },
            "field1": {
              "id": 3,
              "ref": 1,
              "typ": Integer
            },
            "field2": {
              "id": 4,
              "ref": 2,
              "typ": Float
            }
				}
}

RetentionPolicyInfo

type RetentionPolicyInfo struct {
  IndexGroups          []IndexGroupInfo            // each IndexGroupInfo contains Indexes that expire at the same time for all nodes
  Measurements         map[string]*MeasurementInfo // {"cpu_0001": *MeasurementInfo}
  
  // 新增
  KeyID   uint64 // 此RP,保证每一个tag/field能分配一个递增的ID
}

写流程

image

  • 写IndexGroup1(IG1), 写入新tag/field

    • SQL创建IndexGroup,记录Tag/Field的ID。

    • SQL创建Tag/Field,ref加1

  • 已存在IndexGroup1(IG1),写入新tag/field

    • 获取IndexGroup1 ID,增加记录Tag/Field的ID
    • SQL创建Tag/Field,ref加1
  • 已存在IndexGroup1(IG1),写入旧 tag/field

    • 获取IndexGroup ID,此IndexGroup未引用过此tag/field的ID,需要频繁和meta交互。
    • 无需更改meta元数据
  • 写新IndexGroup2,写入新/旧 tag/field

    • SQL创建IndexGroup,记录Tag/Field的ID。
    • SQL【创建】Tag/Field,ref加1
  • 已存在IndexGroup2(IG2),旧 tag/field

schema id

写入时,由meta统一递增分配

tag/field过期老化

随着IndexGroup过期,他所记录的所有tag/field相关的ref都减1,当有ref==0时,可删除。

删除和写互斥判断:TODO

schama查询

  • show tag keys [ condition ]
  • show field keys [ condition ]

修改点

![image-20230406204905577](/Users/xiangyu/Library/Application Support/typora-user-images/image-20230406204905577.png)

Q&As

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant