Coverage Report

Created: 2026-08-24 09:24

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
2
    AsyncIO() {
43
2
        _io_thread_pool = new PriorityThreadPool(
44
2
                config::doris_scanner_thread_pool_thread_num > 0
45
2
                        ? config::doris_scanner_thread_pool_thread_num
46
2
                        : std::max(48, CpuInfo::num_cores() * 2),
47
2
                config::doris_scanner_thread_pool_queue_size, "async_io_thread_pool");
48
2
        _remote_thread_pool = new PriorityThreadPool(
49
2
                config::doris_remote_scanner_thread_pool_thread_num,
50
2
                config::doris_remote_scanner_thread_pool_queue_size, "async_remote_thread_pool");
51
2
    }
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
5
    static AsyncIO& instance() {
62
5
        static AsyncIO instance;
63
5
        return instance;
64
5
    }
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
5
    static void run_task(std::function<void()> fn, io::FileSystemType file_type) {
70
5
        DCHECK(bthread_self() != 0);
71
5
        std::mutex mutex;
72
5
        std::condition_variable cv;
73
5
        std::unique_lock l(mutex);
74
75
5
        AsyncIOCtx* ctx = static_cast<AsyncIOCtx*>(bthread_getspecific(btls_io_ctx_key));
76
5
        int nice = -1;
77
5
        if (ctx == nullptr) {
78
3
            nice = 18;
79
3
        } else {
80
2
            nice = ctx->nice;
81
2
        }
82
83
5
        PriorityThreadPool::Task task;
84
5
        task.priority = nice;
85
5
        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
5
            SCOPED_INIT_THREAD_CONTEXT();
92
5
            fn();
93
5
            std::unique_lock l(mutex);
94
5
            cv.notify_one();
95
5
        };
96
97
5
        if (file_type == io::FileSystemType::LOCAL) {
98
3
            AsyncIO::instance().io_thread_pool()->offer(task);
99
3
        } else {
100
2
            AsyncIO::instance().remote_thread_pool()->offer(task);
101
2
        }
102
5
        cv.wait(l);
103
5
    }
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
2
    PriorityThreadPool* remote_thread_pool() { return _remote_thread_pool; }
116
};
117
118
} // end namespace doris