Skip to content
Merged
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
38 changes: 38 additions & 0 deletions cpp/src/reader/tsfile_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,17 @@ int TsFileReader::close() {
}

int TsFileReader::query(QueryExpression* qe, ResultSet*& ret_qds) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
return tsfile_executor_->execute(qe, ret_qds);
}

int TsFileReader::query(std::vector<std::string>& path_list, int64_t start_time,
int64_t end_time, ResultSet*& result_set) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
std::vector<Path> path_list_vec;
int ret = parse_paths(path_list, path_list_vec);
if (ret != E_OK) {
Expand Down Expand Up @@ -243,6 +249,9 @@ int TsFileReader::query(const std::string& table_name,
int64_t start_time, int64_t end_time, int offset,
int limit, ResultSet*& result_set, Filter* tag_filter,
int batch_size) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
int ret = E_OK;
TsFileMeta* tsfile_meta = nullptr;
if (RET_FAIL(tsfile_executor_->get_tsfile_meta(tsfile_meta))) {
Expand Down Expand Up @@ -270,6 +279,9 @@ int TsFileReader::query(const std::string& table_name,

int TsFileReader::queryByRow(std::vector<std::string>& path_list, int offset,
int limit, ResultSet*& result_set) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
std::vector<Path> path_list_vec;
int ret = parse_paths(path_list, path_list_vec);
if (ret != E_OK) {
Expand All @@ -285,13 +297,19 @@ int TsFileReader::queryByRow(std::vector<std::string>& path_list, int offset,
int TsFileReader::prepare_series(const FileGeneration& generation,
const PreparedLocator& locator,
std::shared_ptr<PreparedSeries>& prepared) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
return tsfile_executor_->prepare_series(generation, locator, prepared);
}

int TsFileReader::prepare_series(
const FileGeneration& generation, const PreparedLocator& locator,
const std::shared_ptr<PreparedSeries>& aligned_time_owner,
std::shared_ptr<PreparedSeries>& prepared) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
return tsfile_executor_->prepare_series(generation, locator,
aligned_time_owner, prepared);
}
Expand Down Expand Up @@ -414,6 +432,9 @@ int TsFileReader::query_prepared(
const std::shared_ptr<PreparedSeries>& prepared, int64_t start_time,
int64_t end_time, int offset, int limit, ResultSet*& result_set,
uint32_t block_rows) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
if (prepared == nullptr || prepared->index() == nullptr) {
return E_INVALID_ARG;
}
Expand All @@ -427,6 +448,9 @@ int TsFileReader::query_prepared_multi(
const std::vector<std::shared_ptr<PreparedSeries>>& prepared,
int64_t start_time, int64_t end_time, int offset, int limit,
ResultSet*& result_set, uint32_t block_rows) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
return tsfile_executor_->execute_prepared_multi(
prepared, start_time, end_time, offset, limit, result_set, block_rows);
}
Expand All @@ -435,6 +459,9 @@ int TsFileReader::queryByRow(const std::string& table_name,
const std::vector<std::string>& column_names,
int offset, int limit, ResultSet*& result_set,
Filter* tag_filter, int batch_size) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
int ret = E_OK;
TsFileMeta* tsfile_meta = nullptr;
if (RET_FAIL(tsfile_executor_->get_tsfile_meta(tsfile_meta))) {
Expand All @@ -456,6 +483,9 @@ int TsFileReader::queryByRow(const std::string& table_name,
int TsFileReader::query_table_on_tree(
const std::vector<std::string>& measurement_names, int64_t star_time,
int64_t end_time, ResultSet*& result_set) {
if (tsfile_executor_ == nullptr) {
return E_INVALID_ARG;
}
int ret = E_OK;
TsFileMeta* tsfile_meta = nullptr;
if (RET_FAIL(tsfile_executor_->get_tsfile_meta(tsfile_meta))) {
Expand Down Expand Up @@ -539,6 +569,14 @@ int TsFileReader::query_table_on_tree(
}

void TsFileReader::destroy_query_data_set(storage::ResultSet* qds) {
if (tsfile_executor_ == nullptr) {
// The executor is gone after close(). The result set object still
// needs freeing; this is safe because the caller has already called
// result->close(), which released its borrows from the (now-deleted)
// io_reader_, so a plain delete cannot touch it again.
delete qds;
return;
}
Comment on lines +572 to +579
tsfile_executor_->destroy_query_data_set(qds);
}

Expand Down
92 changes: 92 additions & 0 deletions cpp/test/reader/tsfile_reader_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,98 @@ TEST_F(TsFileReaderTest, RepeatedGetTimeseriesMetadataDoesNotLeakArena) {
reader.close();
}

// Regression: querying a closed TsFileReader used to dereference the
// already-deleted internal executor and crash with SIGSEGV. After close()
// every query/execute entry point must return a clear error instead.
TEST_F(TsFileReaderTest, QueryAfterCloseReturnsError) {
std::string device_path = "device1";
std::string measurement_name = "temperature";
common::TSDataType data_type = common::TSDataType::INT32;
common::TSEncoding encoding = common::TSEncoding::PLAIN;
common::CompressionType compression_type =
common::CompressionType::UNCOMPRESSED;
tsfile_writer_->register_timeseries(
device_path, storage::MeasurementSchema(measurement_name, data_type,
encoding, compression_type));

for (int i = 0; i < 2; ++i) {
TsRecord record(1622505600000 + i * 1000, device_path);
record.add_point(measurement_name, (int32_t)i);
ASSERT_EQ(tsfile_writer_->write_record(record), E_OK);
}
ASSERT_EQ(tsfile_writer_->flush(), E_OK);
ASSERT_EQ(tsfile_writer_->close(), E_OK);

std::vector<std::string> select_list = {"device1.temperature"};

storage::TsFileReader reader;
ASSERT_EQ(reader.open(file_name_), E_OK);

// A successful query first, so the reader is known-good before closing.
const int64_t start_time = 1622505599000;
const int64_t end_time = 1622505602000;
storage::ResultSet* result_set = nullptr;
ASSERT_EQ(reader.query(select_list, start_time, end_time, result_set),
E_OK);
ASSERT_NE(result_set, nullptr);
reader.destroy_query_data_set(result_set);

ASSERT_EQ(reader.close(), E_OK);

// Querying after close must return a clear error, not crash.
storage::ResultSet* closed_result_set = nullptr;
ASSERT_EQ(
reader.query(select_list, start_time, end_time, closed_result_set),
common::E_INVALID_ARG);
ASSERT_EQ(closed_result_set, nullptr);

// The same guard must hold for the other query entry points.
ASSERT_EQ(reader.queryByRow(select_list, 0, -1, closed_result_set),
common::E_INVALID_ARG);
ASSERT_EQ(closed_result_set, nullptr);
}

// Regression: destroying a result set after the reader is closed must still
// free it (rather than early-returning on the missing executor and leaking).
TEST_F(TsFileReaderTest, DestroyResultSetAfterClose) {
std::string device_path = "device1";
std::string measurement_name = "temperature";
common::TSDataType data_type = common::TSDataType::INT32;
common::TSEncoding encoding = common::TSEncoding::PLAIN;
common::CompressionType compression_type =
common::CompressionType::UNCOMPRESSED;
tsfile_writer_->register_timeseries(
device_path, storage::MeasurementSchema(measurement_name, data_type,
encoding, compression_type));

for (int i = 0; i < 2; ++i) {
TsRecord record(1622505600000 + i * 1000, device_path);
record.add_point(measurement_name, (int32_t)i);
ASSERT_EQ(tsfile_writer_->write_record(record), E_OK);
}
ASSERT_EQ(tsfile_writer_->flush(), E_OK);
ASSERT_EQ(tsfile_writer_->close(), E_OK);

std::vector<std::string> select_list = {"device1.temperature"};

storage::TsFileReader reader;
ASSERT_EQ(reader.open(file_name_), E_OK);

const int64_t start_time = 1622505599000;
const int64_t end_time = 1622505602000;
storage::ResultSet* result_set = nullptr;
ASSERT_EQ(reader.query(select_list, start_time, end_time, result_set),
E_OK);
ASSERT_NE(result_set, nullptr);

// Reader-dependent cleanup must run while the reader is still open.
result_set->close();
ASSERT_EQ(reader.close(), E_OK);

// Destroying the result set after close must still delete it (no leak).
reader.destroy_query_data_set(result_set);
}

// Regression: a non-aligned (tree) scan with a TsBlock capacity smaller than
// a single row spun forever in the query stage. With capacity 8 bytes and a
// 16-byte row (time INT64 + value INT64), TsBlock::init() computes
Expand Down
Loading