1. 设计概述

1.1. 目标场景

在用户密切关心序列最新值的场景中,Last 查询(最新点查询)的性能很重要。

因此设计最新点缓存(lastCache),在内存中缓存序列的最新值、或在磁盘上以一种更高效的格式(ID表)存储每条序列的最新点。


1.2. 概要设计

  1. 为了对每一条时间序列都缓存最新点,因此在元数据树上开辟lastCache字段。
  2. 代码结构
    1. 为了提升功能代码的内聚性,通过设计ILastCacheContainer接口,将LastCache操作与MNode解耦,由LasCacheContainer实现ILastCacheContainer接口,封装lastCache数据同时提供简单的lastCacheValue操作。
    2. 新建LastCacheManager,将MManager中与lastCache相关的操作代码尽可能迁移至 LastCacheManager,MManager中仅保留必要的mtree调用以及mnode获取操作。
  3. 适配物理量模板:
    1. 由于Template由EntityMNode进行使用,因此在EntityMNode中增加Map<String, ILastCacheContainer>字段,用于存储template所表示的时间序列的lastCache。
    2. EntityMNode中存储模板对应序列的lastCache,MeasurementMNode中存储普通序列的lastCache。


2. 类设计


类名

职责

StorageGroupProccessor

与MManager进行交互,在数据插入、数据删除以及上传新tsfile的时候清空对应序列的lastCache

LastQueryExecutor

Last查询执行类,调用MManager进行lastCache操作

MManager

  1. 对IoTDB的其他模块提供lastCache的操作接口
  2. 为LastCacheManager提供node查找功能

IEntityMNode与EntityMNode

实体节点,存储由物理量模板所表示的时间序列的lastCache,即Map<String, ILastCacheContainer>

IMeasurementMNode与MeasurementMNode

物理量节点,手动创建时间序列时产生,存储其表示的时间序列的lastCache

LastCacheManager

封装所有的lastCache处理逻辑,并向mmanager提供lastCache操作接口

ILastCacheContainer

最新点缓存项接口,声明最新点缓存的操作接口,该接口未来可向多条value缓存扩展

LastCacheContainer

实现ILastCacheContainer,定义对于一个最新点缓存值的基本操作

ILastCacheValue

最新点缓存值接口,声明

LastCacheValue

序列的最新点缓存值


MManager对外接口

功能接口解释


更新lastCache
public void updateLastCache(
PartialPath seriesPath,
TimeValuePair timeValuePair,
boolean highPriorityUpdate,
Long latestFlushedTime)

接收seriesPath作为参数,

用于更新序列的lastCache,

需传入序列全路径。

public void updateLastCache(
IMeasurementMNode node,
TimeValuePair timeValuePair,
boolean highPriorityUpdate,
Long latestFlushedTime)

接收单个MeasurementMNode作为参数,

用于更新序列的lastCache

获取lastCache
public TimeValuePair getLastCache(PartialPath seriesPath)

接收seriesPath作为参数,

用于获取序列的lastCache,

需传入序列的全路径

public TimeValuePair getLastCache(IMeasurementMNode node)

接收单个MeasurementMNode作为参数,

用于获取序列的lastCache

删除lastCache
public void resetLastCache(PartialPath seriesPath)

接收seriesPath作为参数,

用于清除序列的lastCache,

需传入序列的全路径

public void deleteLastCacheByDevice(PartialPath deviceId)
用于清除一个device下所有序列的lastCache
public void deleteLastCacheByDevice(
PartialPath deviceId,
PartialPath originalPath,
long startTime,
long endTime)
由于清除一个device下匹配originalPath的、且last值在startTime和endTime之间的所有序列的lastCache

3. 调用场景及执行过程

3.1. last cache查询

last cache查询主要在Last Query的执行过程中,由LastQueryExecutor中的LastCacheAccessor进行调用。

  1. LastCacheAccessor通过调用MManager.getNodeByPath(PartialPath path)获取序列对应的MeasurementMNode
    1. 如果是普通序列,则正常返回MeasurementMNode
    2. 如果是template表示的序列,则将对应的序列schema与封装为临时的MeasurementMNode返回
  2. LastCacheAccessor调用MManager.getLastCache
    1. 若1中的操作成功,则可传入node,避免MManager对measurementMNode的二次查找。
    2. 若1中节点获取失败,则尝试仅基于path查找lastCache结果,此时MManager会基于path进行MeasurementMNode查找
  3. MManager调用LastCacheMManager的getLastCache接口
    1. 判断是否为template表示的序列,即判断传入的measurementMNode的parent EntityMNode是否有对应的子节点,若无则从EntityMNode中获取到LastCacheContainer并赋予该measurementMNode
    2. 如果是普通序列,获取measurementMNode中的lastCacheContainer后获取其持有的lastCacheValue

2中情况的一个典型场景为分布式中,node的获取不会返回本地缓存的remote measurementMNode,但是lastCache查询可以先走remote  measurementMNode中进行查询。

3.2. last cache更新

3.2.1. Last Query执行过程中的last cache更新

last查询过程中,若cache miss,则会从文件中读取last value,并且将该值进行缓存,此时LastCacheAccesso将调用MManager的updateLastCache接口。

与getLastCache接口类似,updateLastCache接口也可以path和node双参数传入。

  1. 如果LastCacheAccessor先前已成功获取node,则将node传入,MManager避免node的二次查找
  2. LastCacheAccessor先前未获取到node,则基于path更新lastCache,MManager将基于path查找对应node
  3. MManager调用LastCacheManager的updateLastCache接口,不同类型序列的处理逻辑与3.1中3的逻辑大致相同

3.2.2. insertPlan执行过程中的last cache更新

insertPlan执行过程中,插入的数据的最新点需缓存,由StorageGroupProcessor调用MManager的updateLastCache接口。

为了解决insert和last query的并发更新,updateLastCache中设置priority参数,insert的last cache更新优先级高于last query。

3.3. last cache 清除

3.3.1. delete执行过程中的last cache清除

StorageGroupProcessor在执行delete的过程中,会基于用户输入的originPath以device为单位进行删除操作,originPath是一个可以包含通配符*的路径,需筛选device下与其相匹配的时间序列。

lastCache的删除过程中需根据删除数据的起始时间进行判断是否要清空缓存。

因此MManager提供deleteLastCacheByDevice(PartialPath deviceId, PartialPath originalPath, long startTime, long endTime)接口,MManager获取节点后,具体清除逻辑由LastCacheManager进行执行。

EntityMNode中template的LastCacheContainer与MeasurementMNode的LastCacheContainer均需检查是否需要清空。

3.3.2. upload tsfile执行过程中的last cache清除

upload tsfile后由于引入了新数据,需将对应序列的last cache清空,此过程也是以device为单位进行执行。

MManager提供deleteLastCacheByDevice(PartialPath deviceId)接口,具体过程与3.3.1类似。


4. LastCache扩展方向

  1. LastCache缓存多条数据,LastCacheContainer向BatchedLastCacheContainer扩展
  2. 底层实现重写,如lastCache存储Memtable中最新的批量数据
  • No labels