Skip to content

Commit 72a87f8

Browse files
authored
Merge pull request #359 from WaberZhuang/main
fix and enhance dynamic prefetcher
2 parents be5e0a4 + cadc820 commit 72a87f8

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

src/image_file.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,20 +493,30 @@ int ImageFile::init_image_file() {
493493
LOG_ERROR("open upper layer failed.");
494494
goto ERROR_EXIT;
495495
}
496-
stack_ret = LSMT::stack_files(upper_file, lower_file, true, false);
496+
// We have to maintain the lower_file because prefetcher need the readonly
497+
// lower_file but not the writable m_file.
498+
//
499+
// Otherwise, lower_file and upper_file will lose the ownership of m_files
500+
// after stack_files(...), so we could safely delete them.
501+
//
502+
// upper_file will be deleted immediately since it's useless.
503+
// lower_file will be held by ImageFile until deconstruct.
504+
stack_ret = LSMT::stack_files(upper_file, lower_file, false, false);
497505
if (!stack_ret) {
498506
LOG_ERROR("LSMT::stack_files(`, `)", (uint64_t)upper_file, true);
499507
goto ERROR_EXIT;
500508
}
501509
m_file = stack_ret;
502510
read_only = false;
511+
delete upper_file;
512+
m_lower_file = lower_file;
503513

504514
SUCCESS_EXIT:
505515
if (conf.download().enable() && !record_no_download) {
506516
start_bk_dl_thread();
507517
}
508518
if (m_prefetcher != nullptr) {
509-
m_prefetcher->replay(m_file);
519+
m_prefetcher->replay(lower_file);
510520
}
511521
return 1;
512522

src/image_file.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static std::string SEALED_FILE_NAME = "overlaybd.sealed";
4242
class ImageFile : public photon::fs::ForwardFile {
4343
public:
4444
ImageFile(ImageConfigNS::ImageConfig &_conf, ImageService &is)
45-
: ForwardFile(nullptr), image_service(is) {
45+
: ForwardFile(nullptr), image_service(is), m_lower_file(nullptr) {
4646
conf.CopyFrom(_conf, conf.GetAllocator());
4747
m_exception = "";
4848
m_status = init_image_file();
@@ -62,6 +62,9 @@ class ImageFile : public photon::fs::ForwardFile {
6262
m_file->close();
6363
delete m_file;
6464
}
65+
if (m_lower_file) {
66+
delete m_lower_file;
67+
}
6568
}
6669

6770
int fstat(struct stat *buf) override {
@@ -117,6 +120,7 @@ class ImageFile : public photon::fs::ForwardFile {
117120
std::list<BKDL::BkDownload *> dl_list;
118121
photon::join_handle *dl_thread_jh = nullptr;
119122
ImageService &image_service;
123+
photon::fs::IFile *m_lower_file = nullptr;
120124

121125
int init_image_file();
122126
template<typename...Ts> void set_failed(const Ts&...xs);

src/overlaybd/gzindex/gzfile.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "photon/photon.h"
2929
#include "photon/fs/virtual-file.h"
3030
#include "photon/common/checksum/crc32c.h"
31+
#include "photon/thread/thread.h"
3132

3233
namespace FileSystem {
3334
using namespace photon::fs;
@@ -62,6 +63,7 @@ class GzFile : public VirtualReadOnlyFile {
6263
struct IndexFileHeader index_header_;
6364
INDEX index_;
6465
bool inited_ = false;
66+
photon::mutex init_mutex_;
6567
int init();
6668
int parse_index();
6769
IndexEntry *seek_index(INDEX &index, off_t offset);
@@ -142,6 +144,7 @@ int GzFile::parse_index() {
142144
}
143145

144146
int GzFile::init() {
147+
SCOPED_LOCK(init_mutex_);
145148
if (inited_) {
146149
return 0;
147150
}

src/prefetch.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ limitations under the License.
2929
#include "prefetch.h"
3030
#include "tools/comm_func.h"
3131
#include "overlaybd/lsmt/file.h"
32+
#include "overlaybd/zfile/crc32/crc32c.h"
33+
3234
#include <photon/common/alog.h>
3335
#include <photon/common/alog-stdstring.h>
3436
#include <photon/fs/forwardfs.h>
3537
#include <photon/fs/localfs.h>
3638
#include <photon/thread/thread11.h>
37-
#include "overlaybd/zfile/crc32/crc32c.h"
38-
#include "photon/fs/filesystem.h"
39-
#include "photon/fs/fiemap.h"
40-
#include "photon/fs/path.h"
41-
#include "photon/common/enumerable.h"
39+
#include <photon/fs/filesystem.h>
40+
#include <photon/fs/fiemap.h>
41+
#include <photon/fs/path.h>
42+
#include <photon/common/enumerable.h>
43+
#include <photon/fs/extfs/extfs.h>
4244

4345

4446
using namespace std;
@@ -370,7 +372,7 @@ class DynamicPrefetcher : public PrefetcherImpl {
370372
while (start <= end && str[start] == c) {
371373
++start;
372374
}
373-
while (end >= start && str[start] == c) {
375+
while (end >= start && str[end] == c) {
374376
--end;
375377
}
376378
return str.substr(start, end - start + 1);
@@ -394,7 +396,7 @@ class DynamicPrefetcher : public PrefetcherImpl {
394396
file.seekg(0, ios::beg);
395397
std::string line;
396398
while (std::getline(file, line)) {
397-
line = trim(trim(line, ' '), '/');
399+
line = trim(line, ' ');
398400
if (line.empty() || !invalid_abs_path(line)) {
399401
continue;
400402
}
@@ -470,7 +472,7 @@ class DynamicPrefetcher : public PrefetcherImpl {
470472
if (fstype == "erofs")
471473
fs = create_erofs_fs(const_cast<IFile*>(imagefile), 4096);
472474
else
473-
fs = create_ext4fs(const_cast<IFile*>(imagefile), false, true, "/");
475+
fs = new_extfs(const_cast<IFile*>(imagefile), true);
474476

475477
if (fs == nullptr) {
476478
LOG_ERROR_RETURN(0, -1, "unrecognized filesystem in dynamic prefetcher");

0 commit comments

Comments
 (0)