Repository navigation
fix(cpp): reject out-of-order timestamps on non-aligned writes - #1176
Merged
Merged
Conversation
The non-aligned write paths (TsFileWriter::write_record, write_tablet and row/tablet batches) never checked timestamp order, so a strictly decreasing timestamp was accepted even with allow_non_decreasing_timestamps_ enabled, and duplicates/decreases were accepted with the flag off. - ChunkWriter now validates write / write_batch / write_string_batch against the last timestamp it appended and returns E_OUT_OF_ORDER on violation. The state lives in ChunkWriter rather than the per-page statistic, so it survives page seals and chunk flushes. - TsFileWriter pre-validates a whole record/tablet (write_record, write_tablet, and the non-aligned table path) before writing any column, so a rejected write leaves no partial measurement columns behind. This also protects the table path's parallel column writes. - The ordering predicate is shared via common::timestamp_out_of_order; the TimePageWriter and recovery checks now use it too. - Two stale-chunk reader fixtures that require a later flush with an earlier time range are built at the chunk layer instead of through TsFileWriter. - C++ and Python tree-model tests cover record/tablet writes, the flag on and off, page-seal and flush boundaries, and partial-write atomicity. Skills docs are updated.
Follow-up to c61c032 from the third-party review: - check_tablet_timestamp_order now also applies the recovered per-device last_time_ floor to each column's first actually written row. A tablet whose leading rows are null for every column previously slipped past the existing tablet.timestamps_[0] check and could write its first non-null row below the recovered end_time. - ChunkWriter::destroy() clears the order state (init() already did). - Document in write_record that the new guarantee covers ordering only; other per-point failures are still ignored as before. - Mark the non-aligned write_table pre-check as currently unreachable (table_aligned_ is fixed to true) and note it is kept in step with write_tablet. - Tests: ChunkWriter string-batch ordering and batch state across page seals; tablet atomicity when the violating row is in a later column; a timestamp decrease on a null row is not a write; the recovered-floor case with a leading all-null row. - Docs: scope the all-or-nothing statement to the non-aligned tree paths.
Resolve the TimePageWriter conflict by keeping upstream's public is_out_of_order / is_batch_in_order / encode_ordered_batch structure and routing is_out_of_order through common::timestamp_out_of_order. Reconcile the Timestamp Ordering skill text with PR #1152's up-front Table-model validation.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1176 +/- ##
===========================================
+ Coverage 69.27% 69.47% +0.20%
===========================================
Files 791 798 +7
Lines 58564 59120 +556
Branches 9516 9639 +123
===========================================
+ Hits 40570 41076 +506
- Misses 16247 16254 +7
- Partials 1747 1790 +43 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the timestamp-order validation changes.
No open issues 🎉
What changed in this PR
Adds timestamp-order validation to C++ non-aligned tree writes, including the shared Python and C write paths.
Changes:
- Rejects ordering violations before writing any measurement and rejects duplicate measurement names in records.
- Preserves per-series timestamp history across page seals, flushes, and recovery; restores alignment per device.
- Adds C++ and Python regression coverage, updates stale-chunk fixtures, and documents the behavior.
| File | Description |
|---|---|
| skills/tsfile/references/advanced-apis.md | Documents ordering, duplicate-name rejection, and recovery behavior. |
| python/tests/test_allow_non_decreasing_timestamps.py | Tests tree record and tablet ordering policies. |
| cpp/test/writer/tsfile_writer_test.cc | Covers rejection, atomicity, and timestamp-history persistence. |
| cpp/test/reader/tree_view/tsfile_tree_query_by_row_test.cc | Builds stale-chunk fixtures through low-level writers. |
| cpp/test/file/restorable_tsfile_io_writer_test.cc | Tests independent recovered histories and mixed alignment. |
| cpp/src/writer/tsfile_writer.cc | Validates writes and restores per-measurement boundaries. |
| cpp/src/writer/chunk_writer.h | Tracks each series’ last appended timestamp. |
| cpp/src/writer/chunk_writer.cc | Initializes timestamp-history state. |
| cpp/src/file/restorable_tsfile_io_writer.h | Keys recovered alignment by full device identity. |
| cpp/src/file/restorable_tsfile_io_writer.cc | Tracks and clears recovered device alignment. |
| cpp/src/common/schema.h | Stores recovered per-measurement timestamp boundaries. |
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Non-aligned writes in C++ (and therefore Python/C through the same entry points) never validated timestamp order. With
allow_non_decreasing_timestampsenabled a strictly decreasing timestamp was accepted; with it disabled (default) equal and decreasing timestamps were accepted. Java'sNonAlignedChunkGroupWriterImpl/TabletWriteContextalready reject these, so files produced from C++/Python could contain out-of-order series that the read path assumes never happen.t <= lastt < last(equal allowed)Changes
TimePageWriter::is_batch_in_order(), including rows where every measurement is null.TimePageWriter::is_out_of_order(). Record writes also check all target measurements before appending.Tests
C++ coverage includes
page_writer_max_point_num_ = 3) and flush survival;TsRecordrejected under strict mode;TsFileIOWriter+ChunkWriter), preserving the original reader coverage;WriteDiffrentTypeCombinationused one constant timestamp for 100 rows; now increments.Python: 4 tree-writer tests in
python/tests/test_allow_non_decreasing_timestamps.py(write_row_record/write_tablet, flag on/off, cross-tablet ordering,OutOfOrderError).Verification
Verified on macOS arm64 at
6c71696b.git diff --checkpassed.Known limitations
write_tablepath.