Skip to content

fix(cpp): reject out-of-order timestamps on non-aligned writes - #1176

Merged
ColinLeeo merged 8 commits into
developfrom
colin/fix-nonaligned-timestamp-order
Dec 2, 2026
Merged

ColinLeeo merged 8 commits into
developfrom
colin/fix-nonaligned-timestamp-order

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Non-aligned writes in C++ (and therefore Python/C through the same entry points) never validated timestamp order. With allow_non_decreasing_timestamps enabled a strictly decreasing timestamp was accepted; with it disabled (default) equal and decreasing timestamps were accepted. Java's NonAlignedChunkGroupWriterImpl / TabletWriteContext already reject these, so files produced from C++/Python could contain out-of-order series that the read path assumes never happen.

flag rejected
off (default) t <= last
on t < last (equal allowed)

Changes

  • The non-aligned tree writer validates the tablet time column once with the existing TimePageWriter::is_batch_in_order(), including rows where every measurement is null.
  • Before appending any column, the writer checks its first non-null timestamp against that measurement's last point using the existing TimePageWriter::is_out_of_order(). Record writes also check all target measurements before appending.
  • The existing chunk write methods remember the last timestamp appended. The series keeps that history across page seals and flushes. Recovery seeds each non-aligned measurement from its own recovered chunks.

Tests

C++ coverage includes

  • record/tablet; flag on/off; equal vs strictly decreasing vs valid non-decreasing;
  • page-seal survival (page_writer_max_point_num_ = 3) and flush survival;
  • duplicate measurement inside one TsRecord rejected under strict mode;
  • a later column conflicting with its own history rejects the whole tablet before earlier columns advance;
  • decreasing or repeated timestamps on null rows are rejected in strict mode;
  • independent recovered measurement histories, leading nulls and continuation after a flush;
  • the two stale-chunk reader fixtures that deliberately need out-of-order chunk groups now build them at the chunk layer (TsFileIOWriter + ChunkWriter), preserving the original reader coverage;
  • WriteDiffrentTypeCombination used 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.

  • The C++ and Python Maven verification passed with LZMA2 enabled. CTest reported zero failures across 1,938 tests. Pytest reported 672 passed and 16 skipped.
  • The RelWithDebInfo C++ sanitizer verification passed with LZMA2 enabled and matching ASan/UBSan flags for bundled C++ dependencies. CTest reported zero failures across 1,939 tests.
  • C++ Spotless and git diff --check passed.

Known limitations

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-commenter

codecov-commenter commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.47%. Comparing base (5db3d0c) to head (27cbed0).
⚠️ Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
cpp/src/writer/tsfile_writer.cc 79.31% 0 Missing and 6 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@ColinLeeo
ColinLeeo merged commit 42f03c6 into develop Dec 2, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants