Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

refactor(fds_service): clean up duplicate code #627

Merged
merged 7 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 27 additions & 83 deletions src/block_service/fds/fds_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ dsn::task_ptr fds_service::list_dir(const ls_request &req,
return t;
}

// TODO(zhaoliwei) refactor these code, because there have same code in get_file_meta()
dsn::task_ptr fds_service::create_file(const create_file_request &req,
dsn::task_code code,
const create_file_callback &cb,
Expand All @@ -286,54 +285,27 @@ dsn::task_ptr fds_service::create_file(const create_file_request &req,
new fds_file_object(this, req.file_name, utils::path_to_fds(req.file_name, false));
t->enqueue_with(resp);
return t;
} else {
auto create_file_in_background = [this, req, t]() {
create_file_response resp;
resp.err = ERR_IO_PENDING;
std::string fds_path = utils::path_to_fds(req.file_name, false);
try {
std::shared_ptr<galaxy::fds::FDSObjectMetadata> metadata =
_client->getObjectMetadata(_bucket_name, fds_path);
// if we get the object metadata succeed, we expect to get the content-md5 and the
// content-length
const std::map<std::string, std::string> &meta_map = metadata->metadata();
auto iter = meta_map.find(FILE_MD5_KEY);
dassert(iter != meta_map.end(),
"can't find %s in object(%s)'s metadata",
FILE_MD5_KEY.c_str(),
fds_path.c_str());
const std::string &md5 = iter->second;

// in a head http-request, the file length is in the x-xiaomi-meta-content-length
// while in a get http-request, the file length is in the contentLength
iter = meta_map.find(FILE_LENGTH_CUSTOM_KEY);
dassert(iter != meta_map.end(),
"can't find %s in object(%s)'s metadata",
FILE_LENGTH_CUSTOM_KEY.c_str(),
fds_path.c_str());
uint64_t size = (uint64_t)atol(iter->second.c_str());
resp.err = dsn::ERR_OK;
resp.file_handle = new fds_file_object(this, req.file_name, fds_path, md5, size);
} catch (const galaxy::fds::GalaxyFDSClientException &ex) {
if (ex.code() == Poco::Net::HTTPResponse::HTTP_NOT_FOUND) {
resp.err = dsn::ERR_OK;
resp.file_handle = new fds_file_object(this, req.file_name, fds_path, "", 0);
} else {
derror("fds getObjectMetadata failed: parameter(%s), code(%d), msg(%s)",
req.file_name.c_str(),
ex.code(),
ex.what());
resp.err = ERR_FS_INTERNAL;
}
}
FDS_EXCEPTION_HANDLE(resp.err, "getObjectMetadata", req.file_name.c_str());
}

t->enqueue_with(resp);
};
auto create_file_in_background = [this, req, t]() {
create_file_response resp;
resp.err = ERR_IO_PENDING;
std::string fds_path = utils::path_to_fds(req.file_name, false);

dsn::tasking::enqueue(LPC_FDS_CALL, nullptr, create_file_in_background);
return t;
}
dsn::ref_ptr<fds_file_object> f = new fds_file_object(this, req.file_name, fds_path);
error_code err = f->get_file_meta();
if (err == ERR_FS_INTERNAL) {
resp.err = err;
} else {
resp.err = ERR_OK;
resp.file_handle = f;
}

t->enqueue_with(resp);
};

dsn::tasking::enqueue(LPC_FDS_CALL, nullptr, create_file_in_background);
return t;
}

dsn::task_ptr fds_service::delete_file(const delete_file_request &req,
Expand Down Expand Up @@ -503,7 +475,7 @@ fds_file_object::fds_file_object(fds_service *s,
: block_file(name),
_service(s),
_fds_path(fds_path),
_md5sum(),
_md5sum(""),
_size(0),
_has_meta_synced(false)
{
Expand All @@ -527,6 +499,7 @@ fds_file_object::~fds_file_object() {}

error_code fds_file_object::get_file_meta()
{
error_code err = ERR_OK;
galaxy::fds::GalaxyFDSClient *c = _service->get_client();
try {
auto meta = c->getObjectMetadata(_service->get_bucket_name(), _fds_path)->metadata();
Expand All @@ -549,18 +522,15 @@ error_code fds_file_object::get_file_meta()
_md5sum = iter->second;

_has_meta_synced = true;
return ERR_OK;
} catch (const galaxy::fds::GalaxyFDSClientException &ex) {
if (ex.code() == Poco::Net::HTTPResponse::HTTP_NOT_FOUND) {
return ERR_OBJECT_NOT_FOUND;
err = ERR_OBJECT_NOT_FOUND;
} else {
derror_f("fds getObjectMetadata failed: parameter({}), code({}), msg({})",
_name.c_str(),
ex.code(),
ex.what());
zhangyifan27 marked this conversation as resolved.
Show resolved Hide resolved
return ERR_FS_INTERNAL;
err = ERR_FS_INTERNAL;
}
}
FDS_EXCEPTION_HANDLE(err, "getObjectMetadata", _fds_path.c_str());
return err;
}

error_code fds_file_object::get_content_in_batches(uint64_t start,
Expand Down Expand Up @@ -681,36 +651,10 @@ error_code fds_file_object::put_content(/*in-out*/ std::istream &is,
return err;
}

try {
// Get Object meta data
std::shared_ptr<galaxy::fds::FDSObjectMetadata> metadata =
c->getObjectMetadata(_service->get_bucket_name(), _fds_path);
const std::map<std::string, std::string> metaMap = metadata->metadata();

auto iter = metaMap.find(fds_service::FILE_MD5_KEY);
dassert(iter != metaMap.end(),
"can't find %s in object(%s)'s metadata",
fds_service::FILE_MD5_KEY.c_str(),
_fds_path.c_str());
_md5sum = iter->second;

// in a head http-request, the file length is in the x-xiaomi-meta-content-length
// while in a get http-request, the file length is in the contentLength
iter = metaMap.find(fds_service::FILE_LENGTH_CUSTOM_KEY);
dassert(iter != metaMap.end(),
"can't find %s in object(%s) metadata",
fds_service::FILE_LENGTH_CUSTOM_KEY.c_str(),
_fds_path.c_str());
_size = (uint64_t)atoll(iter->second.c_str());
hycdong marked this conversation as resolved.
Show resolved Hide resolved
transfered_bytes = _size;
zhangyifan27 marked this conversation as resolved.
Show resolved Hide resolved
} catch (const galaxy::fds::GalaxyFDSClientException &ex) {
derror("fds getObjectMetadata after put failed: remote_file(%s), code(%d), msg(%s)",
file_name().c_str(),
ex.code(),
ex.what());
ddebug("start to check meta data after successfully wrote data to fds");
if (get_file_meta() != ERR_OK) {
err = ERR_FS_INTERNAL;
}
FDS_EXCEPTION_HANDLE(err, "getObjectMetadata_after_put", file_name().c_str())
zhangyifan27 marked this conversation as resolved.
Show resolved Hide resolved
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion src/block_service/fds/fds_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class fds_file_object : public block_file
dsn::task_code code,
const download_callback &cb,
dsn::task_tracker *tracker) override;
error_code get_file_meta();
zhangyifan27 marked this conversation as resolved.
Show resolved Hide resolved

private:
error_code get_content_in_batches(uint64_t start,
Expand All @@ -123,7 +124,6 @@ class fds_file_object : public block_file
error_code put_content(/*in-out*/ std::istream &is,
/*int*/ int64_t to_transfer_bytes,
/*out*/ uint64_t &transfered_bytes);
error_code get_file_meta();

fds_service *_service;
std::string _fds_path;
Expand Down