Coverage Report

Created: 2026-03-12 14:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/backend_options.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 "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
8
bool BackendOptions::init() {
42
8
    if (!analyze_priority_cidrs(config::priority_networks, &_s_priority_cidrs)) {
43
0
        return false;
44
0
    }
45
8
    std::vector<InetAddress> hosts;
46
8
    Status status = get_hosts(&hosts);
47
48
8
    if (!status.ok()) {
49
0
        LOG(FATAL) << status;
50
0
        return false;
51
0
    }
52
53
8
    if (hosts.empty()) {
54
0
        LOG(FATAL) << "failed to get host";
55
0
        return false;
56
0
    }
57
8
    if (!analyze_localhost(_s_localhost, _bind_ipv6, &_s_priority_cidrs, &hosts)) {
58
0
        return false;
59
0
    }
60
8
    if (_bind_ipv6) {
61
0
        _service_bind_address = "[::0]";
62
0
    }
63
8
    LOG(INFO) << "local host ip=" << _s_localhost;
64
8
    return true;
65
8
}
66
67
9.06M
const std::string& BackendOptions::get_localhost() {
68
9.06M
    return _s_localhost;
69
9.06M
}
70
71
885k
std::string BackendOptions::get_be_endpoint() {
72
885k
    return _s_localhost + ":" + std::to_string(config::heartbeat_service_port);
73
885k
}
74
75
38.8k
TBackend BackendOptions::get_local_backend() {
76
38.8k
    TBackend backend;
77
38.8k
    backend.__set_host(_s_localhost);
78
38.8k
    backend.__set_be_port(config::be_port);
79
38.8k
    backend.__set_http_port(config::webserver_port);
80
38.8k
    backend.__set_brpc_port(config::brpc_port);
81
38.8k
    backend.__set_id(_s_backend_id);
82
38.8k
    return backend;
83
38.8k
}
84
85
581
void BackendOptions::set_backend_id(int64_t backend_id) {
86
581
    _s_backend_id = backend_id;
87
581
}
88
89
3
void BackendOptions::set_localhost(const std::string& host) {
90
3
    _s_localhost = host;
91
3
}
92
93
108
bool BackendOptions::is_bind_ipv6() {
94
108
    return _bind_ipv6;
95
108
}
96
97
38
const char* BackendOptions::get_service_bind_address() {
98
38
    return _service_bind_address;
99
38
}
100
101
14
const char* BackendOptions::get_service_bind_address_without_bracket() {
102
14
    if (_bind_ipv6) {
103
0
        return "::0";
104
0
    }
105
14
    return _service_bind_address;
106
14
}
107
108
bool BackendOptions::analyze_priority_cidrs(const std::string& priority_networks,
109
11
                                            std::vector<CIDR>* cidrs) {
110
11
    if (priority_networks.empty()) {
111
5
        return true;
112
5
    }
113
11
    LOG(INFO) << "priority cidrs: " << priority_networks;
114
115
6
    std::vector<std::string> cidr_strs = absl::StrSplit(priority_networks, PRIORITY_CIDR_SEPARATOR);
116
117
6
    for (auto& cidr_str : cidr_strs) {
118
6
        CIDR cidr;
119
6
        if (!cidr.reset(cidr_str)) {
120
0
            LOG(FATAL) << "wrong cidr format. cidr_str=" << cidr_str;
121
0
            return false;
122
0
        }
123
6
        cidrs->push_back(cidr);
124
6
    }
125
6
    return true;
126
6
}
127
128
bool BackendOptions::analyze_localhost(std::string& localhost, bool& bind_ipv6,
129
11
                                       std::vector<CIDR>* cidrs, std::vector<InetAddress>* hosts) {
130
11
    auto addr_it = hosts->begin();
131
11
    if (!cidrs->empty()) {
132
10
        for (; addr_it != hosts->end(); ++addr_it) {
133
10
            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
10
            if (is_in_prior_network(addr_it->get_host_address())) {
137
6
                localhost = addr_it->get_host_address();
138
6
                bind_ipv6 = addr_it->is_ipv6();
139
6
                break;
140
6
            }
141
10
            LOG(INFO) << "skip ip not belonged to priority networks: "
142
4
                      << addr_it->get_host_address();
143
4
        }
144
6
        if (localhost.empty()) {
145
0
            LOG(FATAL) << "fail to find one valid address, exit.";
146
0
            return false;
147
0
        }
148
6
    } else {
149
5
        std::string loopback;
150
11
        for (; addr_it != hosts->end(); ++addr_it) {
151
10
            if ((*addr_it).is_loopback()) {
152
5
                loopback = addr_it->get_host_address();
153
5
                _bind_ipv6 = addr_it->is_ipv6();
154
5
            } else if (!addr_it->is_ipv6()) {
155
4
                localhost = addr_it->get_host_address();
156
4
                _bind_ipv6 = addr_it->is_ipv6();
157
4
                break;
158
4
            }
159
10
        }
160
5
        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
5
    }
165
11
    return true;
166
11
}
167
168
10
bool BackendOptions::is_in_prior_network(const std::string& ip) {
169
10
    for (auto& cidr : _s_priority_cidrs) {
170
10
        CIDR _ip;
171
10
        _ip.reset(ip);
172
10
        if (cidr.contains(_ip)) {
173
6
            return true;
174
6
        }
175
10
    }
176
4
    return false;
177
10
}
178
179
} // namespace doris