diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 94e9df64..3c8372e4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/image_file.cpp b/src/image_file.cpp index f15627dc..7a1b5f25 100644 --- a/src/image_file.cpp +++ b/src/image_file.cpp @@ -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" @@ -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); @@ -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() { @@ -370,14 +362,13 @@ 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; @@ -385,13 +376,13 @@ LSMT::IFileRW *ImageFile::open_upper(ImageConfigNS::UpperConfig &upper) { 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; diff --git a/src/main.cpp b/src/main.cpp index 8f239f8b..4f4f9f34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -94,6 +94,29 @@ class TCMULoop { } }; +using SureIODelegate = Delegate; + +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; @@ -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 { diff --git a/src/sure_file.cpp b/src/sure_file.cpp deleted file mode 100644 index 0072a87a..00000000 --- a/src/sure_file.cpp +++ /dev/null @@ -1,131 +0,0 @@ -/* - Copyright The Overlaybd Authors - - 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. -*/ -#include -#include -#include -#include -#include -#include -#include "image_file.h" -#include "sure_file.h" - -using namespace photon::fs; - -class SureFile : public ForwardFile_Ownership { -public: - SureFile() = delete; - SureFile(IFile *src_file, ImageFile *image_file, bool ownership) - : ForwardFile_Ownership(src_file, ownership), m_ifile(image_file) { - } - -private: - ImageFile *m_ifile = nullptr; - - void io_sleep(uint64_t &try_cnt) { - if (try_cnt < 10) - photon::thread_usleep(500); // 500us - else - photon::thread_usleep(2000); // 2ms - - if (try_cnt > 30000) // >1min - photon::thread_sleep(1); // 1sec - } - - void io_hand() { - while (m_ifile && m_ifile->m_status >= 0) { - LOG_ERROR("write(...) incorrect, io hang here!"); - photon::thread_sleep(300); - } - } - -public: - virtual ssize_t write(const void *buf, size_t count) override { - size_t done_cnt = 0; - while (m_ifile && m_ifile->m_status >= 0 && done_cnt < count) { - ssize_t ret = m_file->write((char *)buf + done_cnt, count - done_cnt); - if (ret > 0) - done_cnt += ret; - if (done_cnt == count) - return count; - if (done_cnt > count) { - LOG_ERROR("write(...), done_cnt(`)>count(`), ret:`, errno:`, need io hang", - done_cnt, count, ret, errno); - io_hand(); - } - - if (ret == -1 && errno == EINTR) { - LOG_INFO("write(...), errno:EINTR, need continue try."); - continue; - } else { - LOG_ERROR("write(...), done_cnt(`)>count(`), ret:`, errno:`, need io hang", - done_cnt, count, ret, errno); - io_hand(); - } - } - return done_cnt; - } - - virtual ssize_t pread(void *buf, size_t count, off_t offset) override { - uint64_t try_cnt = 0; - size_t got_cnt = 0; - auto time_st = photon::now; - while (m_ifile && m_ifile->m_status >= 0 && photon::now - time_st < 1000UL * INT32_MAX) { - // exit on image in exit status, or timeout - ssize_t ret = m_file->pread((char *)buf + got_cnt, count - got_cnt, offset + got_cnt); - if (ret > 0) - got_cnt += ret; - if (got_cnt == count) - return count; - - if ((ret < 0) && (m_ifile->m_status < 1) && (errno == EPERM)) { - // exit when booting. after boot, hang. - m_ifile->set_auth_failed(); - LOG_ERROR_RETURN(0, -1, "authentication failed during image boot."); - } - - if (got_cnt > count) { - LOG_ERROR("pread(,`,`) return `. got_cnt:` > count:`, restart pread.", count, - offset, ret, got_cnt, count); - got_cnt = 0; - } - - io_sleep(try_cnt); - try_cnt++; - - if (try_cnt % 300 == 0) { - LOG_ERROR("pread read partial data. count:`, offset:`, ret:`, got_cnt:`, errno:`", - count, offset, ret, got_cnt, errno); - } - } - return -1; - } -}; - - -IFile *new_sure_file(IFile *src_file, ImageFile *image_file, - bool ownership) { - if (!src_file) { - LOG_ERROR("failed to new_sure_file(null)"); - return nullptr; - } - return new SureFile(src_file, image_file, ownership); -} - -IFile *new_sure_file_by_path(const char *file_path, int open_flags, - ImageFile *image_file, bool ownership) { - auto file = open_localfile_adaptor(file_path, open_flags, 0644, 0); - return new_sure_file(file, image_file, ownership); -} \ No newline at end of file diff --git a/src/sure_file.h b/src/sure_file.h deleted file mode 100644 index 1aaccd74..00000000 --- a/src/sure_file.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - Copyright The Overlaybd Authors - - 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. -*/ -#pragma once -#include - -class ImageFile; - -IFile *new_sure_file(IFile *src_file, ImageFile *image_file, - bool ownership = true); -IFile *new_sure_file_by_path(const char *file_path, int open_flags, - ImageFile *image_file, bool ownership = true); \ No newline at end of file