一、TsFileResource及接口设计修改目标
1. 每个数据文件 TsFile 在内存中对应一个文件索引信息TsFileResource,供查询使用。
2. 目标:
(1) 总体目标:目前tsfile采用一对一的索引方式,每个tsfile都有一个索引文件TsFileResource,tsfile文件内的信息(开始时间、结束时间等)都保存在TsFileResource中。
由于TsFileResource常驻内存中,当有过多设备的情况下,就会爆内存。本次针对TsFileResouce的改进总目标为可以支持更多的设备。
(2) 阶段1目标 —— 接口化:区分【文件句柄TsFileResource】和【文件索引TsFileIndex】,将TsFileResource中开始时间、结束时间等字段移至TsFileIndex中。
3. 实现方案:
方案1(经尝试后,否决):利用FileIndex的接口完成“文件索引”相关功能;TsFileResource中仅保留文件句柄的功能。
使用 FileIndexManager 管理 FileIndex,使用 FileIndexEntries 和 TimeIndexEntry 进行消息传递。
缺点:(1) 设计新文件结构多,模块变动大,修改代码过多,后续难以维护;
(2) 增加新的落盘文件,文件目录庞大;较大地影响分布式部分的代码;
(3) merge 操作和查询操作涉及到 FileIndex 与 TsFileResource的互相调用和转化。
方案2:TsFileIndex 仅作为 TsFileResource 中的一个对象,全部接口均由 TsFileResource 调用,不向外暴露;
TsFileResource 的已有接口不变。后续“实现”的全部修改仅针对 TsFileIndex。
二、阶段1详细设计:新增和修改的数据结构
具体而言,涉及到数据结构如下:
1. TsFileIndex (后续“实现”的全部修改仅针对 TsFileIndex)
protected long[] startTimes; // 开始时间列表 protected long[] endTimes; // 结束时间列表,未封口则为 Long.MIN_VALUE protected Map<String, Integer> deviceToIndex; // device设备名 => 开始 / 结束时间列表index |
2. 第二步的修改(后续)涉及到【封口文件句柄SealedTsfileResource】和【未封口文件句柄UnsealedTsfileResource】
因为封口文件句柄需要的字段较少,未封口文件句柄可以继承封口文件句柄;文件封口时再转化为封口文件句柄。
3. 总结
(1) 第一步接口化(本次)的修改中,可以从TsFileResource中转移走的字段:
protected long[] startTimes; // 开始时间列表 protected long[] endTimes; // 结束时间列表,未封口则为 Long.MIN_VALUE protected Map<String, Integer> deviceToIndex; // device设备名 => 开始 / 结束时间列表index |
(2) 目前TsFileResource文件句柄保留的功能和字段:
Map<String, String> cachedDevicePool // for reducing the String number in memory File file // tsfile TsFileProcessor processor 【未封口】 ModificationFile modFile // modification file boolean closed【未封口】 boolean deleted【未封口】 boolean isMerging Set<Long> historicalVersions // for tracking the merge history of a TsFile TsFileLock tsFileLock List<ChunkMetadata> chunkMetadataList // 【未封口】chunk metadata list of unsealed tsfile. Only be set in a temporal TsFileResource in a query process. List<ReadOnlyMemChunk> readOnlyMemChunk // 【未封口】mem chunk data. Only be set in a temporal TsFileResource in a query process. TimeseriesMetadata timeSeriesMetadata // 【未封口】get TimeseriesMetadata of unsealed file List<TsFileResource> upgradedResources // 【升级】generated upgraded TsFile ResourceList used for upgrading v0.9.x/v1 -> 0.10/v2 UpgradeTsFileResourceCallBack upgradeTsFileResourceCallBack // 【升级】load upgraded TsFile Resources to storage group processor used for upgrading v0.9.x/v1 → 0.10/v2 boolean isSeq // 【升级】indicate if this tsfile resource belongs to a sequence tsfile or not used for upgrading v0.9.x/v1 -> 0.10/v2 TsFileResource originTsFileResource // 【未封口】current tsfile resource is a snapshot of the originTsFileResource. When we want to used the lock, we should try to acquire the lock of originTsFileResource long maxPlanIndex // for cluster, max index of plans executed within this TsFile long minPlanIndex // for cluster, min index of plans executed within this TsFile |