forked from opencurve/curve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibcurveSnapshot.cpp
447 lines (396 loc) · 14.5 KB
/
libcurveSnapshot.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* 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
* File Created: Thursday, 16th May 2019 3:46:05 pm
* Author: tongguangxun
*/
#include <glog/logging.h>
#include <string>
#include <memory>
#include "curvesnapshot_python/libcurveSnapshot.h"
#include "src/client/libcurve_snapshot.h"
#include "src/client/client_config.h"
#include "include/client/libcurve.h"
#include "src/client/client_common.h"
#include "src/common/concurrent/concurrent.h"
using curve::client::UserInfo;
using curve::client::ClientConfig;
using curve::client::SnapshotClient;
using curve::client::SnapCloneClosure;
using curve::client::FileServiceOption;
using curve::client::ClientConfigOption;
using curve::common::Mutex;
using curve::common::ConditionVariable;
class TaskTracker {
public:
TaskTracker()
: concurrent_(0),
lastErr_(0) {}
/**
* @brief 增加一个追踪任务
*/
void AddOneTrace() {
concurrent_.fetch_add(1, std::memory_order_acq_rel);
}
/**
* @brief 获取任务数量
*
* @return 任务数量
*/
uint32_t GetTaskNum() const {
return concurrent_;
}
/**
* @brief 处理任务返回值
*
* @param retCode 返回值
*/
void HandleResponse(int retCode) {
if (retCode < 0) {
lastErr_ = retCode;
}
if (1 == concurrent_.fetch_sub(1, std::memory_order_acq_rel)) {
// 最后一次需拿锁再发信号,防止先发信号后等待导致死锁
std::unique_lock<Mutex> lk(cv_m);
cv_.notify_all();
} else {
cv_.notify_all();
}
}
/**
* @brief 等待追踪的所有任务完成
*/
void Wait() {
std::unique_lock<Mutex> lk(cv_m);
cv_.wait(lk, [this](){
return concurrent_.load(std::memory_order_acquire) == 0;});
}
/**
* @brief 获取最后一个错误
*
* @return 错误码
*/
int GetResult() {
return lastErr_;
}
private:
// 等待的条件变量
ConditionVariable cv_;
Mutex cv_m;
// 并发数量
std::atomic<uint32_t> concurrent_;
// 错误码
int lastErr_;
};
struct SnapCloneTestClosure : public SnapCloneClosure {
explicit SnapCloneTestClosure(std::shared_ptr<TaskTracker> tracker)
: tracker_(tracker) {}
void Run() {
std::unique_ptr<SnapCloneTestClosure> self_guard(this);
tracker_->HandleResponse(GetRetCode());
}
std::shared_ptr<TaskTracker> tracker_;
};
bool globalinited = false;
SnapshotClient* globalSnapshotclient = nullptr;
int Init(const char* path) {
if (globalinited) {
return 0;
}
ClientConfig cc;
if (-1 == cc.Init(path)) {
LOG(ERROR) << "config init failed!";
return -LIBCURVE_ERROR::FAILED;
}
FileServiceOption fileopt = cc.GetFileServiceOption();
ClientConfigOption copt;
copt.loginfo = fileopt.loginfo;
copt.ioOpt = fileopt.ioOpt;
copt.metaServerOpt = fileopt.metaServerOpt;
if (globalSnapshotclient == nullptr) {
globalSnapshotclient = new SnapshotClient();
int ret = globalSnapshotclient->Init(copt);
globalinited = ret == 0 ? true : false;
}
return globalinited ? 0 : -LIBCURVE_ERROR::FAILED;
}
void ChunkIDInfo2LocalInfo(CChunkIDInfo* localinfo,
const curve::client::ChunkIDInfo& idinfo) {
localinfo->cid_.value = idinfo.cid_;
localinfo->cpid_.value = idinfo.cpid_;
localinfo->lpid_.value = idinfo.lpid_;
}
void LocalInfo2ChunkIDInfo(const CChunkIDInfo& localinfo,
curve::client::ChunkIDInfo* idinfo) {
idinfo->cid_ = localinfo.cid_.value;
idinfo->cpid_ = localinfo.cpid_.value;
idinfo->lpid_ = localinfo.lpid_.value;
}
int CreateSnapShot(const char* filename,
const CUserInfo_t userinfo,
type_uInt64_t* seq) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
int ret = globalSnapshotclient->CreateSnapShot(
filename,
UserInfo(userinfo.owner, userinfo.password),
&seq->value);
LOG(INFO) << "create snapshot ret = " << ret
<< ", seq = " << seq->value;
return ret;
}
int DeleteSnapShot(const char* filename,
const CUserInfo_t userinfo,
type_uInt64_t seq) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
return globalSnapshotclient->DeleteSnapShot(filename,
UserInfo(userinfo.owner, userinfo.password),
seq.value);
}
int GetSnapShot(const char* filename, const CUserInfo_t userinfo,
type_uInt64_t seq, CFInfo_t* snapinfo) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::FInfo_t fileinfo;
int ret = globalSnapshotclient->GetSnapShot(filename,
UserInfo(userinfo.owner, userinfo.password),
seq.value,
&fileinfo);
if (ret == LIBCURVE_ERROR::OK) {
snapinfo->id.value = fileinfo.id;
snapinfo->parentid.value = fileinfo.parentid;
snapinfo->filetype = static_cast<CFileType>(fileinfo.filetype);
snapinfo->chunksize.value = fileinfo.chunksize;
snapinfo->segmentsize.value = fileinfo.segmentsize;
snapinfo->length.value = fileinfo.length;
snapinfo->ctime.value = fileinfo.ctime;
snapinfo->seqnum.value = fileinfo.seqnum;
memset(snapinfo->owner, 0, 256);
memset(snapinfo->filename, 0, 256);
memcpy(snapinfo->owner, fileinfo.owner.c_str(), 256);
memcpy(snapinfo->filename, fileinfo.filename.c_str(), 256);
snapinfo->filestatus = static_cast<CFileStatus>(fileinfo.filestatus);
LOG(INFO) << "origin owner = " << fileinfo.owner;
LOG(INFO) << "origin filename = " << fileinfo.filename;
LOG(INFO) << "owner = " << snapinfo->owner;
LOG(INFO) << "filename = " << snapinfo->filename;
}
return ret;
}
int GetSnapshotSegmentInfo(const char* filename,
const CUserInfo_t userinfo,
type_uInt64_t seq,
type_uInt64_t offset,
CSegmentInfo *segInfo) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::SegmentInfo seg;
int ret = globalSnapshotclient->GetSnapshotSegmentInfo(filename,
UserInfo(userinfo.owner, userinfo.password),
seq.value,
offset.value,
&seg);
if (ret == LIBCURVE_ERROR::OK) {
segInfo->segmentsize.value = seg.segmentsize;
segInfo->chunksize.value = seg.chunksize;
segInfo->startoffset.value = seg.startoffset;
segInfo->chunkVecSize.value = seg.chunkvec.size();
for (int i = 0; i < seg.chunkvec.size(); i++) {
CChunkIDInfo_t tempIDInfo;
ChunkIDInfo2LocalInfo(&tempIDInfo, seg.chunkvec[i]);
segInfo->chunkvec.push_back(tempIDInfo);
}
segInfo->lpcpIDInfo.lpid.value = seg.lpcpIDInfo.lpid;
segInfo->lpcpIDInfo.cpidVecSize.value = seg.lpcpIDInfo.cpidVec.size();
for (int i = 0; i < seg.lpcpIDInfo.cpidVec.size(); i++) {
segInfo->lpcpIDInfo.cpidVec.push_back(seg.lpcpIDInfo.cpidVec[i]);
}
}
return ret;
}
int GetOrAllocateSegmentInfo(const char* filename,
type_uInt64_t offset,
type_uInt64_t segmentsize,
type_uInt64_t chunksize,
const CUserInfo_t userinfo,
CSegmentInfo *segInfo) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::FInfo_t fileinfo;
fileinfo.segmentsize = segmentsize.value;
fileinfo.chunksize = chunksize.value;
fileinfo.fullPathName = std::string(filename);
fileinfo.filename = std::string(filename);
fileinfo.userinfo = UserInfo(userinfo.owner, userinfo.password);
curve::client::SegmentInfo seg;
int ret = globalSnapshotclient->GetOrAllocateSegmentInfo(false,
offset.value,
&fileinfo,
&seg);
segInfo->segmentsize.value = seg.segmentsize;
segInfo->chunksize.value = seg.chunksize;
segInfo->startoffset.value = seg.startoffset;
segInfo->chunkVecSize.value = seg.chunkvec.size();
for (int i = 0; i < seg.chunkvec.size(); i++) {
CChunkIDInfo_t tempIDInfo;
ChunkIDInfo2LocalInfo(&tempIDInfo, seg.chunkvec[i]);
segInfo->chunkvec.push_back(tempIDInfo);
}
segInfo->lpcpIDInfo.lpid.value = seg.lpcpIDInfo.lpid;
segInfo->lpcpIDInfo.cpidVecSize.value = seg.lpcpIDInfo.cpidVec.size();
for (int i = 0; i < seg.lpcpIDInfo.cpidVec.size(); i++) {
segInfo->lpcpIDInfo.cpidVec.push_back(seg.lpcpIDInfo.cpidVec[i]);
}
return ret;
}
int ReadChunkSnapshot(CChunkIDInfo cidinfo,
type_uInt64_t seq,
type_uInt64_t offset,
type_uInt64_t len,
char *buf) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::ChunkIDInfo idinfo;
LocalInfo2ChunkIDInfo(cidinfo, &idinfo);
auto tracker = std::make_shared<TaskTracker>();
SnapCloneTestClosure *cb = new SnapCloneTestClosure(tracker);
tracker->AddOneTrace();
int ret = globalSnapshotclient->ReadChunkSnapshot(idinfo, seq.value,
offset.value, len.value,
buf, cb);
tracker->Wait();
if (ret < 0) {
return ret;
} else {
if (tracker->GetResult() < 0) {
return tracker->GetResult();
} else {
return len.value;
}
}
}
int DeleteChunkSnapshotOrCorrectSn(CChunkIDInfo cidinfo,
type_uInt64_t correctedSeq) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::ChunkIDInfo idinfo;
LocalInfo2ChunkIDInfo(cidinfo, &idinfo);
int ret = globalSnapshotclient->DeleteChunkSnapshotOrCorrectSn(idinfo,
correctedSeq.value);
return ret;
}
int GetChunkInfo(CChunkIDInfo cidinfo, CChunkInfoDetail *chunkInfo) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::ChunkInfoDetail cinfodetail;
curve::client::ChunkIDInfo idinfo;
LocalInfo2ChunkIDInfo(cidinfo, &idinfo);
int ret = globalSnapshotclient->GetChunkInfo(idinfo, &cinfodetail);
chunkInfo->snSize.value = cinfodetail.chunkSn.size();
for (int i = 0; i < cinfodetail.chunkSn.size(); i++) {
chunkInfo->chunkSn.push_back(cinfodetail.chunkSn[i]);
}
return ret;
}
int CheckSnapShotStatus(const char* filename,
const CUserInfo_t userinfo,
type_uInt64_t seq,
type_uInt32_t* filestatus) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::FileStatus fs;
int ret = globalSnapshotclient->CheckSnapShotStatus(filename,
UserInfo(userinfo.owner, userinfo.password),
seq.value,
&fs);
filestatus->value = static_cast<uint32_t>(fs);
return ret;
}
int CreateCloneChunk(const char* location,
const CChunkIDInfo chunkidinfo,
type_uInt64_t sn,
type_uInt64_t correntSn,
type_uInt64_t chunkSize) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::ChunkIDInfo idinfo;
LocalInfo2ChunkIDInfo(chunkidinfo, &idinfo);
auto tracker = std::make_shared<TaskTracker>();
SnapCloneTestClosure *cb = new SnapCloneTestClosure(tracker);
tracker->AddOneTrace();
int ret = globalSnapshotclient->CreateCloneChunk(location, idinfo,
sn.value, correntSn.value,
chunkSize.value,
cb);
tracker->Wait();
if (ret < 0) {
return ret;
} else {
return tracker->GetResult();
}
}
int RecoverChunk(const CChunkIDInfo chunkidinfo,
type_uInt64_t offset,
type_uInt64_t len) {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return -LIBCURVE_ERROR::FAILED;
}
curve::client::ChunkIDInfo idinfo;
LocalInfo2ChunkIDInfo(chunkidinfo, &idinfo);
auto tracker = std::make_shared<TaskTracker>();
SnapCloneTestClosure *cb = new SnapCloneTestClosure(tracker);
tracker->AddOneTrace();
int ret = globalSnapshotclient->RecoverChunk(idinfo,
offset.value,
len.value,
cb);
tracker->Wait();
if (ret < 0) {
return ret;
} else {
return tracker->GetResult();
}
}
void UnInit() {
if (globalSnapshotclient == nullptr) {
LOG(ERROR) << "not init!";
return;
}
globalSnapshotclient->UnInit();
}