Skip to content

Commit cb7c2dd

Browse files
jolly-sywu-hanqing
authored andcommitted
curvebs|tools:curve_ops_tool support extend volume
Signed-off-by: jolly-sy <757050468@qq.com>
1 parent 9f2ae52 commit cb7c2dd

10 files changed

+123
-4
lines changed

src/tools/curve_tool_define.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ const char kClientListCmd[] = "client-list";
5555
const char kSnapshotCloneStatusCmd[] = "snapshot-clone-status";
5656
const char kClusterStatusCmd[] = "cluster-status";
5757

58-
// NamesPaceTool相关命令
58+
// NameSpaceTool相关命令
5959
const char kGetCmd[] = "get";
6060
const char kListCmd[] = "list";
6161
const char kSegInfoCmd[] = "seginfo";
6262
const char kDeleteCmd[] = "delete";
6363
const char kCreateCmd[] = "create";
64+
const char kExtendCmd[] = "extend";
6465
const char kCleanRecycleCmd[] = "clean-recycle";
6566
const char kChunkLocatitonCmd[] = "chunk-location";
6667

src/tools/curve_tool_main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const char* kHelpStr = "Usage: curve_ops_tool [Command] [OPTIONS...]\n"
4545
"delete : delete the file, to force delete, should specify the --forcedelete=true\n" //NOLINT
4646
"clean-recycle : clean the RecycleBin\n"
4747
"create : create file, file length unit is GB\n"
48+
"extend : extend volume of file\n"
4849
"chunk-location : query the location of the chunk corresponding to the offset\n" //NOLINT
4950
"check-consistency : check the consistency of three copies\n"
5051
"remove-peer : remove the peer from the copyset\n"

src/tools/mds_client.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,29 @@ int MDSClient::CreateFile(const std::string& fileName, uint64_t length) {
295295
return -1;
296296
}
297297

