Skip to content

fix(cpp): keep table writer usable after out-of-order batch rejection - #1152

Merged
ColinLeeo merged 6 commits into
apache:developfrom
ColinLeeo:fix-171-ooo-unreadable
Dec 1, 2026
Merged

ColinLeeo merged 6 commits into
apache:developfrom
ColinLeeo:fix-171-ooo-unreadable

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Rejecting an out-of-order tablet could leave a C++ aligned table writer unable to flush or close. Rows accepted before the rejection could then become unreadable.

Changes

The writer validates the entire tablet before starting time or value encoding. Each device is checked independently, including when its rows are interleaved with another device's rows. The first timestamp for each device is checked against its last accepted timestamp across tablets and chunk-group flushes. Strict mode rejects duplicates. With allow_non_decreasing_timestamps enabled, equal timestamps are accepted as distinct rows, including at INT64_MIN.

An ordering error returns E_OUT_OF_ORDER with zero rows consumed from the rejected tablet. The caller can correct and retry the whole tablet, then flush or close normally. Java also checks timestamp order per device. Its aligned writer processes one row at a time, so it can accept a valid prefix before a later row fails.

The C++ table writer also checks the tablet's row count against its capacity before splitting by device or reading timestamps. An oversized tablet in a release build returns E_OUT_OF_RANGE. Encoding failures after validation still mark the writer unrecoverable because an encoder may already have produced partial output.

Regression coverage

  • Read back accepted rows after rejecting an out-of-order tablet and closing the file.
  • Exercise interleaved devices, ordering across flushes, and duplicate INT64_MIN in both timestamp modes.
  • Reuse a tablet after an oversized row count is rejected and verify that only the subsequently accepted row is present.

The full Java, C++, and Python Maven build passed on abfa6d18, along with the Linux C++ ASan suite, the Python binding ASan/LSan ownership suite, and Spotless. The compatibility runs read 1,672 Java-generated files in C++ and 1,626 C++-generated files in Java.

In the sequential aligned table write path, TsFileWriter::write_table
marked the writer unrecoverable whenever write_time_segments failed. A
pure out-of-order batch is rejected by TimePageWriter::write_batch
before any time point is appended, so every column's row count stays
unchanged and the writer remains coherent. Marking it unrecoverable
made a later flush()/close() fail with E_DATA_INCONSISTENCY, leaving
the file unreadable and discarding data written before the rejected
batch.

Only mark the writer unrecoverable when the time chunk actually made
progress (wrote into the current page or sealed a page) before failing;
otherwise the out-of-order error is returned and the writer stays
usable, matching the Java behavior.

Add a regression test that writes normal rows, then an out-of-order
batch, and asserts close succeeds and the normal data stays readable.
Reject an out-of-order tablet up front instead of writing the in-order
prefix and then poisoning the writer.  For each device, check that its
timestamps are monotonic across all of its (possibly non-contiguous)
segments and that its first timestamp comes after the device's
already-written data.  A rejected batch now writes zero rows and leaves
the writer usable, matching the Java behavior and removing the
partial-write ambiguity.

Add TimeChunkWriter::get_last_time() so the check can read the highest
timestamp written so far (sealed pages plus the current page), and
update WriteDisorderTest to expect flush/close to succeed after the
up-front rejection.

@ColinLeeo ColinLeeo left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Both updated writer tests passed locally. I also reproduced the existing multi-device reader test failure and checked the new regression case with a flush between the two writes.

