/root/doris/be/src/runtime/exec_env.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include "runtime/exec_env.h" |
19 | | |
20 | | #include <gen_cpp/HeartbeatService_types.h> |
21 | | #include <glog/logging.h> |
22 | | |
23 | | #include <mutex> |
24 | | #include <utility> |
25 | | |
26 | | #include "common/config.h" |
27 | | #include "common/logging.h" |
28 | | #include "olap/olap_define.h" |
29 | | #include "olap/storage_engine.h" |
30 | | #include "olap/tablet_manager.h" |
31 | | #include "runtime/fragment_mgr.h" |
32 | | #include "runtime/frontend_info.h" |
33 | | #include "runtime/load_stream_mgr.h" |
34 | | #include "util/debug_util.h" |
35 | | #include "util/time.h" |
36 | | #include "vec/runtime/vdata_stream_mgr.h" |
37 | | #include "vec/sink/delta_writer_v2_pool.h" |
38 | | #include "vec/sink/load_stream_map_pool.h" |
39 | | |
40 | | namespace doris { |
41 | | |
42 | | #ifdef BE_TEST |
43 | | void ExecEnv::set_inverted_index_searcher_cache( |
44 | 53 | segment_v2::InvertedIndexSearcherCache* inverted_index_searcher_cache) { |
45 | 53 | _inverted_index_searcher_cache = inverted_index_searcher_cache; |
46 | 53 | } |
47 | 234 | void ExecEnv::set_storage_engine(std::unique_ptr<BaseStorageEngine>&& engine) { |
48 | 234 | _storage_engine = std::move(engine); |
49 | 234 | } |
50 | 1 | void ExecEnv::set_write_cooldown_meta_executors() { |
51 | 1 | _write_cooldown_meta_executors = std::make_unique<WriteCooldownMetaExecutors>(); |
52 | 1 | } |
53 | | #endif // BE_TEST |
54 | | |
55 | 4 | Result<BaseTabletSPtr> ExecEnv::get_tablet(int64_t tablet_id) { |
56 | 4 | auto storage_engine = GetInstance()->_storage_engine.get(); |
57 | 4 | return storage_engine != nullptr |
58 | 4 | ? storage_engine->get_tablet(tablet_id) |
59 | 4 | : ResultError(Status::InternalError("failed to get tablet {}", tablet_id)); |
60 | 4 | } |
61 | | |
62 | 0 | const std::string& ExecEnv::token() const { |
63 | 0 | return _cluster_info->token; |
64 | 0 | } |
65 | | |
66 | 0 | void ExecEnv::clear_stream_mgr() { |
67 | 0 | if (_vstream_mgr) { |
68 | 0 | SAFE_DELETE(_vstream_mgr); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | std::vector<TFrontendInfo> ExecEnv::get_frontends() { |
73 | 0 | std::lock_guard<std::mutex> lg(_frontends_lock); |
74 | 0 | std::vector<TFrontendInfo> infos; |
75 | 0 | for (const auto& cur_fe : _frontends) { |
76 | 0 | infos.push_back(cur_fe.second.info); |
77 | 0 | } |
78 | 0 | return infos; |
79 | 0 | } |
80 | | |
81 | 0 | void ExecEnv::update_frontends(const std::vector<TFrontendInfo>& new_fe_infos) { |
82 | 0 | std::lock_guard<std::mutex> lg(_frontends_lock); |
83 | |
|
84 | 0 | std::set<TNetworkAddress> dropped_fes; |
85 | |
|
86 | 0 | for (const auto& cur_fe : _frontends) { |
87 | 0 | dropped_fes.insert(cur_fe.first); |
88 | 0 | } |
89 | |
|
90 | 0 | for (const auto& coming_fe_info : new_fe_infos) { |
91 | 0 | auto itr = _frontends.find(coming_fe_info.coordinator_address); |
92 | |
|
93 | 0 | if (itr == _frontends.end()) { |
94 | 0 | LOG(INFO) << "A completely new frontend, " << PrintFrontendInfo(coming_fe_info); |
95 | |
|
96 | 0 | _frontends.insert(std::pair<TNetworkAddress, FrontendInfo>( |
97 | 0 | coming_fe_info.coordinator_address, |
98 | 0 | FrontendInfo {coming_fe_info, GetCurrentTimeMicros() / 1000, /*first time*/ |
99 | 0 | GetCurrentTimeMicros() / 1000 /*last time*/})); |
100 | |
|
101 | 0 | continue; |
102 | 0 | } |
103 | | |
104 | 0 | dropped_fes.erase(coming_fe_info.coordinator_address); |
105 | |
|
106 | 0 | if (coming_fe_info.process_uuid == 0) { |
107 | 0 | LOG(WARNING) << "Frontend " << PrintFrontendInfo(coming_fe_info) |
108 | 0 | << " is in an unknown state."; |
109 | 0 | } |
110 | |
|
111 | 0 | if (coming_fe_info.process_uuid == itr->second.info.process_uuid) { |
112 | 0 | itr->second.last_reveiving_time_ms = GetCurrentTimeMicros() / 1000; |
113 | 0 | continue; |
114 | 0 | } |
115 | | |
116 | | // If we get here, means this frontend has already restarted. |
117 | 0 | itr->second.info.process_uuid = coming_fe_info.process_uuid; |
118 | 0 | itr->second.first_receiving_time_ms = GetCurrentTimeMicros() / 1000; |
119 | 0 | itr->second.last_reveiving_time_ms = GetCurrentTimeMicros() / 1000; |
120 | 0 | LOG(INFO) << "Update frontend " << PrintFrontendInfo(coming_fe_info); |
121 | 0 | } |
122 | |
|
123 | 0 | for (const auto& dropped_fe : dropped_fes) { |
124 | 0 | LOG(INFO) << "Frontend " << PrintThriftNetworkAddress(dropped_fe) |
125 | 0 | << " has already been dropped, remove it"; |
126 | 0 | _frontends.erase(dropped_fe); |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | 6.91k | std::map<TNetworkAddress, FrontendInfo> ExecEnv::get_running_frontends() { |
131 | 6.91k | std::lock_guard<std::mutex> lg(_frontends_lock); |
132 | 6.91k | std::map<TNetworkAddress, FrontendInfo> res; |
133 | 6.91k | const int expired_duration = config::fe_expire_duration_seconds * 1000; |
134 | 6.91k | const auto now = GetCurrentTimeMicros() / 1000; |
135 | | |
136 | 6.91k | for (const auto& pair : _frontends) { |
137 | 0 | auto& brpc_addr = pair.first; |
138 | 0 | auto& fe_info = pair.second; |
139 | |
|
140 | 0 | if (fe_info.info.process_uuid == 0) { |
141 | | // FE is in an unknown state, regart it as alive. conservative |
142 | 0 | res[brpc_addr] = fe_info; |
143 | 0 | } else { |
144 | 0 | if (now - fe_info.last_reveiving_time_ms < expired_duration) { |
145 | | // If fe info has just been update in last expired_duration, regard it as running. |
146 | 0 | res[brpc_addr] = fe_info; |
147 | 0 | } else { |
148 | | // Fe info has not been udpate for more than expired_duration, regard it as an abnormal. |
149 | | // Abnormal means this fe can not connect to master, and it is not dropped from cluster. |
150 | | // or fe do not have master yet. |
151 | 0 | LOG_EVERY_N(WARNING, 50) << fmt::format( |
152 | 0 | "Frontend {}:{} has not update its hb for more than {} secs, regard it as " |
153 | 0 | "abnormal", |
154 | 0 | brpc_addr.hostname, brpc_addr.port, config::fe_expire_duration_seconds); |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | 6.91k | return res; |
160 | 6.91k | } |
161 | | |
162 | 0 | void ExecEnv::wait_for_all_tasks_done() { |
163 | | // For graceful shutdown, need to wait for all running queries to stop |
164 | 0 | int32_t wait_seconds_passed = 0; |
165 | 0 | while (true) { |
166 | 0 | int num_queries = _fragment_mgr->running_query_num(); |
167 | 0 | if (num_queries < 1) { |
168 | 0 | break; |
169 | 0 | } |
170 | 0 | if (wait_seconds_passed > doris::config::grace_shutdown_wait_seconds) { |
171 | 0 | LOG(INFO) << "There are still " << num_queries << " queries running, but " |
172 | 0 | << wait_seconds_passed << " seconds passed, has to exist now"; |
173 | 0 | break; |
174 | 0 | } |
175 | 0 | LOG(INFO) << "There are still " << num_queries << " queries running, waiting..."; |
176 | 0 | sleep(1); |
177 | 0 | ++wait_seconds_passed; |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | 0 | bool ExecEnv::check_auth_token(const std::string& auth_token) { |
182 | 0 | return _cluster_info->curr_auth_token == auth_token || |
183 | 0 | _cluster_info->last_auth_token == auth_token; |
184 | 0 | } |
185 | | |
186 | | } // namespace doris |