|
26 | 26 | #include <ctime> |
27 | 27 | #include <memory> |
28 | 28 | #include <mutex> |
| 29 | +#include <optional> |
29 | 30 | #include <ostream> |
30 | 31 | #include <shared_mutex> |
31 | 32 | #include <tuple> |
@@ -362,6 +363,61 @@ void ScannerContext::push_completed_scan_task(std::shared_ptr<ScanTask> scan_tas |
362 | 363 | _dependency->set_ready(); |
363 | 364 | } |
364 | 365 |
|
| 366 | +void ScannerContext::park_scan_task(std::shared_ptr<ScanTask> scan_task, |
| 367 | + SharedListenableFuture<Void> waiting_for) { |
| 368 | + { |
| 369 | + std::unique_lock<std::mutex> l(_transfer_lock); |
| 370 | + scan_task->set_state(ScanTask::State::PARKED); |
| 371 | + _in_flight_tasks_num--; |
| 372 | + ++_parked_tasks_num; |
| 373 | + // The slot it held may now admit a pending scanner - one whose block the operator consumed |
| 374 | + // while this context had no slot to spare, holding what this task waits for, among them. |
| 375 | + // Nothing else would look: a parked task never completes a scan attempt to make the operator |
| 376 | + // schedule again. |
| 377 | + if (!done()) { |
| 378 | + Status status = _scanner_scheduler->schedule_scan_task(shared_from_this(), nullptr, l); |
| 379 | + if (!status.ok()) { |
| 380 | + set_context_failure(status, l); |
| 381 | + } |
| 382 | + } |
| 383 | + } |
| 384 | + std::weak_ptr<ScannerContext> weak_ctx = shared_from_this(); |
| 385 | + // Runs on the thread that completes the future, or right here if it is done already. |
| 386 | + waiting_for.add_callback([weak_ctx, scan_task](const Void&, const Status&) { |
| 387 | + if (auto ctx = weak_ctx.lock()) { |
| 388 | + ctx->_resume_parked_task(scan_task); |
| 389 | + } |
| 390 | + }); |
| 391 | +} |
| 392 | + |
| 393 | +void ScannerContext::_resume_parked_task(const std::shared_ptr<ScanTask>& scan_task) { |
| 394 | + auto task_execution_lock = task_exec_ctx(); |
| 395 | + if (task_execution_lock == nullptr) { |
| 396 | + // The query has finished; nothing will read this task again. |
| 397 | + return; |
| 398 | + } |
| 399 | +#ifndef BE_TEST |
| 400 | + // Scheduling allocates for the query, not for the gate's thread that completed the future. A |
| 401 | + // future done before park_scan_task() added this callback runs it on the worker that parked |
| 402 | + // the task, which is attached to this query already and must stay so. |
| 403 | + std::optional<AttachTask> attach; |
| 404 | + if (!thread_context()->is_attach_task()) { |
| 405 | + attach.emplace(_state); |
| 406 | + } |
| 407 | +#endif |
| 408 | + std::unique_lock<std::mutex> l(_transfer_lock); |
| 409 | + --_parked_tasks_num; |
| 410 | + if (done()) { |
| 411 | + return; |
| 412 | + } |
| 413 | + // Like a task whose block the operator just consumed: it may run again. |
| 414 | + scan_task->set_state(ScanTask::State::PENDING); |
| 415 | + Status status = _scanner_scheduler->schedule_scan_task(shared_from_this(), scan_task, l); |
| 416 | + if (!status.ok()) { |
| 417 | + set_context_failure(status, l); |
| 418 | + } |
| 419 | +} |
| 420 | + |
365 | 421 | Status ScannerContext::get_block_from_queue(RuntimeState* state, Block* block, bool* eos, int id) { |
366 | 422 | if (state->is_cancelled()) { |
367 | 423 | _set_scanner_done(); |
@@ -545,14 +601,15 @@ std::string ScannerContext::debug_string() { |
545 | 601 | return fmt::format( |
546 | 602 | "_query_id: {}, id: {}, total scanners: {}, pending tasks: {}, completed tasks: {}," |
547 | 603 | " _should_stop: {}, _is_finished: {}, free blocks: {}," |
548 | | - " limit: {}, remaining_limit: {}, _in_flight_tasks_num: {}, _is_context_queued: {}, " |
549 | | - "_num_finished_scanners: {}, _max_scan_concurrency: {}, expected_scanners: {}," |
| 604 | + " limit: {}, remaining_limit: {}, _in_flight_tasks_num: {}, _parked_tasks_num: {}, " |
| 605 | + "_is_context_queued: {}, _num_finished_scanners: {}, _max_scan_concurrency: {}, " |
| 606 | + "expected_scanners: {}," |
550 | 607 | " _max_bytes_in_queue: {}, _ins_idx: {}, _enable_adaptive_scanners: {}, " |
551 | 608 | "_mem_share_arb: {}, _scanner_mem_limiter: {}", |
552 | 609 | print_id(_query_id), ctx_id, _all_scanners.size(), _pending_tasks.size(), |
553 | 610 | _completed_tasks.size(), _should_stop, _is_finished, _free_blocks.size_approx(), limit, |
554 | 611 | _shared_scan_limit->load(std::memory_order_relaxed), _in_flight_tasks_num, |
555 | | - _is_context_queued, _num_finished_scanners, _max_scan_concurrency, |
| 612 | + _parked_tasks_num, _is_context_queued, _num_finished_scanners, _max_scan_concurrency, |
556 | 613 | _enable_adaptive_scanners ? _adaptive_processor->expected_scanners : -1, |
557 | 614 | _max_bytes_in_queue, _ins_idx, _enable_adaptive_scanners, |
558 | 615 | _enable_adaptive_scanners ? _mem_share_arb->debug_string() : "NULL", |
|
0 commit comments