From 42e1fd5187acabc12b78a4435ec9376cf3e153dc Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:00:01 +0800 Subject: [PATCH 1/7] perf(cpp): enlarge the TsFile write stream page size to 64 KiB Pending bytes are drained to the file one page at a time, so WRITE_STREAM_PAGE_SIZE is both the write() granularity and the per-flush allocation unit. At 512 bytes a single chunk flush issued dozens of tiny write() syscalls and allocated a fresh page chain, which showed up as ~27% of the write path in a sampled profile. Raising it to 64 KiB keeps a chunk's data in one or two pages. On a 3.2M-row / 1000-device write the time drops from 0.60s to 0.45s (~25%) with an identical output size. --- cpp/src/file/tsfile_io_writer.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cpp/src/file/tsfile_io_writer.h b/cpp/src/file/tsfile_io_writer.h index bbb1e4988..7beb6fdce 100644 --- a/cpp/src/file/tsfile_io_writer.h +++ b/cpp/src/file/tsfile_io_writer.h @@ -56,7 +56,12 @@ class TsFileIOWriter { typedef DeviceNodeMap::iterator DeviceNodeMapIterator; public: - static const uint32_t WRITE_STREAM_PAGE_SIZE = 512; // FIXME + // Pending bytes drain to the file one page at a time, so this is both the + // write() granularity and the per-flush allocation unit. 512 bytes turned + // a chunk flush into dozens of tiny write() syscalls and page allocations; + // 64 KiB keeps a chunk's data in one or two pages without wasting much + // memory on the trailing page. + static const uint32_t WRITE_STREAM_PAGE_SIZE = 64 * 1024; public: TsFileIOWriter() : meta_allocator_(), From 1eec8415bb120571556ba71e79f6fa9b1ef017e6 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:29:46 +0800 Subject: [PATCH 2/7] style: apply spotless formatting --- cpp/src/file/tsfile_io_writer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/src/file/tsfile_io_writer.h b/cpp/src/file/tsfile_io_writer.h index 7beb6fdce..6a06d853c 100644 --- a/cpp/src/file/tsfile_io_writer.h +++ b/cpp/src/file/tsfile_io_writer.h @@ -62,6 +62,7 @@ class TsFileIOWriter { // 64 KiB keeps a chunk's data in one or two pages without wasting much // memory on the trailing page. static const uint32_t WRITE_STREAM_PAGE_SIZE = 64 * 1024; + public: TsFileIOWriter() : meta_allocator_(), From f43369348da406281a7a645362883c63a0d87cfe Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 17:04:27 +0800 Subject: [PATCH 3/7] test(cpp): round-trip chunks that span several write-stream pages The I/O writer drains pending bytes one 64 KiB stream page at a time, so a chunk larger than one page is written as a chain of write() calls. Write INT64 series of several sizes around the page boundary (one page, just under, just over, and multiple pages) and read every value back. This checks that the page chain is flushed in order and that a chunk ending on a page boundary is not truncated. --- cpp/test/writer/tsfile_writer_test.cc | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/cpp/test/writer/tsfile_writer_test.cc b/cpp/test/writer/tsfile_writer_test.cc index 860d6a164..ff75bffa5 100644 --- a/cpp/test/writer/tsfile_writer_test.cc +++ b/cpp/test/writer/tsfile_writer_test.cc @@ -333,6 +333,59 @@ TEST_F(TsFileWriterTest, MultiFlushWriteAndRead) { ASSERT_EQ(reader.close(), E_OK); } +// The I/O writer drains its pending bytes one stream page at a time, so a +// chunk larger than one page is written as a chain of write() calls. Sizes +// straddling the 64 KiB page boundary check that the chain is flushed in +// order and that a chunk ending exactly on a page boundary round-trips. +TEST_F(TsFileWriterTest, WriteChunkSpanningMultipleWriteStreamPages) { + const std::vector row_counts = {1000, 8191, 8192, + 8193, 16384, 20000}; + std::vector select_list; + for (size_t i = 0; i < row_counts.size(); ++i) { + const std::string device = "device_" + std::to_string(i); + const std::string measurement = "value"; + ASSERT_EQ(tsfile_writer_->register_timeseries( + device, storage::MeasurementSchema( + measurement, common::TSDataType::INT64, + common::TSEncoding::PLAIN, + common::CompressionType::UNCOMPRESSED)), + E_OK); + select_list.push_back(device + "." + measurement); + for (int64_t row = 0; row < row_counts[i]; ++row) { + TsRecord record(row, device); + record.add_point(measurement, row); + ASSERT_EQ(tsfile_writer_->write_record(record), E_OK); + } + ASSERT_EQ(tsfile_writer_->flush(), E_OK); + } + ASSERT_EQ(tsfile_writer_->close(), E_OK); + + storage::TsFileReader reader; + ASSERT_EQ(reader.open(file_name_), E_OK); + for (size_t i = 0; i < row_counts.size(); ++i) { + SCOPED_TRACE(::testing::Message() << "rows=" << row_counts[i] + << ", select=" << select_list[i]); + storage::ResultSet* tmp_qds = nullptr; + std::vector columns{select_list[i]}; + ASSERT_EQ(reader.query(columns, 0, row_counts[i] - 1, tmp_qds), E_OK); + auto* qds = static_cast(tmp_qds); + int64_t read_rows = 0; + bool has_next = false; + while (true) { + ASSERT_EQ(qds->next(has_next), E_OK); + if (!has_next) { + break; + } + ASSERT_EQ(qds->get_value(1), read_rows); + ASSERT_EQ(qds->get_value(2), read_rows); + ++read_rows; + } + ASSERT_EQ(read_rows, row_counts[i]); + reader.destroy_query_data_set(qds); + } + ASSERT_EQ(reader.close(), E_OK); +} + #if defined(ENABLE_ZLIB) && defined(ENABLE_SNAPPY) && defined(ENABLE_LZ4) && \ defined(ENABLE_LZOKAY) TEST_F(TsFileWriterTest, WriteDiffrentTypeCombination) { From 8964c40336f00438bb225d87d2feedbc9ad3e805 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 17:50:10 +0800 Subject: [PATCH 4/7] test(cpp): cover the write stream's page-boundary drain directly Review follow-up: the integration test claimed to cover a chunk ending exactly on a page boundary, but the per-chunk metadata overhead means no row count in it lands on a multiple of the page size, and ByteStream::Consumer's exact-multiple branch had no test at all. Add a Consumer test that drains byte streams of 0, one byte less than a page, exactly one/two/three pages, and page multiples plus a partial tail, asserting both the per-buffer lengths and the reassembled bytes. Correct the integration test's comment to say what it actually covers. --- cpp/test/common/allocator/byte_stream_test.cc | 50 +++++++++++++++++++ cpp/test/writer/tsfile_writer_test.cc | 9 ++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/cpp/test/common/allocator/byte_stream_test.cc b/cpp/test/common/allocator/byte_stream_test.cc index dde40fba4..aeb91db3e 100644 --- a/cpp/test/common/allocator/byte_stream_test.cc +++ b/cpp/test/common/allocator/byte_stream_test.cc @@ -435,3 +435,53 @@ TEST(OptionalAtomicStorageTest, BackingStorageIsRealAtomic) { } } // namespace common + +// The write path drains a ByteStream page by page via Consumer::get_next_buf(). +// Its exact-multiple branch (a stream whose total size is a multiple of the +// page size) decides whether the last page is full, and had no coverage; a +// regression there would silently truncate or over-read the final page. +TEST(ByteStreamConsumerTest, DrainsExactPageMultiplesAndPartialTails) { + constexpr uint32_t kPageSize = 1024; + struct Case { + uint32_t total; + std::vector expected_lens; + }; + const std::vector cases = { + {0, {}}, + {1, {1}}, + {kPageSize - 1, {kPageSize - 1}}, + {kPageSize, {kPageSize}}, + {kPageSize + 1, {kPageSize, 1}}, + {2 * kPageSize, {kPageSize, kPageSize}}, + {3 * kPageSize, {kPageSize, kPageSize, kPageSize}}, + {3 * kPageSize + 17, {kPageSize, kPageSize, kPageSize, 17}}, + }; + + for (const auto& test_case : cases) { + SCOPED_TRACE(::testing::Message() << "total=" << test_case.total); + common::ByteStream stream(kPageSize, common::MOD_DEFAULT, + /*enable_atomic=*/true); + std::vector payload(test_case.total); + for (uint32_t i = 0; i < test_case.total; i++) { + payload[i] = static_cast((i * 13 + 5) & 0xff); + } + if (test_case.total > 0) { + ASSERT_EQ(stream.write_buf(payload.data(), test_case.total), + common::E_OK); + } + + common::ByteStream::Consumer consumer(stream); + std::vector lens; + std::vector drained; + while (true) { + common::ByteStream::Buffer buf = consumer.get_next_buf(stream); + if (buf.buf_ == nullptr) { + break; + } + lens.push_back(buf.len_); + drained.insert(drained.end(), buf.buf_, buf.buf_ + buf.len_); + } + EXPECT_EQ(lens, test_case.expected_lens); + EXPECT_EQ(drained, payload); + } +} diff --git a/cpp/test/writer/tsfile_writer_test.cc b/cpp/test/writer/tsfile_writer_test.cc index ff75bffa5..a592788b2 100644 --- a/cpp/test/writer/tsfile_writer_test.cc +++ b/cpp/test/writer/tsfile_writer_test.cc @@ -334,9 +334,12 @@ TEST_F(TsFileWriterTest, MultiFlushWriteAndRead) { } // The I/O writer drains its pending bytes one stream page at a time, so a -// chunk larger than one page is written as a chain of write() calls. Sizes -// straddling the 64 KiB page boundary check that the chain is flushed in -// order and that a chunk ending exactly on a page boundary round-trips. +// chunk larger than one page is written as a chain of write() calls. These +// sizes produce 1-, 2- and 3-page chains with partial tails, which checks that +// the chain is flushed in order. (A chunk ending exactly on a page boundary is +// covered by ByteStreamConsumerTest.DrainsExactPageMultiplesAndPartialTails, +// since the per-chunk metadata overhead makes row counts alone unable to land +// on the boundary.) TEST_F(TsFileWriterTest, WriteChunkSpanningMultipleWriteStreamPages) { const std::vector row_counts = {1000, 8191, 8192, 8193, 16384, 20000}; From 7bfc2ad4fe9f1c0fb1e44331de4133757717f08e Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 23:11:29 +0800 Subject: [PATCH 5/7] test(cpp): make the write-page test layout-independent Follow-up to review comments: declare WRITE_STREAM_PAGE_SIZE as constexpr, keep the Consumer test inside namespace common, and assert that the largest case still exceeds one write-stream page so the multi-page drain cannot silently stop being exercised. --- cpp/src/file/tsfile_io_writer.h | 2 +- cpp/test/common/allocator/byte_stream_test.cc | 15 +++++++-------- cpp/test/writer/tsfile_writer_test.cc | 5 +++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cpp/src/file/tsfile_io_writer.h b/cpp/src/file/tsfile_io_writer.h index 6a06d853c..56e86a337 100644 --- a/cpp/src/file/tsfile_io_writer.h +++ b/cpp/src/file/tsfile_io_writer.h @@ -61,7 +61,7 @@ class TsFileIOWriter { // a chunk flush into dozens of tiny write() syscalls and page allocations; // 64 KiB keeps a chunk's data in one or two pages without wasting much // memory on the trailing page. - static const uint32_t WRITE_STREAM_PAGE_SIZE = 64 * 1024; + static constexpr uint32_t WRITE_STREAM_PAGE_SIZE = 64 * 1024; public: TsFileIOWriter() diff --git a/cpp/test/common/allocator/byte_stream_test.cc b/cpp/test/common/allocator/byte_stream_test.cc index aeb91db3e..0ef87dab1 100644 --- a/cpp/test/common/allocator/byte_stream_test.cc +++ b/cpp/test/common/allocator/byte_stream_test.cc @@ -434,8 +434,6 @@ TEST(OptionalAtomicStorageTest, BackingStorageIsRealAtomic) { EXPECT_EQ(oa.load(), 51u); } -} // namespace common - // The write path drains a ByteStream page by page via Consumer::get_next_buf(). // Its exact-multiple branch (a stream whose total size is a multiple of the // page size) decides whether the last page is full, and had no coverage; a @@ -459,22 +457,21 @@ TEST(ByteStreamConsumerTest, DrainsExactPageMultiplesAndPartialTails) { for (const auto& test_case : cases) { SCOPED_TRACE(::testing::Message() << "total=" << test_case.total); - common::ByteStream stream(kPageSize, common::MOD_DEFAULT, - /*enable_atomic=*/true); + ByteStream stream(kPageSize, MOD_DEFAULT, + /*enable_atomic=*/true); std::vector payload(test_case.total); for (uint32_t i = 0; i < test_case.total; i++) { payload[i] = static_cast((i * 13 + 5) & 0xff); } if (test_case.total > 0) { - ASSERT_EQ(stream.write_buf(payload.data(), test_case.total), - common::E_OK); + ASSERT_EQ(stream.write_buf(payload.data(), test_case.total), E_OK); } - common::ByteStream::Consumer consumer(stream); + ByteStream::Consumer consumer(stream); std::vector lens; std::vector drained; while (true) { - common::ByteStream::Buffer buf = consumer.get_next_buf(stream); + ByteStream::Buffer buf = consumer.get_next_buf(stream); if (buf.buf_ == nullptr) { break; } @@ -485,3 +482,5 @@ TEST(ByteStreamConsumerTest, DrainsExactPageMultiplesAndPartialTails) { EXPECT_EQ(drained, payload); } } + +} // namespace common diff --git a/cpp/test/writer/tsfile_writer_test.cc b/cpp/test/writer/tsfile_writer_test.cc index a592788b2..d26d475a5 100644 --- a/cpp/test/writer/tsfile_writer_test.cc +++ b/cpp/test/writer/tsfile_writer_test.cc @@ -343,6 +343,11 @@ TEST_F(TsFileWriterTest, MultiFlushWriteAndRead) { TEST_F(TsFileWriterTest, WriteChunkSpanningMultipleWriteStreamPages) { const std::vector row_counts = {1000, 8191, 8192, 8193, 16384, 20000}; + // Keep at least one case spanning several pages even if the page size or + // the value width changes: the largest payload alone must exceed one page + // (per-chunk metadata only adds bytes on top of this). + EXPECT_LT(storage::TsFileIOWriter::WRITE_STREAM_PAGE_SIZE, + row_counts.back() * static_cast(sizeof(int64_t))); std::vector select_list; for (size_t i = 0; i < row_counts.size(); ++i) { const std::string device = "device_" + std::to_string(i); From 02b61f31d5c4956a3606e7995f09505250775ac1 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 23:39:19 +0800 Subject: [PATCH 6/7] fix(cpp): define WRITE_STREAM_PAGE_SIZE out of class The test binds the constant to a reference (EXPECT_LT), which odr-uses it; before C++17 a static constexpr data member then needs a definition, and the Linux/LTO builds failed to link. Add the definition. --- cpp/src/file/tsfile_io_writer.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cpp/src/file/tsfile_io_writer.cc b/cpp/src/file/tsfile_io_writer.cc index 7e2fc52d5..e58dff6d1 100644 --- a/cpp/src/file/tsfile_io_writer.cc +++ b/cpp/src/file/tsfile_io_writer.cc @@ -36,6 +36,12 @@ using namespace common; namespace storage { +// Definition for the in-class `static constexpr` used as the write-stream +// page size. Before C++17 a static constexpr data member needs an +// out-of-class definition when it is odr-used (e.g. bound to a reference +// by a test assertion); without one the link fails. +constexpr uint32_t TsFileIOWriter::WRITE_STREAM_PAGE_SIZE; + #if 0 #define OFFSET_DEBUG(msg) \ std::cout << "OFFSET_DEBUG: " << msg \ From 1c5927261601f57d6de966c0ff8d7e74c0395e1e Mon Sep 17 00:00:00 2001 From: ColinLee Date: Fri, 13 Nov 2026 01:37:06 +0800 Subject: [PATCH 7/7] test(cpp): give reader-test files a per-process unique name Parallel CTest processes can seed the name generator in the same clock tick; one then removes the file another is opening (E_ALREADY_EXIST at the fixture's open, observed as SEGFAULT / test failure on CI). Append pid and a per-process counter, as the writer tests already do. --- cpp/test/reader/tsfile_reader_test.cc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/cpp/test/reader/tsfile_reader_test.cc b/cpp/test/reader/tsfile_reader_test.cc index 060c7a541..1fe277dde 100644 --- a/cpp/test/reader/tsfile_reader_test.cc +++ b/cpp/test/reader/tsfile_reader_test.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,12 @@ #include "reader/block/single_device_tsblock_reader.h" #include "reader/filter/time_operator.h" #include "reader/qds_without_timegenerator.h" + +#ifdef _WIN32 +#include +#else +#include +#endif #include "reader/tsfile_series_scan_iterator.h" #include "writer/chunk_writer.h" #include "writer/tsfile_writer.h" @@ -118,6 +125,18 @@ class TsFileReaderTest : public ::testing::Test { random_string += chars[dis(gen)]; } + // CTest runs test cases in parallel processes, and two of them can + // seed the generator in the same clock tick. Without a per-process + // unique suffix one process can remove the file another is using, + // which surfaced as E_ALREADY_EXIST from TsFileWriter::open. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic counter{0}; + random_string += "_" + std::to_string(process_id) + "_" + + std::to_string(counter.fetch_add(1)); return random_string; }