Skip to content

Commit

Permalink
Merge pull request #254 from liulanzheng/main
Browse files Browse the repository at this point in the history
refactor io sure
  • Loading branch information
yuchen0cc authored Aug 24, 2023
2 parents 1f433a8 + 712b0ea commit b2c803d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 171 deletions.
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ add_subdirectory(overlaybd)
add_library(overlaybd_image_lib
image_file.cpp
image_service.cpp
sure_file.cpp
switch_file.cpp
bk_download.cpp
prefetch.cpp
Expand Down
19 changes: 5 additions & 14 deletions src/image_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "overlaybd/zfile/zfile.h"
#include "config.h"
#include "image_file.h"
#include "sure_file.h"
#include "switch_file.h"
#include "overlaybd/gzip/gz.h"
#include "overlaybd/gzindex/gzfile.h"
Expand Down Expand Up @@ -168,13 +167,6 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
LOG_ERROR_RETURN(0, nullptr, "failed to open switch file `", url);
}

IFile *sure_file = new_sure_file(switch_file, this);
if (!sure_file) {
set_failed("failed to open sure file `" + url);
delete switch_file;
LOG_ERROR_RETURN(0, nullptr, "failed to open sure file `", url);
}

if (conf.HasMember("download") && conf.download().enable() == 1) {
// download from registry, verify sha256 after downloaded.
IFile *srcfile = image_service.global_fs.srcfs->open(url.c_str(), O_RDONLY);
Expand All @@ -189,7 +181,7 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
}
}

return sure_file;
return switch_file;
}

void ImageFile::start_bk_dl_thread() {
Expand Down Expand Up @@ -370,28 +362,27 @@ LSMT::IFileRW *ImageFile::open_upper(ImageConfigNS::UpperConfig &upper) {
IFile *idx_file = NULL;
IFile *target_file = NULL;
LSMT::IFileRW *ret = NULL;

data_file = new_sure_file_by_path(upper.data().c_str(), O_RDWR, this);
data_file = open_localfile_adaptor(upper.data().c_str(), O_RDWR, 0644);
if (!data_file) {
LOG_ERROR("open(`,flags), `:`", upper.data(), errno, strerror(errno));
goto ERROR_EXIT;
}

idx_file = new_sure_file_by_path(upper.index().c_str(), O_RDWR, this);
idx_file = open_localfile_adaptor(upper.index().c_str(), O_RDWR, 0644);
if (!idx_file) {
LOG_ERROR("open(`,flags), `:`", upper.index(), errno, strerror(errno));
goto ERROR_EXIT;
}

if (upper.target() != "") {
LOG_INFO("turboOCIv1 upper layer : `, `, `, `", upper.index(), upper.data(), upper.target());
target_file = new_sure_file_by_path(upper.target().c_str(), O_RDWR, this);
target_file = open_localfile_adaptor(upper.target().c_str(), O_RDWR, 0644);
if (!target_file) {
LOG_ERROR("open(`,flags), `:`", upper.target(), errno, strerror(errno));
goto ERROR_EXIT;
}
if (upper.gzipIndex() != "") {
auto gzip_index = new_sure_file_by_path(upper.gzipIndex().c_str(), O_RDWR, this);
auto gzip_index = open_localfile_adaptor(upper.gzipIndex().c_str(), O_RDWR, 0644);
if (!gzip_index) {
LOG_ERROR("open(`,flags), `:`", upper.gzipIndex(), errno, strerror(errno));
goto ERROR_EXIT;
Expand Down
26 changes: 25 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ class TCMULoop {
}
};

using SureIODelegate = Delegate<ssize_t, const struct iovec *, int, off_t>;

ssize_t sure(SureIODelegate io, const struct iovec *iov, int iovcnt, off_t offset) {
auto time_st = photon::now;
uint64_t try_cnt = 0, sleep_period = 20UL * 1000;
again:
if (photon::now - time_st > 7LL * 24 * 60 * 60 * 1000 * 1000 /*7days*/) {
LOG_ERROR_RETURN(EIO, -1, "sure request timeout, offset: `", offset);
}
ssize_t ret = io(iov, iovcnt, offset);
if (ret >= 0) {
return ret;
}
if (try_cnt % 10 == 0) {
LOG_ERROR("io request failed, offset: `, ret: `, retry times: `, errno:`", offset, ret,
try_cnt, errno);
}
try_cnt++;
photon::thread_usleep(sleep_period);
sleep_period = std::min(sleep_period * 2, 30UL * 1000 * 1000);
goto again;
}

void cmd_handler(struct tcmu_device *dev, struct tcmulib_cmd *cmd) {
obd_dev *odev = (obd_dev *)tcmu_dev_get_private(dev);
ImageFile *file = odev->file;
Expand Down Expand Up @@ -142,7 +165,8 @@ void cmd_handler(struct tcmu_device *dev, struct tcmulib_cmd *cmd) {
case READ_12:
case READ_16:
length = tcmu_iovec_length(cmd->iovec, cmd->iov_cnt);
ret = file->preadv(cmd->iovec, cmd->iov_cnt, tcmu_cdb_to_byte(dev, cmd->cdb));
ret = sure({file, &ImageFile::preadv}, cmd->iovec, cmd->iov_cnt,
tcmu_cdb_to_byte(dev, cmd->cdb));
if (ret == length) {
tcmulib_command_complete(dev, cmd, TCMU_STS_OK);
} else {
Expand Down
131 changes: 0 additions & 131 deletions src/sure_file.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions src/sure_file.h

This file was deleted.

0 comments on commit b2c803d

Please sign in to comment.