Skip to content
4 changes: 2 additions & 2 deletions be/src/common/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Comment thread
mrhhsg marked this conversation as resolved.
Resolved
Comment thread
mrhhsg marked this conversation as resolved.
Resolved

// number of scanner thread pool size for olap table
// and the min thread num of remote scanner thread pool
Expand Down
6 changes: 6 additions & 0 deletions be/src/exec/operator/mock_scan_operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class MockScanOperatorX final : public ScanOperatorX<MockScanLocalState> {
_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;
Expand Down
10 changes: 6 additions & 4 deletions be/src/exec/operator/scan_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1072,10 +1072,12 @@ int64_t ScanLocalState<Derived>::limit_per_scanner() {

template <typename Derived>
std::atomic<int64_t>* ScanLocalState<Derived>::shared_scan_limit_ptr() {
auto* p = &_parent->cast<typename Derived::Parent>()._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<typename Derived::Parent>();
// Decide from the SQL LIMIT, not from the counter. -1 means "no SQL LIMIT" — return nullptr
// 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;
}

template <typename Derived>
Expand Down
7 changes: 7 additions & 0 deletions be/src/exec/rowid_fetcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
8 changes: 8 additions & 0 deletions be/src/exec/rowid_fetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
106 changes: 60 additions & 46 deletions be/src/exec/scan/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <glog/logging.h>

#include <algorithm>

#include "common/config.h"
#include "common/status.h"
#include "core/block/column_with_type_and_name.h"
Expand Down Expand Up @@ -87,8 +89,18 @@ 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
// 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, and ScannerContext parks the scanner
// rather than resubmitting it while the outstanding work drains.
if (_shared_scan_limit_exhausted()) {
Comment thread
mrhhsg marked this conversation as resolved.
Resolved
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());
Expand All @@ -99,58 +111,68 @@ 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);
}
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;
} else {
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<int>(std::clamp<int64_t>(
_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 &&
!_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.
// 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);
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;
}
break;
}
}

if (_origin_block.empty() && !_padding_block.empty()) {
_padding_block.swap(_origin_block);
if (_origin_block.empty() && !_padding_block.empty()) {
_padding_block.swap(_origin_block);
}
}
return _do_projections(&_origin_block, block);
RETURN_IF_ERROR(_do_projections(&_origin_block, block));
} else {
return get_block(state, block, eos);
RETURN_IF_ERROR(get_block(state, block, eos));
}

// 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.
// 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<int64_t>(block->rows()), std::memory_order_acq_rel);
}
return Status::OK();
}

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;
Expand Down Expand Up @@ -184,13 +206,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);
}
Expand All @@ -203,7 +218,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);
*eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0);

return Status::OK();
}
Expand Down
9 changes: 9 additions & 0 deletions be/src/exec/scan/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
9 changes: 8 additions & 1 deletion be/src/exec/scan/scanner_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -866,7 +872,8 @@ std::shared_ptr<ScanTask> 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;
Expand Down
1 change: 1 addition & 0 deletions be/src/runtime/exec_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 1 addition & 3 deletions be/src/service/internal_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
72 changes: 72 additions & 0 deletions be/test/exec/rowid_fetcher_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@
#include <string>
#include <vector>

#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 {

Expand Down Expand Up @@ -429,4 +438,67 @@ 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<FileMapping>(/*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);

// 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<CgroupCpuCtl> cgroup_cpu_ctl = std::make_shared<CgroupV2CpuCtl>(1);
ThreadPoolSimplifiedScanScheduler scheduler("rowid_rpc_test", cgroup_cpu_ctl);
ASSERT_TRUE(scheduler.start(1, 1, 4, 1).ok());
Defer stop_scheduler {[&] { scheduler.stop(); }};
Comment thread
mrhhsg marked this conversation as resolved.
Resolved

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
Loading
Loading