be/src/runtime/exec_env.cpp
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 | | #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 "exec/exchange/vdata_stream_mgr.h" |
29 | | #include "exec/sink/delta_writer_v2_pool.h" |
30 | | #include "exec/sink/load_stream_map_pool.h" |
31 | | #include "load/channel/load_stream_mgr.h" |
32 | | #include "runtime/fragment_mgr.h" |
33 | | #include "runtime/frontend_info.h" |
34 | | #include "storage/index/index_writer.h" // TmpFileDirs, completed here rather than in the header |
35 | | #include "storage/olap_define.h" |
36 | | #include "storage/storage_engine.h" |
37 | | #include "storage/tablet/tablet_manager.h" |
38 | | #include "util/debug_util.h" |
39 | | #include "util/time.h" |
40 | | |
41 | | namespace doris { |
42 | | |
43 | | #ifdef BE_TEST |
44 | | void ExecEnv::set_inverted_index_searcher_cache( |
45 | 287 | segment_v2::InvertedIndexSearcherCache* inverted_index_searcher_cache) { |
46 | 287 | _inverted_index_searcher_cache = inverted_index_searcher_cache; |
47 | 287 | } |
48 | 329 | void ExecEnv::set_tmp_file_dir(std::unique_ptr<segment_v2::TmpFileDirs> tmp_file_dirs) { |
49 | 329 | _tmp_file_dirs = std::move(tmp_file_dirs); |
50 | 329 | } |
51 | 719 | void ExecEnv::set_storage_engine(std::unique_ptr<BaseStorageEngine>&& engine) { |
52 | 719 | _storage_engine = std::move(engine); |
53 | 719 | } |
54 | 1 | void ExecEnv::set_write_cooldown_meta_executors() { |
55 | 1 | _write_cooldown_meta_executors = std::make_unique<WriteCooldownMetaExecutors>(); |
56 | 1 | } |
57 | | #endif // BE_TEST |
58 | | |
59 | | Result<BaseTabletSPtr> ExecEnv::get_tablet(int64_t tablet_id, SyncRowsetStats* sync_stats, |
60 | 57 | bool force_use_only_cached, bool cache_on_miss) { |
61 | 57 | auto storage_engine = GetInstance()->_storage_engine.get(); |
62 | 57 | return storage_engine != nullptr |
63 | 57 | ? storage_engine->get_tablet(tablet_id, sync_stats, force_use_only_cached, |
64 | 50 | cache_on_miss) |
65 | 57 | : ResultError(Status::InternalError("failed to get tablet {}", tablet_id)); |
66 | 57 | } |
67 | | |
68 | | Status ExecEnv::get_tablet_meta(int64_t tablet_id, TabletMetaSharedPtr* tablet_meta, |
69 | 50 | bool force_use_only_cached) { |
70 | 50 | auto storage_engine = GetInstance()->_storage_engine.get(); |
71 | 50 | if (storage_engine == nullptr) { |
72 | 0 | return Status::InternalError("storage engine is not initialized"); |
73 | 0 | } |
74 | 50 | return storage_engine->get_tablet_meta(tablet_id, tablet_meta, force_use_only_cached); |
75 | 50 | } |
76 | | |
77 | 31 | const std::string& ExecEnv::token() const { |
78 | 31 | return _cluster_info->token; |
79 | 31 | } |
80 | | |
81 | 0 | void ExecEnv::clear_stream_mgr() { |
82 | 0 | if (_vstream_mgr) { |
83 | 0 | SAFE_DELETE(_vstream_mgr); |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | 0 | std::vector<TFrontendInfo> ExecEnv::get_frontends() { |
88 | 0 | std::lock_guard<std::mutex> lg(_frontends_lock); |
89 | 0 | std::vector<TFrontendInfo> infos; |
90 | 0 | for (const auto& cur_fe : _frontends) { |
91 | 0 | infos.push_back(cur_fe.second.info); |
92 | 0 | } |
93 | 0 | return infos; |
94 | 0 | } |
95 | | |
96 | 4 | void ExecEnv::update_frontends(const std::vector<TFrontendInfo>& new_fe_infos) { |
97 | 4 | std::lock_guard<std::mutex> lg(_frontends_lock); |
98 | | |
99 | 4 | std::set<TNetworkAddress> dropped_fes; |
100 | | |
101 | 4 | for (const auto& cur_fe : _frontends) { |
102 | 0 | dropped_fes.insert(cur_fe.first); |
103 | 0 | } |
104 | | |
105 | 4 | for (const auto& coming_fe_info : new_fe_infos) { |
106 | 0 | auto itr = _frontends.find(coming_fe_info.coordinator_address); |
107 | |
|
108 | 0 | if (itr == _frontends.end()) { |
109 | 0 | LOG(INFO) << "A completely new frontend, " << PrintFrontendInfo(coming_fe_info); |
110 | |
|
111 | 0 | _frontends.insert(std::pair<TNetworkAddress, FrontendInfo>( |
112 | 0 | coming_fe_info.coordinator_address, |
113 | 0 | FrontendInfo {coming_fe_info, GetCurrentTimeMicros() / 1000, /*first time*/ |
114 | 0 | GetCurrentTimeMicros() / 1000 /*last time*/})); |
115 | |
|
116 | 0 | continue; |
117 | 0 | } |
118 | | |
119 | 0 | dropped_fes.erase(coming_fe_info.coordinator_address); |
120 | |
|
121 | 0 | if (coming_fe_info.process_uuid == 0) { |
122 | 0 | LOG(WARNING) << "Frontend " << PrintFrontendInfo(coming_fe_info) |
123 | 0 | << " is in an unknown state."; |
124 | 0 | } |
125 | |
|
126 | 0 | if (coming_fe_info.process_uuid == itr->second.info.process_uuid) { |
127 | 0 | itr->second.last_reveiving_time_ms = GetCurrentTimeMicros() / 1000; |
128 | 0 | continue; |
129 | 0 | } |
130 | | |
131 | | // If we get here, means this frontend has already restarted. |
132 | 0 | itr->second.info.process_uuid = coming_fe_info.process_uuid; |
133 | 0 | itr->second.first_receiving_time_ms = GetCurrentTimeMicros() / 1000; |
134 | 0 | itr->second.last_reveiving_time_ms = GetCurrentTimeMicros() / 1000; |
135 | 0 | LOG(INFO) << "Update frontend " << PrintFrontendInfo(coming_fe_info); |
136 | 0 | } |
137 | | |
138 | 4 | for (const auto& dropped_fe : dropped_fes) { |
139 | 0 | LOG(INFO) << "Frontend " << PrintThriftNetworkAddress(dropped_fe) |
140 | 0 | << " has already been dropped, remove it"; |
141 | 0 | _frontends.erase(dropped_fe); |
142 | 0 | } |
143 | 4 | } |
144 | | |
145 | 18 | std::map<TNetworkAddress, FrontendInfo> ExecEnv::get_running_frontends() { |
146 | 18 | std::lock_guard<std::mutex> lg(_frontends_lock); |
147 | 18 | std::map<TNetworkAddress, FrontendInfo> res; |
148 | 18 | const int expired_duration = config::fe_expire_duration_seconds * 1000; |
149 | 18 | const auto now = GetCurrentTimeMicros() / 1000; |
150 | | |
151 | 18 | for (const auto& pair : _frontends) { |
152 | 0 | auto& brpc_addr = pair.first; |
153 | 0 | auto& fe_info = pair.second; |
154 | |
|
155 | 0 | if (fe_info.info.process_uuid == 0) { |
156 | | // FE is in an unknown state, regart it as alive. conservative |
157 | 0 | res[brpc_addr] = fe_info; |
158 | 0 | } else { |
159 | 0 | if (now - fe_info.last_reveiving_time_ms < expired_duration) { |
160 | | // If fe info has just been update in last expired_duration, regard it as running. |
161 | 0 | res[brpc_addr] = fe_info; |
162 | 0 | } else { |
163 | | // Fe info has not been udpate for more than expired_duration, regard it as an abnormal. |
164 | | // Abnormal means this fe can not connect to master, and it is not dropped from cluster. |
165 | | // or fe do not have master yet. |
166 | 0 | LOG_EVERY_N(WARNING, 50) << fmt::format( |
167 | 0 | "Frontend {}:{} has not update its hb for more than {} secs, regard it as " |
168 | 0 | "abnormal", |
169 | 0 | brpc_addr.hostname, brpc_addr.port, config::fe_expire_duration_seconds); |
170 | 0 | } |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | 18 | return res; |
175 | 18 | } |
176 | | |
177 | 0 | void ExecEnv::wait_for_all_tasks_done() { |
178 | | // For graceful shutdown, need to wait for all running queries to stop |
179 | 0 | int32_t wait_seconds_passed = 0; |
180 | 0 | while (true) { |
181 | 0 | int num_queries = _fragment_mgr->running_query_num(); |
182 | 0 | if (num_queries < 1) { |
183 | 0 | break; |
184 | 0 | } |
185 | 0 | if (wait_seconds_passed > doris::config::grace_shutdown_wait_seconds) { |
186 | 0 | LOG(INFO) << "There are still " << num_queries << " queries running, but " |
187 | 0 | << wait_seconds_passed << " seconds passed, has to exist now"; |
188 | 0 | break; |
189 | 0 | } |
190 | 0 | LOG(INFO) << "There are still " << num_queries << " queries running, waiting..."; |
191 | 0 | sleep(1); |
192 | 0 | ++wait_seconds_passed; |
193 | 0 | } |
194 | | // This is a conservative strategy. |
195 | | // Because a query might still have fragments running on other BE nodes. |
196 | | // In other words, the query hasn't truly terminated. |
197 | | // If the current BE is shut down at this point, |
198 | | // the FE will detect the downtime of a related BE and cancel the entire query, |
199 | | // defeating the purpose of a graceful stop. |
200 | 0 | sleep(config::grace_shutdown_post_delay_seconds); |
201 | 0 | } |
202 | | |
203 | 0 | bool ExecEnv::check_auth_token(const std::string& auth_token) { |
204 | 0 | return _cluster_info->curr_auth_token == auth_token || |
205 | 0 | _cluster_info->last_auth_token == auth_token; |
206 | 0 | } |
207 | | |
208 | | } // namespace doris |