Skip to content

Commit 1d9b406

Browse files
committed
[improvement](be) Admit read-ahead ranges within remaining byte budgets
### What problem does this PR solve? Issue Number: apache#67611 Related PR: apache#67292 Problem Summary: A wide scan submits the initial windows of all columns together. Whole-batch byte admission rejected every range when their aggregate size exceeded a query or BE budget, discarding useful concurrency even when many ranges could fit. Admit ranges in input order with one linear pass under both budget locks, skip ranges that do not fit, and allocate only admitted buffers. Return null handles at skipped input positions so the existing planner mapping remains intact. Segment coordination registers admitted page slices and completes skipped pages for synchronous fallback. Preserve per-buffer reservation lifetime, allocation rollback, executor failures, and cancellation. Count partially budget-rejected batches without double-counting an executor rejection; configuration defaults, windows, coalescing, and hole filling are unchanged. ### Release note Retain partial read-ahead concurrency when a planned range batch exceeds its query or BE byte budget. ### Check List (For Author) - Test: ASAN/PCH BE unit tests through ./run-be-ut.sh --run -j100: 141 tests from 8 suites passed. Coverage includes partial query/BE admission, oversized ranges followed by smaller ranges, original page offsets, consumed-range callbacks, cancellation during allocation, allocation rollback, and executor rejection accounting. Clang-format 16, header hygiene and git diff --check passed. - Behavior changed: Yes; ranges that fit remain asynchronous while skipped ranges use the original synchronous path. - Does this need documentation: Yes; update the component behavior in the existing PR and tracking descriptions.
1 parent 60f0f2e commit 1d9b406

7 files changed

Lines changed: 339 additions & 74 deletions

File tree

