Repository navigation
test(global-index): verify concurrent queries through reader wrappers #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,7 +56,11 @@ class PAIMON_EXPORT GlobalIndexReader : public FunctionVisitor<std::shared_ptr<G | |
| virtual Result<std::shared_ptr<ScoredGlobalIndexResult>> VisitFullTextSearch( | ||
| const std::shared_ptr<FullTextSearch>& full_text_search) = 0; | ||
|
|
||
| /// @return true if the reader is thread-safe; false otherwise. | ||
| /// @return true if the same reader supports concurrent queries; false otherwise. | ||
| /// Query arguments must not be modified during a call, and user callbacks and memory pools | ||
| /// used concurrently must be thread-safe. Reader lifetime must cover all outstanding calls. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the concurrency requirement for custom file systems. A multi-shard search can lazily open index files through the same |
||
| /// Submitting nested tasks to the same thread pool and blocking while waiting for them | ||
| /// may cause a deadlock. | ||
| virtual bool IsThreadSafe() const = 0; | ||
|
|
||
| /// @return An identifier representing the index type. (e.g., "btree", "lumina"). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,9 +102,8 @@ class PAIMON_EXPORT FullTextSearchBuilder { | |
| /// Sets the memory pool. If not set, the default pool is used. | ||
| FullTextSearchBuilder& WithMemoryPool(const std::shared_ptr<MemoryPool>& memory_pool); | ||
|
|
||
| /// Sets the executor that searches splits in parallel and reads manifests. If not set, each | ||
| /// read creates an executor with `global-index.thread-num` threads, which defaults to the | ||
| /// number of CPUs. | ||
| /// Sets the executor that searches splits in parallel and reads manifests. If not set, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document the executor re-entry restriction at the public entry point. If a task on a one-worker executor calls |
||
| /// `global-index.thread-num` determines the pool size (default: CPU count). | ||
| FullTextSearchBuilder& WithExecutor(const std::shared_ptr<Executor>& executor); | ||
|
|
||
| /// Sets the file system to access the table with. If not set, the file system is created from | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,21 +39,25 @@ namespace paimon { | |
| /// from the whole index. As Java `AbstractDataEvolutionVectorRead#preFilters` does, a vector search | ||
| /// restricts its pre-filter to those live rows. Predicate visits are delegated unchanged, as the | ||
| /// read drops deleted rows from their untruncated results. | ||
| /// Concurrent queries require a thread-safe wrapped reader and supplier. | ||
| class LiveRowGlobalIndexReader : public GlobalIndexReader { | ||
| public: | ||
| /// Supplies the global row ids of live rows, or null when every row is live. | ||
| /// Must support concurrent calls when shared by parallel shards or concurrent queries. | ||
| /// Returned bitmaps must remain immutable for the lifetime of queries using them. | ||
| using LiveRowsSupplier = std::function<Result<std::shared_ptr<const RoaringBitmap64>>()>; | ||
|
|
||
| /// Returns a supplier that calls `load` on its first call and shares the live rows it returns | ||
| /// with later calls, so they are read at most once. A failed load is retried on the next call. | ||
| /// The returned supplier is thread-safe. | ||
| static LiveRowsSupplier CacheLiveRows( | ||
| std::function<Result<std::optional<RoaringBitmap64>>()> load); | ||
|
|
||
| /// Creates a union of the shard readers of one index whose full-text and vector searches rank | ||
| /// only live rows. Each shard reader is wrapped in a LiveRowGlobalIndexReader. When a search | ||
| /// may rank rows of a shard, live rows are loaded on the visiting thread before it fans out to | ||
| /// the shards, so a load that reads manifests on `executor` never waits for it from one of its | ||
| /// own threads. | ||
| /// the shards. Callers must run outside `executor` when loading or searching waits for tasks | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The live-rows supplier can still run on a shard worker after preloading. |
||
| /// on that executor. | ||
| /// @param shard_readers Readers of the shards. They take and return global row ids. | ||
| /// @param shard_ranges The global row id range (both ends inclusive) of each shard. | ||
| /// @param search_ranges The global row ids each shard ranks in a full-text search, as | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,9 @@ | |
|
|
||
| #include "paimon/core/global_index/live_row_global_index_reader.h" | ||
|
|
||
| #include <atomic> | ||
| #include <cstdint> | ||
| #include <future> | ||
| #include <map> | ||
| #include <memory> | ||
| #include <mutex> | ||
|
|
@@ -30,7 +32,9 @@ | |
| #include <vector> | ||
|
|
||
| #include "gtest/gtest.h" | ||
| #include "paimon/common/global_index/offset_global_index_reader.h" | ||
| #include "paimon/executor.h" | ||
| #include "paimon/global_index/bitmap_scored_global_index_result.h" | ||
| #include "paimon/memory/memory_pool.h" | ||
| #include "paimon/predicate/full_text_search.h" | ||
| #include "paimon/predicate/literal.h" | ||
|
|
@@ -42,6 +46,40 @@ | |
| #include "paimon/utils/roaring_bitmap64.h" | ||
|
|
||
| namespace paimon::test { | ||
| namespace { | ||
| // Applies filters to local row ids and scores each row using the query value and local row id. | ||
| class FilteringGlobalIndexReader : public MockGlobalIndexReader { | ||
| public: | ||
| Result<std::shared_ptr<ScoredGlobalIndexResult>> VisitVectorSearch( | ||
| const std::shared_ptr<VectorSearch>& search) override { | ||
| return Search(search->pre_filter, search->query.front()); | ||
| } | ||
|
|
||
| Result<std::shared_ptr<ScoredGlobalIndexResult>> VisitFullTextSearch( | ||
| const std::shared_ptr<FullTextSearch>& search) override { | ||
| return Search( | ||
| [search](int64_t row_id) { | ||
| return !search->include_row_ids || search->include_row_ids->Contains(row_id); | ||
| }, | ||
| static_cast<float>(search->limit)); | ||
| } | ||
|
|
||
| private: | ||
| Result<std::shared_ptr<ScoredGlobalIndexResult>> Search(const VectorSearch::PreFilter& filter, | ||
| float query_score) { | ||
| RoaringBitmap64 rows; | ||
| std::vector<float> scores; | ||
| for (int64_t row_id = 0; row_id < 4; ++row_id) { | ||
| if (!filter || filter(row_id)) { | ||
| rows.Add(row_id); | ||
| scores.push_back(query_score + static_cast<float>(row_id) + 0.1f); | ||
| } | ||
| } | ||
| return std::make_shared<BitmapScoredGlobalIndexResult>(std::move(rows), std::move(scores)); | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| class LiveRowGlobalIndexReaderTest : public ::testing::Test { | ||
| public: | ||
| static LiveRowGlobalIndexReader::LiveRowsSupplier Supply( | ||
|
|
@@ -321,6 +359,84 @@ TEST_F(LiveRowGlobalIndexReaderTest, TestLiveRowsErrorOnlyFailsSearchesThatRankR | |
| ASSERT_EQ(captured->include_row_ids.value(), RoaringBitmap64::From({100})); | ||
| } | ||
|
|
||
| TEST_F(LiveRowGlobalIndexReaderTest, TestConcurrentSearch) { | ||
| for (bool parallel_shards : {false, true}) { | ||
| SCOPED_TRACE(parallel_shards); | ||
| std::shared_ptr<Executor> executor; | ||
| if (parallel_shards) { | ||
| ASSERT_OK_AND_ASSIGN(executor, CreateDefaultExecutor(/*thread_count=*/4)); | ||
| } | ||
| std::vector<std::shared_ptr<GlobalIndexReader>> shards; | ||
| for (int64_t offset : {100, 200}) { | ||
| shards.push_back(std::make_shared<OffsetGlobalIndexReader>( | ||
| std::make_shared<FilteringGlobalIndexReader>(), offset, offset + 3)); | ||
| } | ||
| std::atomic<int32_t> load_count{0}; | ||
| auto supplier = LiveRowGlobalIndexReader::CacheLiveRows( | ||
| [&]() -> Result<std::optional<RoaringBitmap64>> { | ||
| ++load_count; | ||
| // Shards cover [100, 103] and [200, 203]; DV deletes 101 and 202. | ||
| return std::optional<RoaringBitmap64>( | ||
| RoaringBitmap64::From({100, 102, 103, 200, 201, 203})); | ||
| }); | ||
| auto reader = LiveRowGlobalIndexReader::CreateUnion( | ||
| std::move(shards), {Range(100, 103), Range(200, 203)}, | ||
| {Range(100, 103), Range(200, 203)}, executor, supplier); | ||
| ASSERT_TRUE(reader->IsThreadSafe()); | ||
|
|
||
| std::promise<void> start; | ||
| std::shared_future<void> ready = start.get_future().share(); | ||
| std::vector<std::future<void>> workers; | ||
| for (int32_t thread_id = 0; thread_id < 4; ++thread_id) { | ||
| workers.push_back(std::async(std::launch::async, [reader, ready, thread_id]() { | ||
| ready.wait(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the test observe overlapping queries with different filters. The start promise releases the callers. Each query can complete before the next enters while still satisfying every assertion. All four callers also use the same filter parity in a given iteration ( |
||
| for (int32_t iteration = 0; iteration < 16; ++iteration) { | ||
| const int32_t query_id = thread_id * 16 + iteration; | ||
| const int32_t parity = query_id % 2; | ||
| const float query_score = static_cast<float>(query_id + 3); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the clang-tidy failure on this declaration. The |
||
| const RoaringBitmap64 expected_rows = | ||
| parity == 0 ? RoaringBitmap64::From({100, 102, 200}) | ||
| : RoaringBitmap64::From({103, 201, 203}); | ||
| // Scores follow ascending global row ids; offset changes ids, not scores. | ||
| const std::vector<float> expected_scores = | ||
| parity == 0 ? std::vector<float>{query_score + 0.1f, query_score + 2.1f, | ||
| query_score + 0.1f} | ||
| : std::vector<float>{query_score + 3.1f, query_score + 1.1f, | ||
| query_score + 3.1f}; | ||
| auto vector = | ||
| SearchVector([parity](int64_t row_id) { return row_id % 2 == parity; }); | ||
| vector->query = {query_score}; | ||
| auto text = Search(parity == 0 ? RoaringBitmap64::From({100, 102, 200, 202}) | ||
| : RoaringBitmap64::From({101, 103, 201, 203})); | ||
| text->limit = query_id + 3; | ||
| for (bool vector_query : {false, true}) { | ||
| ASSERT_OK_AND_ASSIGN(std::shared_ptr<ScoredGlobalIndexResult> result, | ||
| vector_query ? reader->VisitVectorSearch(vector) | ||
| : reader->VisitFullTextSearch(text)); | ||
| auto scored = | ||
| std::dynamic_pointer_cast<BitmapScoredGlobalIndexResult>(result); | ||
| ASSERT_TRUE(scored); | ||
| ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* rows, scored->GetBitmap()); | ||
| ASSERT_EQ(*rows, expected_rows); | ||
| const auto& scores = scored->GetScores(); | ||
| ASSERT_EQ(scores.size(), expected_scores.size()); | ||
| for (size_t i = 0; i < scores.size(); ++i) { | ||
| ASSERT_FLOAT_EQ(scores[i], expected_scores[i]); | ||
| } | ||
| } | ||
| ASSERT_TRUE(vector->pre_filter(parity == 0 ? 202 : 101)); | ||
| ASSERT_EQ(text->include_row_ids->Cardinality(), 4); | ||
| } | ||
| })); | ||
| } | ||
| start.set_value(); | ||
| for (auto& worker : workers) { | ||
| worker.get(); | ||
| } | ||
| ASSERT_EQ(load_count.load(), 1); | ||
| } | ||
| } | ||
|
|
||
| TEST_F(LiveRowGlobalIndexReaderTest, TestOtherVisitsDoNotReadLiveRows) { | ||
| auto mock_reader = std::make_shared<MockGlobalIndexReader>(); | ||
| mock_reader->SetDefaultResult({1, 2}); | ||
|
|
@@ -338,6 +454,8 @@ TEST_F(LiveRowGlobalIndexReaderTest, TestOtherVisitsDoNotReadLiveRows) { | |
| ASSERT_EQ(call_count, 0); | ||
| ASSERT_EQ(reader.GetIndexType(), "fake"); | ||
| ASSERT_TRUE(reader.IsThreadSafe()); | ||
| mock_reader->SetThreadSafe(false); | ||
| ASSERT_FALSE(reader.IsThreadSafe()); | ||
| } | ||
|
|
||
| } // namespace paimon::test | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -622,7 +622,7 @@ Result<std::shared_ptr<ScoredGlobalIndexResult>> DataEvolutionFullTextRead::Sear | |
| } | ||
| } | ||
| }); | ||
| if (searches.size() <= 1) { | ||
| if (!executor_ || searches.size() <= 1) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The null-executor fallback is unreachable through |
||
| for (const auto& search : searches) { | ||
| results.push_back(search()); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Account for query lifetime after an exceptional exit.
UnionGlobalIndexReadercan unwind on anExecutor::Add()orfuture.get()exception while accepted tasks still hold the original search object or its pre-filter.TestDeferredTaskOwnsActionAfterEarlierFutureThrowsdemonstrates that a queued task can run after the visit unwinds. A caller following the new wording can reuse the query or release objects borrowed by its pre-filter at that point, while a shard may still access them. Please drain accepted tasks before unwinding, or document that the arguments and callback state must remain valid and immutable until the accepted work finishes.