Skip to content

Commit

Permalink
curvebs|tools:curve_ops_tool support expand volume
Browse files Browse the repository at this point in the history
Signed-off-by: jolly-sy <[email protected]>
  • Loading branch information
jolly-sy authored and ilixiaocui committed Nov 2, 2022
1 parent ba72a68 commit 5e49aed
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/tools/curve_tool_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ const char kSnapshotCloneStatusCmd[] = "snapshot-clone-status";
const char kClusterStatusCmd[] = "cluster-status";
const char kScanStatusCmd[] = "scan-status";

// NamesPaceTool相关命令
// NameSpaceTool相关命令
const char kGetCmd[] = "get";
const char kListCmd[] = "list";
const char kSegInfoCmd[] = "seginfo";
const char kDeleteCmd[] = "delete";
const char kCreateCmd[] = "create";
const char kExtendCmd[] = "extend";
const char kCleanRecycleCmd[] = "clean-recycle";
const char kChunkLocatitonCmd[] = "chunk-location";
const char kUpdateThrottle[] = "update-throttle";
Expand Down
1 change: 1 addition & 0 deletions src/tools/curve_tool_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const char* kHelpStr = "Usage: curve_ops_tool [Command] [OPTIONS...]\n"
"delete : delete the file, to force delete, should specify the --forcedelete=true\n" //NOLINT
"clean-recycle : clean the RecycleBin\n"
"create : create file, file length unit is GB\n"
"extend : extend volume of file\n"
"chunk-location : query the location of the chunk corresponding to the offset\n" //NOLINT
"check-consistency : check the consistency of three copies\n"
"remove-peer : remove the peer from the copyset\n"
Expand Down
23 changes: 23 additions & 0 deletions src/tools/mds_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,29 @@ int MDSClient::CreateFile(const std::string& fileName, uint64_t length,
return -1;
}

int MDSClient::ExtendVolume(const std::string& fileName, uint64_t newSize) {
curve::mds::ExtendFileRequest request;
curve::mds::ExtendFileResponse response;
request.set_filename(fileName);
request.set_newsize(newSize);
FillUserInfo(&request);
curve::mds::CurveFSService_Stub stub(&channel_);
auto fp = &curve::mds::CurveFSService_Stub::ExtendFile;
if (SendRpcToMds(&request, &response, &stub, fp) != 0) {
std::cout << "extendFile from all mds fail!" << std::endl;
return -1;
}

if (response.has_statuscode() &&
response.statuscode() == StatusCode::kOK) {
std::cout << "extendFile success!" << std::endl;
return 0;
}
std::cout << "extendFile fail with errCode: "
<< response.statuscode() << std::endl;
return -1;
}

