Skip to content

Commit

Permalink
Signed-off-by: gueFDF <[email protected]>
Browse files Browse the repository at this point in the history
[fix]curve/src:Added some more meaningful error messages.
[style]curve/test:Modified some code specifications.
[style]curve/src/client:Modified some code specifications.
  • Loading branch information
gueFDF committed Mar 3, 2023
1 parent 9d5a18a commit d817215
Show file tree
Hide file tree
Showing 19 changed files with 1,774 additions and 2,351 deletions.
53 changes: 26 additions & 27 deletions curvefs/src/volume/block_device_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ BlockDeviceClientImpl::BlockDeviceClientImpl()
: fd_(-1), fileClient_(std::make_shared<FileClient>()) {}

BlockDeviceClientImpl::BlockDeviceClientImpl(
const std::shared_ptr<FileClient>& fileClient)
const std::shared_ptr<FileClient> &fileClient)
: fd_(-1), fileClient_(fileClient) {}

bool BlockDeviceClientImpl::Init(const BlockDeviceClientOptions& options) {
auto ret = fileClient_->Init(options.configPath);
if (ret != LIBCURVE_ERROR::OK) {
LOG(ERROR) << "Init file client error: " << ret;
LOG(ERROR) << "Init file client error: " << ret
<< LibCurveErrorName((LIBCURVE_ERROR)ret);
return false;
}

return true;
}

