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