Comment thread cpp/src/writer/tsfile_writer.cc Outdated
// page) before failing: then the value columns lag the
// time column and the aligned chunk group can no longer
// be sealed coherently.
if (ctx.tcw->get_point_numer() > ctx.initial_page_points ||

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Please keep the writer unrecoverable when write_time_segments fails here. The new preflight already returns E_OUT_OF_ORDER before encoding starts. For an encoding error, both counters can stay unchanged even after the time stream has advanced. For example, the PLAIN encoder can fill one output buffer and then return E_OOM while acquiring the next. TimePageWriter::write_batch updates its point count only after the entire encode succeeds. A later flush or retry could therefore write extra timestamps with no corresponding values. Restoring the unconditional unrecoverable_ = true on this path preserves the new behavior for out-of-order input.

Comment thread cpp/src/writer/tsfile_writer.cc Outdated
for (int i = order_start + 1; i < order_end; i++) {
if (out_of_order(tablet.timestamps_[i],
tablet.timestamps_[i - 1])) {
return E_OUT_OF_ORDER;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This check exposes an issue in the TableModelQueryMultiDevices fixture. Its ten intended devices all have the same five TAG values, so the writer sees one device whose timestamp goes from 9999 back to 0. I reproduced the resulting E_OUT_OF_ORDER at the write assertion locally and in CI. Please give each device a distinct value in one TAG (keeping id0 common for the existing query filter) and update the readback assertions for that TAG. That keeps the ordering check and makes the test exercise ten actual devices. Fixture and assertion · CI failure

Comment thread cpp/src/writer/tsfile_writer.cc Outdated
if (schema_it != schemas_.end() &&
schema_it->second != nullptr &&
schema_it->second->time_chunk_writer_ != nullptr &&
schema_it->second->time_chunk_writer_->hasData() &&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The per-device check still loses the last timestamp at a chunk-group flush. I added a single flush() after the successful write in WriteOutOfOrderThenCloseKeepsData: after writing 0 through 9 for d0, writing 5 for d0 returned E_OK. Flushing resets the chunk writer, so hasData() is false on the next write. This gap also exists on the base branch. Could we retain the last accepted timestamp per device across flushes and cover this case in the regression test? The Java table writer already saves it at flush and restores it for the next chunk group.

Address review feedback on the table-model out-of-order handling:

- Restore unconditional unrecoverable_ on a write_time_segments failure:
  the up-front check already rejects out-of-order input before encoding,
  so the only remaining failure is a genuine encoder/allocation error that
  may have advanced the time stream without updating the point counters.
- Retain each device's last accepted timestamp across a chunk-group flush
  (in MeasurementSchemaGroup::last_time_) so the up-front check still
  rejects a timestamp that is out of order against already-flushed data.
- Give each device a distinct id1 value in the TableModelQueryMultiDevices
  fixture so it actually writes ten devices, and cover the flush case in a
  new WriteOutOfOrderAfterFlushRejected regression test.

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

No active review issues remain for the changes intended to keep accepted rows flushable after an out-of-order tablet is rejected.

No open issues 🎉

What changed in this PR

This PR keeps the C++ aligned table writer usable after rejecting an out-of-order tablet, so previously accepted rows remain flushable and readable.

Changes:

  • Validates timestamp order across the entire tablet before encoding, including interleaved devices and ordering across chunk-group flushes.
  • Tracks timestamp presence explicitly to handle INT64_MIN correctly and rejects oversized tablets before accessing their rows.
  • Adds regression coverage and documents rejection and retry behavior.
File Description
skills/​tsfile/​references/​advanced-apis.md Documents ordering guarantees, capacity checks, and retry behavior.
cpp/​test/​writer/​time_page_writer_test.cc Tests timestamp ordering across the full integer range and comparison-block boundaries.
cpp/​test/​writer/​time_chunk_writer_test.cc Tests rejection before encoding and a subsequent successful write.
cpp/​test/​writer/​table_view/​tsfile_writer_table_test.cc Covers readable data after rejection, flush ordering, interleaved devices, capacity limits, and minimum timestamps.
cpp/​test/​reader/​table_view/​tsfile_reader_table_test.cc Gives multi-device fixtures distinct string tags and updates expected results.
cpp/​src/​writer/​tsfile_writer.cc Validates tablets before encoding and preserves timestamp history across flushes and recovery.
cpp/​src/​writer/​time_page_writer.h Centralizes ordering checks and separates validation from encoding.
cpp/​src/​writer/​time_chunk_writer.h Declares timestamp lookup and adds an internal prevalidated batch-writing path.
cpp/​src/​writer/​time_chunk_writer.cc Retrieves the highest timestamp across sealed and current pages.
cpp/​src/​common/​schema.h Tracks whether a device has a prior timestamp independently of its value.

Review effort: Balanced

@ColinLeeo
ColinLeeo merged commit 5db3d0c into apache:develop Dec 1, 2026
49 checks passed
@ColinLeeo
ColinLeeo deleted the fix-171-ooo-unreadable branch December 1, 2026 02:17
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.

2 participants