int MDSClient::ListVolumesOnCopyset(
const std::vector<common::CopysetInfo>& copysets,
std::vector<std::string>* fileNames) {
Expand Down
8 changes: 8 additions & 0 deletions src/tools/mds_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ class MDSClient {
const std::vector<common::CopysetInfo>& copysets,
std::vector<std::string>* fileNames);

/**
* @brief 扩容卷
* @param fileName 文件名
* @param newSize 扩容后的卷大小
* @return 成功返回0,失败返回-1
*/
virtual int ExtendVolume(const std::string& fileName, uint64_t newSize);

/**
* @brief 列出client的dummyserver的地址
* @param[out] clientAddrs client地址列表,返回0时有效
Expand Down
6 changes: 6 additions & 0 deletions src/tools/namespace_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ DEFINE_string(expireTime, "7d", "Time for file in recyclebin exceed expire time
"will be deleted (default: 7d)");
DEFINE_bool(forcedelete, false, "force delete file or not");
DEFINE_uint64(fileLength, 20, "file length (GB)");
DEFINE_uint64(newSize, 30, "the new size of expanded volume(GB)");
DEFINE_bool(isTest, false, "is unit test or not");
DEFINE_uint64(offset, 0, "offset to query chunk location");
DEFINE_uint64(rpc_timeout, 3000, "millisecond for rpc timeout");
Expand Down Expand Up @@ -63,6 +64,7 @@ bool NameSpaceTool::SupportCommand(const std::string& command) {
|| command == kSegInfoCmd
|| command == kDeleteCmd
|| command == kCreateCmd
|| command == kExtendCmd
|| command == kCleanRecycleCmd
|| command == kChunkLocatitonCmd
|| command == kUpdateThrottle);
Expand Down Expand Up @@ -122,6 +124,8 @@ int NameSpaceTool::RunCommand(const std::string &cmd) {
} else if (cmd == kCreateCmd) {
return core_->CreateFile(fileName, FLAGS_fileLength * mds::kGB,
FLAGS_stripeUnit, FLAGS_stripeCount);
} else if (cmd == kExtendCmd) {
return core_->ExtendVolume(fileName, FLAGS_newSize * mds::kGB);
} else if (cmd == kChunkLocatitonCmd) {
return PrintChunkLocation(fileName, FLAGS_offset);
} else if (cmd == kUpdateThrottle) {
Expand All @@ -147,6 +151,8 @@ void NameSpaceTool::PrintHelp(const std::string &cmd) {
std::cout << "expireTime: s=second, m=minute, h=hour, d=day, M=month, y=year" << std::endl; // NOLINT
} else if (cmd == kCreateCmd) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test -userName=test -password=123 -filelength=20 -stripeUnit=32768 -stripeCount=32 [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
} else if (cmd == kExtendCmd) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test -userName=test -password=123 -newSize=30 [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
} else if (cmd == kDeleteCmd) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test -userName=test -password=123 -forcedelete=true [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
} else if (cmd == kChunkLocatitonCmd) {
Expand Down
5 changes: 4 additions & 1 deletion src/tools/namespace_tool_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ int NameSpaceToolCore::CreateFile(const std::string& fileName,
return client_->CreateFile(fileName, length,
stripeUnit, stripeCount);
}

int NameSpaceToolCore::ExtendVolume(const std::string& fileName,
uint64_t newSize) {
return client_->ExtendVolume(fileName, newSize);
}
int NameSpaceToolCore::GetAllocatedSize(const std::string& fileName,
uint64_t* allocSize,
AllocMap* allocMap) {
Expand Down
8 changes: 8 additions & 0 deletions src/tools/namespace_tool_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ class NameSpaceToolCore {
virtual int CreateFile(const std::string& fileName, uint64_t length,
uint64_t stripeUnit, uint64_t stripeCount);

/**
* @brief 扩容卷
* @param fileName 文件名
* @param newSize 扩容后的文件长度
* @return 成功返回0,失败返回-1
*/
virtual int ExtendVolume(const std::string& fileName, uint64_t newSize);

/**
* @brief 计算文件或目录实际分配的空间
* @param fileName 文件名
Expand Down
51 changes: 50 additions & 1 deletion test/tools/mds_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,6 @@ TEST_F(ToolMDSClientTest, CreateFile) {
uint64_t length = 10 * DefaultSegmentSize;
uint64_t stripeUnit = 32 * 1024 *1024;
uint64_t stripeCount = 32;

// 发送RPC失败
EXPECT_CALL(*nameService, CreateFile(_, _, _, _))
.Times(6)
Expand Down Expand Up @@ -550,6 +549,56 @@ TEST_F(ToolMDSClientTest, CreateFile) {
stripeUnit, stripeCount));
}

TEST_F(ToolMDSClientTest, ExtendVolume_success) {
std::string fileName = "/test";
uint64_t length = 10 * DefaultSegmentSize;
curve::mds::ExtendFileResponse response;
response.set_statuscode(curve::mds::StatusCode::kOK);
EXPECT_CALL(*nameService, ExtendFile(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<2>(response),
Invoke([](RpcController *controller,
const curve::mds::ExtendFileRequest *request,
curve::mds::ExtendFileResponse *response,
Closure *done){
brpc::ClosureGuard doneGuard(done);
})));
ASSERT_EQ(0, mdsClient.ExtendVolume(fileName, length));
}

TEST_F(ToolMDSClientTest, ExtendVolume_Fail) {
std::string fileName = "/test";
uint64_t length = 10 * DefaultSegmentSize;

// 发送RPC失败
EXPECT_CALL(*nameService, ExtendFile(_, _, _, _))
.Times(6)
.WillRepeatedly(Invoke([](RpcController *controller,
const curve::mds::ExtendFileRequest *request,
curve::mds::ExtendFileResponse *response,
Closure *done){
brpc::ClosureGuard doneGuard(done);
brpc::Controller *cntl =
dynamic_cast<brpc::Controller *>(controller);
cntl->SetFailed("test");
}));
ASSERT_EQ(-1, mdsClient.ExtendVolume(fileName, length));

return;

// 返回码不为OK
curve::mds::ExtendFileResponse response;
response.set_statuscode(curve::mds::StatusCode::kParaError);
EXPECT_CALL(*nameService, ExtendFile(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<2>(response),
Invoke([](RpcController *controller,
const curve::mds::ExtendFileRequest *request,
curve::mds::ExtendFileResponse *response,
Closure *done){
brpc::ClosureGuard doneGuard(done);
})));
ASSERT_EQ(-1, mdsClient.ExtendVolume(fileName, length));
}

TEST_F(ToolMDSClientTest, GetChunkServerListInCopySets) {
PoolIdType logicalPoolId = 1;
CopySetIdType copysetId = 100;
Expand Down
1 change: 1 addition & 0 deletions test/tools/mock/mock_mds_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class MockMDSClient : public MDSClient {
MOCK_METHOD2(DeleteFile, int(const std::string&, bool));
MOCK_METHOD4(CreateFile, int(const std::string&, uint64_t,
uint64_t, uint64_t));
MOCK_METHOD2(ExtendVolume, int(const std::string&, uint64_t));
MOCK_METHOD3(GetChunkServerListInCopySet, int(const PoolIdType&,
const CopySetIdType&, std::vector<ChunkServerLocation>*));
MOCK_METHOD3(GetChunkServerListInCopySets, int(const PoolIdType&,
Expand Down
17 changes: 17 additions & 0 deletions test/tools/namespace_tool_core_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ TEST_F(NameSpaceToolCoreTest, CreateFile) {
stripeUnit, stripeCount));
}

TEST_F(NameSpaceToolCoreTest, ExtendVolume) {
curve::tool::NameSpaceToolCore namespaceTool(client_);
std::string fileName = "/test";
uint64_t length = 10 * segmentSize;
// 1、正常情况
EXPECT_CALL(*client_, ExtendVolume(_, _))
.Times(1)
.WillOnce(Return(0));
ASSERT_EQ(0, namespaceTool.ExtendVolume(fileName, length));

// 2、创建失败
EXPECT_CALL(*client_, ExtendVolume(_, _))
.Times(1)
.WillOnce(Return(-1));
ASSERT_EQ(-1, namespaceTool.ExtendVolume(fileName, length));
}

TEST_F(NameSpaceToolCoreTest, DeleteFile) {
curve::tool::NameSpaceToolCore namespaceTool(client_);
std::string fileName = "/test";
Expand Down

0 comments on commit 5e49aed

Please sign in to comment.