Skip to content

Commit 5f8d7bd

Browse files
committed
Refactoring part of the code.
1 parent eadd368 commit 5f8d7bd

File tree

7 files changed

+51
-44
lines changed

7 files changed

+51
-44
lines changed

src/common/network/NetworkUtils.cpp

+36-28
Original file line numberDiff line numberDiff line change
@@ -215,24 +215,17 @@ std::string NetworkUtils::intToIPv4(IPv4 ip) {
215215
return buf;
216216
}
217217

218-
StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr(folly::StringPiece ip, int32_t port) {
218+
StatusOr<std::vector<HostAddr>> NetworkUtils::resolveHost(const std::string &ip, int32_t port) {
219219
std::vector<HostAddr> addrs;
220-
IPv4 ipV4;
221-
std::string str = ip.toString();
222-
if (ipv4ToInt(str, ipV4)) {
223-
addrs.emplace_back(std::move(ipV4), port);
224-
return addrs;
225-
}
226-
227220
struct addrinfo hints, *res, *rp;
228221
::memset(&hints, 0, sizeof(struct addrinfo));
229222

230223
hints.ai_family = AF_UNSPEC;
231224
hints.ai_socktype = SOCK_STREAM;
232225
hints.ai_flags = AI_ADDRCONFIG;
233226

234-
if (getaddrinfo(str.c_str(), nullptr, &hints, &res) != 0) {
235-
return Status::Error("host not found:%s", ip.start());
227+
if (getaddrinfo(ip.c_str(), nullptr, &hints, &res) != 0) {
228+
return Status::Error("host not found:%s", ip.c_str());
236229
}
237230

238231
for (rp = res; rp != nullptr; rp = rp->ai_next) {
@@ -255,26 +248,19 @@ StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr(folly::StringPiece ip,
255248
freeaddrinfo(res);
256249

257250
if (addrs.empty()) {
258-
return Status::Error("host not found: %s", str.c_str());
251+
return Status::Error("host not found: %s", ip.c_str());
259252
}
260253

261254
return addrs;
262255
}
263256

264-
StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr(folly::StringPiece ipPort) {
265-
auto pos = ipPort.find(':');
266-
if (pos == folly::StringPiece::npos) {
267-
return Status::Error("Bad peer format: %s", ipPort.start());
268-
}
269257

270-
int32_t port;
271-
try {
272-
port = folly::to<int32_t>(ipPort.subpiece(pos + 1));
273-
} catch (const std::exception& ex) {
274-
return Status::Error("Bad port number, error: %s", ex.what());
258+
StatusOr<HostAddr> NetworkUtils::toHostAddr(const std::string &ip, int32_t port) {
259+
IPv4 ipV4;
260+
if (!ipv4ToInt(ip, ipV4)) {
261+
return Status::Error("Bad ip format:%s", ip.c_str());
275262
}
276-
277-
return toHostAddr(ipPort.subpiece(0, pos), port);
263+
return std::make_pair(ipV4, port);
278264
}
279265

280266
StatusOr<std::vector<HostAddr>> NetworkUtils::toHosts(const std::string& peersStr) {
@@ -283,12 +269,34 @@ StatusOr<std::vector<HostAddr>> NetworkUtils::toHosts(const std::string& peersSt
283269
folly::split(",", peersStr, peers, true);
284270
hosts.reserve(peers.size());
285271
for (auto& peerStr : peers) {
286-
auto hostAddr = network::NetworkUtils::toHostAddr(folly::trimWhitespace(peerStr));
287-
if (!hostAddr.ok()) {
288-
return hostAddr.status();
272+
auto ipPort = folly::trimWhitespace(peerStr);
273+
auto pos = ipPort.find(':');
274+
if (pos == folly::StringPiece::npos) {
275+
return Status::Error("Bad peer format: %s", ipPort.start());
289276
}
290-
hosts.insert(hosts.end(), std::make_move_iterator(hostAddr.value().begin()),
291-
std::make_move_iterator(hostAddr.value().end()));
277+
278+
int32_t port;
279+
try {
280+
port = folly::to<int32_t>(ipPort.subpiece(pos + 1));
281+
} catch (const std::exception& ex) {
282+
return Status::Error("Bad port number, error: %s", ex.what());
283+
}
284+
285+
auto ipAddr = ipPort.subpiece(0, pos).toString();
286+
auto hostAddr = toHostAddr(ipAddr, port);
287+
if (hostAddr.ok()) {
288+
hosts.emplace_back(hostAddr.value());
289+
continue;
290+
}
291+
292+
auto resolveAddr = resolveHost(ipAddr, port);
293+
if (resolveAddr.ok()) {
294+
hosts.insert(hosts.end(), std::make_move_iterator(resolveAddr.value().begin()),
295+
std::make_move_iterator(resolveAddr.value().end()));
296+
continue;
297+
}
298+
299+
return resolveAddr.status();
292300
}
293301
return hosts;
294302
}

src/common/network/NetworkUtils.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ class NetworkUtils final {
3333
// Get a dynamic port that is not in use
3434
static uint16_t getAvailablePort();
3535

36+
static StatusOr<std::vector<HostAddr>> resolveHost(const std::string &ip, int32_t port);
3637
// Convert the given IP/HOST and Port to a HostAddr
37-
static StatusOr<std::vector<HostAddr>> toHostAddr(folly::StringPiece ip, int32_t port);
38-
// Convert the given (IP/HOST):Port to a HostAddr
39-
static StatusOr<std::vector<HostAddr>> toHostAddr(folly::StringPiece ipPort);
38+
static StatusOr<HostAddr> toHostAddr(const std::string &ip, int32_t port);
4039
// Retrieve the string-form IP from the given HostAddr
4140
static std::string ipFromHostAddr(const HostAddr& host);
4241
// Retrieve the port number from the given HostAddr

src/common/network/test/NetworkUtilsTest.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,24 @@ TEST(NetworkUtils, getAvailablePort) {
8888
}
8989

9090
TEST(NetworkUtils, toHostAddr) {
91-
auto s = NetworkUtils::toHostAddr("localhost:1200");
91+
auto s = NetworkUtils::resolveHost("localhost", 1200);
9292
ASSERT_TRUE(s.ok());
9393
auto addr = s.value();
9494
IPv4 ip;
9595
ASSERT_TRUE(NetworkUtils::ipv4ToInt("127.0.0.1", ip));
9696
ASSERT_EQ(addr[0].first, ip);
9797
ASSERT_EQ(addr[0].second, 1200);
9898

99-
s = NetworkUtils::toHostAddr("8.8.8.8:1300");
100-
ASSERT_TRUE(s.ok());
101-
addr = s.value();
99+
auto s2 = NetworkUtils::toHostAddr("8.8.8.8", 1300);
100+
ASSERT_TRUE(s2.ok());
101+
auto addr2 = s2.value();
102102

103103
ASSERT_TRUE(NetworkUtils::ipv4ToInt("8.8.8.8", ip));
104-
ASSERT_EQ(addr[0].first, ip);
105-
ASSERT_EQ(addr[0].second, 1300);
104+
ASSERT_EQ(addr2.first, ip);
105+
ASSERT_EQ(addr2.second, 1300);
106106

107-
s = NetworkUtils::toHostAddr("a.b.c.d:a23");
108-
ASSERT_FALSE(s.ok());
107+
s2 = NetworkUtils::toHostAddr("a.b.c.d:a23", 1200);
108+
ASSERT_FALSE(s2.ok());
109109
}
110110

111111
TEST(NetworkUtils, toHosts) {
@@ -121,7 +121,7 @@ TEST(NetworkUtils, toHosts) {
121121
ASSERT_EQ(addr[1].first, ip);
122122
ASSERT_EQ(addr[1].second, 1200);
123123

124-
s = NetworkUtils::toHostAddr("1.1.2.3:123, a.b.c.d:a23");
124+
s = NetworkUtils::toHosts("1.1.2.3:123, a.b.c.d:a23");
125125
ASSERT_FALSE(s.ok());
126126
}
127127

src/daemons/MetaDaemon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ int main(int argc, char *argv[]) {
203203
LOG(ERROR) << "Bad local host addr, status:" << hostAddrRet.status();
204204
return EXIT_FAILURE;
205205
}
206-
auto& localhost = hostAddrRet.value()[0];
206+
auto& localhost = hostAddrRet.value();
207207
auto peersRet = nebula::network::NetworkUtils::toHosts(FLAGS_meta_server_addrs);
208208
if (!peersRet.ok()) {
209209
LOG(ERROR) << "Can't get peers address, status:" << peersRet.status();

src/daemons/StorageDaemon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
104104
return EXIT_FAILURE;
105105
}
106106

107-
gStorageServer = std::make_unique<nebula::storage::StorageServer>(hostRet.value()[0],
107+
gStorageServer = std::make_unique<nebula::storage::StorageServer>(hostRet.value(),
108108
metaAddrsRet.value(),
109109
paths);
110110
if (!gStorageServer->start()) {

src/graph/test/TestEnv.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void TestEnv::SetUp() {
4646
if (!hostRet.ok()) {
4747
LOG(ERROR) << "Bad local host addr, status:" << hostRet.status();
4848
}
49-
auto& localhost = hostRet.value()[0];
49+
auto& localhost = hostRet.value();
5050

5151
mClient_ = std::make_unique<meta::MetaClient>(threadPool,
5252
std::move(addrsRet.value()),

src/storage/test/StorageClientTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TEST(StorageClientTest, VerticesInterfacesTest) {
4848
auto& addrs = addrsRet.value();
4949
uint32_t localDataPort = network::NetworkUtils::getAvailablePort();
5050
auto hostRet = nebula::network::NetworkUtils::toHostAddr("127.0.0.1", localDataPort);
51-
auto& localHost = hostRet.value()[0];
51+
auto& localHost = hostRet.value();
5252
auto mClient = std::make_unique<meta::MetaClient>(threadPool,
5353
std::move(addrs),
5454
localHost,

0 commit comments

Comments
 (0)