From 12dc428a7454de2f8e7361a01a49bdf9db3aa9d4 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Tue, 29 Sep 2026 10:31:00 +0800 Subject: [PATCH 1/9] [improvement](be) Use thread pool scan scheduler by default ### What problem does this PR solve? Issue Number: None Related PR: #51690, #58144, #58273, #65814, #67070 Problem Summary: Workload groups pick the scan scheduler for internal and external table scans from `enable_task_executor_in_internal_table` and `enable_task_executor_in_external_table`. Both configs currently default to `true` on master, so every workload group creates `TaskExecutorSimplifiedScanScheduler` (the time-sharing task executor) for local (`ls_*`) and remote (`rs_*`) scans. The time-sharing executor is still maturing (for example the self-deadlock fixed in #58273; the internal-table default was already reverted once in #58144), so the default should be the `ThreadPoolSimplifiedScanScheduler` for both internal and external tables. This change sets both configs' default values to `false`. The task executor can still be enabled explicitly through `be.conf`. With the thread pool path as the default, a `SELECT ... LIMIT n` over many tablets keeps admitting its remaining pending scanners after peer scanners have exhausted the shared scan LIMIT (`ScannerContext::can_admit_scan_task`, rewritten in #67070, only applies the concurrency limit). Admission is kept that way on purpose: holding pending scanners back based on the in-flight or completed task counters, as #65814 does on the TaskExecutor path, ties the schedule to bookkeeping that is easy to get wrong and can leave a context with nothing to wake the operator. Instead, `Scanner::get_block_after_projects()` now checks the shared LIMIT before it reads: once the counter is exhausted, the scanner returns an empty block with EOS, so an admitted scanner never reads its range for rows the operator will cut anyway. ### Release note The BE configs `enable_task_executor_in_internal_table` and `enable_task_executor_in_external_table` now default to `false`; internal and external table scans use the thread pool scan scheduler by default. ### Check List (For Author) - Test: - Unit Test: added `WorkloadGroupManagerTest.DefaultScanSchedulersUseThreadPool` (registered defaults are `false` and the workload group creates thread pool schedulers for local and remote scans), `ScannerContextTest.thread_pool_admission_ignores_shared_limit` (a pending scanner is still admitted after the shared LIMIT is exhausted while tasks are in flight or completed) and `ScannerProjectionTest.shared_limit_exhausted_before_call_reports_eos_without_reading` (the admitted scanner reports EOS with an empty block without reading); ran `WorkloadGroupManagerTest.*`, `ScannerContextTest.*`, `ScannerProjectionTest.*` and other scanner/scheduler/task-executor UTs - Regression test: ran existing suites on a local cluster with the new default: correctness_p0/test_shared_scan_limit_pending_tasks, query_p0 limit pushdown suites, query_p0/scan_range, and stream load CSV suites - Behavior changed: Yes (default scan scheduler for internal and external table scans changes from the task executor to the thread pool; it can be switched back via the two BE configs) - Does this need documentation: No --- be/src/common/config.cpp | 4 +- be/src/exec/scan/scanner.cpp | 7 +++ be/test/exec/scan/scanner_context_test.cpp | 53 +++++++++++++++++ .../scan/scanner_late_arrival_rf_test.cpp | 40 +++++++++++++ .../workload_group_manager_test.cpp | 57 +++++++++++++++++++ 5 files changed, 159 insertions(+), 2 deletions(-) diff --git a/be/src/common/config.cpp b/be/src/common/config.cpp index e8e31d06e5dafa..91b6e0a64a03a4 100644 --- a/be/src/common/config.cpp +++ b/be/src/common/config.cpp @@ -339,9 +339,9 @@ DEFINE_Int32(task_executor_max_concurrency_per_task, "-1"); DEFINE_Int32(task_executor_initial_max_concurrency_per_task, "-1"); // Enable task executor in internal table scan. -DEFINE_Bool(enable_task_executor_in_internal_table, "true"); +DEFINE_Bool(enable_task_executor_in_internal_table, "false"); // Enable task executor in external table scan. -DEFINE_Bool(enable_task_executor_in_external_table, "true"); +DEFINE_Bool(enable_task_executor_in_external_table, "false"); // number of scanner thread pool size for olap table // and the min thread num of remote scanner thread pool diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index f16a46f3a4acd0..b961bb38c6743c 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -89,6 +89,13 @@ Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); + // Peer scanners may have exhausted the shared LIMIT while this scanner waited to be + // scheduled. Admission does not look at the LIMIT, so the scanner reports EOS itself with an + // empty block instead of reading on; the operator already has enough rows and cuts the rest. + if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) { + *eos = true; + return Status::OK(); + } const auto& row_descriptor = _local_state->_parent->operator_row_desc_before_projection(); if (_has_projection) { _origin_block.clear_column_data(row_descriptor.num_slots()); diff --git a/be/test/exec/scan/scanner_context_test.cpp b/be/test/exec/scan/scanner_context_test.cpp index a6b0e975ce3f22..0c0ba91435fade 100644 --- a/be/test/exec/scan/scanner_context_test.cpp +++ b/be/test/exec/scan/scanner_context_test.cpp @@ -711,6 +711,59 @@ TEST_F(ScannerContextTest, thread_pool_admission_state) { EXPECT_EQ(scanner_context->try_get_next_scan_task(context_transfer_lock), nullptr); } +TEST_F(ScannerContextTest, thread_pool_admission_ignores_shared_limit) { + const int parallel_tasks = 1; + const int64_t limit = 100; + auto scan_operator = std::make_unique(obj_pool.get(), tnode, 0, *descs, + parallel_tasks, TQueryCacheParam {}); + auto olap_scan_local_state = + OlapScanLocalState::create_unique(state.get(), scan_operator.get()); + + OlapScanner::Params scanner_params; + scanner_params.state = state.get(); + scanner_params.profile = profile.get(); + scanner_params.limit = limit; + scanner_params.key_ranges = std::vector(); + std::shared_ptr scanner = + OlapScanner::create_shared(olap_scan_local_state.get(), std::move(scanner_params)); + std::list> scanners { + std::make_shared(scanner)}; + auto scanner_context = ScannerContext::create_shared( + state.get(), olap_scan_local_state.get(), output_tuple_desc, false, scanners, limit, + scan_dependency, &shared_limit, nullptr, nullptr, 0, false, parallel_tasks); + + std::unique_ptr scheduler = + std::make_unique(cgroup_cpu_ctl); + EXPECT_CALL(*scheduler, get_active_threads()).WillRepeatedly(testing::Return(0)); + EXPECT_CALL(*scheduler, get_queue_size()).WillRepeatedly(testing::Return(0)); + scanner_context->_scanner_scheduler = scheduler.get(); + scanner_context->_min_scan_concurrency_of_scan_scheduler = 20; + scanner_context->_max_scan_concurrency = 4; + + std::unique_lock transfer_lock(scanner_context->transfer_lock()); + scanner_context->_pending_tasks = std::stack>(); + scanner_context->_completed_tasks.clear(); + scanner_context->push_pending_scan_task( + std::make_shared(std::make_shared(scanner)), transfer_lock); + + // The shared LIMIT is exhausted while a task is in flight and a completed result waits for the + // operator. Admission only applies the concurrency limit: the pending scanner is still + // admitted and reports EOS itself in Scanner::get_block_after_projects(). Holding it back + // based on the in-flight or completed tasks is what can leave a context with nothing to + // wake the operator. + shared_limit.store(0); + scanner_context->_in_flight_tasks_num = 1; + auto completed_task = std::make_shared(std::make_shared(scanner)); + completed_task->set_state(ScanTask::State::IN_FLIGHT); + completed_task->set_state(ScanTask::State::EOS); + scanner_context->_completed_tasks.push_back(completed_task); + EXPECT_TRUE(scanner_context->can_admit_scan_task(transfer_lock)); + auto admitted_task = scanner_context->try_get_next_scan_task(transfer_lock); + ASSERT_NE(admitted_task, nullptr); + EXPECT_EQ(scanner_context->_in_flight_tasks_num, 2); + EXPECT_TRUE(scanner_context->_pending_tasks.empty()); +} + TEST_F(ScannerContextTest, thread_pool_admission_refreshes_adaptive_limit) { const int parallel_tasks = 2; auto scan_operator = std::make_unique(obj_pool.get(), tnode, 0, *descs, diff --git a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp index a21e7f47bdaedf..3661e68ddd4a4b 100644 --- a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp +++ b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp @@ -62,6 +62,7 @@ class TestScanner final : public Scanner { : Scanner(state, local_state, limit, profile), _allow_padding(allow_padding) {} void add_block(Block block) { _blocks.push_back(std::move(block)); } + size_t unread_blocks() const { return _blocks.size(); } protected: bool _can_merge_padding_blocks(const Block& /*left*/, const Block& /*right*/) const override { @@ -739,6 +740,45 @@ TEST_F(ScannerLateArrivalRfTest, olap_scanner_releases_resources_before_open) { prepared_scanner->update_realtime_counters(); } +// Admission does not look at the shared LIMIT, so a pending scanner is still scheduled after its +// peers have exhausted it. The scanner reports EOS with an empty block on that call instead of +// reading its range. +TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_reports_eos_without_reading) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->_shared_scan_limit.store(4); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0, 1})); + scanner.add_block(ColumnHelper::create_block({2, 3, 4})); + + // Peer scanners returned the whole LIMIT before this scanner ran. + op->_shared_scan_limit.fetch_sub(4); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + EXPECT_EQ(output.rows(), 0); + EXPECT_EQ(scanner.unread_blocks(), 2); + EXPECT_EQ(op->_shared_scan_limit.load(), 0); +} + TEST(ScannerProjectionTest, merges_padding_block_when_limit_eos_without_extra_flag) { ObjectPool pool; auto data_type = std::make_shared(); diff --git a/be/test/runtime/workload_group/workload_group_manager_test.cpp b/be/test/runtime/workload_group/workload_group_manager_test.cpp index db047257af55d5..a7f4543dc81b9b 100644 --- a/be/test/runtime/workload_group/workload_group_manager_test.cpp +++ b/be/test/runtime/workload_group/workload_group_manager_test.cpp @@ -41,6 +41,7 @@ #include "common/status.h" #include "cpp/sync_point.h" #include "exec/pipeline/dependency.h" +#include "exec/scan/scanner_scheduler.h" #include "exec/spill/spill_file_manager.h" #include "load/memtable/memtable_memory_limiter.h" #include "runtime/exec_env.h" @@ -1489,6 +1490,62 @@ TEST_F(WorkloadGroupManagerTest, FailedInternalGroupSetupCancelsAdaptiveFlush) { controller->stop(); } +TEST_F(WorkloadGroupManagerTest, DefaultScanSchedulersUseThreadPool) { + // Check the registered defaults, which do not depend on the be.conf loaded by the UT runner. + const auto& fields = *config::Register::_s_field_map; + EXPECT_STREQ("false", fields.at("enable_task_executor_in_internal_table").defval); + EXPECT_STREQ("false", fields.at("enable_task_executor_in_external_table").defval); + + const auto saved_config = std::make_tuple( + config::enable_adaptive_flush_threads, config::enable_task_executor_in_internal_table, + config::enable_task_executor_in_external_table, config::pipeline_executor_size, + config::blocking_pipeline_executor_size, config::doris_scanner_thread_pool_thread_num, + config::doris_max_remote_scanner_thread_pool_thread_num, + config::doris_scanner_min_thread_pool_thread_num, config::min_active_scan_threads, + config::min_active_file_scan_threads, config::flush_thread_num_per_store); + Defer restore {[&] { + std::tie(config::enable_adaptive_flush_threads, + config::enable_task_executor_in_internal_table, + config::enable_task_executor_in_external_table, config::pipeline_executor_size, + config::blocking_pipeline_executor_size, + config::doris_scanner_thread_pool_thread_num, + config::doris_max_remote_scanner_thread_pool_thread_num, + config::doris_scanner_min_thread_pool_thread_num, config::min_active_scan_threads, + config::min_active_file_scan_threads, config::flush_thread_num_per_store) = + saved_config; + }}; + config::enable_adaptive_flush_threads = false; + config::enable_task_executor_in_internal_table = false; + config::enable_task_executor_in_external_table = false; + config::pipeline_executor_size = 1; + config::blocking_pipeline_executor_size = 1; + config::doris_scanner_thread_pool_thread_num = 1; + config::doris_max_remote_scanner_thread_pool_thread_num = 1; + config::doris_scanner_min_thread_pool_thread_num = 1; + config::min_active_scan_threads = 1; + config::min_active_file_scan_threads = 1; + config::flush_thread_num_per_store = 1; + + ASSERT_TRUE(_wg_manager->create_internal_wg().ok()); + Defer stop_schedulers {[&] { + for (auto& entry : _wg_manager->_workload_groups) { + entry.second->try_stop_schedulers(); + entry.second->destroy_schedulers(); + } + }}; + ASSERT_EQ(_wg_manager->_workload_groups.size(), 1); + auto wg = _wg_manager->_workload_groups.begin()->second; + + TaskScheduler* exec_sched = nullptr; + ScannerScheduler* scan_sched = nullptr; + ScannerScheduler* remote_scan_sched = nullptr; + wg->get_query_scheduler(&exec_sched, &scan_sched, &remote_scan_sched); + ASSERT_NE(scan_sched, nullptr); + ASSERT_NE(remote_scan_sched, nullptr); + EXPECT_NE(dynamic_cast(scan_sched), nullptr); + EXPECT_NE(dynamic_cast(remote_scan_sched), nullptr); +} + // Exercise the actual registration/cancellation paths without starting query schedulers. TEST_F(WorkloadGroupManagerTest, AdaptiveFlushRegistrationSurvivesIdChangeAndReuse) { auto* env = ExecEnv::GetInstance(); From ef593094bace30e9adfc54cd8fca4aec76446430 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Fri, 9 Oct 2026 13:26:00 +0800 Subject: [PATCH 2/9] [fix](be) Refund shared scan LIMIT rows held in scanner padding ### What problem does this PR solve? Issue Number: None Related PR: #68610, #62222 Problem Summary: `Scanner::get_block()` charges the shared scan LIMIT counter as soon as rows pass the filters, but with projections `Scanner::get_block_after_projects()` may keep a small block in `_padding_block` and emit a later block first. Once peer scanners exhaust the counter, `ScannerContext` stops admitting pending scanners and reaches EOS when no completed or in-flight task remains, so rows that were charged but still held in a pending scanner's padding block were never emitted. A parallel projected `SELECT ... LIMIT n` could return fewer than `n` rows: with LIMIT 4096, scanner A holds 10 rows and emits 2500 (2510 charged), scanner B emits 1586 and exhausts the counter, and the query returns 4086 rows. The counter now equals "LIMIT minus returned rows" between calls. Rows still held in `_padding_block` when `get_block_after_projects()` returns are refunded to the counter, and they are charged again when the next call starts, so during a call every row the scanner holds is charged and the read loop still stops as soon as the LIMIT is reached. An exhausted counter therefore always means that at least LIMIT rows were returned, which is what the EOS checks in `Scanner` and `ScannerContext` rely on, while projected scans with selective filters keep coalescing small blocks up to half a batch per call. The scan does not need to cap its output at the LIMIT, since the operator cuts the extra rows. ### Release note None ### Check List (For Author) - Test: Unit Test / Regression test - Unit Test: added `ScannerProjectionTest.shared_limit_refunds_rows_held_in_padding`, `ScannerProjectionTest.shared_limit_keeps_padding_small_blocks` and `ScannerProjectionTest.shared_limit_reports_eos_when_scanner_rows_exhaust_it`; ran `ScannerProjectionTest.*`, `ScannerContextTest.*`, `ScannerLateArrivalRfTest.*`, `WorkloadGroupManagerTest.*` - Regression test: query_p0/limit and correctness_p0/test_shared_scan_limit_pending_tasks on a local cluster - Behavior changed: No - Does this need documentation: No --- be/src/exec/scan/scanner.cpp | 13 ++ .../scan/scanner_late_arrival_rf_test.cpp | 134 ++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index b961bb38c6743c..1f55cb7d4adebf 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -108,6 +108,16 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); return _do_projections(&_origin_block, block); } + // get_block() charges the shared LIMIT as soon as rows pass the filters, but rows that stay + // in _padding_block when this call returns are not returned by it. Peer scanners may + // exhaust the LIMIT before this scanner runs again, and the context then finishes without + // running it, so those rows would be charged but never returned. Keep the counter equal + // to "LIMIT minus returned rows" between calls: charge the held rows again on entry, so + // every row this scanner holds during the call is charged and the read loop below stops + // as soon as the LIMIT is reached, and refund whatever is still held on exit. + if (_shared_scan_limit && !_padding_block.empty()) { + _shared_scan_limit->fetch_sub(_padding_block.rows(), std::memory_order_acq_rel); + } const auto min_batch_size = std::max(state->batch_size() / 2, 1); const auto block_max_bytes = state->preferred_block_size_bytes(); while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && @@ -140,6 +150,9 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool if (_origin_block.empty() && !_padding_block.empty()) { _padding_block.swap(_origin_block); } + if (_shared_scan_limit && !_padding_block.empty()) { + _shared_scan_limit->fetch_add(_padding_block.rows(), std::memory_order_acq_rel); + } return _do_projections(&_origin_block, block); } else { return get_block(state, block, eos); diff --git a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp index 3661e68ddd4a4b..8261f361c6ec0a 100644 --- a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp +++ b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp @@ -894,4 +894,138 @@ TEST(ScannerProjectionTest, projects_incompatible_blocks_before_reading_the_next EXPECT_EQ(final_output.rows(), 0); } +// With parallel scanners, the scan context may finish as soon as the shared LIMIT counter is +// exhausted, without running a pending scanner again. get_block() charges rows as soon as they pass +// the filters, so rows that stay in the padding block when a call returns must be refunded, or they +// are charged but silently dropped. Between calls the counter equals LIMIT minus the returned rows. +TEST(ScannerProjectionTest, shared_limit_refunds_rows_held_in_padding) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->_shared_scan_limit.store(10); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + // The first block is below the padding threshold and is held while the second block, which + // reaches the threshold on its own, is emitted. + scanner.add_block(ColumnHelper::create_block({0})); + scanner.add_block(ColumnHelper::create_block({1, 2, 3, 4, 5})); + + Block first_output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &first_output, &eos).ok()); + EXPECT_FALSE(eos); + ASSERT_EQ(first_output.rows(), 5); + EXPECT_EQ(first_output.get_by_position(0).column->get_int(0), 1); + // Both blocks were charged when they were read, but only the returned rows stay charged. + EXPECT_EQ(op->_shared_scan_limit.load(), 5); + + // Peer scanners return the remaining rows of the LIMIT. Had the held row stayed charged, the + // counter would already be exhausted here and the context could finish without this scanner. + op->_shared_scan_limit.fetch_sub(4); + EXPECT_EQ(op->_shared_scan_limit.load(), 1); + + // The held row is charged again on entry, which exhausts the counter, so it is flushed with eos + // instead of reading on. + Block final_output; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &final_output, &eos).ok()); + EXPECT_TRUE(eos); + ASSERT_EQ(final_output.rows(), 1); + EXPECT_EQ(final_output.get_by_position(0).column->get_int(0), 0); + EXPECT_EQ(op->_shared_scan_limit.load(), 0); +} + +// A selective scan under a large shared LIMIT still coalesces small blocks: one call reads several +// source blocks and returns them as one block, instead of handing every surviving row to the +// operator alone. +TEST(ScannerProjectionTest, shared_limit_keeps_padding_small_blocks) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->_shared_scan_limit.store(100); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + for (int i = 0; i < 6; ++i) { + scanner.add_block(ColumnHelper::create_block({i})); + } + + // Four single-row blocks fill half a batch and are returned together. + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_FALSE(eos); + ASSERT_EQ(output.rows(), 4); + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(output.get_by_position(0).column->get_int(i), i); + } + EXPECT_EQ(op->_shared_scan_limit.load(), 96); + + // The tail is returned with eos; nothing is held across the calls. + Block tail; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &tail, &eos).ok()); + EXPECT_TRUE(eos); + ASSERT_EQ(tail.rows(), 2); + EXPECT_EQ(op->_shared_scan_limit.load(), 94); +} + +// The scanner stops once its rows exhaust the shared LIMIT, even though its reader still has data. +TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_it) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->_shared_scan_limit.store(3); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0, 1, 2, 3, 4})); + scanner.add_block(ColumnHelper::create_block({5, 6, 7, 8, 9})); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + EXPECT_EQ(output.rows(), 5); + EXPECT_EQ(op->_shared_scan_limit.load(), -2); +} + } // namespace doris From 119201e926396c280aaf2936d67c0337f7b495d7 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Fri, 9 Oct 2026 13:26:01 +0800 Subject: [PATCH 3/9] [fix](be) Catch exceptions thrown by the row-ID fetch RPC callback ### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: `PInternalService::multiget_data_v2` submits its callback to the remote scan scheduler. With the thread pool scheduler, which this PR makes the default, the callback runs directly on a `ThreadPool` worker, and `ThreadPool::dispatch_thread()` has no exception boundary. For a request with an existing file mapping and row IDs, `RowIdStorageReader::read_by_rowids()` constructs `SlotDescriptor(pslot)` before its local `try` block, and an unknown `PTypeNode.type` (for example one sent by a newer FE) throws `doris::Exception`. The exception escaped the worker thread and terminated the BE. The task executor path caught it in `PrioritizedSplitRunner::process()`, but the RPC still completed with a success status and no row data. The callback now calls `RowIdStorageReader::read_by_rowids_for_rpc()`, which runs `read_by_rowids()` under `ASSIGN_STATUS_IF_CATCH_EXCEPTION` and serializes the resulting status into the response before the closure completes the RPC, mirroring the exception conversion the external-row path already has in `submit_external_scan_tasks()`. `ExecEnv::set_id_manager()` is added under `BE_TEST` so a unit test can install the `IdManager` the reader looks up. ### Release note None ### Check List (For Author) - Test: Unit Test - Unit Test: added `RowIdStorageReaderTest.RpcHelperReportsThrownExceptionOnThreadPoolPath`, which runs the helper on a real `ThreadPoolSimplifiedScanScheduler` with a slot whose type node kind is unknown and asserts the error status on the response; ran `RowIdStorageReaderTest.*` and `SubmitExternalScanTasksTest.*` - Behavior changed: No - Does this need documentation: No --- be/src/exec/rowid_fetcher.cpp | 7 +++ be/src/exec/rowid_fetcher.h | 8 ++++ be/src/runtime/exec_env.h | 1 + be/src/service/internal_service.cpp | 4 +- be/test/exec/rowid_fetcher_test.cpp | 69 +++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) diff --git a/be/src/exec/rowid_fetcher.cpp b/be/src/exec/rowid_fetcher.cpp index fc359de9db4cef..3fbd45fd5f4f33 100644 --- a/be/src/exec/rowid_fetcher.cpp +++ b/be/src/exec/rowid_fetcher.cpp @@ -196,6 +196,13 @@ static void scatter_scan_blocks_to_result_block( } } +void RowIdStorageReader::read_by_rowids_for_rpc(const PMultiGetRequestV2& request, + PMultiGetResponseV2* response) { + Status st = Status::OK(); + ASSIGN_STATUS_IF_CATCH_EXCEPTION(st = read_by_rowids(request, response), st); + st.to_protobuf(response->mutable_status()); +} + Status RowIdStorageReader::read_by_rowids(const PMultiGetRequestV2& request, PMultiGetResponseV2* response) { if (request.request_block_descs_size()) { diff --git a/be/src/exec/rowid_fetcher.h b/be/src/exec/rowid_fetcher.h index 60b18fb8e824bc..916b77ff214a62 100644 --- a/be/src/exec/rowid_fetcher.h +++ b/be/src/exec/rowid_fetcher.h @@ -82,6 +82,14 @@ class RowIdStorageReader { static Status read_by_rowids(const PMultiGetRequestV2& request, PMultiGetResponseV2* response); + // Runs read_by_rowids() for the multiget_data_v2 RPC and serializes the outcome into the + // response status. The remote scan scheduler runs the RPC callback on a plain ThreadPool + // worker, which has no exception boundary of its own, so a doris::Exception thrown while + // reading (for example by a slot of the request whose PTypeNode this BE does not know) is + // converted into an error status here instead of terminating the process. + static void read_by_rowids_for_rpc(const PMultiGetRequestV2& request, + PMultiGetResponseV2* response); + private: static bool should_use_file_scanner_v2(const TQueryOptions& query_options, const TFileScanRangeParams& scan_params, diff --git a/be/src/runtime/exec_env.h b/be/src/runtime/exec_env.h index 8d30f60258ae01..be7d92c4ffff75 100644 --- a/be/src/runtime/exec_env.h +++ b/be/src/runtime/exec_env.h @@ -348,6 +348,7 @@ class ExecEnv { _inverted_index_query_cache = inverted_index_query_cache; } void set_process_profile(ProcessProfile* pp) { this->_process_profile = pp; } + void set_id_manager(IdManager* id_manager) { this->_id_manager = id_manager; } void set_delete_bitmap_agg_cache(DeleteBitmapAggCache* c) { _delete_bitmap_agg_cache = c; } void set_storage_page_cache(StoragePageCache* c) { this->_storage_page_cache = c; } void set_ann_index_ivf_list_cache(AnnIndexIVFListCache* c) { diff --git a/be/src/service/internal_service.cpp b/be/src/service/internal_service.cpp index a2014b7bb3d452..d64349a6d297f6 100644 --- a/be/src/service/internal_service.cpp +++ b/be/src/service/internal_service.cpp @@ -2063,9 +2063,7 @@ void PInternalService::multiget_data_v2(google::protobuf::RpcController* control MonotonicStopWatch watch; watch.start(); brpc::ClosureGuard closure_guard(done); - response->mutable_status()->set_status_code(0); - Status st = RowIdStorageReader::read_by_rowids(*request, response); - st.to_protobuf(response->mutable_status()); + RowIdStorageReader::read_by_rowids_for_rpc(*request, response); LOG(INFO) << "multiget_data finished, cost(us):" << watch.elapsed_time() / 1000; return true; diff --git a/be/test/exec/rowid_fetcher_test.cpp b/be/test/exec/rowid_fetcher_test.cpp index 4eb1739d0f87b1..df96ec73da3b91 100644 --- a/be/test/exec/rowid_fetcher_test.cpp +++ b/be/test/exec/rowid_fetcher_test.cpp @@ -22,13 +22,22 @@ #include #include +#include "agent/cgroup_cpu_ctl.h" +#include "common/exception.h" #include "exec/operator/file_scan_operator.h" #include "exec/scan/file_scanner_v2.h" +#include "exec/scan/scanner_scheduler.h" #include "format_v2/column_mapper.h" #include "format_v2/table/hive_reader.h" +#include "gen_cpp/internal_service.pb.h" #include "runtime/descriptor_helper.h" #include "runtime/descriptors.h" +#include "runtime/exec_env.h" #include "runtime/runtime_state.h" +#include "storage/id_manager.h" +#include "storage/rowset_id.h" +#include "util/countdown_latch.h" +#include "util/defer_op.h" namespace doris { @@ -429,4 +438,64 @@ TEST_F(SubmitExternalScanTasksTest, ThrownExceptionReachesTheCaller) { EXPECT_NE(result.to_string().find("scanner threw"), std::string::npos); } +// multiget_data_v2 runs read_by_rowids() on the remote scan scheduler, which by default is a +// ThreadPool whose worker has no exception boundary. A request whose file mapping exists but +// whose slot carries a PTypeNode this BE cannot decode makes SlotDescriptor's constructor throw; +// the RPC helper must turn that into an error status on the response instead of letting the +// exception leave the worker and terminate the process. +TEST_F(RowIdStorageReaderTest, RpcHelperReportsThrownExceptionOnThreadPoolPath) { + IdManager id_manager; + ExecEnv* env = ExecEnv::GetInstance(); + IdManager* saved_id_manager = env->get_id_manager(); + env->set_id_manager(&id_manager); + Defer restore_id_manager {[&] { env->set_id_manager(saved_id_manager); }}; + + PMultiGetRequestV2 request; + request.mutable_query_id()->set_hi(68610); + request.mutable_query_id()->set_lo(1); + request.set_wg_id(1); + auto id_file_map = id_manager.add_id_file_map(request.query_id(), 1024); + const uint32_t file_id = id_file_map->get_file_mapping_id( + std::make_shared(/*tablet_id=*/1, RowsetId(), /*segment_id=*/0)); + + PRequestBlockDesc* block_desc = request.add_request_block_descs(); + block_desc->add_file_id(file_id); + block_desc->add_row_id(0); + PSlotDescriptor* slot = block_desc->add_slots(); + slot->set_id(0); + slot->set_parent(0); + slot->set_column_pos(0); + slot->set_byte_offset(0); + slot->set_null_indicator_byte(0); + slot->set_null_indicator_bit(-1); + slot->set_col_name("c"); + slot->set_slot_idx(0); + // A type node kind unknown to this BE, as a newer FE could send. + slot->mutable_slot_type()->add_types()->set_type(99); + + std::shared_ptr cgroup_cpu_ctl = std::make_shared(1); + ThreadPoolSimplifiedScanScheduler scheduler("rowid_rpc_test", cgroup_cpu_ctl); + ASSERT_TRUE(scheduler.start(1, 1, 4, 1).ok()); + Defer stop_scheduler {[&] { scheduler.stop(); }}; + + PMultiGetResponseV2 response; + CountDownLatch finished(1); + ASSERT_TRUE(scheduler + .submit_scan_task(SimplifiedScanTask( + [&]() { + RowIdStorageReader::read_by_rowids_for_rpc( + request, &response); + finished.count_down(); + return true; + }, + nullptr, nullptr), + "rowid_rpc_test_task") + .ok()); + ASSERT_TRUE(finished.wait_for(std::chrono::seconds(30))); + + EXPECT_NE(response.status().status_code(), 0); + ASSERT_EQ(response.status().error_msgs_size(), 1); + EXPECT_NE(response.status().error_msgs(0).find("invalid TTypeNodeType"), std::string::npos); +} + } // namespace doris From f9afa797a8f4ef3b08a268117025ebfb76128d51 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Fri, 9 Oct 2026 14:27:05 +0800 Subject: [PATCH 4/9] [fix](be) Decide the shared scan LIMIT pointer from the SQL LIMIT ### What problem does this PR solve? Issue Number: close #xxx Related PR: #xxx Problem Summary: `ScanLocalState::shared_scan_limit_ptr()` returned null whenever the live counter was negative, and `Scanner::init()` caches that pointer once. The counter is only negative for a moment: a scanner charges the rows it holds in its padding block when `get_block_after_projects()` starts, reads on while peers charge their rows, and refunds the held rows when the call returns. A scanner initialized during that moment (a local state of a parallel instance opening late) cached null, never checked or charged the shared LIMIT, and could read a long selective tail after its peers had already satisfied the query. Decide pointer presence from the immutable SQL LIMIT of the operator instead of the mutable counter, which is how `ScannerContext` already decides it. `MockScanOperatorX::set_limit_for_test()` sets both the LIMIT and the counter the way the operator constructor does. ### Release note None ### Check List (For Author) - Test: Unit Test - `ScannerProjectionTest.shared_limit_observed_by_scanner_initialized_under_negative_counter` initializes a scanner while the counter is negative and checks that it still charges the shared LIMIT and stops once it is exhausted. - `ScannerProjectionTest.no_shared_limit_without_sql_limit` keeps the no-LIMIT case on the null pointer. - Behavior changed: No - Does this need documentation: No --- be/src/exec/operator/mock_scan_operator.h | 6 ++ be/src/exec/operator/scan_operator.cpp | 10 ++- .../scan/scanner_late_arrival_rf_test.cpp | 76 ++++++++++++++++++- 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/be/src/exec/operator/mock_scan_operator.h b/be/src/exec/operator/mock_scan_operator.h index 609e631ce8c123..27f1f8ef53d905 100644 --- a/be/src/exec/operator/mock_scan_operator.h +++ b/be/src/exec/operator/mock_scan_operator.h @@ -84,6 +84,12 @@ class MockScanOperatorX final : public ScanOperatorX { _output_blocks.push_back(std::move(block)); } + // Mirrors the constructor of ScanOperatorX: the shared counter starts at the SQL LIMIT. + void set_limit_for_test(int64_t limit) { + _limit = limit; + _shared_scan_limit.store(limit, std::memory_order_relaxed); + } + Status get_block_impl(RuntimeState* state, Block* block, bool* eos) override { if (_output_blocks.empty()) { *eos = true; diff --git a/be/src/exec/operator/scan_operator.cpp b/be/src/exec/operator/scan_operator.cpp index 0ed2d762953c64..a3d69765949287 100644 --- a/be/src/exec/operator/scan_operator.cpp +++ b/be/src/exec/operator/scan_operator.cpp @@ -1072,10 +1072,12 @@ int64_t ScanLocalState::limit_per_scanner() { template std::atomic* ScanLocalState::shared_scan_limit_ptr() { - auto* p = &_parent->cast()._shared_scan_limit; - // -1 means "no SQL LIMIT" — return nullptr so callers naturally skip - // all limit logic. - return p->load(std::memory_order_relaxed) < 0 ? nullptr : p; + auto& parent = _parent->cast(); + // Decide from the SQL LIMIT, not from the counter. -1 means "no SQL LIMIT" — return nullptr + // so callers naturally skip all limit logic. The counter itself can be negative for a moment + // while a scanner holds charged padding rows that it refunds when its call returns, and a + // scanner initialized at that moment must still observe and charge the shared LIMIT. + return parent.limit() < 0 ? nullptr : &parent._shared_scan_limit; } template diff --git a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp index 8261f361c6ec0a..23f1f3f342c633 100644 --- a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp +++ b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp @@ -756,7 +756,7 @@ TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_reports_eos_witho auto& projection = op->set_projection_for_test( MockRowDescriptor(std::vector {data_type}, &pool)); op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; - op->_shared_scan_limit.store(4); + op->set_limit_for_test(4); auto local_state = std::make_shared(&state, op.get()); local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); @@ -911,7 +911,7 @@ TEST(ScannerProjectionTest, shared_limit_refunds_rows_held_in_padding) { auto& projection = op->set_projection_for_test( MockRowDescriptor(std::vector {data_type}, &pool)); op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; - op->_shared_scan_limit.store(10); + op->set_limit_for_test(10); auto local_state = std::make_shared(&state, op.get()); local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); @@ -964,7 +964,7 @@ TEST(ScannerProjectionTest, shared_limit_keeps_padding_small_blocks) { auto& projection = op->set_projection_for_test( MockRowDescriptor(std::vector {data_type}, &pool)); op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; - op->_shared_scan_limit.store(100); + op->set_limit_for_test(100); auto local_state = std::make_shared(&state, op.get()); local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); @@ -995,6 +995,74 @@ TEST(ScannerProjectionTest, shared_limit_keeps_padding_small_blocks) { EXPECT_EQ(op->_shared_scan_limit.load(), 94); } +// A scanner initialized while peers hold the counter below zero still observes the shared LIMIT. +// The counter dips below zero for a moment when a scanner charges its held padding rows and reads +// on while peers charge their own rows, and it is restored when the padding rows are refunded. +TEST(ScannerProjectionTest, shared_limit_observed_by_scanner_initialized_under_negative_counter) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(8); + // Peers charged 9 rows in total while one of them still holds 2 of them in its padding block. + op->_shared_scan_limit.store(-1); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + ASSERT_EQ(scanner._shared_scan_limit, &op->_shared_scan_limit); + + // The holder refunds its 2 padding rows: 1 row of the LIMIT is still missing. + op->_shared_scan_limit.fetch_add(2); + ASSERT_EQ(op->_shared_scan_limit.load(), 1); + scanner.add_block(ColumnHelper::create_block({0, 1, 2})); + scanner.add_block(ColumnHelper::create_block({3, 4, 5})); + + // The first block satisfies the LIMIT, so the scanner charges it and stops without reading on. + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + EXPECT_EQ(output.rows(), 3); + EXPECT_EQ(op->_shared_scan_limit.load(), -2); + EXPECT_EQ(scanner._blocks.size(), 1); +} + +// Without a SQL LIMIT the scanner gets no counter, whatever the counter holds. +TEST(ScannerProjectionTest, no_shared_limit_without_sql_limit) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + ASSERT_EQ(op->limit(), -1); + op->_shared_scan_limit.store(5); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + EXPECT_EQ(scanner._shared_scan_limit, nullptr); +} + // The scanner stops once its rows exhaust the shared LIMIT, even though its reader still has data. TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_it) { ObjectPool pool; @@ -1009,7 +1077,7 @@ TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_i auto& projection = op->set_projection_for_test( MockRowDescriptor(std::vector {data_type}, &pool)); op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; - op->_shared_scan_limit.store(3); + op->set_limit_for_test(3); auto local_state = std::make_shared(&state, op.get()); local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); From f1712a4c57f6dbc7c7bff9aa7ffc02bf02b56c1c Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Wed, 14 Oct 2026 17:56:50 +0800 Subject: [PATCH 5/9] [fix](be) Count scan LIMIT after producing output rows ### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: Projected scanners could charge rows retained in their padding block against the shared scan LIMIT. If another scanner then exhausted the counter, the held rows might never reach the operator and a LIMIT query could return too few rows. Deduct only the rows actually produced by get_block_after_projects, and flush local padding when it can satisfy the remaining LIMIT without reading another source block. ### Release note Fix projected scan LIMIT queries that could return fewer rows than requested. ### Check List (For Author) - Test: Unit Test: 66 focused BE tests passed; formatting and build hygiene checks passed. Clang-tidy was attempted but remains blocked by pre-existing diagnostics and a missing stddef.h in the toolchain. - Behavior changed: Yes (shared scan LIMIT counts produced rows rather than held padding) - Does this need documentation: No --- be/src/exec/operator/scan_operator.cpp | 6 +- be/src/exec/scan/scanner.cpp | 110 +++++---- be/src/exec/scan/scanner.h | 2 + .../scan/scanner_late_arrival_rf_test.cpp | 223 ++++++++++++++++-- 4 files changed, 265 insertions(+), 76 deletions(-) diff --git a/be/src/exec/operator/scan_operator.cpp b/be/src/exec/operator/scan_operator.cpp index a3d69765949287..6b8d7781f74069 100644 --- a/be/src/exec/operator/scan_operator.cpp +++ b/be/src/exec/operator/scan_operator.cpp @@ -1074,9 +1074,9 @@ template std::atomic* ScanLocalState::shared_scan_limit_ptr() { auto& parent = _parent->cast(); // Decide from the SQL LIMIT, not from the counter. -1 means "no SQL LIMIT" — return nullptr - // so callers naturally skip all limit logic. The counter itself can be negative for a moment - // while a scanner holds charged padding rows that it refunds when its call returns, and a - // scanner initialized at that moment must still observe and charge the shared LIMIT. + // so callers naturally skip all limit logic. Concurrent scanners can return more than the + // LIMIT before the operator makes its final cut, leaving this counter negative. A scanner + // initialized then must still observe that the shared LIMIT is exhausted. return parent.limit() < 0 ? nullptr : &parent._shared_scan_limit; } diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index 1f55cb7d4adebf..845e4cca6b0f77 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -106,57 +106,70 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool // before the next split is read. Padding those blocks first would make correctness // depend on whether two file tails happen to share one output batch. RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); - return _do_projections(&_origin_block, block); + } else { + RETURN_IF_ERROR(_get_block_with_padding(state, row_descriptor.num_slots(), eos)); } - // get_block() charges the shared LIMIT as soon as rows pass the filters, but rows that stay - // in _padding_block when this call returns are not returned by it. Peer scanners may - // exhaust the LIMIT before this scanner runs again, and the context then finishes without - // running it, so those rows would be charged but never returned. Keep the counter equal - // to "LIMIT minus returned rows" between calls: charge the held rows again on entry, so - // every row this scanner holds during the call is charged and the read loop below stops - // as soon as the LIMIT is reached, and refund whatever is still held on exit. - if (_shared_scan_limit && !_padding_block.empty()) { - _shared_scan_limit->fetch_sub(_padding_block.rows(), std::memory_order_acq_rel); - } - const auto min_batch_size = std::max(state->batch_size() / 2, 1); - const auto block_max_bytes = state->preferred_block_size_bytes(); - while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && - !*eos) { - RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); - if (*eos) { - // For the final block, merge any padding directly and return eos in this call. - // The merged tail can be larger than the target batch, but each source block is - // already bounded by the lower scanner. - RETURN_IF_ERROR(_merge_padding_block()); - _origin_block.clear_column_data(row_descriptor.num_slots()); - break; - } - if (_origin_block.rows() >= min_batch_size) { - break; - } + RETURN_IF_ERROR(_do_projections(&_origin_block, block)); + } else { + RETURN_IF_ERROR(get_block(state, block, eos)); + } - if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && - _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { - RETURN_IF_ERROR(_merge_padding_block()); - _origin_block.clear_column_data(row_descriptor.num_slots()); - } else { - if (_origin_block.rows() < _padding_block.rows()) { - _padding_block.swap(_origin_block); - } - break; - } + // Only rows actually produced by this call consume the shared LIMIT. In particular, padding + // kept for a later call must not make the context finish before that call can return it. + if (_shared_scan_limit) { + if (block->rows() > 0) { + _shared_scan_limit->fetch_sub(cast_set(block->rows()), + std::memory_order_acq_rel); } + *eos = *eos || _shared_scan_limit->load(std::memory_order_acquire) <= 0; + } + return Status::OK(); +} - if (_origin_block.empty() && !_padding_block.empty()) { - _padding_block.swap(_origin_block); +// get_block() updates eos through this pointer; clang-tidy does not follow that call. +// NOLINTNEXTLINE(readability-non-const-parameter) +Status Scanner::_get_block_with_padding(RuntimeState* state, int64_t num_slots, bool* eos) { + const auto min_batch_size = std::max(state->batch_size() / 2, 1); + const auto block_max_bytes = state->preferred_block_size_bytes(); + while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && + !*eos) { + // With LIMIT 1, a single surviving row can sit in padding below the half-batch target. + // Return it now instead of scanning a long filtered tail to fill the batch. The shared + // counter tracks only blocks produced by scanners, not held padding. + if (_shared_scan_limit && !_padding_block.empty() && + cast_set(_padding_block.rows()) >= + _shared_scan_limit->load(std::memory_order_acquire)) { + break; } - if (_shared_scan_limit && !_padding_block.empty()) { - _shared_scan_limit->fetch_add(_padding_block.rows(), std::memory_order_acq_rel); + RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); + if (*eos) { + // For the final block, merge any padding directly and return eos in this call. + // The merged tail can be larger than the target batch, but each source block is + // already bounded by the lower scanner. + RETURN_IF_ERROR(_merge_padding_block()); + _origin_block.clear_column_data(num_slots); + break; } - return _do_projections(&_origin_block, block); - } else { - return get_block(state, block, eos); + if (_origin_block.rows() >= min_batch_size) { + break; + } + + if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && + _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { + RETURN_IF_ERROR(_merge_padding_block()); + _origin_block.clear_column_data(num_slots); + } else { + if (_origin_block.rows() < _padding_block.rows()) { + _padding_block.swap(_origin_block); + } + break; + } + } + + if (_origin_block.empty() && !_padding_block.empty()) { + _padding_block.swap(_origin_block); } + return Status::OK(); } Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { @@ -204,13 +217,6 @@ Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { } // record rows return (after filter) for _limit check _num_rows_return += block->rows(); - // Publish progress to the shared counter so peer scanners can - // observe it. The counter may go negative when several scanners - // subtract concurrently; that is harmless because the operator's - // reached_limit() makes the final cut. - if (_shared_scan_limit && block->rows() > 0) { - _shared_scan_limit->fetch_sub(block->rows(), std::memory_order_acq_rel); - } } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) && _num_rows_read < rows_read_threshold); } @@ -223,6 +229,8 @@ Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { // set eof to true if per scanner limit is reached // currently for query: ORDER BY key LIMIT n *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); + // A peer may have returned enough rows while this scanner was reading. The caller can still + // project the current block, and the scan operator makes the final LIMIT cut. *eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0); return Status::OK(); diff --git a/be/src/exec/scan/scanner.h b/be/src/exec/scan/scanner.h index 927472b2fa207c..10e66ded673d7b 100644 --- a/be/src/exec/scan/scanner.h +++ b/be/src/exec/scan/scanner.h @@ -140,6 +140,8 @@ class Scanner { return Status::OK(); } + Status _get_block_with_padding(RuntimeState* state, int64_t num_slots, bool* eos); + // Update the counters before closing this scanner virtual void _collect_profile_before_close(); diff --git a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp index 23f1f3f342c633..9334e42be145ea 100644 --- a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp +++ b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp @@ -895,10 +895,9 @@ TEST(ScannerProjectionTest, projects_incompatible_blocks_before_reading_the_next } // With parallel scanners, the scan context may finish as soon as the shared LIMIT counter is -// exhausted, without running a pending scanner again. get_block() charges rows as soon as they pass -// the filters, so rows that stay in the padding block when a call returns must be refunded, or they -// are charged but silently dropped. Between calls the counter equals LIMIT minus the returned rows. -TEST(ScannerProjectionTest, shared_limit_refunds_rows_held_in_padding) { +// exhausted. Count only the rows actually produced: after the first call, one row is still held +// in padding, so a peer returning four rows leaves one row for this scanner to produce. +TEST(ScannerProjectionTest, shared_limit_counts_only_emitted_rows) { ObjectPool pool; auto data_type = std::make_shared(); auto row_descriptor = MockRowDescriptor({data_type}, &pool); @@ -930,16 +929,21 @@ TEST(ScannerProjectionTest, shared_limit_refunds_rows_held_in_padding) { EXPECT_FALSE(eos); ASSERT_EQ(first_output.rows(), 5); EXPECT_EQ(first_output.get_by_position(0).column->get_int(0), 1); - // Both blocks were charged when they were read, but only the returned rows stay charged. + // Only the five returned rows consume the shared LIMIT; the held row does not. EXPECT_EQ(op->_shared_scan_limit.load(), 5); - // Peer scanners return the remaining rows of the LIMIT. Had the held row stayed charged, the - // counter would already be exhausted here and the context could finish without this scanner. - op->_shared_scan_limit.fetch_sub(4); + // A peer returns four more rows. Counting the held row before it is produced would let the + // context finish at nine output rows instead of scheduling this scanner again. + TestScanner peer(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(peer.init(&state, {}).ok()); + peer.add_block(ColumnHelper::create_block({6, 7, 8, 9})); + Block peer_output; + bool peer_eos = false; + ASSERT_TRUE(peer.get_block_after_projects(&state, &peer_output, &peer_eos).ok()); + EXPECT_EQ(peer_output.rows(), 4); EXPECT_EQ(op->_shared_scan_limit.load(), 1); - // The held row is charged again on entry, which exhausts the counter, so it is flushed with eos - // instead of reading on. + // The held row already satisfies the one remaining row, so flush it without reading on. Block final_output; ASSERT_TRUE(scanner.get_block_after_projects(&state, &final_output, &eos).ok()); EXPECT_TRUE(eos); @@ -948,6 +952,187 @@ TEST(ScannerProjectionTest, shared_limit_refunds_rows_held_in_padding) { EXPECT_EQ(op->_shared_scan_limit.load(), 0); } +// Padding can be discarded when a peer has already produced the whole LIMIT. It never consumed +// shared credit, so skipping the scanner cannot leave the operator short of rows. +TEST(ScannerProjectionTest, shared_limit_drops_padding_after_peer_satisfies_limit) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(10); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0})); + scanner.add_block(ColumnHelper::create_block({1, 2, 3, 4, 5})); + Block first_output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &first_output, &eos).ok()); + ASSERT_EQ(first_output.rows(), 5); + ASSERT_EQ(op->_shared_scan_limit.load(), 5); + + TestScanner peer(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(peer.init(&state, {}).ok()); + peer.add_block(ColumnHelper::create_block({6, 7, 8, 9, 10})); + Block peer_output; + bool peer_eos = false; + ASSERT_TRUE(peer.get_block_after_projects(&state, &peer_output, &peer_eos).ok()); + ASSERT_EQ(peer_output.rows(), 5); + ASSERT_EQ(op->_shared_scan_limit.load(), 0); + + Block skipped_output; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &skipped_output, &eos).ok()); + EXPECT_TRUE(eos); + EXPECT_EQ(skipped_output.rows(), 0); + EXPECT_EQ(op->_shared_scan_limit.load(), 0); +} + +// LIMIT 1 must not turn padding into a long scan through a selective tail just to reach half a +// batch. The first surviving row is returned and charged before the next source block is read. +TEST(ScannerProjectionTest, shared_limit_flushes_padding_before_reading_more) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(1); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0})); + scanner.add_block(ColumnHelper::create_block({1, 2, 3, 4, 5})); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + ASSERT_EQ(output.rows(), 1); + EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0); + EXPECT_EQ(scanner.unread_blocks(), 1); + EXPECT_EQ(op->_shared_scan_limit.load(), 0); +} + +// Several small source blocks can satisfy the LIMIT together without giving up the normal +// small-block merge or scanning one more source block to reach the half-batch target. +TEST(ScannerProjectionTest, shared_limit_flushes_merged_padding_at_limit) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(2); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0})); + scanner.add_block(ColumnHelper::create_block({1})); + scanner.add_block(ColumnHelper::create_block({2, 3, 4, 5})); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + ASSERT_EQ(output.rows(), 2); + EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0); + EXPECT_EQ(output.get_by_position(0).column->get_int(1), 1); + EXPECT_EQ(scanner.unread_blocks(), 1); + EXPECT_EQ(op->_shared_scan_limit.load(), 0); +} + +// The no-projection path must charge the returned block once as well; it does not use padding. +TEST(ScannerProjectionTest, shared_limit_counts_returned_rows_without_projection) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + op->_output_tuple_desc = row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(2); + + auto local_state = std::make_shared(&state, op.get()); + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0, 1, 2})); + scanner.add_block(ColumnHelper::create_block({3})); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + EXPECT_EQ(output.rows(), 3); + EXPECT_EQ(scanner.unread_blocks(), 1); + EXPECT_EQ(op->_shared_scan_limit.load(), -1); +} + +// File-local projections that cannot pad still use the same output-only shared accounting. +TEST(ScannerProjectionTest, shared_limit_counts_returned_rows_without_padding) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(2); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile, false); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + scanner.add_block(ColumnHelper::create_block({0, 1, 2})); + scanner.add_block(ColumnHelper::create_block({3})); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_TRUE(eos); + EXPECT_EQ(output.rows(), 3); + EXPECT_EQ(scanner.unread_blocks(), 1); + EXPECT_EQ(op->_shared_scan_limit.load(), -1); +} + // A selective scan under a large shared LIMIT still coalesces small blocks: one call reads several // source blocks and returns them as one block, instead of handing every surviving row to the // operator alone. @@ -995,9 +1180,8 @@ TEST(ScannerProjectionTest, shared_limit_keeps_padding_small_blocks) { EXPECT_EQ(op->_shared_scan_limit.load(), 94); } -// A scanner initialized while peers hold the counter below zero still observes the shared LIMIT. -// The counter dips below zero for a moment when a scanner charges its held padding rows and reads -// on while peers charge their own rows, and it is restored when the padding rows are refunded. +// Concurrent scanners may produce more than the shared LIMIT before the operator cuts their +// output. A scanner initialized after that must still retain the pointer and stop without reading. TEST(ScannerProjectionTest, shared_limit_observed_by_scanner_initialized_under_negative_counter) { ObjectPool pool; auto data_type = std::make_shared(); @@ -1012,7 +1196,7 @@ TEST(ScannerProjectionTest, shared_limit_observed_by_scanner_initialized_under_n MockRowDescriptor(std::vector {data_type}, &pool)); op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; op->set_limit_for_test(8); - // Peers charged 9 rows in total while one of them still holds 2 of them in its padding block. + // Peers produced nine rows for LIMIT 8 before the operator made its final cut. op->_shared_scan_limit.store(-1); auto local_state = std::make_shared(&state, op.get()); @@ -1023,20 +1207,15 @@ TEST(ScannerProjectionTest, shared_limit_observed_by_scanner_initialized_under_n ASSERT_TRUE(scanner.init(&state, {}).ok()); ASSERT_EQ(scanner._shared_scan_limit, &op->_shared_scan_limit); - // The holder refunds its 2 padding rows: 1 row of the LIMIT is still missing. - op->_shared_scan_limit.fetch_add(2); - ASSERT_EQ(op->_shared_scan_limit.load(), 1); scanner.add_block(ColumnHelper::create_block({0, 1, 2})); - scanner.add_block(ColumnHelper::create_block({3, 4, 5})); - // The first block satisfies the LIMIT, so the scanner charges it and stops without reading on. Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); EXPECT_TRUE(eos); - EXPECT_EQ(output.rows(), 3); - EXPECT_EQ(op->_shared_scan_limit.load(), -2); - EXPECT_EQ(scanner._blocks.size(), 1); + EXPECT_EQ(output.rows(), 0); + EXPECT_EQ(op->_shared_scan_limit.load(), -1); + EXPECT_EQ(scanner.unread_blocks(), 1); } // Without a SQL LIMIT the scanner gets no counter, whatever the counter holds. From ff4dfa39da6072d633ff39e9af3410458afcb6e2 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Wed, 14 Oct 2026 18:18:21 +0800 Subject: [PATCH 6/9] [refactor](be) Cap scan batch target by remaining LIMIT ### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: The projected-scanner padding loop separately compared held rows with the shared LIMIT on each iteration to stop reading once enough rows were collected. Derive the batch target once from the shared remaining LIMIT before the loop instead. The shared counter is still decremented only for the block actually produced; concurrent peer updates may make this snapshot stale, but the scan operator retains the final LIMIT cut. ### Release note None ### Check List (For Author) - Test: Unit Test: 66 focused BE tests passed. Formatting, build hygiene, and diff checks passed. Clang-tidy was attempted; it still reports a pre-existing scanner constructor warning and the toolchain missing stddef.h. - Behavior changed: Yes (the padding batch target is capped by a one-time shared remaining-LIMIT snapshot) - Does this need documentation: No --- be/src/exec/scan/scanner.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index 845e4cca6b0f77..04d9650dca2778 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -19,6 +19,8 @@ #include +#include + #include "common/config.h" #include "common/status.h" #include "core/block/column_with_type_and_name.h" @@ -129,18 +131,16 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool // get_block() updates eos through this pointer; clang-tidy does not follow that call. // NOLINTNEXTLINE(readability-non-const-parameter) Status Scanner::_get_block_with_padding(RuntimeState* state, int64_t num_slots, bool* eos) { - const auto min_batch_size = std::max(state->batch_size() / 2, 1); + auto min_batch_size = std::max(state->batch_size() / 2, 1); + if (_shared_scan_limit) { + // Pending padding is not charged until it is returned. Bound this call's batch target by + // the shared remaining rows so a small LIMIT does not read on just to fill half a batch. + min_batch_size = cast_set(std::clamp( + _shared_scan_limit->load(std::memory_order_acquire), 0, min_batch_size)); + } const auto block_max_bytes = state->preferred_block_size_bytes(); while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && !*eos) { - // With LIMIT 1, a single surviving row can sit in padding below the half-batch target. - // Return it now instead of scanning a long filtered tail to fill the batch. The shared - // counter tracks only blocks produced by scanners, not held padding. - if (_shared_scan_limit && !_padding_block.empty() && - cast_set(_padding_block.rows()) >= - _shared_scan_limit->load(std::memory_order_acquire)) { - break; - } RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); if (*eos) { // For the final block, merge any padding directly and return eos in this call. From 1b47b99121f3bc5731da5dea386be12a64e9acc8 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Wed, 14 Oct 2026 18:31:01 +0800 Subject: [PATCH 7/9] [refactor](be) Keep scan padding loop inline ### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: The padding loop was extracted into a member function used only once, increasing the API and review surface without sharing logic. Put the loop back in get_block_after_projects while retaining output-only shared LIMIT accounting and the entry-time batch target derived from the remaining LIMIT. ### Release note None ### Check List (For Author) - Test: Unit Test: 66 focused BE tests passed. Formatting and build hygiene checks passed. Clang-tidy was attempted; remaining diagnostics are pre-existing warnings and a toolchain missing stddef.h. - Behavior changed: No (single-use helper inlined without changing scan results) - Does this need documentation: No --- be/src/exec/scan/scanner.cpp | 85 +++++++++++++++++------------------- be/src/exec/scan/scanner.h | 2 - 2 files changed, 40 insertions(+), 47 deletions(-) diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index 04d9650dca2778..8412b08c3d83a0 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -89,6 +89,8 @@ Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { return Status::OK(); } +// Keep the single-use padding loop inline with its projection and LIMIT accounting. +// NOLINTNEXTLINE(readability-function-cognitive-complexity) Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); // Peer scanners may have exhausted the shared LIMIT while this scanner waited to be @@ -109,7 +111,44 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool // depend on whether two file tails happen to share one output batch. RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); } else { - RETURN_IF_ERROR(_get_block_with_padding(state, row_descriptor.num_slots(), eos)); + auto min_batch_size = std::max(state->batch_size() / 2, 1); + if (_shared_scan_limit) { + // Pending padding is not charged until it is returned. Bound this call's batch + // target by the shared remaining rows to avoid reading on just to fill a batch. + min_batch_size = cast_set(std::clamp( + _shared_scan_limit->load(std::memory_order_acquire), 0, min_batch_size)); + } + const auto block_max_bytes = state->preferred_block_size_bytes(); + while (_padding_block.rows() < min_batch_size && + _padding_block.bytes() < block_max_bytes && !*eos) { + RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); + if (*eos) { + // For the final block, merge any padding directly and return eos in this call. + // The merged tail can be larger than the target batch, but each source block + // is already bounded by the lower scanner. + RETURN_IF_ERROR(_merge_padding_block()); + _origin_block.clear_column_data(row_descriptor.num_slots()); + break; + } + if (_origin_block.rows() >= min_batch_size) { + break; + } + + if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && + _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { + RETURN_IF_ERROR(_merge_padding_block()); + _origin_block.clear_column_data(row_descriptor.num_slots()); + } else { + if (_origin_block.rows() < _padding_block.rows()) { + _padding_block.swap(_origin_block); + } + break; + } + } + + if (_origin_block.empty() && !_padding_block.empty()) { + _padding_block.swap(_origin_block); + } } RETURN_IF_ERROR(_do_projections(&_origin_block, block)); } else { @@ -128,50 +167,6 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool return Status::OK(); } -// get_block() updates eos through this pointer; clang-tidy does not follow that call. -// NOLINTNEXTLINE(readability-non-const-parameter) -Status Scanner::_get_block_with_padding(RuntimeState* state, int64_t num_slots, bool* eos) { - auto min_batch_size = std::max(state->batch_size() / 2, 1); - if (_shared_scan_limit) { - // Pending padding is not charged until it is returned. Bound this call's batch target by - // the shared remaining rows so a small LIMIT does not read on just to fill half a batch. - min_batch_size = cast_set(std::clamp( - _shared_scan_limit->load(std::memory_order_acquire), 0, min_batch_size)); - } - const auto block_max_bytes = state->preferred_block_size_bytes(); - while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && - !*eos) { - RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); - if (*eos) { - // For the final block, merge any padding directly and return eos in this call. - // The merged tail can be larger than the target batch, but each source block is - // already bounded by the lower scanner. - RETURN_IF_ERROR(_merge_padding_block()); - _origin_block.clear_column_data(num_slots); - break; - } - if (_origin_block.rows() >= min_batch_size) { - break; - } - - if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && - _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { - RETURN_IF_ERROR(_merge_padding_block()); - _origin_block.clear_column_data(num_slots); - } else { - if (_origin_block.rows() < _padding_block.rows()) { - _padding_block.swap(_origin_block); - } - break; - } - } - - if (_origin_block.empty() && !_padding_block.empty()) { - _padding_block.swap(_origin_block); - } - return Status::OK(); -} - Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { // only empty block should be here DCHECK(block->rows() == 0); diff --git a/be/src/exec/scan/scanner.h b/be/src/exec/scan/scanner.h index 10e66ded673d7b..927472b2fa207c 100644 --- a/be/src/exec/scan/scanner.h +++ b/be/src/exec/scan/scanner.h @@ -140,8 +140,6 @@ class Scanner { return Status::OK(); } - Status _get_block_with_padding(RuntimeState* state, int64_t num_slots, bool* eos); - // Update the counters before closing this scanner virtual void _collect_profile_before_close(); From e275f858f1dd4e12faa754e5b897fd3ba3b688b0 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Thu, 15 Oct 2026 15:51:38 +0800 Subject: [PATCH 8/9] [fix](be) Leave shared scan LIMIT EOS to the scan operator ### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: Scanner::get_block_after_projects() and Scanner::get_block() turned an exhausted shared scan LIMIT into EOS: a scanner scheduled after its peers had produced the whole LIMIT, or whose own rows exhausted it, reported EOS although its reader still had data. EOS means the underlying read is exhausted, while the LIMIT belongs to the scan operator, which cuts the extra rows and stops the scanners once it holds enough. Deriving EOS from the LIMIT bookkeeping means any mistake there ends a scan early and returns too few rows, instead of, at worst, letting a scanner run until the operator stops it. The scanner now stops reading when the shared LIMIT is exhausted but never reports EOS for it: get_block_after_projects() returns an empty block on entry, the padding loop stops reading and returns the rows it already holds, and the tail only charges the produced rows. The two pre-existing counter checks in get_block() are removed so that the shared LIMIT is handled in one place. The query still finishes as before: ScannerContext::get_block_from_queue() completes the context once the counter is exhausted with nothing in flight, and ScanOperatorX::get_block_impl() stops the scanners after reached_limit(). ### Release note None ### Check List (For Author) - Test: Unit Test: ScannerProjectionTest cases updated to expect no EOS from the shared LIMIT, plus two new cases (own rows exhaust the LIMIT, a peer exhausts it mid-read); ran the scan/workload-group/row-ID UT filter. Regression test: query_p0/limit and correctness_p0/test_shared_scan_limit_pending_tasks on a local cluster with the thread pool scheduler default. - Behavior changed: Yes (scanners no longer report EOS for an exhausted shared scan LIMIT; the scan operator stops them) - Does this need documentation: No --- be/src/exec/scan/scanner.cpp | 34 +++---- be/src/exec/scan/scanner.h | 9 ++ be/src/exec/scan/scanner_context.cpp | 3 +- be/test/exec/scan/scanner_context_test.cpp | 7 +- .../scan/scanner_late_arrival_rf_test.cpp | 98 +++++++++++++++---- 5 files changed, 108 insertions(+), 43 deletions(-) diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index 8412b08c3d83a0..8e4f6487cb0093 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -94,10 +94,10 @@ Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); // Peer scanners may have exhausted the shared LIMIT while this scanner waited to be - // scheduled. Admission does not look at the LIMIT, so the scanner reports EOS itself with an - // empty block instead of reading on; the operator already has enough rows and cuts the rest. - if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) { - *eos = true; + // scheduled. Admission does not look at the LIMIT, so the scanner returns an empty block + // instead of reading on. It does not report EOS: the operator already holds enough rows and + // stops the scanners after cutting the LIMIT itself. + if (_shared_scan_limit_exhausted()) { return Status::OK(); } const auto& row_descriptor = _local_state->_parent->operator_row_desc_before_projection(); @@ -119,8 +119,11 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool _shared_scan_limit->load(std::memory_order_acquire), 0, min_batch_size)); } const auto block_max_bytes = state->preferred_block_size_bytes(); + // Stop reading as soon as peers exhaust the shared LIMIT; the rows already held are + // still returned below. while (_padding_block.rows() < min_batch_size && - _padding_block.bytes() < block_max_bytes && !*eos) { + _padding_block.bytes() < block_max_bytes && !*eos && + !_shared_scan_limit_exhausted()) { RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); if (*eos) { // For the final block, merge any padding directly and return eos in this call. @@ -157,12 +160,10 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool // Only rows actually produced by this call consume the shared LIMIT. In particular, padding // kept for a later call must not make the context finish before that call can return it. - if (_shared_scan_limit) { - if (block->rows() > 0) { - _shared_scan_limit->fetch_sub(cast_set(block->rows()), - std::memory_order_acq_rel); - } - *eos = *eos || _shared_scan_limit->load(std::memory_order_acquire) <= 0; + // Exhausting the counter is not EOS either: the next call returns an empty block, and the + // scan operator stops this scanner once it has cut the LIMIT. + if (_shared_scan_limit != nullptr && block->rows() > 0) { + _shared_scan_limit->fetch_sub(cast_set(block->rows()), std::memory_order_acq_rel); } return Status::OK(); } @@ -171,14 +172,6 @@ Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { // only empty block should be here DCHECK(block->rows() == 0); - // Stop early if other scanners have already collected enough rows - // for the SQL LIMIT. Skipped when _shared_scan_limit is null (topn - // path or no LIMIT). - if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) { - *eof = true; - return Status::OK(); - } - // scanner running time SCOPED_RAW_TIMER(&_per_scanner_timer); int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num; @@ -224,9 +217,6 @@ Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { // set eof to true if per scanner limit is reached // currently for query: ORDER BY key LIMIT n *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); - // A peer may have returned enough rows while this scanner was reading. The caller can still - // project the current block, and the scan operator makes the final LIMIT cut. - *eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0); return Status::OK(); } diff --git a/be/src/exec/scan/scanner.h b/be/src/exec/scan/scanner.h index 927472b2fa207c..dfed572406eb7e 100644 --- a/be/src/exec/scan/scanner.h +++ b/be/src/exec/scan/scanner.h @@ -140,6 +140,15 @@ class Scanner { return Status::OK(); } + // True once scanners of this operator have produced the whole shared LIMIT. The scanner then + // stops reading but does not report EOS for it: EOS means the underlying read is exhausted, + // while the LIMIT is applied by the scan operator, which stops the scanners once it holds + // enough rows. + bool _shared_scan_limit_exhausted() const { + return _shared_scan_limit != nullptr && + _shared_scan_limit->load(std::memory_order_acquire) <= 0; + } + // Update the counters before closing this scanner virtual void _collect_profile_before_close(); diff --git a/be/src/exec/scan/scanner_context.cpp b/be/src/exec/scan/scanner_context.cpp index 944cef09d8dc35..4c229989876ab3 100644 --- a/be/src/exec/scan/scanner_context.cpp +++ b/be/src/exec/scan/scanner_context.cpp @@ -866,7 +866,8 @@ std::shared_ptr ScannerContext::_pull_next_scan_task( if (!_pending_tasks.empty()) { // Do not submit more pending scanners after the shared LIMIT is exhausted while // completed or in-flight tasks can still make progress. If neither exists, allow pending - // scanners to be submitted so they can report EOS and wake the pipeline task. + // scanners to be submitted so they can complete without reading and wake the pipeline + // task, which finishes the context once nothing is in flight. if (_is_shared_scan_limit_exhausted() && (_in_flight_tasks_num != 0 || !_completed_tasks.empty())) { return nullptr; diff --git a/be/test/exec/scan/scanner_context_test.cpp b/be/test/exec/scan/scanner_context_test.cpp index 0c0ba91435fade..62650ff445a33d 100644 --- a/be/test/exec/scan/scanner_context_test.cpp +++ b/be/test/exec/scan/scanner_context_test.cpp @@ -748,9 +748,10 @@ TEST_F(ScannerContextTest, thread_pool_admission_ignores_shared_limit) { // The shared LIMIT is exhausted while a task is in flight and a completed result waits for the // operator. Admission only applies the concurrency limit: the pending scanner is still - // admitted and reports EOS itself in Scanner::get_block_after_projects(). Holding it back - // based on the in-flight or completed tasks is what can leave a context with nothing to - // wake the operator. + // admitted, and Scanner::get_block_after_projects() returns an empty block without reading + // (and without EOS, which the scan operator decides from the LIMIT). Holding it back based on + // the in-flight or completed tasks is what can leave a context with nothing to wake the + // operator. shared_limit.store(0); scanner_context->_in_flight_tasks_num = 1; auto completed_task = std::make_shared(std::make_shared(scanner)); diff --git a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp index 9334e42be145ea..5989ce1e7f0c4e 100644 --- a/be/test/exec/scan/scanner_late_arrival_rf_test.cpp +++ b/be/test/exec/scan/scanner_late_arrival_rf_test.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -63,6 +64,8 @@ class TestScanner final : public Scanner { void add_block(Block block) { _blocks.push_back(std::move(block)); } size_t unread_blocks() const { return _blocks.size(); } + // Runs after each source block is handed out, to model a peer scanner acting mid-read. + void set_after_read_hook(std::function hook) { _after_read_hook = std::move(hook); } protected: bool _can_merge_padding_blocks(const Block& /*left*/, const Block& /*right*/) const override { @@ -77,12 +80,16 @@ class TestScanner final : public Scanner { *eof = false; block->swap(_blocks.front()); _blocks.pop_front(); + if (_after_read_hook) { + _after_read_hook(); + } return Status::OK(); } private: bool _allow_padding = true; std::list _blocks; + std::function _after_read_hook; }; class FakeTablet final : public BaseTablet { @@ -741,9 +748,9 @@ TEST_F(ScannerLateArrivalRfTest, olap_scanner_releases_resources_before_open) { } // Admission does not look at the shared LIMIT, so a pending scanner is still scheduled after its -// peers have exhausted it. The scanner reports EOS with an empty block on that call instead of -// reading its range. -TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_reports_eos_without_reading) { +// peers have exhausted it. The scanner returns an empty block on that call instead of reading its +// range, and does not report EOS: the scan operator decides that from the LIMIT. +TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_returns_empty_block_without_eos) { ObjectPool pool; auto data_type = std::make_shared(); auto row_descriptor = MockRowDescriptor({data_type}, &pool); @@ -773,7 +780,7 @@ TEST(ScannerProjectionTest, shared_limit_exhausted_before_call_reports_eos_witho Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); EXPECT_EQ(output.rows(), 0); EXPECT_EQ(scanner.unread_blocks(), 2); EXPECT_EQ(op->_shared_scan_limit.load(), 0); @@ -943,17 +950,19 @@ TEST(ScannerProjectionTest, shared_limit_counts_only_emitted_rows) { EXPECT_EQ(peer_output.rows(), 4); EXPECT_EQ(op->_shared_scan_limit.load(), 1); - // The held row already satisfies the one remaining row, so flush it without reading on. + // The held row already satisfies the one remaining row, so flush it without reading on. The + // reader still has no EOS to report; the operator stops the scanner after cutting the LIMIT. Block final_output; ASSERT_TRUE(scanner.get_block_after_projects(&state, &final_output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); ASSERT_EQ(final_output.rows(), 1); EXPECT_EQ(final_output.get_by_position(0).column->get_int(0), 0); EXPECT_EQ(op->_shared_scan_limit.load(), 0); } -// Padding can be discarded when a peer has already produced the whole LIMIT. It never consumed -// shared credit, so skipping the scanner cannot leave the operator short of rows. +// Padding stays unreturned when a peer has already produced the whole LIMIT. It never consumed +// shared credit, so skipping it cannot leave the operator short of rows, and the scanner leaves +// EOS to the operator's LIMIT cut. TEST(ScannerProjectionTest, shared_limit_drops_padding_after_peer_satisfies_limit) { ObjectPool pool; auto data_type = std::make_shared(); @@ -994,7 +1003,7 @@ TEST(ScannerProjectionTest, shared_limit_drops_padding_after_peer_satisfies_limi Block skipped_output; ASSERT_TRUE(scanner.get_block_after_projects(&state, &skipped_output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); EXPECT_EQ(skipped_output.rows(), 0); EXPECT_EQ(op->_shared_scan_limit.load(), 0); } @@ -1028,7 +1037,7 @@ TEST(ScannerProjectionTest, shared_limit_flushes_padding_before_reading_more) { Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); ASSERT_EQ(output.rows(), 1); EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0); EXPECT_EQ(scanner.unread_blocks(), 1); @@ -1065,7 +1074,7 @@ TEST(ScannerProjectionTest, shared_limit_flushes_merged_padding_at_limit) { Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); ASSERT_EQ(output.rows(), 2); EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0); EXPECT_EQ(output.get_by_position(0).column->get_int(1), 1); @@ -1095,7 +1104,7 @@ TEST(ScannerProjectionTest, shared_limit_counts_returned_rows_without_projection Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); EXPECT_EQ(output.rows(), 3); EXPECT_EQ(scanner.unread_blocks(), 1); EXPECT_EQ(op->_shared_scan_limit.load(), -1); @@ -1127,7 +1136,7 @@ TEST(ScannerProjectionTest, shared_limit_counts_returned_rows_without_padding) { Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); EXPECT_EQ(output.rows(), 3); EXPECT_EQ(scanner.unread_blocks(), 1); EXPECT_EQ(op->_shared_scan_limit.load(), -1); @@ -1212,7 +1221,7 @@ TEST(ScannerProjectionTest, shared_limit_observed_by_scanner_initialized_under_n Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); EXPECT_EQ(output.rows(), 0); EXPECT_EQ(op->_shared_scan_limit.load(), -1); EXPECT_EQ(scanner.unread_blocks(), 1); @@ -1242,8 +1251,10 @@ TEST(ScannerProjectionTest, no_shared_limit_without_sql_limit) { EXPECT_EQ(scanner._shared_scan_limit, nullptr); } -// The scanner stops once its rows exhaust the shared LIMIT, even though its reader still has data. -TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_it) { +// Once its own rows exhaust the shared LIMIT the scanner stops reading, even though its reader +// still has data. It does not report EOS for that: the next call returns an empty block and the +// scan operator stops the scanner after cutting the LIMIT. +TEST(ScannerProjectionTest, shared_limit_exhausted_by_own_rows_stops_reading_without_eos) { ObjectPool pool; auto data_type = std::make_shared(); auto row_descriptor = MockRowDescriptor({data_type}, &pool); @@ -1270,9 +1281,62 @@ TEST(ScannerProjectionTest, shared_limit_reports_eos_when_scanner_rows_exhaust_i Block output; bool eos = false; ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); - EXPECT_TRUE(eos); + EXPECT_FALSE(eos); EXPECT_EQ(output.rows(), 5); + EXPECT_EQ(scanner.unread_blocks(), 1); + EXPECT_EQ(op->_shared_scan_limit.load(), -2); + + Block next_output; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &next_output, &eos).ok()); + EXPECT_FALSE(eos); + EXPECT_EQ(next_output.rows(), 0); + EXPECT_EQ(scanner.unread_blocks(), 1); EXPECT_EQ(op->_shared_scan_limit.load(), -2); } +// A peer may exhaust the shared LIMIT while this scanner is still padding small blocks. The read +// loop stops at once, the rows already held are returned, and EOS is again left to the operator. +TEST(ScannerProjectionTest, shared_limit_exhausted_by_peer_mid_read_returns_held_rows_without_eos) { + ObjectPool pool; + auto data_type = std::make_shared(); + auto row_descriptor = MockRowDescriptor({data_type}, &pool); + + MockRuntimeState state; + state._batch_size = 8; + + auto op = std::make_shared(); + op->_row_descriptor = row_descriptor; + auto& projection = op->set_projection_for_test( + MockRowDescriptor(std::vector {data_type}, &pool)); + op->_output_tuple_desc = projection.output_row_descriptor.tuple_descriptors()[0]; + op->set_limit_for_test(10); + + auto local_state = std::make_shared(&state, op.get()); + local_state->_projections = MockSlotRef::create_mock_contexts(0, data_type); + + RuntimeProfile profile("scanner"); + TestScanner scanner(&state, local_state.get(), -1, &profile); + ASSERT_TRUE(scanner.init(&state, {}).ok()); + for (int i = 0; i < 5; ++i) { + scanner.add_block(ColumnHelper::create_block({i})); + } + // A peer produces the whole LIMIT right after this scanner reads its first block. + bool peer_done = false; + scanner.set_after_read_hook([&] { + if (!peer_done) { + peer_done = true; + op->_shared_scan_limit.fetch_sub(10); + } + }); + + Block output; + bool eos = false; + ASSERT_TRUE(scanner.get_block_after_projects(&state, &output, &eos).ok()); + EXPECT_FALSE(eos); + ASSERT_EQ(output.rows(), 1); + EXPECT_EQ(output.get_by_position(0).column->get_int(0), 0); + EXPECT_EQ(scanner.unread_blocks(), 4); + EXPECT_EQ(op->_shared_scan_limit.load(), -1); +} + } // namespace doris From 62afc6b03b7e55ba938eec1a5e13c2cad1cd8844 Mon Sep 17 00:00:00 2001 From: Hu Shenggang Date: Thu, 15 Oct 2026 16:54:17 +0800 Subject: [PATCH 9/9] [fix](be) Park scanners after the shared scan LIMIT is exhausted ### What problem does this PR solve? Issue Number: None Related PR: #68610 Problem Summary: Since the scanner no longer reports EOS for an exhausted shared scan LIMIT, a scanner that is scheduled after its peers produced the whole LIMIT completes with an empty block and eos == false. ScannerContext::get_block_from_queue() treated such a task like any consumed non-EOS result and resubmitted it. With parallel scanning, another instance can exhaust the operator-wide counter while this context still has a scanner blocked in I/O, so the empty scanner was admitted, completed empty and resubmitted again for as long as that I/O lasted, burning scanner and pipeline CPU for a LIMIT that was already satisfied. Both schedulers resubmit from this one call: the ThreadPool path queues the task and a Context runnable whose admission ignores the LIMIT by design, and the TaskExecutor path returns the current task from _pull_next_scan_task() unconditionally. get_block_from_queue() now parks a consumed non-EOS task in the pending queue without rescheduling it when the shared LIMIT is exhausted. The branch is only reached when the finish check did not fire, so a completed or in-flight task remains to wake the operator, and the context finishes once the counter is exhausted with nothing in flight, as before. The parked scanner never runs again; the scanner itself still only stops reading and never reports EOS for the LIMIT. Also fix the teardown order in RowIdStorageReaderTest.RpcHelperReportsThrownExceptionOnThreadPoolPath: the response and the latch captured by the pool worker are declared before the scheduler, so a failed wait joins the worker in the stop guard before either captured object is destroyed. ### Release note None ### Check List (For Author) - Test: Unit Test: new ScannerContextTest.exhausted_shared_limit_parks_empty_task_until_in_flight_drains (counter at 0, one task in flight, an empty non-EOS completion is parked, the scheduler is never asked to resubmit it, and the context finishes when the in-flight task drains); ran the scan/workload-group/row-ID UT filter (142 tests). Regression test: query_p0/limit and correctness_p0/test_shared_scan_limit_pending_tasks on a local cluster with the thread pool scheduler default. - Behavior changed: Yes (an admitted scanner is not resubmitted after the shared scan LIMIT is exhausted while other work is outstanding) - Does this need documentation: No --- be/src/exec/scan/scanner.cpp | 3 +- be/src/exec/scan/scanner_context.cpp | 6 ++ be/test/exec/rowid_fetcher_test.cpp | 7 +- be/test/exec/scan/scanner_context_test.cpp | 80 ++++++++++++++++++++++ 4 files changed, 93 insertions(+), 3 deletions(-) diff --git a/be/src/exec/scan/scanner.cpp b/be/src/exec/scan/scanner.cpp index 8e4f6487cb0093..60b0ada453647e 100644 --- a/be/src/exec/scan/scanner.cpp +++ b/be/src/exec/scan/scanner.cpp @@ -96,7 +96,8 @@ Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool // Peer scanners may have exhausted the shared LIMIT while this scanner waited to be // scheduled. Admission does not look at the LIMIT, so the scanner returns an empty block // instead of reading on. It does not report EOS: the operator already holds enough rows and - // stops the scanners after cutting the LIMIT itself. + // stops the scanners after cutting the LIMIT itself, and ScannerContext parks the scanner + // rather than resubmitting it while the outstanding work drains. if (_shared_scan_limit_exhausted()) { return Status::OK(); } diff --git a/be/src/exec/scan/scanner_context.cpp b/be/src/exec/scan/scanner_context.cpp index 4c229989876ab3..0452a53f4504b9 100644 --- a/be/src/exec/scan/scanner_context.cpp +++ b/be/src/exec/scan/scanner_context.cpp @@ -418,6 +418,12 @@ Status ScannerContext::get_block_from_queue(RuntimeState* state, Block* block, b (_is_shared_scan_limit_exhausted() && _in_flight_tasks_num == 0))) { _set_scanner_done(); _is_finished = true; + } else if (scan_task != nullptr && !scan_task->is_eos() && _is_shared_scan_limit_exhausted()) { + // Scanners of this operator have already produced the whole shared LIMIT, so this scanner + // would only return empty blocks until the outstanding work drains. Park it instead of + // resubmitting it. The finish check above did not fire, so a completed or in-flight task + // remains to wake the operator, and the context finishes once nothing is in flight. + push_pending_scan_task(std::move(scan_task), l); } else if (scan_task != nullptr) { // Check the terminal state before scheduling more work. In particular, after the shared // LIMIT is exhausted, submitting another Context runnable can turn a completed query into diff --git a/be/test/exec/rowid_fetcher_test.cpp b/be/test/exec/rowid_fetcher_test.cpp index df96ec73da3b91..dcd9e7483ef13e 100644 --- a/be/test/exec/rowid_fetcher_test.cpp +++ b/be/test/exec/rowid_fetcher_test.cpp @@ -473,13 +473,16 @@ TEST_F(RowIdStorageReaderTest, RpcHelperReportsThrownExceptionOnThreadPoolPath) // A type node kind unknown to this BE, as a newer FE could send. slot->mutable_slot_type()->add_types()->set_type(99); + // The worker captures the response and the latch by reference. Declare them before the + // scheduler so every exit, including a failed wait below, joins the worker in stop_scheduler + // before either captured object is destroyed. + PMultiGetResponseV2 response; + CountDownLatch finished(1); std::shared_ptr cgroup_cpu_ctl = std::make_shared(1); ThreadPoolSimplifiedScanScheduler scheduler("rowid_rpc_test", cgroup_cpu_ctl); ASSERT_TRUE(scheduler.start(1, 1, 4, 1).ok()); Defer stop_scheduler {[&] { scheduler.stop(); }}; - PMultiGetResponseV2 response; - CountDownLatch finished(1); ASSERT_TRUE(scheduler .submit_scan_task(SimplifiedScanTask( [&]() { diff --git a/be/test/exec/scan/scanner_context_test.cpp b/be/test/exec/scan/scanner_context_test.cpp index 62650ff445a33d..4105d8f4a876b0 100644 --- a/be/test/exec/scan/scanner_context_test.cpp +++ b/be/test/exec/scan/scanner_context_test.cpp @@ -765,6 +765,86 @@ TEST_F(ScannerContextTest, thread_pool_admission_ignores_shared_limit) { EXPECT_TRUE(scanner_context->_pending_tasks.empty()); } +TEST_F(ScannerContextTest, exhausted_shared_limit_parks_empty_task_until_in_flight_drains) { + const int parallel_tasks = 2; + const int64_t limit = 10; + auto scan_operator = std::make_unique(obj_pool.get(), tnode, 0, *descs, + parallel_tasks, TQueryCacheParam {}); + auto olap_scan_local_state = + OlapScanLocalState::create_unique(state.get(), scan_operator.get()); + + OlapScanner::Params scanner_params; + scanner_params.state = state.get(); + scanner_params.profile = profile.get(); + scanner_params.limit = limit; + scanner_params.key_ranges = std::vector(); + std::shared_ptr scanner = + OlapScanner::create_shared(olap_scan_local_state.get(), std::move(scanner_params)); + std::list> scanners { + std::make_shared(scanner), std::make_shared(scanner)}; + auto scanner_context = ScannerContext::create_shared( + state.get(), olap_scan_local_state.get(), output_tuple_desc, false, scanners, limit, + scan_dependency, &shared_limit, nullptr, nullptr, 0, false, parallel_tasks); + scanner_context->_newly_create_free_blocks_num = newly_create_free_blocks_num.get(); + scanner_context->_scanner_memory_used_counter = scanner_memory_used_counter.get(); + + // Both schedulers resubmit through schedule_scan_task(); an exhausted LIMIT must not reach it. + std::unique_ptr scheduler = + std::make_unique(cgroup_cpu_ctl); + EXPECT_CALL(*scheduler, schedule_scan_task(testing::_, testing::_, testing::_)).Times(0); + scanner_context->_scanner_scheduler = scheduler.get(); + + // Scanners of a parallel peer instance produced the whole operator-wide LIMIT while this + // context has scanner B blocked in I/O. Scanner A returned fewer rows than this instance's + // LIMIT and now completes with an empty block but without EOS, as the LIMIT is not EOS. + auto task_a = std::make_shared(scanners.front()); + auto task_b = std::make_shared(scanners.back()); + { + std::unique_lock transfer_lock(scanner_context->transfer_lock()); + scanner_context->_pending_tasks = std::stack>(); + shared_limit.store(0); + task_a->set_state(ScanTask::State::IN_FLIGHT); + task_b->set_state(ScanTask::State::IN_FLIGHT); + scanner_context->_in_flight_tasks_num = 2; + } + task_a->cached_block = Block::create_unique(); + task_a->set_state(ScanTask::State::COMPLETED); + scanner_context->push_completed_scan_task(task_a); + + // Consuming the empty result parks A instead of resubmitting it: A would only complete empty + // again for as long as B's I/O lasts. B's completion is what wakes the operator next. + Block block; + bool eos = false; + Status st = scanner_context->get_block_from_queue(state.get(), &block, &eos, 0); + ASSERT_TRUE(st.ok()) << st.to_string(); + EXPECT_FALSE(eos); + EXPECT_EQ(block.rows(), 0); + { + std::unique_lock transfer_lock(scanner_context->transfer_lock()); + EXPECT_FALSE(scanner_context->done()); + EXPECT_EQ(scanner_context->_in_flight_tasks_num, 1); + EXPECT_EQ(task_a->_state, ScanTask::State::PENDING); + ASSERT_EQ(scanner_context->_pending_tasks.size(), 1); + EXPECT_EQ(scanner_context->_pending_tasks.top(), task_a); + } + + // B drains with no more rows to add. Nothing is in flight anymore, so the context finishes + // without ever running A again. + task_b->cached_block = Block::create_unique(); + task_b->set_state(ScanTask::State::COMPLETED); + scanner_context->push_completed_scan_task(task_b); + st = scanner_context->get_block_from_queue(state.get(), &block, &eos, 0); + ASSERT_TRUE(st.ok()) << st.to_string(); + EXPECT_TRUE(eos); + { + std::unique_lock transfer_lock(scanner_context->transfer_lock()); + EXPECT_TRUE(scanner_context->_is_finished); + EXPECT_EQ(scanner_context->_in_flight_tasks_num, 0); + ASSERT_EQ(scanner_context->_pending_tasks.size(), 1); + EXPECT_EQ(scanner_context->_pending_tasks.top(), task_a); + } +} + TEST_F(ScannerContextTest, thread_pool_admission_refreshes_adaptive_limit) { const int parallel_tasks = 2; auto scan_operator = std::make_unique(obj_pool.get(), tnode, 0, *descs,