Coverage Report

Created: 2024-11-20 15:52

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