pr: https://github.com/apache/iotdb/pull/19417 Background: DataNodes maintain a TableDeviceSchemaCache that stores device schema and last values. The analyze phase fills device schema, while LAST queries declare last-cache placeholders and put back the values they observe. Delete procedures mark the target schema as pre-deleted or blacklisted, invalidate caches before data deletion, delete data, and then delete metadata. Problem: The cache invalidation performed by a delete procedure is a point-in-time operation. A LAST query can be analyzed before the deletion but declare or put back its result after the first invalidation. Because the LAST path also wrote device schema, the deleted schema can be restored after cleanup. A later INSERT then finds the stale entry, skips auto-creation, writes data, and leaves metadata absent; SHOW and fuzzy SELECT cannot see the series while an exact SELECT can. The same class of race exists when write FI execution writes device schema, when a fenced DataNode keeps updating the cache while metadata is being pulled, and when concurrent LAST queries leave a stale value depending on their put-back order. Solution: - Query side: LAST declaration and put-back become last-cache only; device schema is written only by the analyze-time schema fetch. An all-null LAST result caches the empty marker or removes a previously cached value, and a valid value arriving while the cache holds the all-null marker removes the entry instead of overwriting it. - Delete side: delete procedures append a second cache cleanup after data deletion and before metadata deletion. New states are appended to the existing state machines so persisted ordinal IDs remain stable. DELETE DATABASE relies on DN-side invalidation after region deletion. The second cleanup only clears TableDeviceSchemaCache, not DataNodeTableCache. - Execution side: write FI execution updates last values only. Fenced DataNodes skip device-cache writes; when a fragment instance ends in a failure state, the devices declared by that query attempt are removed from the cache and later put-back attempts are skipped. - Memory accounting: last-cache size calculation and attribute removal now use the actual stored key and value sizes. Impact: Read results are unchanged; a cache miss falls back to SchemaRegion. The change adds one extra invalidation or scan in deletion and race scenarios. Existing persisted procedures keep their state IDs; new states are appended. No data or metadata format changes. The only protocol change is an optional field on TInvalidateTableCacheReq that distinguishes the first and second table-cache cleanup. --- 主题:避免 LAST 查询与元数据删除并发时残留 device schema 缓存 背景: DataNode 使用 TableDeviceSchemaCache 缓存设备 schema 和 last value。分析阶段会填充 device schema;LAST 查询会声明 last cache 占位符,并在执行结束后回填观测到的值。删除 procedure 会先把目标 schema 标记为预删除或加入黑名单,在删除数据前清理缓存,然后删除数据,最后删除元数据。 问题: 删除 procedure 的缓存失效只是某一时刻的清理。一个 LAST 查询可能在删除前完成分析,但在第一次缓存清理之后才声明或回填结果。由于 LAST 路径以前也会写 device schema,已删除的 schema 可能在清理之后被重新写入缓存。后续 INSERT 命中这个残留条目,跳过自动建模,把数据写入数据区,但元数据不存在;SHOW 和模糊 SELECT 看不到该序列,而精确 SELECT 可以读到。类似的竞态也存在于写入 FI 执行阶段写 device schema、隔离状态的 DataNode 在拉取元数据期间继续更新缓存,以及两个 LAST 查询因回填顺序不同而留下脏值的情况。 解决方案: - 查询侧:LAST 声明和回填只操作 last cache;device schema 只由分析阶段的 schema 拉取写入。全 null 的 LAST 结果缓存空标记,或删除已有的有效值;有效值到达时如果缓存里是全 null 标记,则删除条目而不是覆盖。 - 删除侧:各删除 procedure 在删除数据之后、删除元数据之前追加一次缓存清理。新状态追加在现有状态机末尾,保证持久化的 ordinal ID 不变。DELETE DATABASE 依赖 DN 侧在删除 Region 后清理。第二次清理只清 TableDeviceSchemaCache,不碰 DataNodeTableCache。 - 执行侧:写入 FI 执行阶段只更新 last value。隔离状态的 DataNode 跳过 device cache 写入;FragmentInstance 进入失败终态时,清理本查询声明过的设备缓存,并阻止后续回填。 - 内存计算:last cache 大小计算和属性删除改为使用实际存储的 key、value 大小。 影响: 查询结果不变,缓存未命中会回源 SchemaRegion。删除和竞态场景会多一次缓存失效或扫描。已持久化的 procedure 保持原有状态 ID,新状态追加在末尾。数据格式和元数据格式不变。协议层唯一变化是 TInvalidateTableCacheReq 增加一个 optional 字段,用于区分第一次和第二次 table cache 清理。
Best regards, Yaobin Chen
