Coverage Report

Created: 2025-04-15 11:51

/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
161
const std::string& BackendOptions::get_localhost() {
67
161
    return _s_localhost;
68
161
}
69
70
0
TBackend BackendOptions::get_local_backend() {
71
0
    TBackend backend;
72
0
    backend.__set_host(_s_localhost);
73
0
    backend.__set_be_port(config::be_port);
74
0
    backend.__set_http_port(config::webserver_port);
75
0
    backend.__set_brpc_port(config::brpc_port);
76
0
    backend.__set_id(_s_backend_id);
77
0
    return backend;
78
0
}
79
80
0
void BackendOptions::set_backend_id(int64_t backend_id) {
81
0
    _s_backend_id = backend_id;
82
0
}
83
84
0
void BackendOptions::set_localhost(const std::string& host) {
85
0
    _s_localhost = host;
86
0
}
87
88
1
bool BackendOptions::is_bind_ipv6() {
89
1
    return _bind_ipv6;
90
1
}
91
92
2
const char* BackendOptions::get_service_bind_address() {
93
2
    return _service_bind_address;
94
2
}
95
96
0
const char* BackendOptions::get_service_bind_address_without_bracket() {
97
0
    if (_bind_ipv6) {
98
0
        return "::0";
99
0
    }
100
0
    return _service_bind_address;
101
0
}
102
103
bool BackendOptions::analyze_priority_cidrs(const std::string& priority_networks,
104
4
                                            std::vector<CIDR>* cidrs) {
105
4
    if (priority_networks == "") {
106
4
        return true;
107
4
    }
108
0
    LOG(INFO) << "priority cidrs: " << priority_networks;
109
110
0
    std::vector<std::string> cidr_strs = strings::Split(priority_networks, PRIORITY_CIDR_SEPARATOR);
111
112
0
    for (auto& cidr_str : cidr_strs) {
113
0
        CIDR cidr;
114
0
        if (!cidr.reset(cidr_str)) {
115
0
            LOG(FATAL) << "wrong cidr format. cidr_str=" << cidr_str;
116
0
            return false;
117
0
        }
118
0
        cidrs->push_back(cidr);
119
0
    }
120
0
    return true;
121
0
}
122
123
bool BackendOptions::analyze_localhost(std::string& localhost, bool& bind_ipv6,
124
4
                                       std::vector<CIDR>* cidrs, std::vector<InetAddress>* hosts) {
125
4
    std::vector<InetAddress>::iterator addr_it = hosts->begin();
126
4
    if (!cidrs->empty()) {
127
0
        for (; addr_it != hosts->end(); ++addr_it) {
128
0
            VLOG_CRITICAL << "check ip=" << addr_it->get_host_address();
129
            // Whether to use IPV4 or IPV6, it's configured by CIDR format.
130
            // If both IPV4 and IPV6 are configured, the config order decides priority.
131
0
            if (is_in_prior_network(addr_it->get_host_address())) {
132
0
                localhost = addr_it->get_host_address();
133
0
                bind_ipv6 = addr_it->is_ipv6();
134
0
                break;
135
0
            }
136
0
            LOG(INFO) << "skip ip not belonged to priority networks: "
137
0
                      << addr_it->get_host_address();
138
0
        }
139
0
        if (localhost.empty()) {
140
0
            LOG(FATAL) << "fail to find one valid address, exit.";
141
0
            return false;
142
0
        }
143
4
    } else {
144
4
        std::string loopback;
145
9
        for (; addr_it != hosts->end(); ++addr_it) {
146
8
            if ((*addr_it).is_loopback()) {
147
4
                loopback = addr_it->get_host_address();
148
4
                _bind_ipv6 = addr_it->is_ipv6();
149
4
            } else if (!addr_it->is_ipv6()) {
150
3
                localhost = addr_it->get_host_address();
151
3
                _bind_ipv6 = addr_it->is_ipv6();
152
3
                break;
153
3
            }
154
8
        }
155
4
        if (localhost.empty()) {
156
1
            LOG(INFO) << "fail to find one valid non-loopback address, use loopback address.";
157
1
            localhost = loopback;
158
1
        }
159
4
    }
160
4
    return true;
161
4
}
162
163
0
bool BackendOptions::is_in_prior_network(const std::string& ip) {
164
0
    for (auto& cidr : _s_priority_cidrs) {
165
0
        CIDR _ip;
166
0
        _ip.reset(ip);
167
0
        if (cidr.contains(_ip)) {
168
0
            return true;
169
0
        }
170
0
    }
171
0
    return false;
172
0
}
173
174
} // namespace doris