‎be/src/io/fs/file_range_read_scheduler.cpp‎

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,18 @@ void FileRangeReadContext::_register_reads(
104104
std::erase_if(_reads, [](const auto& read) { return read.expired(); });
105105
_reads.reserve(_reads.size() + reads.size());
106106
for (const auto& read : reads) {
107-
_reads.emplace_back(read);
107+
// Partial admission leaves null slots for ranges without a buffer or task.
108+
if (read != nullptr) {
109+
_reads.emplace_back(read);
110+
}
108111
}
109112
}
110113
}
111114
if (cancel_reads) {
112115
for (const auto& read : reads) {
113-
read->request_cancel();
116+
if (read != nullptr) {
117+
read->request_cancel();
118+
}
114119
}
115120
}
116121
}
@@ -396,7 +401,7 @@ std::shared_ptr<FileRangeReadContext> FileRangeReadScheduler::create_context() c
396401
FileRangeReadSubmitResult FileRangeReadScheduler::try_submit(
397402
const std::vector<FileRange>& ranges, FileReaderSPtr reader,
398403
FileRangeReadIOContext io_context, const std::shared_ptr<FileRangeReadContext>& context) {
399-
const auto reject = [&](FileRangeReadRejectReason reason, Status status = Status::OK()) {
404+
const auto record_rejection = [&](FileRangeReadRejectReason reason) {
400405
read_ahead_bvars().rejected_batches << 1;
401406
if (io_context.statistics != nullptr) {
402407
COUNTER_UPDATE(&io_context.statistics->rejected_batches, 1);
@@ -406,10 +411,12 @@ FileRangeReadSubmitResult FileRangeReadScheduler::try_submit(
406411
COUNTER_UPDATE(&io_context.statistics->be_budget_rejected_batches, 1);
407412
}
408413
}
414+
};
415+
const auto reject = [&](FileRangeReadRejectReason reason, Status status = Status::OK()) {
416+
record_rejection(reason);
409417
return FileRangeReadSubmitResult::rejected(reason, std::move(status));
410418
};
411-
size_t total_bytes = 0;
412-
Status validation_status = _validate_request(ranges, reader, context, &total_bytes);
419+
Status validation_status = _validate_request(ranges, reader, context);
413420
if (!validation_status.ok()) {
414421
return reject(FileRangeReadRejectReason::INVALID_REQUEST, std::move(validation_status));
415422
}
@@ -422,28 +429,32 @@ FileRangeReadSubmitResult FileRangeReadScheduler::try_submit(
422429
Status::Cancelled("query cancelled before file range submission"));
423430
}
424431

425-
std::vector<FileRangeReadReservation> reservations;
426-
reservations.reserve(ranges.size());
427-
const auto reserve_reason = _reserve_batch(context, ranges, total_bytes, &reservations);
428-
if (reserve_reason != FileRangeReadRejectReason::NONE) {
429-
return reject(reserve_reason);
430-
}
432+
std::vector<std::optional<FileRangeReadReservation>> reservations(ranges.size());
433+
const auto reserve_reason = _reserve_batch(context, ranges, &reservations);
431434

432-
std::vector<std::shared_ptr<FileRangeRead>> reads;
433-
reads.reserve(ranges.size());
435+
std::vector<std::shared_ptr<FileRangeRead>> reads(ranges.size());
434436
std::vector<ReadTask> tasks;
435437
tasks.reserve(ranges.size());
436438
for (size_t index = 0; index < ranges.size(); ++index) {
439+
// Keep the input slot empty when admission skipped a range, so page mappings stay valid.
440+
if (!reservations[index].has_value()) {
441+
continue;
442+
}
437443
std::shared_ptr<FileRangeRead> read;
438444
Status status =
439-
FileRangeRead::create(ranges[index], _mem_tracker, std::move(reservations[index]),
445+
FileRangeRead::create(ranges[index], _mem_tracker, std::move(*reservations[index]),
440446
&read, io_context.statistics);
441447
if (!status.ok()) {
442448
return reject(FileRangeReadRejectReason::ALLOCATION_FAILED, std::move(status));
443449
}
444450
tasks.push_back(ReadTask {
445451
.reader = reader, .read = read, .context = context, .io_context = io_context});
446-
reads.emplace_back(std::move(read));
452+
reads[index] = std::move(read);
453+
}
454+
// Every range may exceed the remaining budgets, leaving the whole request on the sync path.
455+
if (tasks.empty()) {
456+
DORIS_CHECK(reserve_reason != FileRangeReadRejectReason::NONE);
457+
return reject(reserve_reason);
447458
}
448459
context->_register_reads(reads);
449460

@@ -452,15 +463,19 @@ FileRangeReadSubmitResult FileRangeReadScheduler::try_submit(
452463
if (!submit_status.ok()) {
453464
return reject(submit_reason, std::move(submit_status));
454465
}
466+
// Record a partially budget-rejected batch once, after execution admission succeeds. If every
467+
// executor task was rejected, the failure above already counted this submission.
468+
if (reserve_reason != FileRangeReadRejectReason::NONE) {
469+
record_rejection(reserve_reason);
470+
}
455471
return {.reads = std::move(reads),
456472
.reject_reason = FileRangeReadRejectReason::NONE,
457473
.status = Status::OK()};
458474
}
459475

460476
Status FileRangeReadScheduler::_validate_request(
461477
const std::vector<FileRange>& ranges, const FileReaderSPtr& reader,
462-
const std::shared_ptr<FileRangeReadContext>& context, size_t* total_bytes) const {
463-
DORIS_CHECK(total_bytes != nullptr);
478+
const std::shared_ptr<FileRangeReadContext>& context) const {
464479
if (reader == nullptr) {
465480
return Status::InvalidArgument("file range reader is null");
466481
}
@@ -471,7 +486,6 @@ Status FileRangeReadScheduler::_validate_request(
471486
return Status::InvalidArgument("file range read batch is empty");
472487
}
473488

474-
*total_bytes = 0;
475489
for (const auto& range : ranges) {
476490
if (range.size == 0) {
477491
return Status::InvalidArgument("file range at offset {} is empty", range.offset);
@@ -484,36 +498,45 @@ Status FileRangeReadScheduler::_validate_request(
484498
return Status::InvalidArgument("file range [{}, {}) exceeds file size {}", range.offset,
485499
range.end(), reader->size());
486500
}
487-
if (add_overflows(*total_bytes, range.size)) {
488-
return Status::InvalidArgument("file range read batch byte size overflows");
489-
}
490-
*total_bytes += range.size;
491501
}
492502
return Status::OK();
493503
}
494504

495505
FileRangeReadRejectReason FileRangeReadScheduler::_reserve_batch(
496506
const std::shared_ptr<FileRangeReadContext>& context, const std::vector<FileRange>& ranges,
497-
size_t total_bytes, std::vector<FileRangeReadReservation>* reservations) {
507+
std::vector<std::optional<FileRangeReadReservation>>* reservations) {
498508
DORIS_CHECK(reservations != nullptr);
499-
DORIS_CHECK(reservations->capacity() >= ranges.size());
509+
DORIS_CHECK(reservations->size() == ranges.size());
500510
auto& query_budget = context->_budget;
501511
std::scoped_lock lock(query_budget._mutex, _global_budget->_mutex);
502512

503-
if (total_bytes > query_budget._max_bytes - query_budget._resident_bytes) {
504-
return FileRangeReadRejectReason::QUERY_BYTE_LIMIT;
505-
}
506-
if (total_bytes > _global_budget->_max_bytes - _global_budget->_resident_bytes) {
507-
return FileRangeReadRejectReason::GLOBAL_BYTE_LIMIT;
513+
size_t query_remaining = query_budget._max_bytes - query_budget._resident_bytes;
514+
size_t be_remaining = _global_budget->_max_bytes - _global_budget->_resident_bytes;
515+
size_t reserved_bytes = 0;
516+
auto reject_reason = FileRangeReadRejectReason::NONE;
517+
for (size_t index = 0; index < ranges.size(); ++index) {
518+
const size_t bytes = ranges[index].size;
519+
const auto reason = bytes > query_remaining ? FileRangeReadRejectReason::QUERY_BYTE_LIMIT
520+
: bytes > be_remaining ? FileRangeReadRejectReason::GLOBAL_BYTE_LIMIT
521+
: FileRangeReadRejectReason::NONE;
522+
// With 3 MiB available and ranges of 2, 2, and 1 MiB, admit the first and third ranges.
523+
// A range that cannot fit must not prevent later, smaller ranges from using the budget.
524+
if (reason != FileRangeReadRejectReason::NONE) {
525+
if (reject_reason == FileRangeReadRejectReason::NONE) {
526+
reject_reason = reason;
527+
}
528+
continue;
529+
}
530+
(*reservations)[index].emplace(FileRangeReadReservation(context, _global_budget, bytes));
531+
query_remaining -= bytes;
532+
be_remaining -= bytes;
533+
reserved_bytes += bytes;
508534
}
509535

510-
query_budget._reserve_unlocked(total_bytes);
511-
_global_budget->_reserve_unlocked(total_bytes);
512-
read_ahead_bvars().resident_bytes << static_cast<int64_t>(total_bytes);
513-
for (const auto& range : ranges) {
514-
reservations->push_back(FileRangeReadReservation(context, _global_budget, range.size));
515-
}
516-
return FileRangeReadRejectReason::NONE;
536+
query_budget._reserve_unlocked(reserved_bytes);
537+
_global_budget->_reserve_unlocked(reserved_bytes);
538+
read_ahead_bvars().resident_bytes << static_cast<int64_t>(reserved_bytes);
539+
return reject_reason;
517540
}
518541

519542
Status FileRangeReadScheduler::_submit_tasks(std::vector<ReadTask> tasks,

‎be/src/io/fs/file_range_read_scheduler.h‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <cstdint>
2424
#include <memory>
2525
#include <mutex>
26+
#include <optional>
2627
#include <unordered_set>
2728
#include <vector>
2829

@@ -42,15 +43,15 @@ namespace io {
4243
struct ReadAheadStatistics;
4344

4445
enum class FileRangeReadRejectReason : uint8_t {
45-
/// At least one executor task was accepted. Other handles may fail to submit or read.
46+
/// At least one executor task was accepted. Other ranges may be skipped or fail to submit/read.
4647
NONE,
47-
/// A reader, context, range, or aggregate byte count failed request validation.
48+
/// A reader, context, or range failed request validation.
4849
INVALID_REQUEST,
4950
/// The query context had already been cancelled before admission.
5051
QUERY_CANCELLED,
51-
/// Reserving the complete batch would exceed its query-scoped byte budget.
52+
/// The query-scoped byte budget prevented range admission.
5253
QUERY_BYTE_LIMIT,
53-
/// Reserving the complete batch would exceed the BE-wide byte budget.
54+
/// The BE-wide byte budget prevented range admission.
5455
GLOBAL_BYTE_LIMIT,
5556
/// At least one range buffer could not be allocated; no read handles are returned.
5657
ALLOCATION_FAILED,
@@ -221,6 +222,8 @@ struct FileRangeReadIOContext {
221222
};
222223

223224
struct FileRangeReadSubmitResult {
225+
/// One slot per input range on acceptance; a null slot was skipped by byte-budget admission.
226+
/// A completely rejected submission returns an empty vector.
224227
std::vector<std::shared_ptr<FileRangeRead>> reads;
225228
FileRangeReadRejectReason reject_reason {FileRangeReadRejectReason::NONE};
226229
Status status;
@@ -249,10 +252,10 @@ class FileRangeReadScheduler {
249252
/// Create a query-scoped cancellation and byte-budget context.
250253
std::shared_ptr<FileRangeReadContext> create_context() const;
251254

252-
/// Atomically admits and allocates the complete batch, then submits one executor task per
253-
/// range. A rejected batch returns no read handles and leaves both budgets unchanged. If the
254-
/// executor accepts only part of a batch, the returned handles expose each rejected range as a
255-
/// failed read.
255+
/// Admit ranges in input order under both byte-budget locks, skipping ranges that do not fit,
256+
/// then allocate and submit one task per admitted range. Returned slots preserve input indices;
257+
/// unadmitted ranges have null handles. Allocation failure rolls back all admitted buffers.
258+
/// Executor rejection is exposed as a failed handle when other tasks were accepted.
256259
FileRangeReadSubmitResult try_submit(const std::vector<FileRange>& ranges,
257260
FileReaderSPtr reader, FileRangeReadIOContext io_context,
258261
const std::shared_ptr<FileRangeReadContext>& context);
@@ -273,15 +276,15 @@ class FileRangeReadScheduler {
273276

274277
FileRangeReadScheduler(FileRangeReadSchedulerOptions options, ThreadPool* executor);
275278

276-
/// Validate the complete request and compute its total buffer capacity.
279+
/// Validate the reader, context, and individual ranges before reserving any memory.
277280
Status _validate_request(const std::vector<FileRange>& ranges, const FileReaderSPtr& reader,
278-
const std::shared_ptr<FileRangeReadContext>& context,
279-
size_t* total_bytes) const;
280-
/// Atomically reserve query and BE bytes for every range in the batch.
281-
FileRangeReadRejectReason _reserve_batch(const std::shared_ptr<FileRangeReadContext>& context,
282-
const std::vector<FileRange>& ranges,
283-
size_t total_bytes,
284-
std::vector<FileRangeReadReservation>* reservations);
281+
const std::shared_ptr<FileRangeReadContext>& context) const;
282+
/// Reserve query and BE bytes for ranges that fit, preserving input indices. Return the first
283+
/// encountered byte-budget rejection, including when some ranges were admitted.
284+
FileRangeReadRejectReason _reserve_batch(
285+
const std::shared_ptr<FileRangeReadContext>& context,
286+
const std::vector<FileRange>& ranges,
287+
std::vector<std::optional<FileRangeReadReservation>>* reservations);
285288
/// Submit one task per range while retaining accepted handles for shutdown coordination.
286289
Status _submit_tasks(std::vector<ReadTask> tasks, FileRangeReadRejectReason* reject_reason);
287290
void _task_finished(const std::shared_ptr<FileRangeRead>& read);

‎be/src/io/fs/read_ahead_metrics.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct ReadAheadStatistics {
5555
RuntimeProfile::Counter planned_bytes {TUnit::BYTES};
5656
RuntimeProfile::Counter submitted_ranges {TUnit::UNIT};
5757
RuntimeProfile::Counter submitted_bytes {TUnit::BYTES};
58+
/// Whole-batch rejections and byte-budget partial rejections. The budget counters below also
59+
/// include partially admitted batches; their cause is the first budget rejection in that batch.
5860
RuntimeProfile::Counter rejected_batches {TUnit::UNIT};
5961
RuntimeProfile::Counter query_budget_rejected_batches {TUnit::UNIT};
6062
RuntimeProfile::Counter be_budget_rejected_batches {TUnit::UNIT};

‎be/src/storage/segment/segment_read_ahead.cpp‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,6 @@ SegmentReadAheadResult SegmentReadAhead::apply_plans(std::vector<ColumnReadAhead
396396
return result;
397397
}
398398

399-
for (const auto& range : range_plan.ranges) {
400-
result.submitted_bytes += range.size;
401-
}
402399
ReadAheadRangeConsumer consumer;
403400
if (_options.range_consumer_factory) {
404401
consumer = _options.range_consumer_factory();
@@ -408,21 +405,25 @@ SegmentReadAheadResult SegmentReadAhead::apply_plans(std::vector<ColumnReadAhead
408405
result.reject_reason = submit_result.reject_reason;
409406
result.status = submit_result.status;
410407
if (!submit_result.accepted()) {
411-
result.submitted_bytes = 0;
412408
for (const auto& miss : misses) {
413409
miss.column->complete(miss.page.page_index);
414410
}
415411
return result;
416412
}
417413

418-
result.submitted_ranges = submit_result.reads.size();
419414
DORIS_CHECK(submit_result.reads.size() == range_plan.ranges.size());
420-
std::vector<std::shared_ptr<SegmentReadAheadFileReader::BufferedRange>> ranges;
421-
ranges.reserve(range_plan.ranges.size());
415+
std::vector<std::shared_ptr<SegmentReadAheadFileReader::BufferedRange>> ranges(
416+
range_plan.ranges.size());
422417
for (size_t index = 0; index < range_plan.ranges.size(); ++index) {
423-
ranges.push_back(std::make_shared<SegmentReadAheadFileReader::BufferedRange>(
418+
// Byte-budget admission may skip individual ranges while preserving their input slots.
419+
if (submit_result.reads[index] == nullptr) {
420+
continue;
421+
}
422+
++result.submitted_ranges;
423+
result.submitted_bytes += range_plan.ranges[index].size;
424+
ranges[index] = std::make_shared<SegmentReadAheadFileReader::BufferedRange>(
424425
SegmentReadAheadFileReader::BufferedRange {
425-
.read = std::move(submit_result.reads[index]), .consumer = consumer}));
426+
.read = std::move(submit_result.reads[index]), .consumer = consumer});
426427
}
427428

428429
DORIS_CHECK(range_plan.input_locations.size() == input_ranges.size());
@@ -434,6 +435,11 @@ SegmentReadAheadResult SegmentReadAhead::apply_plans(std::vector<ColumnReadAhead
434435
DORIS_CHECK(input_index < range_plan.input_locations.size());
435436
const auto& location = range_plan.input_locations[input_index];
436437
DORIS_CHECK(location.range_index < ranges.size());
438+
// No buffer was admitted for this page. Clear pending now; PageIO uses the original reader.
439+
if (ranges[location.range_index] == nullptr) {
440+
misses[index].column->complete(misses[index].page.page_index);
441+
continue;
442+
}
437443
_reader->_register_page(misses[index].column, misses[index].page,
438444
ranges[location.range_index], location.buffer_offset);
439445
}

‎be/src/storage/segment/segment_read_ahead.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ struct SegmentReadAheadResult {
6060
size_t new_pages {0};
6161
/// Newly requested pages already available from StoragePageCache.
6262
size_t page_cache_hits {0};
63-
/// Physical range handles returned for an accepted scheduler batch.
63+
/// Physical ranges admitted to buffering; byte-budget skips are excluded.
6464
size_t submitted_ranges {0};
65-
/// Sum of that batch's range sizes, including coalescing and block-fill bytes.
65+
/// Sum of admitted range sizes, including coalescing and block-fill bytes.
6666
size_t submitted_bytes {0};
6767
/// Batch-level scheduler rejection; NONE also covers plans that required no source reads.
6868
io::FileRangeReadRejectReason reject_reason {io::FileRangeReadRejectReason::NONE};

0 commit comments

Comments
 (0)