Coverage Report

Created: 2026-07-31 14:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
        segment_v2::InvertedIndexSearcherCache* inverted_index_searcher_cache) {
46
    _inverted_index_searcher_cache = inverted_index_searcher_cache;
47
}
48
void ExecEnv::set_tmp_file_dir(std::unique_ptr<segment_v2::TmpFileDirs> tmp_file_dirs) {
49
    _tmp_file_dirs = std::move(tmp_file_dirs);
50
}
51
void ExecEnv::set_storage_engine(std::unique_ptr<BaseStorageEngine>&& engine) {
52
    _storage_engine = std::move(engine);
53
}
54
void ExecEnv::set_write_cooldown_meta_executors() {
55
    _write_cooldown_meta_executors = std::make_unique<WriteCooldownMetaExecutors>();
56
}
57
#endif // BE_TEST
58
59
Result<BaseTabletSPtr> ExecEnv::get_tablet(int64_t tablet_id, SyncRowsetStats* sync_stats,
60
1.67M
                                           bool force_use_only_cached, bool cache_on_miss) {
61
1.67M
    auto storage_engine = GetInstance()->_storage_engine.get();
62
1.67M
    return storage_engine != nullptr
63
1.67M
                   ? storage_engine->get_tablet(tablet_id, sync_stats, force_use_only_cached,
64
1.67M
                                                cache_on_miss)
65
1.67M
                   : ResultError(Status::InternalError("failed to get tablet {}", tablet_id));
66
1.67M
}
67
68
Status ExecEnv::get_tablet_meta(int64_t tablet_id, TabletMetaSharedPtr* tablet_meta,
69
219k
                                bool force_use_only_cached) {
70
219k
    auto storage_engine = GetInstance()->_storage_engine.get();
71
219k
    if (storage_engine == nullptr) {
72
0
        return Status::InternalError("storage engine is not initialized");
73
0
    }
74
219k
    return storage_engine->get_tablet_meta(tablet_id, tablet_meta, force_use_only_cached);
75
219k
}
76
77
33
const std::string& ExecEnv::token() const {
78
33
    return _cluster_info->token;
79
33
}
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
2.07k
void ExecEnv::update_frontends(const std::vector<TFrontendInfo>& new_fe_infos) {
97
2.07k
    std::lock_guard<std::mutex> lg(_frontends_lock);
98
99
2.07k
    std::set<TNetworkAddress> dropped_fes;
100
101
2.07k
    for (const auto& cur_fe : _frontends) {
102
2.06k
        dropped_fes.insert(cur_fe.first);
103
2.06k
    }
104
105
2.07k
    for (const auto& coming_fe_info : new_fe_infos) {
106
2.07k
        auto itr = _frontends.find(coming_fe_info.coordinator_address);
107
108
2.07k
        if (itr == _frontends.end()) {
109
7
            LOG(INFO) << "A completely new frontend, " << PrintFrontendInfo(coming_fe_info);
110
111
7
            _frontends.insert(std::pair<TNetworkAddress, FrontendInfo>(
112
7
                    coming_fe_info.coordinator_address,
113
7
                    FrontendInfo {coming_fe_info, GetCurrentTimeMicros() / 1000, /*first time*/
114
7
                                  GetCurrentTimeMicros() / 1000 /*last time*/}));
115
116
7
            continue;
117
7
        }
118
119
2.06k
        dropped_fes.erase(coming_fe_info.coordinator_address);
120
121
2.06k
        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
2.06k
        if (coming_fe_info.process_uuid == itr->second.info.process_uuid) {
127
2.06k
            itr->second.last_reveiving_time_ms = GetCurrentTimeMicros() / 1000;
128
2.06k
            continue;
129
2.06k
        }
130
131
        // If we get here, means this frontend has already restarted.
132
2
        itr->second.info.process_uuid = coming_fe_info.process_uuid;
133
2
        itr->second.first_receiving_time_ms = GetCurrentTimeMicros() / 1000;
134
2
        itr->second.last_reveiving_time_ms = GetCurrentTimeMicros() / 1000;
135
2
        LOG(INFO) << "Update frontend " << PrintFrontendInfo(coming_fe_info);
136
2
    }
137
138
2.07k
    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
2.07k
}
144
145
331k
std::map<TNetworkAddress, FrontendInfo> ExecEnv::get_running_frontends() {
146
331k
    std::lock_guard<std::mutex> lg(_frontends_lock);
147
331k
    std::map<TNetworkAddress, FrontendInfo> res;
148
331k
    const int expired_duration = config::fe_expire_duration_seconds * 1000;
149
331k
    const auto now = GetCurrentTimeMicros() / 1000;
150
151
331k
    for (const auto& pair : _frontends) {
152
331k
        auto& brpc_addr = pair.first;
153
331k
        auto& fe_info = pair.second;
154
155
331k
        if (fe_info.info.process_uuid == 0) {
156
            // FE is in an unknown state, regart it as alive. conservative
157
44
            res[brpc_addr] = fe_info;
158
330k
        } else {
159
330k
            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
330k
                res[brpc_addr] = fe_info;
162
330k
            } 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
330k
        }
172
331k
    }
173
174
331k
    return res;
175
331k
}
176
177
3
void ExecEnv::wait_for_all_tasks_done() {
178
    // For graceful shutdown, need to wait for all running queries to stop
179
3
    int32_t wait_seconds_passed = 0;
180
3
    while (true) {
181
3
        int num_queries = _fragment_mgr->running_query_num();
182
3
        if (num_queries < 1) {
183
3
            break;
184
3
        }
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
3
    sleep(config::grace_shutdown_post_delay_seconds);
201
3
}
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