Coverage Report

Created: 2026-08-06 13:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/async_io.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <bthread/bthread.h>
21
22
#include <condition_variable>
23
#include <mutex>
24
25
#include "io/fs/file_system.h"
26
#include "runtime/thread_context.h"
27
#include "storage/olap_define.h"
28
#include "util/cpu_info.h"
29
#include "util/work_thread_pool.hpp"
30
31
namespace doris {
32
33
struct AsyncIOCtx {
34
    int nice;
35
};
36
37
/**
38
 * Separate task from bthread to pthread, specific for IO task.
39
 */
40
class AsyncIO {
41
public:
42
1
    AsyncIO() {
43
1
        _io_thread_pool = new PriorityThreadPool(
44
1
                config::doris_scanner_thread_pool_thread_num > 0
45
1
                        ? config::doris_scanner_thread_pool_thread_num
46
1
                        : std::max(48, CpuInfo::num_cores() * 2),
47
1
                config::doris_scanner_thread_pool_queue_size, "async_io_thread_pool");
48
1
        _remote_thread_pool = new PriorityThreadPool(
49
1
                config::doris_remote_scanner_thread_pool_thread_num,
50
1
                config::doris_remote_scanner_thread_pool_queue_size, "async_remote_thread_pool");
51
1
    }
52
53
1
    ~AsyncIO() {
54
1
        SAFE_DELETE(_io_thread_pool);
55
1
        SAFE_DELETE(_remote_thread_pool);
56
1
    }
57
58
    AsyncIO& operator=(const AsyncIO&) = delete;
59
    AsyncIO(const AsyncIO&) = delete;
60
61
3
    static AsyncIO& instance() {
62
3
        static AsyncIO instance;
63
3
        return instance;
64
3
    }
65
66
    // This function should run on the bthread, and it will put the task into
67
    // thread_pool and release the bthread_worker at cv.wait. When the task is completed,
68
    // the bthread will continue to execute.
69
3
    static void run_task(std::function<void()> fn, io::FileSystemType file_type) {
70
3
        DCHECK(bthread_self() != 0);
71
3
        std::mutex mutex;
72
3
        std::condition_variable cv;
73
3
        std::unique_lock l(mutex);
74
75
3
        AsyncIOCtx* ctx = static_cast<AsyncIOCtx*>(bthread_getspecific(btls_io_ctx_key));
76
3
        int nice = -1;
77
3
        if (ctx == nullptr) {
78
0
            nice = 18;
79
3
        } else {
80
3
            nice = ctx->nice;
81
3
        }
82
83
3
        PriorityThreadPool::Task task;
84
3
        task.priority = nice;
85
3
        task.work_function = [&] {
86
            // The AsyncIO worker is a plain infra pthread that is not bound to any task,
87
            // so it has no ThreadContext. fn() (e.g. a FILESYSTEM_M dispatched upload/read)
88
            // may touch thread_context() (memory tracking, LIMIT_*_SCAN_IO, ...), which would
89
            // FatalError without a context. Initialize an (unattached) ThreadContext here,
90
            // same pattern as StorageEngine background threads.
91
3
            SCOPED_INIT_THREAD_CONTEXT();
92
3
            fn();
93
3
            std::unique_lock l(mutex);
94
3
            cv.notify_one();
95
3
        };
96
97
3
        if (file_type == io::FileSystemType::LOCAL) {
98
3
            AsyncIO::instance().io_thread_pool()->offer(task);
99
3
        } else {
100
0
            AsyncIO::instance().remote_thread_pool()->offer(task);
101
0
        }
102
3
        cv.wait(l);
103
3
    }
104
105
    inline static bthread_key_t btls_io_ctx_key;
106
107
0
    static void io_ctx_key_deleter(void* d) { delete static_cast<AsyncIOCtx*>(d); }
108
109
private:
110
    PriorityThreadPool* _io_thread_pool = nullptr;
111
    PriorityThreadPool* _remote_thread_pool = nullptr;
112
113
private:
114
3
    PriorityThreadPool* io_thread_pool() { return _io_thread_pool; }
115
0
    PriorityThreadPool* remote_thread_pool() { return _remote_thread_pool; }
116
};
117
118
} // end namespace doris