Hi Yuan,

Thanks for the detailed write-up and the reproduction steps — the analysis 
matches what I see in the code. I've opened a fix for the first part on master:
- master: https://github.com/apache/iotdb/pull/19490
It follows your first suggestion in the issue: in 
ReadChunkCompactionPerformer#compactAlignedSeries, writer.endChunkGroup() is 
now called before writer.checkMetadataSizeAndMayFlush(), the same order 
FastCompactionPerformer and ReadPointCompactionPerformer already use. With that 
order the device's ChunkMetadata is still in chunkMetadataList when 
CompactionTsFileWriter#endChunkGroup() runs, so Schema#updateTableSchema(...) 
is called for every device and removeUnusedTableSchema() no longer sees a 
TAG-only schema.
I also dropped the checkMetadataSizeAndMayFlush() call from 
AlignedSeriesCompactionExecutor#execute(). After the reordering, the aligned 
flush boundary is fully owned by the performer, and that extra call only left a 
second place where a flush could happen in the middle of a chunk group.
For the regression test, TableModelReadChunkCompactionPerformerTest now 
compacts two aligned sequence TsFiles with chunk_metadata_size_proportion set 
to 0, which forces the spilling path, and asserts that the target file's 
getTableSchemaMap() still contains t1 with id1, s1 and s2. That covers both the 
"entire table removed" and the "FIELD column omitted" cases at the metadata 
level. I haven't run Pipe or LOAD TSFILE end-to-end for this; the test verifies 
the compacted TsFile metadata directly, so it would be good to confirm the 
downstream symptoms disappear in your wide-table setup.
On your second discussion point: this PR only prevents new compacted files from 
losing schema; it does not repair files that are already on disk. I'll open a 
follow-up PR that adds a repair tool for that existing scenario, so affected 
deployments can detect and restore the missing table and FIELD schemas in 
already-compacted TsFiles instead of having to rewrite them from scratch. 
Separately, it's still worth deciding whether TsFileInsertionEventTableParser 
should fall back to DataNodeTableCache or log an explicit warning when 
tableMetadataIndexNodeMap contains tables absent from tableSchemaMap — that 
read-path change can be discussed independently of the repair tool.
Thanks again for the thorough analysis.

Best regards,
Wenwei Shu

On 2026-12-02 01:02 UTC Yuan Tian wrote:
> 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
> 

Reply via email to