Coverage Report

Created: 2026-07-01 18:51

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 "io/fs/file_system.h"
23
#include "runtime/thread_context.h"
24
#include "storage/olap_define.h"
25
#include "util/cpu_info.h"
26
#include "util/work_thread_pool.hpp"
27
28
namespace doris {
29
30
struct AsyncIOCtx {
31
    int nice;
32
};
33
34
/**
35
 * Separate task from bthread to pthread, specific for IO task.
36
 */
37
class AsyncIO {
38
public:
39
1
    AsyncIO() {
40
1
        _io_thread_pool = new PriorityThreadPool(
41
1
                config::doris_scanner_thread_pool_thread_num > 0
42
1
                        ? config::doris_scanner_thread_pool_thread_num
43
1
                        : std::max(48, CpuInfo::num_cores() * 2),
44
1
                config::doris_scanner_thread_pool_queue_size, "async_io_thread_pool");
45
1
        _remote_thread_pool = new PriorityThreadPool(
46
1
                config::doris_remote_scanner_thread_pool_thread_num,
47
1
                config::doris_remote_scanner_thread_pool_queue_size, "async_remote_thread_pool");
48
1
    }
49
50
1
    ~AsyncIO() {
51
1
        SAFE_DELETE(_io_thread_pool);
52
1
        SAFE_DELETE(_remote_thread_pool);
53
1
    }
54
55
    AsyncIO& operator=(const AsyncIO&) = delete;
56
    AsyncIO(const AsyncIO&) = delete;
57
58
3
    static AsyncIO& instance() {
59
3
        static AsyncIO instance;
60
3
        return instance;
61
3
    }
62
63
    // This function should run on the bthread, and it will put the task into
64
    // thread_pool and release the bthread_worker at cv.wait. When the task is completed,
65
    // the bthread will continue to execute.
66
3
    static void run_task(std::function<void()> fn, io::FileSystemType file_type) {
67
3
        DCHECK(bthread_self() != 0);
68
3
        std::mutex mutex;
69
3
        std::condition_variable cv;
70
3
        std::unique_lock l(mutex);
71
72
3
        AsyncIOCtx* ctx = static_cast<AsyncIOCtx*>(bthread_getspecific(btls_io_ctx_key));
73
3
        int nice = -1;
74
3
        if (ctx == nullptr) {
75
1
            nice = 18;
76
2
        } else {
77
2
            nice = ctx->nice;
78
2
        }
79
80
3
        PriorityThreadPool::Task task;
81
3
        task.priority = nice;
82
3
        task.work_function = [&] {
83
            // The AsyncIO worker is a plain infra pthread that is not bound to any task,
84
            // so it has no ThreadContext. fn() (e.g. a FILESYSTEM_M dispatched upload/read)
85
            // may touch thread_context() (memory tracking, LIMIT_*_SCAN_IO, ...), which would
86
            // FatalError without a context. Initialize an (unattached) ThreadContext here,
87
            // same pattern as StorageEngine background threads.
88
3
            SCOPED_INIT_THREAD_CONTEXT();
89
3
            fn();
90
3
            std::unique_lock l(mutex);
91
3
            cv.notify_one();
92
3
        };
93
94
3
        if (file_type == io::FileSystemType::LOCAL) {
95
3
            AsyncIO::instance().io_thread_pool()->offer(task);
96
3
        } else {
97
0
            AsyncIO::instance().remote_thread_pool()->offer(task);
98
0
        }
99
3
        cv.wait(l);
100
3
    }
101
102
    inline static bthread_key_t btls_io_ctx_key;
103
104
0
    static void io_ctx_key_deleter(void* d) { delete static_cast<AsyncIOCtx*>(d); }
105
106
private:
107
    PriorityThreadPool* _io_thread_pool = nullptr;
108
    PriorityThreadPool* _remote_thread_pool = nullptr;
109
110
private:
111
3
    PriorityThreadPool* io_thread_pool() { return _io_thread_pool; }
112
0
    PriorityThreadPool* remote_thread_pool() { return _remote_thread_pool; }
113
};
114
115
} // end namespace doris