void BlockDeviceClientImpl::UnInit() {
fileClient_->UnInit();
}
Expand Down Expand Up @@ -93,7 +93,8 @@ bool BlockDeviceClientImpl::Close() {

int retCode;
if ((retCode = fileClient_->Close(fd_)) != LIBCURVE_ERROR::OK) {
LOG(ERROR) << "Close file failed, retCode = " << retCode;
LOG(ERROR) << "Close file failed, retCode = " << retCode
<< LibCurveErrorName((LIBCURVE_ERROR)retCode);
return false;
}

Expand All @@ -103,19 +104,22 @@ bool BlockDeviceClientImpl::Close() {

bool BlockDeviceClientImpl::Stat(const std::string& filename,
const std::string& owner,
BlockDeviceStat* statInfo) {
BlockDeviceStat* statInfo){
FileStatInfo fileStatInfo;
UserInfo userInfo(owner);
auto retCode = fileClient_->StatFile(filename, userInfo, &fileStatInfo);
if (retCode != LIBCURVE_ERROR::OK) {
LOG(ERROR) << "Stat file failed, retCode = " << retCode;
LOG(ERROR) << "Stat file failed, retCode = " << retCode
<< LibCurveErrorName((LIBCURVE_ERROR)retCode);
return false;
}

statInfo->length = fileStatInfo.length;
if (!ConvertFileStatus(fileStatInfo.fileStatus, &statInfo->status)) {
LOG(ERROR) << "Stat file failed, unknown file status: "
<< fileStatInfo.fileStatus;
<< fileStatInfo.fileStatus
<< LibCurveErrorName((LIBCURVE_ERROR)retCode);

return false;
}

Expand Down Expand Up @@ -174,7 +178,6 @@ ssize_t BlockDeviceClientImpl::Readv(const std::vector<ReadPart>& iov) {
ssize_t BlockDeviceClientImpl::Write(const char* buf,
off_t offset,
size_t length) {
VLOG(9) << "write request, offset: " << offset << ", length: " << length;

LatencyUpdater updater(&g_write_latency);

Expand All @@ -197,7 +200,7 @@ ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart>& iov) {
std::vector<std::unique_ptr<AioWrite>> requests;
requests.reserve(iov.size());

for (const auto& io : iov) {
for (const auto &io : iov) {
requests.push_back(absl::make_unique<AioWrite>(
io.offset, io.length, io.data, fileClient_.get(), fd_));

Expand All @@ -206,7 +209,7 @@ ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart>& iov) {

bool error = false;
ssize_t total = 0;
for (const auto& r : requests) {
for (const auto &r : requests) {
auto nr = r->Wait();
if (nr < 0) {
error = true;
Expand All @@ -220,12 +223,11 @@ ssize_t BlockDeviceClientImpl::Writev(const std::vector<WritePart>& iov) {
return error ? -1 : total;
}

bool BlockDeviceClientImpl::WritePadding(char* writeBuffer,
off_t writeStart,
bool BlockDeviceClientImpl::WritePadding(char *writeBuffer, off_t writeStart,
off_t writeEnd,
off_t offset, // actual offset
size_t length) { // actual length
std::vector<std::pair<off_t, size_t>> readvec; // Align reads
std::vector<std::pair<off_t, size_t>> readvec; // Align reads
off_t readEnd = 0;

// Padding leading
Expand All @@ -244,7 +246,7 @@ bool BlockDeviceClientImpl::WritePadding(char* writeBuffer,
}
}

for (const auto& item : readvec) {
for (const auto &item : readvec) {
auto retCode = AlignRead(writeBuffer + item.first - writeStart,
item.first, item.second);
if (retCode != item.second) {
Expand All @@ -255,8 +257,7 @@ bool BlockDeviceClientImpl::WritePadding(char* writeBuffer,
return true;
}

ssize_t BlockDeviceClientImpl::AlignRead(char* buf,
off_t offset,
ssize_t BlockDeviceClientImpl::AlignRead(char *buf, off_t offset,
size_t length) {
auto ret = fileClient_->Read(fd_, buf, offset, length);
if (ret < 0) {
Expand All @@ -271,8 +272,7 @@ ssize_t BlockDeviceClientImpl::AlignRead(char* buf,
return length;
}

ssize_t BlockDeviceClientImpl::AlignWrite(const char* buf,
off_t offset,
ssize_t BlockDeviceClientImpl::AlignWrite(const char *buf, off_t offset,
size_t length) {
auto ret = fileClient_->Write(fd_, buf, offset, length);
if (ret < 0) {
Expand All @@ -288,15 +288,14 @@ ssize_t BlockDeviceClientImpl::AlignWrite(const char* buf,
}

bool BlockDeviceClientImpl::ConvertFileStatus(int fileStatus,
BlockDeviceStatus* bdStatus) {
static const std::map<int, BlockDeviceStatus> fileStatusMap {
{ 0, BlockDeviceStatus::CREATED },
{ 1, BlockDeviceStatus::DELETING },
{ 2, BlockDeviceStatus::CLONING },
{ 3, BlockDeviceStatus::CLONE_META_INSTALLED },
{ 4, BlockDeviceStatus::CLONED },
{ 5, BlockDeviceStatus::BEING_CLONED }
};
BlockDeviceStatus *bdStatus) {
static const std::map<int, BlockDeviceStatus> fileStatusMap{
{0, BlockDeviceStatus::CREATED},
{1, BlockDeviceStatus::DELETING},
{2, BlockDeviceStatus::CLONING},
{3, BlockDeviceStatus::CLONE_META_INSTALLED},
{4, BlockDeviceStatus::CLONED},
{5, BlockDeviceStatus::BEING_CLONED}};

auto iter = fileStatusMap.find(fileStatus);
if (iter == fileStatusMap.end()) {
Expand Down
102 changes: 51 additions & 51 deletions include/client/libcbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,77 +47,77 @@ extern "C" {
#define CBD_BACKEND_EXT4
#endif

#define CBD_MAX_FILE_PATH_LEN 1024
#define CBD_MAX_BUF_LEN 1024 * 1024 * 32
#define CBD_MAX_FILE_PATH_LEN 1024
#define CBD_MAX_BUF_LEN 1024 * 1024 * 32

typedef int CurveFd;

typedef struct CurveOptions {
bool inited;
char* conf;
bool inited;
char *conf;
#ifdef CBD_BACKEND_EXT4
char* datahome;
char *datahome;
#endif
} CurveOptions;

int cbd_ext4_init(const CurveOptions* options);
int cbd_ext4_init(const CurveOptions *options);
int cbd_ext4_fini(void);
int cbd_ext4_open(const char* filename);
int cbd_ext4_open(const char *filename);
int cbd_ext4_close(int fd);
int cbd_ext4_pread(int fd, void* buf, off_t offset, size_t length);
int cbd_ext4_pwrite(int fd, const void* buf, off_t offset, size_t length);
int cbd_ext4_pread(int fd, void *buf, off_t offset, size_t length);
int cbd_ext4_pwrite(int fd, const void *buf, off_t offset, size_t length);
int cbd_ext4_pdiscard(int fd, off_t offset, size_t length);
int cbd_ext4_aio_pread(int fd, CurveAioContext* context);
int cbd_ext4_aio_pwrite(int fd, CurveAioContext* context);
int cbd_ext4_aio_pdiscard(int fd, CurveAioContext* context);
int cbd_ext4_aio_pread(int fd, CurveAioContext *context);
int cbd_ext4_aio_pwrite(int fd, CurveAioContext *context);
int cbd_ext4_aio_pdiscard(int fd, CurveAioContext *context);
int cbd_ext4_sync(int fd);
int64_t cbd_ext4_filesize(const char* filename);
int cbd_ext4_increase_epoch(const char* filename);
int64_t cbd_ext4_filesize(const char *filename);
int cbd_ext4_increase_epoch(const char *filename);

int cbd_libcurve_init(const CurveOptions* options);
int cbd_libcurve_init(const CurveOptions *options);
int cbd_libcurve_fini(void);
int cbd_libcurve_open(const char* filename);
int cbd_libcurve_open(const char *filename);
int cbd_libcurve_close(int fd);
int cbd_libcurve_pread(int fd, void* buf, off_t offset, size_t length);
int cbd_libcurve_pwrite(int fd, const void* buf, off_t offset, size_t length);
int cbd_libcurve_pread(int fd, void *buf, off_t offset, size_t length);
int cbd_libcurve_pwrite(int fd, const void *buf, off_t offset, size_t length);
int cbd_libcurve_pdiscard(int fd, off_t offset, size_t length);
int cbd_libcurve_aio_pread(int fd, CurveAioContext* context);
int cbd_libcurve_aio_pwrite(int fd, CurveAioContext* context);
int cbd_libcurve_aio_pdiscard(int fd, CurveAioContext* context);
int cbd_libcurve_aio_pread(int fd, CurveAioContext *context);
int cbd_libcurve_aio_pwrite(int fd, CurveAioContext *context);
int cbd_libcurve_aio_pdiscard(int fd, CurveAioContext *context);
int cbd_libcurve_sync(int fd);
int64_t cbd_libcurve_filesize(const char* filename);
int cbd_libcurve_resize(const char* filename, int64_t size);
int cbd_libcurve_increase_epoch(const char* filename);
int64_t cbd_libcurve_filesize(const char *filename);
int cbd_libcurve_resize(const char *filename, int64_t size);
int cbd_libcurve_increase_epoch(const char *filename);

#ifndef CBD_BACKEND_FAKE
#define cbd_lib_init cbd_libcurve_init
#define cbd_lib_fini cbd_libcurve_fini
#define cbd_lib_open cbd_libcurve_open
#define cbd_lib_close cbd_libcurve_close
#define cbd_lib_pread cbd_libcurve_pread
#define cbd_lib_pwrite cbd_libcurve_pwrite
#define cbd_lib_pdiscard cbd_libcurve_pdiscard
#define cbd_lib_aio_pread cbd_libcurve_aio_pread
#define cbd_lib_aio_pwrite cbd_libcurve_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_libcurve_aio_pdiscard
#define cbd_lib_sync cbd_libcurve_sync
#define cbd_lib_filesize cbd_libcurve_filesize
#define cbd_lib_resize cbd_libcurve_resize
#define cbd_lib_increase_epoch cbd_libcurve_increase_epoch
#define cbd_lib_init cbd_libcurve_init
#define cbd_lib_fini cbd_libcurve_fini
#define cbd_lib_open cbd_libcurve_open
#define cbd_lib_close cbd_libcurve_close
#define cbd_lib_pread cbd_libcurve_pread
#define cbd_lib_pwrite cbd_libcurve_pwrite
#define cbd_lib_pdiscard cbd_libcurve_pdiscard
#define cbd_lib_aio_pread cbd_libcurve_aio_pread
#define cbd_lib_aio_pwrite cbd_libcurve_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_libcurve_aio_pdiscard
#define cbd_lib_sync cbd_libcurve_sync
#define cbd_lib_filesize cbd_libcurve_filesize
#define cbd_lib_resize cbd_libcurve_resize
#define cbd_lib_increase_epoch cbd_libcurve_increase_epoch
#else
#define cbd_lib_init cbd_ext4_init
#define cbd_lib_fini cbd_ext4_fini
#define cbd_lib_open cbd_ext4_open
#define cbd_lib_close cbd_ext4_close
#define cbd_lib_pread cbd_ext4_pread
#define cbd_lib_pwrite cbd_ext4_pwrite
#define cbd_lib_pdiscard cbd_ext4_pdiscard
#define cbd_lib_aio_pread cbd_ext4_aio_pread
#define cbd_lib_aio_pwrite cbd_ext4_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_ext4_aio_pdiscard
#define cbd_lib_sync cbd_ext4_sync
#define cbd_lib_filesize cbd_ext4_filesize
#define cbd_lib_increase_epoch cbd_ext4_increase_epoch
#define cbd_lib_init cbd_ext4_init
#define cbd_lib_fini cbd_ext4_fini
#define cbd_lib_open cbd_ext4_open
#define cbd_lib_close cbd_ext4_close
#define cbd_lib_pread cbd_ext4_pread
#define cbd_lib_pwrite cbd_ext4_pwrite
#define cbd_lib_pdiscard cbd_ext4_pdiscard
#define cbd_lib_aio_pread cbd_ext4_aio_pread
#define cbd_lib_aio_pwrite cbd_ext4_aio_pwrite
#define cbd_lib_aio_pdiscard cbd_ext4_aio_pdiscard
#define cbd_lib_sync cbd_ext4_sync
#define cbd_lib_filesize cbd_ext4_filesize
#define cbd_lib_increase_epoch cbd_ext4_increase_epoch
#endif

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit d817215

Please sign in to comment.