Skip to content

Commit

Permalink
[feat]curvefs: change hostname to optional
Browse files Browse the repository at this point in the history
Signed-off-by: swj <[email protected]>
  • Loading branch information
201341 authored and ilixiaocui committed Jul 21, 2023
1 parent ec35407 commit c30eff0
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 14 deletions.
8 changes: 4 additions & 4 deletions curvefs/proto/topology.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions curvefs/src/mds/topology/topology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions curvefs/src/metaserver/register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 6 additions & 4 deletions curvefs/src/tools/create/curvefs_create_topology_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions curvefs/test/mds/topology/test_topology_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions curvefs/test/mds/topology/test_topology_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);

Expand Down Expand Up @@ -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_);

Expand Down

0 comments on commit c30eff0

Please sign in to comment.