From c30eff073d91fabb7a51c84663da1aad144c5f46 Mon Sep 17 00:00:00 2001 From: swj <1186093704@qq.com> Date: Fri, 7 Jul 2023 14:33:56 +0800 Subject: [PATCH] [feat]curvefs: change hostname to optional Signed-off-by: swj <1186093704@qq.com> --- curvefs/proto/topology.proto | 8 +-- curvefs/src/mds/topology/topology.cpp | 8 ++- curvefs/src/metaserver/register.cpp | 7 +-- .../create/curvefs_create_topology_tool.cpp | 10 ++-- .../mds/topology/test_topology_manager.cpp | 59 +++++++++++++++++++ .../mds/topology/test_topology_service.cpp | 54 +++++++++++++++++ 6 files changed, 132 insertions(+), 14 deletions(-) diff --git a/curvefs/proto/topology.proto b/curvefs/proto/topology.proto index c882499853..c9d70682b5 100644 --- a/curvefs/proto/topology.proto +++ b/curvefs/proto/topology.proto @@ -79,7 +79,7 @@ message ZoneData { message ServerData { required uint32 serverId = 1; - required string hostName = 2; + optional string hostName = 2; required string internalIP = 3; required uint32 internalPort = 4; required string externalIP = 5; @@ -90,7 +90,7 @@ message ServerData { message MetaServerData { required uint32 MetaServerId = 1; - required string hostName = 2; + optional string hostName = 2; required string token = 3; required string internalIP = 4; required uint32 internalPort = 5; @@ -131,7 +131,7 @@ message MetaServerInfo { // MetaServer message message MetaServerRegistRequest { - required string hostName = 1; + optional string hostName = 1; required string internalIp = 2; required uint32 internalPort = 3; required string externalIp = 4; @@ -188,7 +188,7 @@ message ServerInfo { } message ServerRegistRequest { - required string hostName = 1; + optional string hostName = 1; required string internalIp = 2; optional uint32 internalPort = 3; required string externalIp = 4; diff --git a/curvefs/src/mds/topology/topology.cpp b/curvefs/src/mds/topology/topology.cpp index 9e3acbd181..334845580f 100644 --- a/curvefs/src/mds/topology/topology.cpp +++ b/curvefs/src/mds/topology/topology.cpp @@ -1666,8 +1666,12 @@ std::string TopologyImpl::GetHostNameAndPortById(MetaServerIdType msId) { return ""; } - // get hostName of the metaserver - return server.GetHostName() + ":" + std::to_string(ms.GetInternalPort()); + // get hostname of the metaserver + std::string hostname = server.GetHostName(); + if (hostname.empty()) { + hostname = server.GetInternalIp(); + } + return hostname + ":" + std::to_string(ms.GetInternalPort()); } bool TopologyImpl::IsCopysetCreating(const CopySetKey &key) const { diff --git a/curvefs/src/metaserver/register.cpp b/curvefs/src/metaserver/register.cpp index 011c696b6c..26f9d39a39 100644 --- a/curvefs/src/metaserver/register.cpp +++ b/curvefs/src/metaserver/register.cpp @@ -62,11 +62,10 @@ int Register::RegisterToMDS(MetaServerMetadata *metadata) { char hostname[128]; int ret = gethostname(hostname, sizeof(hostname)); if (ret == -1) { - LOG(ERROR) << "get hostname failed!"; - return -1; + LOG(INFO) << "get hostname failed!"; + } else { + req.set_hostname(hostname); } - - req.set_hostname(hostname); req.set_internalip(ops_.metaserverInternalIp); req.set_internalport(ops_.metaserverInternalPort); req.set_externalip(ops_.metaserverExternalIp); diff --git a/curvefs/src/tools/create/curvefs_create_topology_tool.cpp b/curvefs/src/tools/create/curvefs_create_topology_tool.cpp index 0e7d572581..59e7a27588 100644 --- a/curvefs/src/tools/create/curvefs_create_topology_tool.cpp +++ b/curvefs/src/tools/create/curvefs_create_topology_tool.cpp @@ -220,10 +220,10 @@ int CurvefsBuildTopologyTool::InitServerZoneData() { Server serverData; Zone zoneData; if (!server[kName].isString()) { - LOG(ERROR) << "server name must be string"; - return -1; + LOG(ERROR) << "server name should be string"; + } else { + serverData.name = server[kName].asString(); } - serverData.name = server[kName].asString(); if (!server[kInternalIp].isString()) { LOG(ERROR) << "server internal ip must be string"; return -1; @@ -628,7 +628,9 @@ int CurvefsBuildTopologyTool::CreateServer() { for (auto it : serverDatas) { ServerRegistRequest request; ServerRegistResponse response; - request.set_hostname(it.name); + if (!it.name.empty()) { + request.set_hostname(it.name); + } request.set_internalip(it.internalIp); request.set_internalport(it.internalPort); request.set_externalip(it.externalIp); diff --git a/curvefs/test/mds/topology/test_topology_manager.cpp b/curvefs/test/mds/topology/test_topology_manager.cpp index a7794b7011..cd9097a166 100644 --- a/curvefs/test/mds/topology/test_topology_manager.cpp +++ b/curvefs/test/mds/topology/test_topology_manager.cpp @@ -294,6 +294,40 @@ TEST_F(TestTopologyManager, test_RegistMetaServer_SuccessWithExIp) { ASSERT_EQ(TopoStatusCode::TOPO_METASERVER_EXIST, response.statuscode()); } +TEST_F(TestTopologyManager, test_RegistMetaServer_SuccesssWithoutHostname) { + MetaServerIdType msId = 0x41; + ServerIdType serverId = 0x31; + std::string token = "token"; + + PrepareAddPool(); + PrepareAddZone(); + PrepareAddServer(serverId, "", "testInternalIp", 0, "externalIp1", + 0); + + MetaServerRegistRequest request; + request.set_internalip("testInternalIp"); + request.set_internalport(0); + request.set_externalip("externalIp1"); + request.set_externalport(0); + + MetaServerRegistResponse response; + + EXPECT_CALL(*tokenGenerator_, GenToken()).WillOnce(Return(token)); + EXPECT_CALL(*idGenerator_, GenMetaServerId()).WillOnce(Return(msId)); + + EXPECT_CALL(*storage_, StorageMetaServer(_)).WillOnce(Return(true)); + serviceManager_->RegistMetaServer(&request, &response); + + ASSERT_EQ(TopoStatusCode::TOPO_OK, response.statuscode()); + ASSERT_TRUE(response.has_metaserverid()); + ASSERT_EQ(msId, response.metaserverid()); + ASSERT_TRUE(response.has_token()); + ASSERT_EQ(token, response.token()); + MetaServer metaserver; + ASSERT_TRUE(topology_->GetMetaServer(msId, &metaserver)); + ASSERT_EQ("externalIp1", metaserver.GetExternalIp()); +} + TEST_F(TestTopologyManager, test_RegistMetaServer_SuccessWithOfflineMs) { MetaServerIdType msId = 0x41; MetaServerIdType msId2 = 0x42; @@ -604,6 +638,31 @@ TEST_F(TestTopologyManager, test_DeleteMetaServer_MetaServerNotFound) { ASSERT_EQ(TopoStatusCode::TOPO_METASERVER_NOT_FOUND, response.statuscode()); } +TEST_F(TestTopologyManager, test_RegistServer_WithoutHostnameSuccess) { + ServerIdType id = 0x31; + PoolIdType poolId = 0x11; + ZoneIdType zoneId = 0x21; + PrepareAddPool(poolId, "pool1"); + PrepareAddZone(zoneId, "zone1", poolId); + + ServerRegistRequest request; + request.set_internalip("ip1"); + request.set_externalip("ip2"); + request.set_zonename("zone1"); + request.set_poolname("pool1"); + + ServerRegistResponse response; + + EXPECT_CALL(*idGenerator_, GenServerId()).WillOnce(Return(id)); + EXPECT_CALL(*storage_, StorageServer(_)).WillOnce(Return(true)); + + serviceManager_->RegistServer(&request, &response); + + ASSERT_EQ(TopoStatusCode::TOPO_OK, response.statuscode()); + ASSERT_TRUE(response.has_serverid()); + ASSERT_EQ(id, response.serverid()); +} + TEST_F(TestTopologyManager, test_RegistServer_ByZoneAndPoolNameSuccess) { ServerIdType id = 0x31; PoolIdType poolId = 0x11; diff --git a/curvefs/test/mds/topology/test_topology_service.cpp b/curvefs/test/mds/topology/test_topology_service.cpp index 116070ce64..d8d49be389 100644 --- a/curvefs/test/mds/topology/test_topology_service.cpp +++ b/curvefs/test/mds/topology/test_topology_service.cpp @@ -129,6 +129,32 @@ TEST_F(TestTopologyService, test_RegistMetaServer_success) { ASSERT_EQ(TopoStatusCode::TOPO_OK, response.statuscode()); } +TEST_F(TestTopologyService, test_RegistMetaServer_withouthostname_success) { + TopologyService_Stub stub(&channel_); + + brpc::Controller cntl; + MetaServerRegistRequest request; + request.set_internalip("127.0.0.1"); + request.set_internalport(8888); + request.set_externalip("127.0.0.1"); + request.set_externalport(9999); + + MetaServerRegistResponse response; + + MetaServerRegistResponse reps; + reps.set_statuscode(TopoStatusCode::TOPO_OK); + EXPECT_CALL(*manager_, RegistMetaServer(_, _)) + .WillRepeatedly(SetArgPointee<1>(reps)); + + stub.RegistMetaServer(&cntl, &request, &response, nullptr); + + if (cntl.Failed()) { + FAIL() << cntl.ErrorText() << std::endl; + } + + ASSERT_EQ(TopoStatusCode::TOPO_OK, response.statuscode()); +} + TEST_F(TestTopologyService, test_RegistMetaServer_fail) { TopologyService_Stub stub(&channel_); @@ -323,6 +349,34 @@ TEST_F(TestTopologyService, test_RegistServer_success) { ASSERT_EQ(TopoStatusCode::TOPO_OK, response.statuscode()); } +TEST_F(TestTopologyService, test_RegistServer_WithoutHostname_success) { + TopologyService_Stub stub(&channel_); + + brpc::Controller cntl; + ServerRegistRequest request; + request.set_internalip("2"); + request.set_internalport(2); + request.set_externalip("3"); + request.set_externalport(3); + request.set_zonename("zone"); + request.set_poolname("pool"); + + ServerRegistResponse response; + + ServerRegistResponse reps; + reps.set_statuscode(TopoStatusCode::TOPO_OK); + EXPECT_CALL(*manager_, RegistServer(_, _)) + .WillRepeatedly(SetArgPointee<1>(reps)); + + stub.RegistServer(&cntl, &request, &response, nullptr); + + if (cntl.Failed()) { + FAIL() << cntl.ErrorText() << std::endl; + } + + ASSERT_EQ(TopoStatusCode::TOPO_OK, response.statuscode()); +} + TEST_F(TestTopologyService, test_RegistServer_fail) { TopologyService_Stub stub(&channel_);