298+
int MDSClient::ExtendVolume(const std::string& fileName, uint64_t newSize) {
299+
curve::mds::ExtendFileRequest request;
300+
curve::mds::ExtendFileResponse response;
301+
request.set_filename(fileName);
302+
request.set_newsize(newSize);
303+
FillUserInfo(&request);
304+
curve::mds::CurveFSService_Stub stub(&channel_);
305+
auto fp = &curve::mds::CurveFSService_Stub::ExtendFile;
306+
if (SendRpcToMds(&request, &response, &stub, fp) != 0) {
307+
std::cout << "extendFile from all mds fail!" << std::endl;
308+
return -1;
309+
}
310+
311+
if (response.has_statuscode() &&
312+
response.statuscode() == StatusCode::kOK) {
313+
std::cout << "extendFile success!" << std::endl;
314+
return 0;
315+
}
316+
std::cout << "extendFile fail with errCode: "
317+
<< response.statuscode() << std::endl;
318+
return -1;
319+
}
320+
298321
int MDSClient::ListVolumesOnCopyset(
299322
const std::vector<common::CopysetInfo>& copysets,
300323
std::vector<std::string>* fileNames) {

src/tools/mds_client.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ class MDSClient {
184184
const std::vector<common::CopysetInfo>& copysets,
185185
std::vector<std::string>* fileNames);
186186

187+
/**
188+
* @brief 扩容卷
189+
* @param fileName 文件名
190+
* @param newSize 扩容后的卷大小
191+
* @return 成功返回0,失败返回-1
192+
*/
193+
virtual int ExtendVolume(const std::string& fileName, uint64_t newSize);
194+
187195
/**
188196
* @brief 列出client的dummyserver的地址
189197
* @param[out] clientAddrs client地址列表,返回0时有效

src/tools/namespace_tool.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
DEFINE_string(fileName, "", "file name");
2626
DEFINE_bool(forcedelete, false, "force delete file or not");
2727
DEFINE_uint64(fileLength, 20, "file length (GB)");
28+
DEFINE_uint64(newSize, 30, "the new size of expanded volume(GB)");
2829
DEFINE_bool(isTest, false, "is unit test or not");
2930
DEFINE_uint64(offset, 0, "offset to query chunk location");
3031
DEFINE_uint64(rpc_timeout, 3000, "millisecond for rpc timeout");
@@ -54,6 +55,7 @@ bool NameSpaceTool::SupportCommand(const std::string& command) {
5455
|| command == kSegInfoCmd
5556
|| command == kDeleteCmd
5657
|| command == kCreateCmd
58+
|| command == kExtendCmd
5759
|| command == kCleanRecycleCmd
5860
|| command == kChunkLocatitonCmd);
5961
}
@@ -103,6 +105,8 @@ int NameSpaceTool::RunCommand(const std::string &cmd) {
103105
}
104106
} else if (cmd == kCreateCmd) {
105107
return core_->CreateFile(fileName, FLAGS_fileLength * mds::kGB);
108+
} else if (cmd == kExtendCmd) {
109+
return core_->ExtendVolume(fileName, FLAGS_newSize * mds::kGB);
106110
} else if (cmd == kChunkLocatitonCmd) {
107111
return PrintChunkLocation(fileName, FLAGS_offset);
108112
} else {
@@ -122,7 +126,9 @@ void NameSpaceTool::PrintHelp(const std::string &cmd) {
122126
std::cout << "curve_ops_tool " << cmd << " [-fileName=/cinder] [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
123127
std::cout << "If -fileName is specified, delete the files in recyclebin that the original directory is fileName" << std::endl; // NOLINT
124128
} else if (cmd == kCreateCmd) {
125-
std::cout << "curve_ops_tool " << cmd << " -fileName=/test -userName=test -password=123 -fileLength=20‬ [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
129+
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
130+
} else if (cmd == kExtendCmd) {
131+
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
126132
} else if (cmd == kDeleteCmd) {
127133
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
128134
} else if (cmd == kChunkLocatitonCmd) {

src/tools/namespace_tool_core.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ int NameSpaceToolCore::CreateFile(const std::string& fileName,
6464
uint64_t length) {
6565
return client_->CreateFile(fileName, length);
6666
}
67-
67+
int NameSpaceToolCore::ExtendVolume(const std::string& fileName,
68+
uint64_t newSize) {
69+
return client_->ExtendVolume(fileName, newSize);
70+
}
6871
int NameSpaceToolCore::GetAllocatedSize(const std::string& fileName,
6972
uint64_t* allocSize,
7073
AllocMap* allocMap) {

src/tools/namespace_tool_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class NameSpaceToolCore {
108108
*/
109109
virtual int CreateFile(const std::string& fileName, uint64_t length);
110110

111+
/**
112+
* @brief 扩容卷
113+
* @param fileName 文件名
114+
* @param newSize 扩容后的文件长度
115+
* @return 成功返回0,失败返回-1
116+
*/
117+
virtual int ExtendVolume(const std::string& fileName, uint64_t newSize);
118+
111119
/**
112120
* @brief 计算文件或目录实际分配的空间
113121
* @param fileName 文件名

test/tools/mds_client_test.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ TEST_F(ToolMDSClientTest, DeleteFile) {
488488
TEST_F(ToolMDSClientTest, CreateFile) {
489489
std::string fileName = "/test";
490490
uint64_t length = 10 * DefaultSegmentSize;
491-
491+
uint64_t stripeUnit = 32 * 1024 *1024;
492+
uint64_t stripeCount = 32;
492493
// 发送RPC失败
493494
EXPECT_CALL(*nameService, CreateFile(_, _, _, _))
494495
.Times(6)
@@ -529,6 +530,56 @@ TEST_F(ToolMDSClientTest, CreateFile) {
529530
ASSERT_EQ(0, mdsClient.CreateFile(fileName, length));
530531
}
531532

533+
TEST_F(ToolMDSClientTest, ExtendVolume_success) {
534+
std::string fileName = "/test";
535+
uint64_t length = 10 * DefaultSegmentSize;
536+
curve::mds::ExtendFileResponse response;
537+
response.set_statuscode(curve::mds::StatusCode::kOK);
538+
EXPECT_CALL(*nameService, ExtendFile(_, _, _, _))
539+
.WillOnce(DoAll(SetArgPointee<2>(response),
540+
Invoke([](RpcController *controller,
541+
const curve::mds::ExtendFileRequest *request,
542+
curve::mds::ExtendFileResponse *response,
543+
Closure *done){
544+
brpc::ClosureGuard doneGuard(done);
545+
})));
546+
ASSERT_EQ(0, mdsClient.ExtendVolume(fileName, length));
547+
}
548+
549+
TEST_F(ToolMDSClientTest, ExtendVolume_Fail) {
550+
std::string fileName = "/test";
551+
uint64_t length = 10 * DefaultSegmentSize;
552+
553+
// 发送RPC失败
554+
EXPECT_CALL(*nameService, ExtendFile(_, _, _, _))
555+
.Times(6)
556+
.WillRepeatedly(Invoke([](RpcController *controller,
557+
const curve::mds::ExtendFileRequest *request,
558+
curve::mds::ExtendFileResponse *response,
559+
Closure *done){
560+
brpc::ClosureGuard doneGuard(done);
561+
brpc::Controller *cntl =
562+
dynamic_cast<brpc::Controller *>(controller);
563+
cntl->SetFailed("test");
564+
}));
565+
ASSERT_EQ(-1, mdsClient.ExtendVolume(fileName, length));
566+
567+
return;
568+
569+
// 返回码不为OK
570+
curve::mds::ExtendFileResponse response;
571+
response.set_statuscode(curve::mds::StatusCode::kParaError);
572+
EXPECT_CALL(*nameService, ExtendFile(_, _, _, _))
573+
.WillOnce(DoAll(SetArgPointee<2>(response),
574+
Invoke([](RpcController *controller,
575+
const curve::mds::ExtendFileRequest *request,
576+
curve::mds::ExtendFileResponse *response,
577+
Closure *done){
578+
brpc::ClosureGuard doneGuard(done);
579+
})));
580+
ASSERT_EQ(-1, mdsClient.ExtendVolume(fileName, length));
581+
}
582+
532583
TEST_F(ToolMDSClientTest, GetChunkServerListInCopySets) {
533584
PoolIdType logicalPoolId = 1;
534585
CopySetIdType copysetId = 100;

test/tools/mock/mock_mds_client.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class MockMDSClient : public MDSClient {
4949
uint64_t, PageFileSegment*));
5050
MOCK_METHOD2(DeleteFile, int(const std::string&, bool));
5151
MOCK_METHOD2(CreateFile, int(const std::string&, uint64_t));
52+
MOCK_METHOD2(ExtendVolume, int(const std::string&, uint64_t));
5253
MOCK_METHOD3(GetChunkServerListInCopySet, int(const PoolIdType&,
5354
const CopySetIdType&, std::vector<ChunkServerLocation>*));
5455
MOCK_METHOD3(GetChunkServerListInCopySets, int(const PoolIdType&,

test/tools/namespace_tool_core_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ TEST_F(NameSpaceToolCoreTest, CreateFile) {
151151
ASSERT_EQ(-1, namespaceTool.CreateFile(fileName, length));
152152
}
153153

154+
TEST_F(NameSpaceToolCoreTest, ExtendVolume) {
155+
curve::tool::NameSpaceToolCore namespaceTool(client_);
156+
std::string fileName = "/test";
157+
uint64_t length = 10 * segmentSize;
158+
// 1、正常情况
159+
EXPECT_CALL(*client_, ExtendVolume(_, _))
160+
.Times(1)
161+
.WillOnce(Return(0));
162+
ASSERT_EQ(0, namespaceTool.ExtendVolume(fileName, length));
163+
164+
// 2、创建失败
165+
EXPECT_CALL(*client_, ExtendVolume(_, _))
166+
.Times(1)
167+
.WillOnce(Return(-1));
168+
ASSERT_EQ(-1, namespaceTool.ExtendVolume(fileName, length));
169+
}
170+
154171
TEST_F(NameSpaceToolCoreTest, DeleteFile) {
155172
curve::tool::NameSpaceToolCore namespaceTool(client_);
156173
std::string fileName = "/test";

0 commit comments

Comments
 (0)