Skip to content

Commit 27bd4b2

Browse files
authored
fix: fix IsThreadSafe() in UnionGlobalIndexReader (alibaba#395)
1 parent 9012f0a commit 27bd4b2

3 files changed

Lines changed: 33 additions & 9 deletions

File tree

src/paimon/common/global_index/union_global_index_reader.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,15 @@ Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::Union(ReaderA
182182
return merged_result;
183183
}
184184

185+
bool UnionGlobalIndexReader::IsThreadSafe() const {
186+
for (const auto& reader : readers_) {
187+
if (!reader->IsThreadSafe()) {
188+
return false;
189+
}
190+
}
191+
return true;
192+
}
193+
185194
template <typename R>
186195
std::vector<R> UnionGlobalIndexReader::ExecuteAllReaders(
187196
const std::function<R(const std::shared_ptr<GlobalIndexReader>&)>& action) {
@@ -199,7 +208,7 @@ std::vector<R> UnionGlobalIndexReader::ExecuteAllReaders(
199208
futures.reserve(readers_.size());
200209
for (const auto& reader : readers_) {
201210
futures.push_back(
202-
Via(executor_.get(), [&action, &reader]() -> R { return action(reader); }));
211+
Via(executor_.get(), [&action, reader]() -> R { return action(reader); }));
203212
}
204213
return CollectAll(futures);
205214
}

src/paimon/common/global_index/union_global_index_reader.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ class UnionGlobalIndexReader : public GlobalIndexReader {
5858
Result<std::shared_ptr<GlobalIndexResult>> VisitFullTextSearch(
5959
const std::shared_ptr<FullTextSearch>& full_text_search) override;
6060

61-
bool IsThreadSafe() const override {
62-
return false;
63-
}
61+
bool IsThreadSafe() const override;
6462

6563
std::string GetIndexType() const override {
6664
return "union";

src/paimon/common/global_index/union_global_index_reader_test.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class FakeReader : public GlobalIndexReader {
6161
has_scored_result_ = true;
6262
}
6363

64+
void SetThreadSafe(bool thread_safe) {
65+
thread_safe_ = thread_safe;
66+
}
67+
6468
/// Counts how many times any Visit* method was invoked. Useful to assert all readers
6569
/// are exercised by UnionGlobalIndexReader.
6670
int InvocationCount() const {
@@ -147,7 +151,7 @@ class FakeReader : public GlobalIndexReader {
147151
}
148152

149153
bool IsThreadSafe() const override {
150-
return true;
154+
return thread_safe_;
151155
}
152156

153157
std::string GetIndexType() const override {
@@ -176,6 +180,7 @@ class FakeReader : public GlobalIndexReader {
176180
std::vector<int64_t> scored_row_ids_;
177181
std::vector<float> scored_scores_;
178182
bool has_scored_result_ = false;
183+
bool thread_safe_ = true;
179184
std::atomic<int32_t> invocation_count_{0};
180185
};
181186

@@ -515,12 +520,24 @@ TEST_F(UnionGlobalIndexReaderTest, TestVisitVectorSearchErrorPropagation) {
515520
ASSERT_NOK_WITH_MSG(union_reader.VisitVectorSearch(nullptr), "vector search failure");
516521
}
517522

518-
TEST_F(UnionGlobalIndexReaderTest, TestIsThreadSafeAlwaysFalse) {
519-
auto reader = std::make_shared<FakeReader>();
520-
std::vector<std::shared_ptr<GlobalIndexReader>> readers = {reader};
523+
TEST_F(UnionGlobalIndexReaderTest, TestIsThreadSafeReturnsTrueWhenAllReadersAreSafe) {
524+
auto reader1 = std::make_shared<FakeReader>();
525+
auto reader2 = std::make_shared<FakeReader>();
526+
527+
std::vector<std::shared_ptr<GlobalIndexReader>> readers = {reader1, reader2};
528+
UnionGlobalIndexReader union_reader(std::move(readers), nullptr);
529+
530+
ASSERT_TRUE(union_reader.IsThreadSafe());
531+
}
532+
533+
TEST_F(UnionGlobalIndexReaderTest, TestIsThreadSafeReturnsFalseWhenAnyReaderIsNotSafe) {
534+
auto reader1 = std::make_shared<FakeReader>();
535+
auto reader2 = std::make_shared<FakeReader>();
536+
reader2->SetThreadSafe(false);
537+
538+
std::vector<std::shared_ptr<GlobalIndexReader>> readers = {reader1, reader2};
521539
UnionGlobalIndexReader union_reader(std::move(readers), nullptr);
522540

523-
// UnionGlobalIndexReader is not thread-safe regardless of inner readers
524541
ASSERT_FALSE(union_reader.IsThreadSafe());
525542
}
526543

0 commit comments

Comments
 (0)