/root/doris/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 "olap/olap_define.h" |
24 | | #include "util/cpu_info.h" |
25 | | #include "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 | 1 | AsyncIO() { |
39 | 1 | _io_thread_pool = new PriorityThreadPool( |
40 | 1 | config::doris_scanner_thread_pool_thread_num > 0 |
41 | 1 | ? config::doris_scanner_thread_pool_thread_num |
42 | 1 | : std::max(48, CpuInfo::num_cores() * 2), |
43 | 1 | config::doris_scanner_thread_pool_queue_size, "async_io_thread_pool"); |
44 | 1 | _remote_thread_pool = new PriorityThreadPool( |
45 | 1 | config::doris_remote_scanner_thread_pool_thread_num, |
46 | 1 | config::doris_remote_scanner_thread_pool_queue_size, "async_remote_thread_pool"); |
47 | 1 | } |
48 | | |
49 | 1 | ~AsyncIO() { |
50 | 1 | SAFE_DELETE(_io_thread_pool); |
51 | 1 | SAFE_DELETE(_remote_thread_pool); |
52 | 1 | } |
53 | | |
54 | | AsyncIO& operator=(const AsyncIO&) = delete; |
55 | | AsyncIO(const AsyncIO&) = delete; |
56 | | |
57 | 3 | static AsyncIO& instance() { |
58 | 3 | static AsyncIO instance; |
59 | 3 | return instance; |
60 | 3 | } |
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 | 3 | static void run_task(std::function<void()> fn, io::FileSystemType file_type) { |
66 | 3 | DCHECK(bthread_self() != 0); |
67 | 3 | std::mutex mutex; |
68 | 3 | std::condition_variable cv; |
69 | 3 | std::unique_lock l(mutex); |
70 | | |
71 | 3 | AsyncIOCtx* ctx = static_cast<AsyncIOCtx*>(bthread_getspecific(btls_io_ctx_key)); |
72 | 3 | int nice = -1; |
73 | 3 | if (ctx == nullptr) { |
74 | 1 | nice = 18; |
75 | 2 | } else { |
76 | 2 | nice = ctx->nice; |
77 | 2 | } |
78 | | |
79 | 3 | PriorityThreadPool::Task task; |
80 | 3 | task.priority = nice; |
81 | 3 | task.work_function = [&] { |
82 | 3 | fn(); |
83 | 3 | std::unique_lock l(mutex); |
84 | 3 | cv.notify_one(); |
85 | 3 | }; |
86 | | |
87 | 3 | if (file_type == io::FileSystemType::LOCAL) { |
88 | 3 | AsyncIO::instance().io_thread_pool()->offer(task); |
89 | 3 | } else { |
90 | 0 | AsyncIO::instance().remote_thread_pool()->offer(task); |
91 | 0 | } |
92 | 3 | cv.wait(l); |
93 | 3 | } |
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 | 3 | PriorityThreadPool* io_thread_pool() { return _io_thread_pool; } |
105 | 0 | PriorityThreadPool* remote_thread_pool() { return _remote_thread_pool; } |
106 | | }; |
107 | | |
108 | | } // end namespace doris |