Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions cpp/src/cwrapper/tsfile_cwrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2204,9 +2204,18 @@ ERRNO tsfile_reader_get_timeseries_metadata_all(
try {
auto* r = static_cast<storage::TsFileReader*>(reader);
storage::DeviceTimeseriesMetadataMap cpp_map;
const int ret = r->get_timeseries_metadata(cpp_map);
if (ret != common::E_OK) return ret;
return populate_c_metadata_map_from_cpp(cpp_map, out_map);
// get_timeseries_metadata() returns indexes allocated in the reader's
// shared per-caller arena, and any later metadata call on this reader
// (from any thread) resets that arena. Convert into caller-owned C
// structures while still holding the reader lock so another thread
// cannot free the indexes between the call and the copy.
return r->with_reader_lock([&]() -> int {
const int ret = r->get_timeseries_metadata(cpp_map);
if (ret != common::E_OK) {
return ret;
}
return populate_c_metadata_map_from_cpp(cpp_map, out_map);
});
} catch (const std::bad_alloc&) {
return common::E_OOM;
} catch (...) {
Expand Down Expand Up @@ -2265,9 +2274,15 @@ ERRNO tsfile_reader_get_timeseries_metadata_for_devices(
std::make_shared<storage::StringArrayDeviceID>(segments));
}
storage::DeviceTimeseriesMetadataMap cpp_map;
const int ret = r->get_timeseries_metadata(query_ids, cpp_map);
if (ret != common::E_OK) return ret;
return populate_c_metadata_map_from_cpp(cpp_map, out_map);
// Same arena-lifetime rationale as tsfile_reader_get_timeseries_
// metadata_all(): copy while the reader lock is held.
return r->with_reader_lock([&]() -> int {
const int ret = r->get_timeseries_metadata(query_ids, cpp_map);
if (ret != common::E_OK) {
return ret;
}
return populate_c_metadata_map_from_cpp(cpp_map, out_map);
});
} catch (const std::bad_alloc&) {
return common::E_OOM;
} catch (...) {
Expand Down
11 changes: 11 additions & 0 deletions cpp/src/cwrapper/tsfile_cwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,17 @@ TsFileWriter tsfile_writer_new_with_memory_threshold(WriteFile file,
* @return TsFileReader Valid handle on success, NULL on failure.
*
* @note Call tsfile_reader_close() to release resources.
* @note A reader handle may be shared by several threads. Every operation on
* the reader, and every next()/get_next_tsblock()/close() call on a
* result set it returned, is serialized on one internal per-reader
* lock: concurrent use is safe but does not run in parallel. Advance
* one result set from one thread at a time, and close every result set
* before calling tsfile_reader_close(); iterating a result set after the
* reader is closed is not supported. Metadata returned by
* tsfile_reader_get_timeseries_metadata_all() and
* tsfile_reader_get_timeseries_metadata_for_devices() is copied into
* caller-owned C structures before the call returns, while the reader
* lock is still held.
*/

TsFileReader tsfile_reader_new(const char* pathname, ERRNO* err_code);
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/reader/qds_with_timegenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ void destroy_node(Node* node) {
}

void QDSWithTimeGenerator::close() {
ExclusiveReaderLock read_lock(this);
if (row_record_ != nullptr) {
delete row_record_;
row_record_ = nullptr;
Expand All @@ -357,6 +358,7 @@ void QDSWithTimeGenerator::close() {
}

int QDSWithTimeGenerator::next(bool& has_next) {
ExclusiveReaderLock read_lock(this);
if (tree_ == nullptr) {
has_next = false;
return E_OK;
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/reader/qds_without_timegenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ int QDSWithoutTimeGenerator::init_internal(TsFileIOReader* io_reader,
}

void QDSWithoutTimeGenerator::close() {
ExclusiveReaderLock read_lock(this);
if (row_record_ != nullptr) {
delete row_record_;
row_record_ = nullptr;
Expand Down Expand Up @@ -256,6 +257,7 @@ void QDSWithoutTimeGenerator::close() {
}

int QDSWithoutTimeGenerator::next(bool& has_next) {
ExclusiveReaderLock read_lock(this);
has_next = false;
if (read_error_ != E_OK) {
return read_error_;
Expand Down
80 changes: 80 additions & 0 deletions cpp/src/reader/result_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@

#include <algorithm>
#include <cctype>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
#include <utility>

#include "common/row_record.h"
#include "common/tsblock/tsblock.h"
Expand Down Expand Up @@ -98,11 +101,52 @@ class ResultSetIterator;
* @note When using the tree model and the filter is not a global time filter,
* it should be QDSWithTimeGenerator.
* @note If the query uses the table model, the cast should be TableResultSet
*
* @par Thread safety
* A result set returned by TsFileReader reads lazily from the reader's shared
* file source and executor state. Such a result set is bound to the reader's
* lock (set_reader_lock()) and every next()/get_next_tsblock()/close() call
* acquires that lock, so operations on result sets of the same reader are
* serialized with each other and with new queries on that reader.
*
* One result set must be advanced by one thread at a time. next() and the
* value accessors that read the current row (get_value(), get_row_record(),
* is_null()) are not atomic with each other: a thread reading the row while
* another thread advances the result set can observe a torn row. Concurrent
* next() calls on one result set do not crash, but they interleave rows.
*
* A result set is invalid for iteration once the reader that produced it is
* closed or reopened; destroy result sets before calling TsFileReader::close().
* Destroying a result set after close() is safe (it keeps its executor alive
* and close() only releases in-memory state).
*/
class ResultSet : std::enable_shared_from_this<ResultSet> {
public:
ResultSet() {}
virtual ~ResultSet() {}
/**
* @brief Bind this result set to the lock that guards the reader state it
* reads from.
*
* TsFileReader calls this on every result set it returns. Result sets
* built directly on a TsFileExecutor (a lower-level API) are unbound and
* are not thread-safe.
*/
void set_reader_lock(std::shared_ptr<std::recursive_mutex> reader_lock) {
reader_lock_ = std::move(reader_lock);
}
/**
* @brief Keep an internal object of the producing reader alive for as long
* as this result set exists.
*
* A table result set borrows the metadata querier and file reader owned by
* the reader's table executor. Keeping that executor alive prevents the
* result set from dangling when a later query replaces the executor (for
* example when it uses a different batch size).
*/
void set_keep_alive(std::shared_ptr<void> keep_alive) {
keep_alive_ = std::move(keep_alive);
}
/**
* @brief Get the next row of the result set
*
Expand Down Expand Up @@ -212,6 +256,39 @@ class ResultSet : std::enable_shared_from_this<ResultSet> {
virtual void close() = 0;

protected:
/**
* @brief Acquires the reader's exclusive lock for the lifetime of one
* operation.
*
* The lock is not a read lock: it serializes the operation against every
* other reader operation, including queries and other result sets that
* write reader-owned state. Subclasses wrap
* next()/get_next_tsblock()/close() in this guard. It is a no-op for
* unbound result sets (those built directly on an executor).
*/
class ExclusiveReaderLock {
public:
explicit ExclusiveReaderLock(ResultSet* result_set)
: result_set_(result_set) {
if (result_set_ != nullptr &&
result_set_->reader_lock_ != nullptr) {
result_set_->reader_lock_->lock();
locked_ = true;
}
}
~ExclusiveReaderLock() {
if (locked_) {
result_set_->reader_lock_->unlock();
}
}
ExclusiveReaderLock(const ExclusiveReaderLock&) = delete;
ExclusiveReaderLock& operator=(const ExclusiveReaderLock&) = delete;

private:
ResultSet* result_set_;
bool locked_ = false;
};

struct CaseInsensitiveHash {
std::size_t operator()(const std::string& str) const {
std::string lowerStr = str;
Expand Down Expand Up @@ -240,6 +317,9 @@ class ResultSet : std::enable_shared_from_this<ResultSet> {
index_lookup_;
RowRecord* row_record_ = nullptr;
common::PageArena pa_;
// Set by TsFileReader; see set_reader_lock()/set_keep_alive().
std::shared_ptr<std::recursive_mutex> reader_lock_;
std::shared_ptr<void> keep_alive_;
};

template <>
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/reader/table_result_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void TableResultSet::init() {
TableResultSet::~TableResultSet() { close(); }

int TableResultSet::next(bool& has_next) {
ExclusiveReaderLock read_lock(this);
int ret = common::E_OK;
has_next = false;
if (read_error_ != common::E_OK) {
Expand Down Expand Up @@ -192,6 +193,7 @@ std::shared_ptr<ResultSetMetadata> TableResultSet::get_metadata() {
}

int TableResultSet::get_next_tsblock(common::TsBlock*& block) {
ExclusiveReaderLock read_lock(this);
int ret = common::E_OK;
block = nullptr;
if (read_error_ != common::E_OK) {
Expand Down Expand Up @@ -226,6 +228,7 @@ int TableResultSet::get_next_tsblock(common::TsBlock*& block) {
}

void TableResultSet::close() {
ExclusiveReaderLock read_lock(this);
if (closed_) {
return;
}
Expand Down
Loading
Loading