-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathnamespace_tool.cpp
360 lines (340 loc) · 14.2 KB
/
namespace_tool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (c) 2020 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Project: curve
* Created Date: 2019-09-25
* Author: charisu
* Copyright (c) 2018 netease
*/
#include "src/tools/namespace_tool.h"
DEFINE_string(fileName, "", "file name");
DEFINE_string(expireTime, "7d", "Time for file in recyclebin exceed expire time " // NOLINT
"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");
DEFINE_bool(showAllocSize, true, "If specified, the allocated size will not be computed"); // NOLINT
DEFINE_bool(showFileSize, true, "If specified, the file size will not be computed"); // NOLINT
DECLARE_string(mdsAddr);
DEFINE_bool(showAllocMap, false, "If specified, the allocated size in each"
" logical pool will be print");
DEFINE_string(throttleType, "", "throttle type");
DEFINE_uint64(limit, 0, "throttle limit");
DEFINE_int64(burst, -1, "throttle burst");
DEFINE_int64(burstLength, -1, "throttle burst length");
DEFINE_uint64(stripeUnit, 0, "stripe unit size");
DEFINE_uint64(stripeCount, 0, "strip count");
namespace curve {
namespace tool {
int NameSpaceTool::Init() {
if (!inited_) {
int res = core_->Init(FLAGS_mdsAddr);
if (res != 0) {
std::cout << "Init nameSpaceToolCore fail!" << std::endl;
return -1;
}
inited_ = true;
}
return 0;
}
bool NameSpaceTool::SupportCommand(const std::string& command) {
return (command == kGetCmd || command == kListCmd
|| command == kSegInfoCmd
|| command == kDeleteCmd
|| command == kCreateCmd
|| command == kExtendCmd
|| command == kCleanRecycleCmd
|| command == kChunkLocatitonCmd
|| command == kUpdateThrottle);
}
// 根据命令行参数选择对应的操作
int NameSpaceTool::RunCommand(const std::string &cmd) {
if (Init() != 0) {
std::cout << "Init NameSpaceTool failed" << std::endl;
return -1;
}
std::string fileName = FLAGS_fileName;
TrimEndingSlash(&fileName);
if (cmd == kGetCmd) {
return PrintFileInfoAndActualSize(fileName);
} else if (cmd == kListCmd) {
return PrintListDir(fileName);
} else if (cmd == kSegInfoCmd) {
return PrintSegmentInfo(fileName);
} else if (cmd == kDeleteCmd) {
// 单元测试不判断输入
if (FLAGS_isTest) {
return core_->DeleteFile(fileName, FLAGS_forcedelete);
}
std::cout << "Are you sure you want to delete "
<< fileName << "?" << "(yes/no)" << std::endl;
std::string str;
std::cin >> str;
if (str == "yes") {
return core_->DeleteFile(fileName, FLAGS_forcedelete);
} else {
std::cout << "Delete cancled!" << std::endl;
return 0;
}
} else if (cmd == kCleanRecycleCmd) {
uint64_t expireTime;
if (!::curve::common::StringToTime(FLAGS_expireTime, &expireTime)) {
std::cout << "Clean recyclebin: invalid expireTime!" << std::endl;
return 0;
}
auto dirname = fileName.empty() ? "/" : fileName;
if (FLAGS_isTest) {
return core_->CleanRecycleBin(dirname, expireTime);
}
std::cout << "Are you sure you want to clean the RecycleBin, "
<< "the file in the directory " << dirname
<< " will be deleted forever (yes/no)" << std::endl;
std::string str;
std::cin >> str;
if (str == "yes") {
return core_->CleanRecycleBin(dirname, expireTime);
} else {
std::cout << "Clean RecycleBin cancled!" << std::endl;
return 0;
}
} 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) {
return core_->UpdateFileThrottle(fileName, FLAGS_throttleType,
FLAGS_limit, FLAGS_burst,
FLAGS_burstLength);
} else {
std::cout << "Command not support!" << std::endl;
return -1;
}
}
void NameSpaceTool::PrintHelp(const std::string &cmd) {
std::cout << "Example: " << std::endl;
if (cmd == kGetCmd || cmd == kListCmd) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test [-mdsAddr=127.0.0.1:6666]" // NOLINT
" [-showAllocSize=false] [-showFileSize=false] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
} else if (cmd == kSegInfoCmd) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
} else if (cmd == kCleanRecycleCmd) {
std::cout << "curve_ops_tool " << cmd << " [-fileName=/cinder] [-expireTime=1(s|m|h|d|M|y)] [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
std::cout << "If -fileName is specified, delete the files in recyclebin that the original directory is fileName" << std::endl; // NOLINT
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) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test -offset=16777216 [-mdsAddr=127.0.0.1:6666] [-confPath=/etc/curve/tools.conf]" << std::endl; // NOLINT
} else if (cmd == kUpdateThrottle) {
std::cout << "curve_ops_tool " << cmd << " -fileName=/test -throttleType=(IOPS_TOTAL|IOPS_READ|IOPS_WRITE|BPS_TOTAL|BPS_READ|BPS_WRITE) -limit=20000 [-burst=30000] [-burstLength=10]" << std::endl; // NOLINT
} else {
std::cout << "command not found!" << std::endl;
}
}
int NameSpaceTool::PrintFileInfoAndActualSize(const std::string& fileName) {
FileInfo fileInfo;
int ret = core_->GetFileInfo(fileName, &fileInfo);
if (ret != 0) {
return -1;
}
return PrintFileInfoAndActualSize(fileName, fileInfo);
}
int NameSpaceTool::PrintFileInfoAndActualSize(const std::string& fullName,
const FileInfo& fileInfo) {
PrintFileInfo(fileInfo);
int ret = GetAndPrintAllocSize(fullName);
// 如果是目录的话,计算目录中的文件大小(用户创建时指定的)
if (fileInfo.filetype() == curve::mds::FileType::INODE_DIRECTORY) {
ret = GetAndPrintFileSize(fullName);
}
return ret;
}
int NameSpaceTool::GetAndPrintFileSize(const std::string& fileName) {
if (!FLAGS_showFileSize) {
return 0;
}
uint64_t size;
int res = core_->GetFileSize(fileName, &size);
if (res != 0) {
std::cout << "Get file size fail!" << std::endl;
return -1;
}
double fileSize = static_cast<double>(size) / curve::mds::kGB;
std::cout << "file size: " << fileSize << "GB" << std::endl;
return 0;
}
int NameSpaceTool::GetAndPrintAllocSize(const std::string& fileName) {
if (!FLAGS_showAllocSize) {
return 0;
}
uint64_t size;
AllocMap allocMap;
int res = core_->GetAllocatedSize(fileName, &size, &allocMap);
if (res != 0) {
std::cout << "Get allocated size fail!" << std::endl;
return -1;
}
double allocSize = static_cast<double>(size) / curve::mds::kGB;
std::cout << "allocated size: " << allocSize << "GB" << std::endl;
if (FLAGS_showAllocMap) {
for (const auto& item : allocMap) {
allocSize = static_cast<double>(item.second) / curve::mds::kGB;
std::cout << "logical pool id: " << item.first
<< ", allocated size: " << allocSize << "GB" << std::endl;
}
}
return 0;
}
void NameSpaceTool::PrintFileInfo(const FileInfo& fileInfo) {
std::string fileInfoStr = fileInfo.DebugString();
std::vector<std::string> items;
curve::common::SplitString(fileInfoStr, "\n", &items);
for (const auto& item : items) {
if (item.compare(0, 5, "ctime") == 0) {
// ctime是微妙,打印的时候只打印到秒
time_t ctime = fileInfo.ctime() / 1000000;
std::string standard;
curve::common::TimeUtility::TimeStampToStandard(ctime, &standard);
std::cout << "ctime: " << standard << std::endl;
continue;
}
// 把length转换成GB
if (item.compare(0, 6, "length") == 0) {
uint64_t length = fileInfo.length();
double fileSize = static_cast<double>(length) / curve::mds::kGB;
std::cout << "length: " << fileSize << "GB" << std::endl;
continue;
}
std::cout << item << std::endl;
}
}
int NameSpaceTool::PrintListDir(const std::string& dirName) {
std::vector<FileInfo> files;
int ret = core_->ListDir(dirName, &files);
if (ret != 0) {
std::cout << "List directory failed!" << std::endl;
return -1;
}
for (uint64_t i = 0; i < files.size(); ++i) {
if (i != 0) {
std::cout << std::endl;
}
std::string fullPathName;
if (dirName == "/") {
fullPathName = dirName + files[i].filename();
} else {
fullPathName = dirName + "/" + files[i].filename();
}
if (PrintFileInfoAndActualSize(fullPathName, files[i]) != 0) {
ret = -1;
}
}
if (!files.empty()) {
std::cout << std::endl;
}
std::cout << "Total file number: " << files.size() << std::endl;
return ret;
}
int NameSpaceTool::PrintSegmentInfo(const std::string &fileName) {
std::vector<PageFileSegment> segments;
if (core_->GetFileSegments(fileName, &segments) != 0) {
std::cout << "GetFileSegments fail!" << std::endl;
return -1;
}
for (auto& segment : segments) {
PrintSegment(segment);
}
return 0;
}
void NameSpaceTool::PrintSegment(const PageFileSegment& segment) {
if (segment.has_logicalpoolid()) {
std::cout << "logicalPoolID: " << segment.logicalpoolid() << std::endl;
}
if (segment.has_startoffset()) {
std::cout << "startOffset: " << segment.startoffset() << std::endl;
}
if (segment.has_segmentsize()) {
std::cout << "segmentSize: " << segment.segmentsize() << std::endl;
}
if (segment.has_chunksize()) {
std::cout << "chunkSize: " << segment.chunksize() << std::endl;
}
std::cout << "chunks: " << std::endl;
for (int i = 0; i < segment.chunks_size(); ++i) {
uint64_t chunkId = 0;
uint32_t copysetId = 0;
if (segment.chunks(i).has_chunkid()) {
chunkId = segment.chunks(i).chunkid();
}
if (segment.chunks(i).has_copysetid()) {
copysetId = segment.chunks(i).copysetid();
}
std::cout << "chunkID: " << chunkId << ", copysetID: "
<< copysetId << std::endl;
}
}
int NameSpaceTool::PrintChunkLocation(const std::string& fileName,
uint64_t offset) {
uint64_t chunkId;
std::pair<uint32_t, uint32_t> copyset;
if (core_->QueryChunkCopyset(fileName, offset, &chunkId, ©set) != 0) {
std::cout << "QueryChunkCopyset fail!" << std::endl;
return -1;
}
uint32_t logicPoolId = copyset.first;
uint32_t copysetId = copyset.second;
uint64_t groupId = (static_cast<uint64_t>(logicPoolId) << 32) | copysetId;
std::cout << "chunkId: " << chunkId
<< ", logicalPoolId: " << logicPoolId
<< ", copysetId: " << copysetId
<< ", groupId: " << groupId << std::endl;
std::vector<ChunkServerLocation> csLocs;
int res = core_->GetChunkServerListInCopySet(logicPoolId,
copysetId, &csLocs);
if (res != 0) {
std::cout << "GetChunkServerListInCopySet fail!" << std::endl;
return -1;
}
std::cout << "location: {";
for (uint64_t i = 0; i < csLocs.size(); ++i) {
if (i != 0) {
std::cout << ", ";
}
auto location = csLocs[i];
std::cout << location.hostip() << ":"
<< std::to_string(location.port());
}
std::cout << "}" << std::endl;
return 0;
}
void NameSpaceTool::TrimEndingSlash(std::string* fileName) {
// 如果最后面有/,去掉
if (fileName->size() > 1 && fileName->back() == '/') {
fileName->pop_back();
}
}
} // namespace tool
} // namespace curve