Hi all, While investigating a wide-table Pipe replication issue on the 2.x table model, we uncovered a critical compaction bug on `master` where `ReadChunkCompactionPerformer` (the default `inner_seq_performer=read_chunk`) can silently drop tables—or `FIELD` columns—from `TsFileMetadata.tableSchemaMap` in compacted TsFiles, even though all chunks and device index nodes for those tables are written into the file.
I have filed a GitHub issue with full details here: https://github.com/apache/iotdb/issues/19488 1. Root Cause (1) At the start of compaction, `CompactionTableSchemaCollector#collectSchema` initializes each table's `CompactionTableSchema` with `TAG` columns only and intentionally leaves `FIELD` columns empty, relying on `endChunkGroup()` -> `Schema#updateTableSchema(ChunkGroupMetadata)` to populate `FIELD` columns as each device's chunk group finishes. (2) Unlike `FastCompactionPerformer` and `ReadPointCompactionPerformer` (which call `endChunkGroup()` before `checkAndMayFlushChunkMetadata()`), `ReadChunkCompactionPerformer#compactAlignedSeries` calls them in the reverse order: writer.checkMetadataSizeAndMayFlush(); writer.endChunkGroup(); (3) Whenever the accumulated in-memory `ChunkMetadata` size exceeds `maxMetadataSize` (`memoryBudgetForFileWriter`), `TsFileIOWriter#sortAndFlushChunkMetadata()` flushes the active device's `chunkMetadataList` to `.chunk_meta.temp` (so the device's `TimeseriesMetadata` and `tableMetadataIndexNodeMap` entry ARE written into the final TsFile), and then clears `chunkMetadataList`. (4) Immediately afterwards, `CompactionTsFileWriter#endChunkGroup()` sees `chunkMetadataList.isEmpty() == true` and returns early without calling `getSchema().updateTableSchema(...)` (and without resetting `currentDeviceId`). (5) At `CompactionTsFileWriter#endFile()`, `removeUnusedTableSchema()` removes any `TableSchema` that has neither `FIELD` nor `TIME` columns: - If all devices of a table in the target TsFile hit the `checkMetadataSizeAndMayFlush()` boundary (for example, 1-device wide tables where every N-th table crosses the ~4 MB metadata budget), the entire table is deleted from `tableSchemaMap`. - If a table has multiple devices and only a subset of them hit the flush boundary, any `FIELD` column present only on the flushed device(s) is silently omitted from that table's `TableSchema`. 2. Impact & Consequences - Masked on local SQL queries: Normal `SELECT` queries on the sender DataNode still return complete results because query planning/execution resolves table schemas from the in-memory `DataNodeTableCache` and locates devices via `TsFileMetadata.tableMetadataIndexNodeMap`. As a result, the on-disk metadata loss goes completely unnoticed during normal read/write operations. - Silent data loss in Pipe TsFile synchronization: `TsFileInsertionEventTableParser` discovers tables and field columns exclusively from `fileMetadata.getTableSchemaMap()`. When Pipe parses such a compacted TsFile (historical sync, `realtime.mode=file`, stream-to-TsFile fallback, or receiver-side tablet conversion), any table or column missing from `tableSchemaMap` is silently skipped—0 rows synced and no error or warning logged—leading to silent data divergence between sender and receiver clusters. - Silent data loss or failure in `LOAD TSFILE`: - When `LOAD TSFILE` converts TsFiles into tablets (e.g., mini-file conversion, type-mismatch conversion, cross-partition split, or read-only mode), it delegates to `TsFileInsertionEventTableParser` and silently drops the missing tables/columns. - When `LOAD TSFILE` loads the compacted file directly onto a fresh cluster where the table has not been pre-created, `LoadTsFileTableSchemaCache` finds the table in neither the file's `tableSchemaMap` nor `DataNodeTableCache`, causing `LOAD TSFILE` to fail instead of auto-creating the table. - Propagation to higher-level compactions & external readers: Subsequent compactions also collect `TAG` column definitions from source TsFiles' `getTableSchemaMap()`. Once all TsFiles containing a table's `TableSchema` have had it stripped, later compactions permanently lose that table's `TableSchema` (including its `TAG` columns). External `TsFileSequenceReader` consumers and connectors enumerating tables via `getTableSchemaMap()` are similarly affected. 3. Proposed Fix & Discussion Points (1) Fix compaction writer lifecycle: - In `ReadChunkCompactionPerformer#compactAlignedSeries`, call `writer.endChunkGroup()` before `writer.checkMetadataSizeAndMayFlush()`, consistent with `FastCompactionPerformer` and `ReadPointCompactionPerformer`. - Defensively in `CompactionTsFileWriter`: if `sortAndFlushChunkMetadata()` is ever triggered while an open chunk group has non-empty `chunkMetadataList`, update the `TableSchema` before `chunkMetadataList` is cleared, and ensure `endChunkGroup()` always resets `currentDeviceId` / `currentChunkGroupDeviceId` cleanly. (2) Handling already-compacted TsFiles in existing deployments: Since existing 2.x clusters may already have compacted TsFiles on disk where `tableMetadataIndexNodeMap` contains tables missing from `tableSchemaMap`, should we also make `TsFileInsertionEventTableParser` (when running inside DataNode) fall back to `DataNodeTableCache` or log an explicit warning/error when `tableMetadataIndexNodeMap.keySet()` contains tables absent from `tableSchemaMap` (similar to the schema completion added for direct `LOAD TSFILE` in #19100)? Any thoughts or suggestions are welcome! Best regards, Yuan Tian
