Coverage Report

Created: 2026-03-24 20:45

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 "storage/olap_define.h"
24
#include "util/cpu_info.h"
25
#include "util/work_thread_pool.hpp"
26
27
namespace doris {
28
29
struct AsyncIOCtx {
30
    int nice;
31
};
32
33
/**
34
 * Separate task from bthread to pthread, specific for IO task.
35
 */
36
class AsyncIO {
37
public:
38
2
    AsyncIO() {
39
2
        _io_thread_pool = new PriorityThreadPool(
40
2
                config::doris_scanner_thread_pool_thread_num > 0
41
2
                        ? config::doris_scanner_thread_pool_thread_num
42
2
                        : std::max(48, CpuInfo::num_cores() * 2),
43
2
                config::doris_scanner_thread_pool_queue_size, "async_io_thread_pool");
44
2
        _remote_thread_pool = new PriorityThreadPool(
45
2
                config::doris_remote_scanner_thread_pool_thread_num,
46
2
                config::doris_remote_scanner_thread_pool_queue_size, "async_remote_thread_pool");
47
2
    }
48
49
2
    ~AsyncIO() {
50
2
        SAFE_DELETE(_io_thread_pool);
51
2
        SAFE_DELETE(_remote_thread_pool);
52
2
    }
53
54
    AsyncIO& operator=(const AsyncIO&) = delete;
55
    AsyncIO(const AsyncIO&) = delete;
56
57
6
    static AsyncIO& instance() {
58
6
        static AsyncIO instance;
59
6
        return instance;
60
6
    }
61
62
    // This function should run on the bthread, and it will put the task into
63
    // thread_pool and release the bthread_worker at cv.wait. When the task is completed,
64
    // the bthread will continue to execute.
65
6
    static void run_task(std::function<void()> fn, io::FileSystemType file_type) {
66
6
        DCHECK(bthread_self() != 0);
67
6
        std::mutex mutex;
68
6
        std::condition_variable cv;
69
6
        std::unique_lock l(mutex);
70
71
6
        AsyncIOCtx* ctx = static_cast<AsyncIOCtx*>(bthread_getspecific(btls_io_ctx_key));
72
6
        int nice = -1;
73
6
        if (ctx == nullptr) {
74
1
            nice = 18;
75
5
        } else {
76
5
            nice = ctx->nice;
77
5
        }
78
79
6
        PriorityThreadPool::Task task;
80
6
        task.priority = nice;
81
6
        task.work_function = [&] {
82
6
            fn();
83
6
            std::unique_lock l(mutex);
84
6
            cv.notify_one();
85
6
        };
86
87
6
        if (file_type == io::FileSystemType::LOCAL) {
88
6
            AsyncIO::instance().io_thread_pool()->offer(task);
89
6
        } else {
90
0
            AsyncIO::instance().remote_thread_pool()->offer(task);
91
0
        }
92
6
        cv.wait(l);
93
6
    }
94
95
    inline static bthread_key_t btls_io_ctx_key;
96
97
0
    static void io_ctx_key_deleter(void* d) { delete static_cast<AsyncIOCtx*>(d); }
98
99
private:
100
    PriorityThreadPool* _io_thread_pool = nullptr;
101
    PriorityThreadPool* _remote_thread_pool = nullptr;
102
103
private:
104
6
    PriorityThreadPool* io_thread_pool() { return _io_thread_pool; }
105
0
    PriorityThreadPool* remote_thread_pool() { return _remote_thread_pool; }
106
};
107
108
} // end namespace doris