Coverage Report

Created: 2025-05-09 16:48

/root/doris/be/src/service/backend_options.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 "service/backend_options.h"
19
20
#include <absl/strings/str_split.h>
21
22
#include <algorithm>
23
#include <ostream>
24
25
#include "common/config.h"
26
#include "common/logging.h"
27
#include "common/status.h"
28
#include "util/cidr.h"
29
#include "util/network_util.h"
30
31
namespace doris {
32
33
static const std::string PRIORITY_CIDR_SEPARATOR = ";";
34
35
std::string BackendOptions::_s_localhost;
36
std::vector<CIDR> BackendOptions::_s_priority_cidrs;
37
bool BackendOptions::_bind_ipv6 = false;
38
const char* _service_bind_address = "0.0.0.0";
39
int64_t BackendOptions::_s_backend_id = 0;
40
41
1
bool BackendOptions::init() {
42
1
    if (!analyze_priority_cidrs(config::priority_networks, &_s_priority_cidrs)) {
43
0
        return false;
44
0
    }
45
1
    std::vector<InetAddress> hosts;
46
1
    Status status = get_hosts(&hosts);
47
48
1
    if (!status.ok()) {
49
0
        LOG(FATAL) << status;
50
0
        return false;
51
0
    }
52
53
1
    if (hosts.empty()) {
54
0
        LOG(FATAL) << "failed to get host";
55
0
        return false;
56
0
    }
57
1
    if (!analyze_localhost(_s_localhost, _bind_ipv6, &_s_priority_cidrs, &hosts)) {
58
0
        return false;
59
0
    }
60
1
    if (_bind_ipv6) {
61
0
        _service_bind_address = "[::0]";
62
0
    }
63
1
    LOG(INFO) << "local host ip=" << _s_localhost;
64
1
    return true;
65
1
}
66
67
246
const std::string& BackendOptions::get_localhost() {
68
246
    return _s_localhost;
69
246
}
70
71
0
std::string BackendOptions::get_be_endpoint() {
72
0
    return _s_localhost + ":" + std::to_string(config::heartbeat_service_port);
73
0
}
74
75
0
TBackend BackendOptions::get_local_backend() {
76
0
    TBackend backend;
77
0
    backend.__set_host(_s_localhost);
78
0
    backend.__set_be_port(config::be_port);
79
0
    backend.__set_http_port(config::webserver_port);
80
0
    backend.__set_brpc_port(config::brpc_port);
81
0
    backend.__set_id(_s_backend_id);
82
0
    return backend;
83
0
}
84
85
0
void BackendOptions::set_backend_id(int64_t backend_id) {
86
0
    _s_backend_id = backend_id;
87
0
}
88
89
0
void BackendOptions::set_localhost(const std::string& host) {
90
0
    _s_localhost = host;
91
0
}
92
93
1
bool BackendOptions::is_bind_ipv6() {
94
1
    return _bind_ipv6;
95
1
}
96
97
3
const char* BackendOptions::get_service_bind_address() {
98
3
    return _service_bind_address;
99
3
}
100
101
0
const char* BackendOptions::get_service_bind_address_without_bracket() {
102
0
    if (_bind_ipv6) {
103
0
        return "::0";
104
0
    }
105
0
    return _service_bind_address;
106
0
}
107
108
bool BackendOptions::analyze_priority_cidrs(const std::string& priority_networks,
109
4
                                            std::vector<CIDR>* cidrs) {
110
4
    if (priority_networks.empty()) {
111
4
        return true;
112
4
    }
113
0
    LOG(INFO) << "priority cidrs: " << priority_networks;
114
115
0
    std::vector<std::string> cidr_strs = absl::StrSplit(priority_networks, PRIORITY_CIDR_SEPARATOR);
116
117
0
    for (auto& cidr_str : cidr_strs) {
118
0
        CIDR cidr;
119
0
        if (!cidr.reset(cidr_str)) {
120
0
            LOG(FATAL) << "wrong cidr format. cidr_str=" << cidr_str;
121
0
            return false;
122
0
        }
123
0
        cidrs->push_back(cidr);
124
0
    }
125
0
    return true;
126
0
}
127
128
bool BackendOptions::analyze_localhost(std::string& localhost, bool& bind_ipv6,
129
4
                                       std::vector<CIDR>* cidrs, std::vector<InetAddress>* hosts) {
130
4
    auto addr_it = hosts->begin();
131
4
    if (!cidrs->empty()) {
132
0
        for (; addr_it != hosts->end(); ++addr_it) {
133
0
            VLOG_CRITICAL << "check ip=" << addr_it->get_host_address();
134
            // Whether to use IPV4 or IPV6, it's configured by CIDR format.
135
            // If both IPV4 and IPV6 are configured, the config order decides priority.
136
0
            if (is_in_prior_network(addr_it->get_host_address())) {
137
0
                localhost = addr_it->get_host_address();
138
0
                bind_ipv6 = addr_it->is_ipv6();
139
0
                break;
140
0
            }
141
0
            LOG(INFO) << "skip ip not belonged to priority networks: "
142
0
                      << addr_it->get_host_address();
143
0
        }
144
0
        if (localhost.empty()) {
145
0
            LOG(FATAL) << "fail to find one valid address, exit.";
146
0
            return false;
147
0
        }
148
4
    } else {
149
4
        std::string loopback;
150
9
        for (; addr_it != hosts->end(); ++addr_it) {
151
8
            if ((*addr_it).is_loopback()) {
152
4
                loopback = addr_it->get_host_address();
153
4
                _bind_ipv6 = addr_it->is_ipv6();
154
4
            } else if (!addr_it->is_ipv6()) {
155
3
                localhost = addr_it->get_host_address();
156
3
                _bind_ipv6 = addr_it->is_ipv6();
157
3
                break;
158
3
            }
159
8
        }
160
4
        if (localhost.empty()) {
161
1
            LOG(INFO) << "fail to find one valid non-loopback address, use loopback address.";
162
1
            localhost = loopback;
163
1
        }
164
4
    }
165
4
    return true;
166
4
}
167
168
0
bool BackendOptions::is_in_prior_network(const std::string& ip) {
169
0
    for (auto& cidr : _s_priority_cidrs) {
170
0
        CIDR _ip;
171
0
        _ip.reset(ip);
172
0
        if (cidr.contains(_ip)) {
173
0
            return true;
174
0
        }
175
0
    }
176
0
    return false;
177
0
}
178
179
} // namespace doris