Skip to content

Commit

Permalink
Merge pull request #258 from yuchen0cc/main
Browse files Browse the repository at this point in the history
fix bug in retry open zfile
  • Loading branch information
BigVan authored Aug 29, 2023
2 parents 0b3d978 + ad2a737 commit 9194f47
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
24 changes: 20 additions & 4 deletions src/image_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "switch_file.h"
#include "overlaybd/gzip/gz.h"
#include "overlaybd/gzindex/gzfile.h"
#include "overlaybd/tar/tar_file.h"

#define PARALLEL_LOAD_INDEX 32
using namespace photon::fs;
Expand Down Expand Up @@ -71,10 +72,18 @@ IFile *ImageFile::__open_ro_file(const std::string &path) {
}
file = aligned_file;
}

auto tar_file = new_tar_file_adaptor(file);
if (!tar_file) {
set_failed("failed to open file as tar file " + path);
delete file;
LOG_ERROR_RETURN(0, nullptr, "new_tar_file_adaptor(`) failed", path);
}
file = tar_file;
// set to local, no need to switch, for zfile and audit
ISwitchFile *switch_file = new_switch_file(file, true, path.c_str());
if (!switch_file) {
set_failed("failed to open switch file `" + path);
set_failed("failed to open switch file " + path);
delete file;
LOG_ERRNO_RETURN(0, nullptr, "new_switch_file(`) failed", path);
}
Expand Down Expand Up @@ -158,10 +167,17 @@ IFile *ImageFile::__open_ro_remote(const std::string &dir, const std::string &di
remote_file->ioctl(SET_SIZE, size);
remote_file->ioctl(SET_LOCAL_DIR, dir);

ISwitchFile *switch_file = new_switch_file(remote_file);
if (!switch_file) {
set_failed("failed to open switch file `" + url);
IFile *tar_file = new_tar_file_adaptor(remote_file);
if (!tar_file) {
set_failed("failed to open remote file as tar file " + url);
delete remote_file;
LOG_ERROR_RETURN(0, nullptr, "failed to open remote file as tar file `", url);
}

ISwitchFile *switch_file = new_switch_file(tar_file, false, url.c_str());
if (!switch_file) {
set_failed("failed to open switch file " + url);
delete tar_file;
LOG_ERROR_RETURN(0, nullptr, "failed to open switch file `", url);
}

Expand Down
7 changes: 6 additions & 1 deletion src/overlaybd/zfile/zfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,18 @@ class CompressionFile : public VirtualReadOnlyFile {

int get_current_block() {
m_reader->m_buf_offset = m_reader->get_buf_offset(m_reader->m_idx);
if ((size_t)(m_reader->m_buf_offset) > sizeof(m_buf)) {
if ((size_t)(m_reader->m_buf_offset) >= sizeof(m_buf)) {
m_reader->m_eno = ERANGE;
LOG_ERRNO_RETURN(0, -1, "get inner buffer offset failed.");
}

auto blk_idx = m_reader->m_idx;
compressed_size = m_reader->compressed_size();
if ((size_t)(m_reader->m_buf_offset) + compressed_size > sizeof(m_buf)) {
m_reader->m_eno = ERANGE;
LOG_ERRNO_RETURN(0, -1, "inner buffer offset (`) + compressed size (`) overflow.",
m_reader->m_buf_offset, compressed_size);
}

if (blk_idx == m_reader->m_begin_idx) {
cp_begin = m_reader->get_inblock_offset(m_reader->m_offset);
Expand Down
18 changes: 12 additions & 6 deletions src/switch_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ using namespace photon::fs;
static IFile *try_open_zfile(IFile *file, bool verify, const char *file_path) {
auto is_zfile = ZFile::is_zfile(file);
if (is_zfile == -1) {
delete file;
LOG_ERRNO_RETURN(0, nullptr, "check file type failed.");
}
// open zfile
Expand Down Expand Up @@ -82,9 +81,16 @@ class SwitchFile : public ISwitchFile {
LOG_ERROR("failed to open commit file, path: `", m_filepath);
return;
}

file = try_open_zfile(new_tar_file_adaptor(file), false, m_filepath.c_str());
if (file == nullptr) {
auto tarfile = new_tar_file_adaptor(file);
if (tarfile == nullptr) {
delete file;
LOG_ERROR("failed to open commit file as tar file, path: `", m_filepath);
return;
}
file = tarfile;
auto zfile = try_open_zfile(file, false, m_filepath.c_str());
if (zfile == nullptr) {
delete file;
LOG_ERROR("failed to open commit file as zfile, path: `", m_filepath);
return;
}
Expand Down Expand Up @@ -157,11 +163,11 @@ class SwitchFile : public ISwitchFile {
};

ISwitchFile *new_switch_file(IFile *source, bool local, const char *file_path) {
// if tar file, open tar file
int retry = 1;
again:
auto file = try_open_zfile(new_tar_file_adaptor(source), !local, file_path);
auto file = try_open_zfile(source, !local, file_path);
if (file == nullptr) {
LOG_ERROR("failed to open source file as zfile, path: `, retry: `", file_path, retry);
if (retry--) // may retry after cache evict
goto again;
return nullptr;
Expand Down

0 comments on commit 9194f47

Please sign in to comment.