From d0b4b895758b456f559cfd557931be743332847d Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:10:23 +0800 Subject: [PATCH 01/11] perf(cpp): cache deserialized device index nodes Each device lookup walks the device-level index tree with search_from_internal_node(), which used to read and deserialize the same node from the file every time. A leaf node holds up to max_degree_of_index_node entries, so a scan over N devices in one leaf deserialized that node N times -- together with the heap allocations for every device id in it -- and this dominated the profile's malloc/free. Add a small LRU (capacity 16) keyed by the node's file offset that keeps the deserialized node and the arena backing it. Cached nodes are immutable: callers only binary-search them and clone entries into their own arena, so they must not destroy them (the previous code called destroy() on the temporary node; that is no longer done). 3.2M-row scan over 1000 devices: 0.180s -> 0.113s (~37%); malloc/free samples in the profile drop from ~2800 to ~280. --- cpp/src/file/tsfile_io_reader.cc | 95 ++++++++++++++++++++++---------- cpp/src/file/tsfile_io_reader.h | 26 +++++++++ 2 files changed, 91 insertions(+), 30 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 7c2b1897b..a7f930f37 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -66,6 +66,10 @@ void TsFileIOReader::reset() { std::lock_guard lk(device_node_cache_mu_); device_node_cache_.clear(); } + { + std::lock_guard lk(index_node_cache_mu_); + index_node_cache_.clear(); + } tsfile_meta_ready_ = false; } } @@ -998,6 +1002,56 @@ int TsFileIOReader::search_from_leaf_node( return ret; } +int TsFileIOReader::get_cached_index_node(int64_t start_offset, + int64_t end_offset, bool is_device, + std::shared_ptr& out) { + { + std::lock_guard lk(index_node_cache_mu_); + CachedIndexNode cached; + if (index_node_cache_.tryGetCopy(start_offset, cached)) { + out = cached.node; + return E_OK; + } + } + + ASSERT(start_offset < end_offset); + const int32_t read_size = static_cast(end_offset - start_offset); + auto arena = std::make_shared(); + arena->init(512, common::MOD_TSFILE_READER); + void* node_buf = arena->alloc(sizeof(MetaIndexNode)); + char* data_buf = static_cast(arena->alloc(read_size)); + if (IS_NULL(node_buf) || IS_NULL(data_buf)) { + return E_OOM; + } + auto* node_ptr = new (node_buf) MetaIndexNode(arena.get()); + std::shared_ptr node(node_ptr, MetaIndexNode::self_deleter); + int32_t ret_read_len = 0; + int ret = read_file_->read(start_offset, data_buf, read_size, ret_read_len); + if (RET_FAIL(ret)) { + return ret; + } else if (ret_read_len != read_size) { + return E_FILE_READ_ERR; + } + if (is_device) { + ret = node->device_deserialize_from(data_buf, read_size); + } else { + ret = node->deserialize_from(data_buf, read_size); + } + if (ret != E_OK) { + return ret; + } + + CachedIndexNode entry; + entry.arena = arena; + entry.node = node; + { + std::lock_guard lk(index_node_cache_mu_); + index_node_cache_.insert(start_offset, std::move(entry)); + } + out = std::move(node); + return E_OK; +} + int TsFileIOReader::search_from_internal_node( std::shared_ptr target_name, bool is_device, std::shared_ptr index_node, @@ -1018,54 +1072,35 @@ int TsFileIOReader::search_from_internal_node( while (IS_SUCC(ret)) { // reader next level index node - const int read_size = end_offset - index_entry->get_offset(); + const int64_t read_size = end_offset - index_entry->get_offset(); #if DEBUG_SE std::cout << "search_from_internal_node, end_offset=" << end_offset << ", index_entry.offset_=" << index_entry.get_offset() << std::endl; #endif ASSERT(read_size > 0 && read_size < (1 << 30)); - PageArena cur_level_index_node_pa; - void* buf = cur_level_index_node_pa.alloc(sizeof(MetaIndexNode)); - char* data_buf = (char*)cur_level_index_node_pa.alloc(read_size); - if (IS_NULL(buf) || IS_NULL(data_buf)) { - return E_OOM; - } - MetaIndexNode* cur_level_index_node = - new (buf) MetaIndexNode(&cur_level_index_node_pa); - int32_t ret_read_len = 0; - if (RET_FAIL(read_file_->read(index_entry->get_offset(), data_buf, - read_size, ret_read_len))) { - return ret; - } else if (read_size != ret_read_len) { - return E_FILE_READ_ERR; - } - if (!is_device) { - ret = cur_level_index_node->deserialize_from(data_buf, read_size); - } else { - ret = cur_level_index_node->device_deserialize_from(data_buf, - read_size); - } - if (ret != E_OK) { + // Served from the index-node cache when it is the same node as a + // previous lookup: one leaf covers up to max_degree_of_index_node + // devices, so an uncached walk would re-read and re-deserialize the + // same node for every device in it. + std::shared_ptr cur_level_index_node; + if (RET_FAIL(get_cached_index_node(index_entry->get_offset(), + end_offset, is_device, + cur_level_index_node))) { return ret; } if (cur_level_index_node->node_type_ == LEAF_DEVICE) { - ret = cur_level_index_node->binary_search_children( + return cur_level_index_node->binary_search_children( target_name, /*exact=*/true, ret_index_entry, ret_end_offset, output_pa); - cur_level_index_node->destroy(); - return ret; //// FIXME } else if (cur_level_index_node->node_type_ == LEAF_MEASUREMENT) { - ret = cur_level_index_node->binary_search_children( + return cur_level_index_node->binary_search_children( target_name, /*exact=*/false, ret_index_entry, ret_end_offset, output_pa); - cur_level_index_node->destroy(); - return ret; //// FIXME } else { ret = cur_level_index_node->binary_search_children( target_name, /*exact=*/false, index_entry, end_offset, output_pa); - cur_level_index_node->destroy(); } } return ret; diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index 82ee60f99..d97338a5c 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -233,6 +233,29 @@ class TsFileIOReader { static constexpr size_t DEVICE_NODE_CACHE_CAPACITY = 64; + // A deserialized index-tree node (device- or measurement-level) together + // with the arena backing it. Declared before `node` so the node is + // destroyed first. + struct CachedIndexNode { + std::shared_ptr arena; + std::shared_ptr node; + }; + + // One leaf of the device index holds up to max_degree_of_index_node + // devices, so an uncached tree walk re-deserializes the same node (and + // allocates every device-id string in it) once per device. A small LRU + // keyed by the node's file offset turns that into once per node. + static constexpr size_t INDEX_NODE_CACHE_CAPACITY = 16; + + /** + * Returns the index node serialized at [start_offset, end_offset), + * reading and deserializing it on a cache miss. The returned node is + * immutable and owned by the cache; callers must not destroy or mutate it. + */ + int get_cached_index_node(int64_t start_offset, int64_t end_offset, + bool is_device, + std::shared_ptr& out); + // Returns E_OK on hit (out is filled), or an error code on miss / load // failure (E_DEVICE_NOT_EXIST when the device is absent, the propagated // error otherwise). Copying into out keeps the caller safe from rehash / @@ -277,6 +300,9 @@ class TsFileIOReader { // shared ownership keeps an entry alive after a concurrent eviction. common::Cache device_node_cache_; mutable std::mutex device_node_cache_mu_; + // Keyed by the node's start offset in the file. + common::Cache index_node_cache_; + mutable std::mutex index_node_cache_mu_; }; } // end namespace storage From 5725dcbb35c314928d279c73a292e1cacff91736 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:40:42 +0800 Subject: [PATCH 02/11] perf(cpp): initialize the index node cache with its capacity The member was default-constructed, so it used common::Cache's default bounds (64 entries, elasticity 10) instead of INDEX_NODE_CACHE_CAPACITY. Caught by the new test that asserts the cache honours its capacity. test(cpp): cover the device index node cache Repeated lookups across 40 devices (small index degree, several levels) must return the same indexes and keep the cache within capacity, and reset() must drop every cached node. --- cpp/src/file/tsfile_io_reader.h | 10 +- cpp/test/file/index_node_cache_test.cc | 159 +++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 cpp/test/file/index_node_cache_test.cc diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index d97338a5c..45abf3366 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -52,7 +52,8 @@ class TsFileIOReader { tsfile_meta_(&tsfile_meta_page_arena_), tsfile_meta_ready_(false), read_file_created_(false), - device_node_cache_(DEVICE_NODE_CACHE_CAPACITY, 0) { + device_node_cache_(DEVICE_NODE_CACHE_CAPACITY, 0), + index_node_cache_(INDEX_NODE_CACHE_CAPACITY, 0) { tsfile_meta_page_arena_.init(512, common::MOD_TSFILE_READER); } @@ -276,6 +277,13 @@ class TsFileIOReader { static size_t TEST_device_node_cache_capacity() { return DEVICE_NODE_CACHE_CAPACITY; } + size_t TEST_index_node_cache_size() const { + std::lock_guard lk(index_node_cache_mu_); + return index_node_cache_.size(); + } + static size_t TEST_index_node_cache_capacity() { + return INDEX_NODE_CACHE_CAPACITY; + } #endif private: diff --git a/cpp/test/file/index_node_cache_test.cc b/cpp/test/file/index_node_cache_test.cc new file mode 100644 index 000000000..f9fd555d8 --- /dev/null +++ b/cpp/test/file/index_node_cache_test.cc @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "common/device_id.h" +#include "file/tsfile_io_reader.h" +#include "file/write_file.h" +#include "writer/tsfile_table_writer.h" +#include "writer/tsfile_writer.h" + +namespace storage { + +/** + * The reader keeps a bounded cache of deserialized index nodes, so repeated + * device lookups reuse the node instead of re-reading and re-deserializing it. + */ +class IndexNodeCacheTest : public ::testing::Test { + protected: + static constexpr int kDevices = 40; + static constexpr int kRowsPerDevice = 2; + + void SetUp() override { + storage::libtsfile_init(); + saved_degree_ = common::g_config_value_.max_degree_of_index_node_; + // A small degree forces several index-node levels for kDevices devices, + // so the walk in search_from_internal_node() actually reads nodes. + ASSERT_EQ(storage::set_max_degree_of_index_node(2), common::E_OK); + file_name_ = + "index_node_cache_" + + std::to_string( + std::chrono::steady_clock::now().time_since_epoch().count()) + + ".tsfile"; + ASSERT_NO_FATAL_FAILURE(WriteFile_()); + } + + void TearDown() override { + storage::set_max_degree_of_index_node(saved_degree_); + remove(file_name_.c_str()); + } + + void WriteFile_() { + storage::WriteFile file; + int flags = O_WRONLY | O_CREAT | O_TRUNC; +#ifdef _WIN32 + flags |= O_BINARY; +#endif + ASSERT_EQ(file.create(file_name_, flags, 0666), common::E_OK); + std::vector columns; + columns.emplace_back("id0", common::STRING, common::UNCOMPRESSED, + common::PLAIN, common::ColumnCategory::TAG); + columns.emplace_back("value", common::INT64, common::UNCOMPRESSED, + common::PLAIN, common::ColumnCategory::FIELD); + storage::TableSchema schema("test", columns); + storage::TsFileTableWriter writer(&file, &schema); + storage::Tablet tablet( + "test", schema.get_measurement_names(), schema.get_data_types(), + schema.get_column_categories(), kDevices * kRowsPerDevice); + for (int device = 0; device < kDevices; ++device) { + for (int row = 0; row < kRowsPerDevice; ++row) { + const int index = device * kRowsPerDevice + row; + ASSERT_EQ(tablet.add_timestamp(index, row), common::E_OK); + ASSERT_EQ( + tablet.add_value(index, "id0", + ("d" + std::to_string(device)).c_str()), + common::E_OK); + ASSERT_EQ(tablet.add_value(index, "value", + static_cast(index)), + common::E_OK); + } + } + ASSERT_EQ(writer.write_table(tablet), common::E_OK); + ASSERT_EQ(writer.close(), common::E_OK); + } + + int64_t saved_degree_ = 0; + std::string file_name_; +}; + +TEST_F(IndexNodeCacheTest, ReusesNodesAcrossDeviceLookups) { + storage::TsFileIOReader reader; + ASSERT_EQ(reader.init(file_name_), common::E_OK); + TsFileMeta* meta = nullptr; + ASSERT_EQ(reader.get_tsfile_meta(meta), common::E_OK); + ASSERT_NE(meta, nullptr); + + common::PageArena pa; + pa.init(512, common::MOD_TSFILE_READER); + const std::unordered_set measurement_names{"value"}; + + // Two rounds over every device: the second round must be served from the + // cache and still produce the same indexes. + for (int round = 0; round < 2; ++round) { + for (int device = 0; device < kDevices; ++device) { + auto device_id = std::make_shared( + "test.d" + std::to_string(device)); + std::vector indexes(1, nullptr); + ASSERT_EQ(reader.get_timeseries_indexes( + device_id, measurement_names, indexes, pa), + common::E_OK) + << "device " << device << " round " << round; + ASSERT_NE(indexes[0], nullptr) + << "device " << device << " round " << round; + pa.reset(); + } + } + + EXPECT_GT(reader.TEST_index_node_cache_size(), 0u); + EXPECT_LE(reader.TEST_index_node_cache_size(), + reader.TEST_index_node_cache_capacity()); +} + +TEST_F(IndexNodeCacheTest, ResetClearsCache) { + storage::TsFileIOReader reader; + ASSERT_EQ(reader.init(file_name_), common::E_OK); + TsFileMeta* meta = nullptr; + ASSERT_EQ(reader.get_tsfile_meta(meta), common::E_OK); + + common::PageArena pa; + pa.init(512, common::MOD_TSFILE_READER); + const std::unordered_set measurement_names{"value"}; + auto device_id = + std::make_shared(std::string("test.d0")); + std::vector indexes(1, nullptr); + ASSERT_EQ(reader.get_timeseries_indexes(device_id, measurement_names, + indexes, pa), + common::E_OK); + EXPECT_GT(reader.TEST_index_node_cache_size(), 0u); + + reader.reset(); + EXPECT_EQ(reader.TEST_index_node_cache_size(), 0u); + pa.destroy(); +} + +} // namespace storage From 27be62d759ade577c5ca5ed5281a1f117b4527ec Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 16:42:10 +0800 Subject: [PATCH 03/11] fix(cpp): drop arena-backed index nodes before releasing the reader arena TsFileIOReader::reset() destroyed tsfile_meta_page_arena_ while tsfile_meta_ still held the MetaIndexNode graph allocated inside it; the shared_ptr deleters then ran the nodes' destructors (and their children vectors) on freed memory at reader destruction. Clear the map first, while the arena is alive. Found by IndexNodeCacheTest.ResetClearsCache. --- cpp/src/file/tsfile_io_reader.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index a7f930f37..b4203cbf4 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -61,6 +61,10 @@ void TsFileIOReader::reset() { delete read_file_; } read_file_ = nullptr; + // Drop the arena-backed index graph before the arena is released: the + // nodes (and their children vectors) live inside it, and clearing the + // map while the arena is still alive lets their destructors run safely. + tsfile_meta_.table_metadata_index_node_map_.clear(); tsfile_meta_page_arena_.destroy(); { std::lock_guard lk(device_node_cache_mu_); From 77aed5495ee8f8ed88c7b79fe7d833ed21ac109b Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 17:15:47 +0800 Subject: [PATCH 04/11] fix(cpp): release the metadata bloom filter before the reader arena reset() frees tsfile_meta_page_arena_, and TsFileMeta::bloom_filter_ is placement-new'ed inside that arena. The reader's own destructor runs ~TsFileMeta later, which dereferenced the freed BloomFilter and called destroy() on it: a use-after-free that MSVC debug-heap and ASan report as an access violation (IndexNodeCacheTest.ResetClearsCache failed on Windows) while release builds on Linux/macOS silently read stale memory. Destroy the bloom filter and clear the pointer while the arena is still alive, alongside the existing index-node-map cleanup. --- cpp/src/file/tsfile_io_reader.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index b4203cbf4..dcb4574aa 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -61,9 +61,17 @@ void TsFileIOReader::reset() { delete read_file_; } read_file_ = nullptr; - // Drop the arena-backed index graph before the arena is released: the - // nodes (and their children vectors) live inside it, and clearing the - // map while the arena is still alive lets their destructors run safely. + // Drop everything that lives inside tsfile_meta_page_arena_ before + // the arena is released: the index graph and the bloom filter are + // both allocated there, and touching them afterwards is a + // use-after-free (traps under MSVC debug heap and ASan). Destroying + // the bloom filter first releases its separately allocated bit set; + // tsfile_meta_'s destructor would otherwise dereference the freed + // BloomFilter object. + if (tsfile_meta_.bloom_filter_ != nullptr) { + tsfile_meta_.bloom_filter_->destroy(); + tsfile_meta_.bloom_filter_ = nullptr; + } tsfile_meta_.table_metadata_index_node_map_.clear(); tsfile_meta_page_arena_.destroy(); { From 044e890d710fd23e8ac0ffd5280d3367eed1a58e Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 17:33:49 +0800 Subject: [PATCH 05/11] fix(cpp): keep the cached index node's arena alive for the caller get_cached_index_node() handed out only the node, while the arena holding the node, its children and their names stayed owned by the cache entry. An LRU eviction or replacement of that entry (possible from another query on the same reader, which the header says is supported) released the arena while the caller was still walking the node: use-after-free on the node and on the node destructor when the caller's shared_ptr was released. Return the whole CachedIndexNode and hold it for the duration of the search, mirroring get_cached_device_node(). Also validate the [start_offset, end_offset) pair before allocating, as the device-node path already does, so a corrupt index cannot turn into a ~4 GiB or truncated allocation. Tests: assert the second lookup round performs no new node reads (the fixture now has few enough nodes to fit the 16-entry cache), so a broken cache can no longer pass as long as its size is non-zero. --- cpp/src/file/tsfile_io_reader.cc | 43 ++++++++++++++++---------- cpp/src/file/tsfile_io_reader.h | 21 ++++++++++--- cpp/test/file/index_node_cache_test.cc | 11 +++++-- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index dcb4574aa..0f62547a8 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -1016,18 +1016,22 @@ int TsFileIOReader::search_from_leaf_node( int TsFileIOReader::get_cached_index_node(int64_t start_offset, int64_t end_offset, bool is_device, - std::shared_ptr& out) { + CachedIndexNode& out) { { std::lock_guard lk(index_node_cache_mu_); - CachedIndexNode cached; - if (index_node_cache_.tryGetCopy(start_offset, cached)) { - out = cached.node; + if (index_node_cache_.tryGetCopy(start_offset, out)) { return E_OK; } } - ASSERT(start_offset < end_offset); - const int32_t read_size = static_cast(end_offset - start_offset); + if (start_offset >= end_offset) { + return E_TSFILE_CORRUPTED; + } + const int64_t read_size_i64 = end_offset - start_offset; + if (read_size_i64 > INT32_MAX) { + return E_OVERFLOW; + } + const int32_t read_size = static_cast(read_size_i64); auto arena = std::make_shared(); arena->init(512, common::MOD_TSFILE_READER); void* node_buf = arena->alloc(sizeof(MetaIndexNode)); @@ -1056,11 +1060,17 @@ int TsFileIOReader::get_cached_index_node(int64_t start_offset, CachedIndexNode entry; entry.arena = arena; entry.node = node; +#ifdef ENABLE_TEST + { + std::lock_guard lk(index_node_cache_mu_); + ++index_node_cache_misses_; + } +#endif { std::lock_guard lk(index_node_cache_mu_); - index_node_cache_.insert(start_offset, std::move(entry)); + index_node_cache_.insert(start_offset, entry); } - out = std::move(node); + out = std::move(entry); return E_OK; } @@ -1095,22 +1105,23 @@ int TsFileIOReader::search_from_internal_node( // previous lookup: one leaf covers up to max_degree_of_index_node // devices, so an uncached walk would re-read and re-deserialize the // same node for every device in it. - std::shared_ptr cur_level_index_node; + // Keep the arena alive for as long as the node is used: the cache + // may evict this entry (and release its arena) concurrently. + CachedIndexNode cur_level; if (RET_FAIL(get_cached_index_node(index_entry->get_offset(), - end_offset, is_device, - cur_level_index_node))) { + end_offset, is_device, cur_level))) { return ret; } - if (cur_level_index_node->node_type_ == LEAF_DEVICE) { - return cur_level_index_node->binary_search_children( + if (cur_level.node->node_type_ == LEAF_DEVICE) { + return cur_level.node->binary_search_children( target_name, /*exact=*/true, ret_index_entry, ret_end_offset, output_pa); - } else if (cur_level_index_node->node_type_ == LEAF_MEASUREMENT) { - return cur_level_index_node->binary_search_children( + } else if (cur_level.node->node_type_ == LEAF_MEASUREMENT) { + return cur_level.node->binary_search_children( target_name, /*exact=*/false, ret_index_entry, ret_end_offset, output_pa); } else { - ret = cur_level_index_node->binary_search_children( + ret = cur_level.node->binary_search_children( target_name, /*exact=*/false, index_entry, end_offset, output_pa); } diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index 45abf3366..a584cbfd9 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -250,12 +250,16 @@ class TsFileIOReader { /** * Returns the index node serialized at [start_offset, end_offset), - * reading and deserializing it on a cache miss. The returned node is - * immutable and owned by the cache; callers must not destroy or mutate it. + * reading and deserializing it on a cache miss. + * + * The out parameter carries the node *and* the arena it was allocated in: + * the node, its children and their names are placement-new'ed into that + * arena, and the cache may evict the entry (and release the arena) at any + * time. Callers must keep the returned CachedIndexNode alive for as long + * as they touch the node. */ int get_cached_index_node(int64_t start_offset, int64_t end_offset, - bool is_device, - std::shared_ptr& out); + bool is_device, CachedIndexNode& out); // Returns E_OK on hit (out is filled), or an error code on miss / load // failure (E_DEVICE_NOT_EXIST when the device is absent, the propagated @@ -284,6 +288,12 @@ class TsFileIOReader { static size_t TEST_index_node_cache_capacity() { return INDEX_NODE_CACHE_CAPACITY; } + // Nodes actually read from the file (i.e. cache misses). Lets a test + // prove that a second lookup round is served from the cache. + size_t TEST_index_node_cache_misses() const { + std::lock_guard lk(index_node_cache_mu_); + return index_node_cache_misses_; + } #endif private: @@ -311,6 +321,9 @@ class TsFileIOReader { // Keyed by the node's start offset in the file. common::Cache index_node_cache_; mutable std::mutex index_node_cache_mu_; +#ifdef ENABLE_TEST + size_t index_node_cache_misses_ = 0; +#endif }; } // end namespace storage diff --git a/cpp/test/file/index_node_cache_test.cc b/cpp/test/file/index_node_cache_test.cc index f9fd555d8..d5419af38 100644 --- a/cpp/test/file/index_node_cache_test.cc +++ b/cpp/test/file/index_node_cache_test.cc @@ -41,7 +41,7 @@ namespace storage { */ class IndexNodeCacheTest : public ::testing::Test { protected: - static constexpr int kDevices = 40; + static constexpr int kDevices = 8; static constexpr int kRowsPerDevice = 2; void SetUp() override { @@ -113,7 +113,8 @@ TEST_F(IndexNodeCacheTest, ReusesNodesAcrossDeviceLookups) { const std::unordered_set measurement_names{"value"}; // Two rounds over every device: the second round must be served from the - // cache and still produce the same indexes. + // cache (no new node reads) and still produce the same indexes. + size_t misses_after_first_round = 0; for (int round = 0; round < 2; ++round) { for (int device = 0; device < kDevices; ++device) { auto device_id = std::make_shared( @@ -127,8 +128,14 @@ TEST_F(IndexNodeCacheTest, ReusesNodesAcrossDeviceLookups) { << "device " << device << " round " << round; pa.reset(); } + if (round == 0) { + misses_after_first_round = reader.TEST_index_node_cache_misses(); + EXPECT_GT(misses_after_first_round, 0u); + } } + // A second pass over the same devices is served entirely from the cache. + EXPECT_EQ(reader.TEST_index_node_cache_misses(), misses_after_first_round); EXPECT_GT(reader.TEST_index_node_cache_size(), 0u); EXPECT_LE(reader.TEST_index_node_cache_size(), reader.TEST_index_node_cache_capacity()); From 8d036a6e8d5d62246d6bb470a3ed5940de1c214d Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 23:11:39 +0800 Subject: [PATCH 06/11] test(cpp): destroy the caller arena in the cache hit test --- cpp/test/file/index_node_cache_test.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/cpp/test/file/index_node_cache_test.cc b/cpp/test/file/index_node_cache_test.cc index d5419af38..196d66dd2 100644 --- a/cpp/test/file/index_node_cache_test.cc +++ b/cpp/test/file/index_node_cache_test.cc @@ -139,6 +139,7 @@ TEST_F(IndexNodeCacheTest, ReusesNodesAcrossDeviceLookups) { EXPECT_GT(reader.TEST_index_node_cache_size(), 0u); EXPECT_LE(reader.TEST_index_node_cache_size(), reader.TEST_index_node_cache_capacity()); + pa.destroy(); } TEST_F(IndexNodeCacheTest, ResetClearsCache) { From 4eb43ba54178f386607e7083c0077b9b73306ddf Mon Sep 17 00:00:00 2001 From: ColinLee Date: Thu, 12 Nov 2026 23:45:24 +0800 Subject: [PATCH 07/11] test(cpp): give reader-test files a per-process unique name CTest runs cases in parallel processes; two can seed the name generator in the same clock tick, and one then removes the file the other is opening (E_ALREADY_EXIST from TsFileWriter::open, observed on the MSVC Release job). Append the 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; } From bba6c971217a1801a63da52f84ac5f50b3f646bb Mon Sep 17 00:00:00 2001 From: ColinLee Date: Fri, 13 Nov 2026 10:42:14 +0800 Subject: [PATCH 08/11] fix(cpp): stop the node cache from releasing an arena before its node Cache::insert() replaces an existing entry with a move assignment. For CachedIndexNode/CachedDeviceNode the implicit member-wise form assigns the arena first, so when the cache was the sole owner the arena's pages were freed before the old node's deleter ran on them: a use-after-free when two queries missed the same offset and the second inserted after the first. Re-check the cache under the lock before inserting (as the device-node path already does), and give both cached types assignment operators that release the node before the arena. --- cpp/src/file/tsfile_io_reader.cc | 14 ++++++--- cpp/src/file/tsfile_io_reader.h | 50 ++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 0f62547a8..6b27c5d3d 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -1060,14 +1060,20 @@ int TsFileIOReader::get_cached_index_node(int64_t start_offset, CachedIndexNode entry; entry.arena = arena; entry.node = node; -#ifdef ENABLE_TEST { std::lock_guard lk(index_node_cache_mu_); +#ifdef ENABLE_TEST ++index_node_cache_misses_; - } #endif - { - std::lock_guard lk(index_node_cache_mu_); + // Re-check under the lock: another query may have loaded the same + // node while we were reading it outside the lock. Replacing an + // existing entry would move-assign over it, and the old entry must + // be released through its own assignment (node before arena). + CachedIndexNode existing; + if (index_node_cache_.tryGetCopy(start_offset, existing)) { + out = std::move(existing); + return E_OK; + } index_node_cache_.insert(start_offset, entry); } out = std::move(entry); diff --git a/cpp/src/file/tsfile_io_reader.h b/cpp/src/file/tsfile_io_reader.h index a584cbfd9..667db154f 100644 --- a/cpp/src/file/tsfile_io_reader.h +++ b/cpp/src/file/tsfile_io_reader.h @@ -225,11 +225,36 @@ class TsFileIOReader { common::PageArena& pa); struct CachedDeviceNode { - // Declared before top_node so top_node is destroyed first. - // MetaIndexNode and its children live in this arena. + // The node and its children live in this arena, so the node must be + // released before the arena. Destruction already does that (reverse + // declaration order); assignment is explicit because the implicit + // member-wise form would release the arena first. std::shared_ptr arena; std::shared_ptr top_node; bool is_aligned = false; + + CachedDeviceNode() = default; + CachedDeviceNode(const CachedDeviceNode&) = default; + CachedDeviceNode& operator=(const CachedDeviceNode& other) { + if (this != &other) { + top_node = other.top_node; + arena = other.arena; + is_aligned = other.is_aligned; + } + return *this; + } + CachedDeviceNode(CachedDeviceNode&& other) noexcept + : arena(std::move(other.arena)), + top_node(std::move(other.top_node)), + is_aligned(other.is_aligned) {} + CachedDeviceNode& operator=(CachedDeviceNode&& other) noexcept { + if (this != &other) { + top_node = std::move(other.top_node); + arena = std::move(other.arena); + is_aligned = other.is_aligned; + } + return *this; + } }; static constexpr size_t DEVICE_NODE_CACHE_CAPACITY = 64; @@ -240,6 +265,27 @@ class TsFileIOReader { struct CachedIndexNode { std::shared_ptr arena; std::shared_ptr node; + + CachedIndexNode() = default; + CachedIndexNode(const CachedIndexNode&) = default; + // node must be released before the arena it was allocated in; the + // implicit member-wise assignment would release the arena first. + CachedIndexNode& operator=(const CachedIndexNode& other) { + if (this != &other) { + node = other.node; + arena = other.arena; + } + return *this; + } + CachedIndexNode(CachedIndexNode&& other) noexcept + : arena(std::move(other.arena)), node(std::move(other.node)) {} + CachedIndexNode& operator=(CachedIndexNode&& other) noexcept { + if (this != &other) { + node = std::move(other.node); + arena = std::move(other.arena); + } + return *this; + } }; // One leaf of the device index holds up to max_degree_of_index_node From efc71e26d3c5e514ecb2c4970571f76f2e0887e4 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Fri, 13 Nov 2026 14:53:17 +0800 Subject: [PATCH 09/11] test(cpp): give clock-seeded test files unique names Several fixtures build their .tsfile name from a clock count alone. CTest runs cases in parallel processes, so two can read the same tick and then one removes the file the other is opening or reopening (E_ALREADY_EXIST / E_FILE_OPEN_ERR, seen as SEGFAULT on macOS). Append the process id and a per-process counter, the pattern the writer tests already use. Covers table_read_failure, table_field_filter, chunk_reader_resource, aligned_chunk_reader_bulk, aligned_field_filter_regression, masked_bulk_copy and timeseries_schema_codec. --- .../reader/aligned_chunk_reader_bulk_test.cc | 18 ++++++++++++++++++ .../aligned_field_filter_regression_test.cc | 17 ++++++++++++++++- cpp/test/reader/block/masked_bulk_copy_test.cc | 17 ++++++++++++++++- cpp/test/reader/chunk_reader_resource_test.cc | 17 +++++++++++++++++ cpp/test/reader/table_field_filter_test.cc | 17 ++++++++++++++++- cpp/test/reader/table_read_failure_test.cc | 17 ++++++++++++++++- .../reader/timeseries_schema_codec_test.cc | 17 +++++++++++++++++ 7 files changed, 116 insertions(+), 4 deletions(-) diff --git a/cpp/test/reader/aligned_chunk_reader_bulk_test.cc b/cpp/test/reader/aligned_chunk_reader_bulk_test.cc index 0be62def8..d3efd8796 100644 --- a/cpp/test/reader/aligned_chunk_reader_bulk_test.cc +++ b/cpp/test/reader/aligned_chunk_reader_bulk_test.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -41,6 +42,12 @@ #include "writer/tsfile_table_writer.h" #ifdef ENABLE_THREADS #include "common/thread_pool.h" + +#ifdef _WIN32 +#include +#else +#include +#endif #endif using namespace common; @@ -93,6 +100,17 @@ class AlignedBulkCopyTest : public ::testing::Test { "_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + + "_" + std::to_string([] { +#ifdef _WIN32 + return static_cast(_getpid()); +#else + return static_cast(getpid()); +#endif + }()) + + "_" + std::to_string([] { + static std::atomic counter{0}; + return counter.fetch_add(1); + }()) + ".tsfile"; std::remove(path_.c_str()); { diff --git a/cpp/test/reader/aligned_field_filter_regression_test.cc b/cpp/test/reader/aligned_field_filter_regression_test.cc index c2b7506d1..265a075aa 100644 --- a/cpp/test/reader/aligned_field_filter_regression_test.cc +++ b/cpp/test/reader/aligned_field_filter_regression_test.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,12 @@ #include "reader/table_query_executor.h" #include "writer/tsfile_table_writer.h" +#ifdef _WIN32 +#include +#else +#include +#endif + namespace { using namespace common; using namespace storage; @@ -70,11 +77,19 @@ class AlignedFieldFilterRegressionTest saved_ = g_config_value_; g_config_value_.page_writer_max_point_num_ = 3; g_config_value_.parallel_read_enabled_ = std::get<0>(GetParam()); + // Parallel CTest processes can seed from the same clock tick. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic filename_counter{0}; filename_ = "aligned_field_filter_regression_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + - ".tsfile"; + "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)) + ".tsfile"; } void TearDown() override { std::remove(filename_.c_str()); diff --git a/cpp/test/reader/block/masked_bulk_copy_test.cc b/cpp/test/reader/block/masked_bulk_copy_test.cc index 21a9e1b50..d2a467a31 100644 --- a/cpp/test/reader/block/masked_bulk_copy_test.cc +++ b/cpp/test/reader/block/masked_bulk_copy_test.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,12 @@ #include "reader/block/single_device_tsblock_reader.h" #include "writer/tsfile_table_writer.h" +#ifdef _WIN32 +#include +#else +#include +#endif + namespace { using namespace common; using namespace storage; @@ -47,11 +54,19 @@ class MaskedBulkCopyTest : public ::testing::TestWithParam { // Source tuple: time + v + n + s + hidden + empty = 72 bytes/row. // Each source TsBlock has 137 rows, crossing two bitmap words. g_config_value_.tsblock_max_memory_ = 72 * 137; + // Parallel CTest processes can seed from the same clock tick. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic filename_counter{0}; filename_ = "masked_bulk_copy_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + - ".tsfile"; + "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)) + ".tsfile"; const std::vector columns{ {"v", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, {"n", INT64, UNCOMPRESSED, PLAIN, ColumnCategory::FIELD}, diff --git a/cpp/test/reader/chunk_reader_resource_test.cc b/cpp/test/reader/chunk_reader_resource_test.cc index 7e0a0af73..159db638c 100644 --- a/cpp/test/reader/chunk_reader_resource_test.cc +++ b/cpp/test/reader/chunk_reader_resource_test.cc @@ -41,6 +41,12 @@ #include "writer/tsfile_table_writer.h" #ifdef ENABLE_THREADS #include "common/thread_pool.h" + +#ifdef _WIN32 +#include +#else +#include +#endif #endif namespace storage { @@ -180,6 +186,17 @@ class AlignedReaderFailureTest : public ::testing::Test { "_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + + "_" + std::to_string([] { +#ifdef _WIN32 + return static_cast(_getpid()); +#else + return static_cast(getpid()); +#endif + }()) + + "_" + std::to_string([] { + static std::atomic counter{0}; + return counter.fetch_add(1); + }()) + ".tsfile"; WriteFile file; int flags = O_WRONLY | O_CREAT | O_TRUNC; diff --git a/cpp/test/reader/table_field_filter_test.cc b/cpp/test/reader/table_field_filter_test.cc index 62b495a83..a7c9aebc0 100644 --- a/cpp/test/reader/table_field_filter_test.cc +++ b/cpp/test/reader/table_field_filter_test.cc @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -36,6 +37,12 @@ #include "reader/table_query_executor.h" #include "writer/tsfile_table_writer.h" +#ifdef _WIN32 +#include +#else +#include +#endif + namespace { using namespace common; using namespace storage; @@ -164,11 +171,19 @@ class TableFieldFilterTest saved_ = g_config_value_; g_config_value_.page_writer_max_point_num_ = 3; g_config_value_.parallel_read_enabled_ = std::get<0>(GetParam()); + // Parallel CTest processes can seed from the same clock tick. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic filename_counter{0}; filename_ = "table_field_filter_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + - ".tsfile"; + "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)) + ".tsfile"; WriteFile file; int flags = O_WRONLY | O_CREAT | O_TRUNC; #ifdef _WIN32 diff --git a/cpp/test/reader/table_read_failure_test.cc b/cpp/test/reader/table_read_failure_test.cc index 9e8076612..99d02669b 100644 --- a/cpp/test/reader/table_read_failure_test.cc +++ b/cpp/test/reader/table_read_failure_test.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -42,6 +43,12 @@ #include "utils/injection.h" #include "writer/tsfile_table_writer.h" +#ifdef _WIN32 +#include +#else +#include +#endif + namespace { class InjectionGuard { @@ -106,11 +113,19 @@ class TableReadFailureTest storage::libtsfile_init(); saved_index_degree_ = common::g_config_value_.max_degree_of_index_node_; ASSERT_EQ(storage::set_max_degree_of_index_node(2), common::E_OK); + // Parallel CTest processes can seed from the same clock tick. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic filename_counter{0}; filename_ = "table_read_failure_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + - ".tsfile"; + "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)) + ".tsfile"; storage::WriteFile file; int flags = O_WRONLY | O_CREAT | O_TRUNC; #ifdef _WIN32 diff --git a/cpp/test/reader/timeseries_schema_codec_test.cc b/cpp/test/reader/timeseries_schema_codec_test.cc index cebb51541..613dda28a 100644 --- a/cpp/test/reader/timeseries_schema_codec_test.cc +++ b/cpp/test/reader/timeseries_schema_codec_test.cc @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -31,6 +32,12 @@ #include "reader/tsfile_reader.h" #include "writer/tsfile_writer.h" +#ifdef _WIN32 +#include +#else +#include +#endif + namespace storage { // get_timeseries_schema() reports the encoding and the compressor of every @@ -77,6 +84,16 @@ class TimeseriesSchemaCodecTest : public ::testing::Test { for (int i = 0; i < length; ++i) { s += chars[static_cast(dis(gen))]; } + // CTest runs cases in parallel processes; two can seed from the same + // clock tick and then collide on the file name. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic filename_counter{0}; + s += "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)); return s; } From 3f8078b253436e44e7b89b5c7ec1fcbce99ef829 Mon Sep 17 00:00:00 2001 From: ColinLee Date: Sat, 14 Nov 2026 15:35:53 +0800 Subject: [PATCH 10/11] test(cpp): keep the pid includes outside ENABLE_THREADS getpid()/_getpid() are used in the fixtures unconditionally, but the process.h/unistd.h block landed inside the ENABLE_THREADS guard in aligned_chunk_reader_bulk_test and chunk_reader_resource_test, which would break a build with threads disabled. --- cpp/test/reader/aligned_chunk_reader_bulk_test.cc | 12 ++++++------ cpp/test/reader/chunk_reader_resource_test.cc | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cpp/test/reader/aligned_chunk_reader_bulk_test.cc b/cpp/test/reader/aligned_chunk_reader_bulk_test.cc index d3efd8796..c5c5d2291 100644 --- a/cpp/test/reader/aligned_chunk_reader_bulk_test.cc +++ b/cpp/test/reader/aligned_chunk_reader_bulk_test.cc @@ -29,6 +29,12 @@ #include #include +#ifdef _WIN32 +#include +#else +#include +#endif + #include "common/allocator/alloc_base.h" #include "common/config/config.h" #include "common/tablet.h" @@ -42,12 +48,6 @@ #include "writer/tsfile_table_writer.h" #ifdef ENABLE_THREADS #include "common/thread_pool.h" - -#ifdef _WIN32 -#include -#else -#include -#endif #endif using namespace common; diff --git a/cpp/test/reader/chunk_reader_resource_test.cc b/cpp/test/reader/chunk_reader_resource_test.cc index 159db638c..3da4d840d 100644 --- a/cpp/test/reader/chunk_reader_resource_test.cc +++ b/cpp/test/reader/chunk_reader_resource_test.cc @@ -29,6 +29,12 @@ #include #include +#ifdef _WIN32 +#include +#else +#include +#endif + #include "common/allocator/alloc_base.h" #include "common/config/config.h" #include "common/tablet.h" @@ -41,12 +47,6 @@ #include "writer/tsfile_table_writer.h" #ifdef ENABLE_THREADS #include "common/thread_pool.h" - -#ifdef _WIN32 -#include -#else -#include -#endif #endif namespace storage { From e257a18126ef33b9c29827d53cc3c8e89682132f Mon Sep 17 00:00:00 2001 From: ColinLee Date: Sat, 14 Nov 2026 15:40:34 +0800 Subject: [PATCH 11/11] fix(cpp): read cached index nodes through a temporary buffer The buffer used to deserialize a cached node was allocated from the node's arena, so every cache entry pinned read_size bytes until eviction; use a temporary array instead. Also make the cache test file name unique per process. --- cpp/src/file/tsfile_io_reader.cc | 16 +++++++++++----- cpp/test/file/index_node_cache_test.cc | 17 ++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 6e6ed92b1..fc96ab086 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -1080,23 +1080,29 @@ int TsFileIOReader::get_cached_index_node(int64_t start_offset, auto arena = std::make_shared(); arena->init(512, common::MOD_TSFILE_READER); void* node_buf = arena->alloc(sizeof(MetaIndexNode)); - char* data_buf = static_cast(arena->alloc(read_size)); - if (IS_NULL(node_buf) || IS_NULL(data_buf)) { + if (IS_NULL(node_buf)) { + return E_OOM; + } + // The read buffer is only needed while deserializing; keeping it in the + // cached arena would pin read_size bytes per cached node until eviction. + std::unique_ptr data_buf(new (std::nothrow) char[read_size]); + if (data_buf == nullptr) { return E_OOM; } auto* node_ptr = new (node_buf) MetaIndexNode(arena.get()); std::shared_ptr node(node_ptr, MetaIndexNode::self_deleter); int32_t ret_read_len = 0; - int ret = read_file_->read(start_offset, data_buf, read_size, ret_read_len); + int ret = + read_file_->read(start_offset, data_buf.get(), read_size, ret_read_len); if (RET_FAIL(ret)) { return ret; } else if (ret_read_len != read_size) { return E_FILE_READ_ERR; } if (is_device) { - ret = node->device_deserialize_from(data_buf, read_size); + ret = node->device_deserialize_from(data_buf.get(), read_size); } else { - ret = node->deserialize_from(data_buf, read_size); + ret = node->deserialize_from(data_buf.get(), read_size); } if (ret != E_OK) { return ret; diff --git a/cpp/test/file/index_node_cache_test.cc b/cpp/test/file/index_node_cache_test.cc index 196d66dd2..24403d4d1 100644 --- a/cpp/test/file/index_node_cache_test.cc +++ b/cpp/test/file/index_node_cache_test.cc @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -33,6 +34,12 @@ #include "writer/tsfile_table_writer.h" #include "writer/tsfile_writer.h" +#ifdef _WIN32 +#include +#else +#include +#endif + namespace storage { /** @@ -50,11 +57,19 @@ class IndexNodeCacheTest : public ::testing::Test { // A small degree forces several index-node levels for kDevices devices, // so the walk in search_from_internal_node() actually reads nodes. ASSERT_EQ(storage::set_max_degree_of_index_node(2), common::E_OK); + // Parallel CTest processes can seed from the same clock tick. +#ifdef _WIN32 + const auto process_id = static_cast(_getpid()); +#else + const auto process_id = static_cast(getpid()); +#endif + static std::atomic filename_counter{0}; file_name_ = "index_node_cache_" + std::to_string( std::chrono::steady_clock::now().time_since_epoch().count()) + - ".tsfile"; + "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)) + ".tsfile"; ASSERT_NO_FATAL_FAILURE(WriteFile_()); }