diff --git a/cpp/src/file/tsfile_io_reader.cc b/cpp/src/file/tsfile_io_reader.cc index 3abe2eeb4..fc96ab086 100644 --- a/cpp/src/file/tsfile_io_reader.cc +++ b/cpp/src/file/tsfile_io_reader.cc @@ -61,11 +61,27 @@ void TsFileIOReader::reset() { delete read_file_; } read_file_ = nullptr; + // 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(); { 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; } } @@ -1043,6 +1059,78 @@ 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, + CachedIndexNode& out) { + { + std::lock_guard lk(index_node_cache_mu_); + if (index_node_cache_.tryGetCopy(start_offset, out)) { + return E_OK; + } + } + + 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)); + 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.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.get(), read_size); + } else { + ret = node->deserialize_from(data_buf.get(), read_size); + } + if (ret != E_OK) { + return ret; + } + + CachedIndexNode entry; + entry.arena = arena; + entry.node = node; + { + std::lock_guard lk(index_node_cache_mu_); +#ifdef ENABLE_TEST + ++index_node_cache_misses_; +#endif + // 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); + return E_OK; +} + int TsFileIOReader::search_from_internal_node( std::shared_ptr target_name, bool is_device, std::shared_ptr index_node, @@ -1063,54 +1151,36 @@ 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. + // 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))) { return ret; } - if (cur_level_index_node->node_type_ == LEAF_DEVICE) { - ret = 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); - 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( + } 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); - cur_level_index_node->destroy(); - return ret; //// FIXME } 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); - 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 598d6092d..22283ebc5 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); } @@ -232,15 +233,88 @@ 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; + // 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; + + 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 + // 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 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, 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 // error otherwise). Copying into out keeps the caller safe from rehash / @@ -261,6 +335,19 @@ 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; + } + // 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: @@ -285,6 +372,12 @@ 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_; +#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 new file mode 100644 index 000000000..24403d4d1 --- /dev/null +++ b/cpp/test/file/index_node_cache_test.cc @@ -0,0 +1,182 @@ +/* + * 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 + +#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" + +#ifdef _WIN32 +#include +#else +#include +#endif + +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 = 8; + 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); + // 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()) + + "_" + std::to_string(process_id) + "_" + + std::to_string(filename_counter.fetch_add(1)) + ".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 (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( + "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(); + } + 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()); + pa.destroy(); +} + +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 diff --git a/cpp/test/reader/aligned_chunk_reader_bulk_test.cc b/cpp/test/reader/aligned_chunk_reader_bulk_test.cc index 0be62def8..c5c5d2291 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 @@ -28,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" @@ -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..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" @@ -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; }