Coverage Report

Created: 2024-11-21 17:33

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