Skip to content

Commit 99b3160

Browse files
committed
[feat.] Parallel load ZFile index
To speed up zfile index loading, split a huge zfile index into multiple 1MB chunks and read them in parallel Signed-off-by: Yifan Yuan <tuji.yyf@alibaba-inc.com>
1 parent 82f95a6 commit 99b3160

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/image_file.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,6 @@ int ImageFile::compact(IFile *as) {
520520
return ((LSMT::IFileRO*)m_file)->flatten(as);
521521
}
522522

523-
524-
525523
void ImageFile::set_auth_failed() {
526524
if (m_status == 0) // only set exit in image boot phase
527525
{

src/overlaybd/lsmt/file.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ struct CommitArgs {
7575
class IFileRW : public IFileRO {
7676
public:
7777
virtual IMemoryIndex0 *index() const override = 0;
78-
7978
const int Index_Group_Commit = 10;
8079

8180
static const int RemoteData = 11;

src/overlaybd/zfile/zfile.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "compressor.h"
3434
#include <atomic>
3535
#include <thread>
36+
#include "photon/thread/thread11.h"
3637

3738
using namespace photon::fs;
3839

@@ -938,6 +939,16 @@ class ZFileBuilderMP : public ZFileBuilderBase {
938939
UNIMPLEMENTED(int fstat(struct stat *buf) override);
939940
};
940941

942+
ssize_t read_chunk(IFile *file, char *buf, off_t offset, size_t length, ssize_t *result) {
943+
LOG_DEBUG("read zfile index chunk {offset: `, len: `}", offset, length);
944+
if (file->pread(buf, length, offset) != (ssize_t)length) {
945+
*result = -1;
946+
LOG_ERRNO_RETURN(0, -1, "failed to read index chunk {offset: `, len: `}.", offset, length);
947+
}
948+
*result = 0;
949+
return 0;
950+
}
951+
941952
bool load_jump_table(IFile *file, CompressionFile::HeaderTrailer *pheader_trailer,
942953
CompressionFile::JumpTable &jump_table, bool trailer = true) {
943954
char buf[CompressionFile::HeaderTrailer::SPACE];
@@ -994,8 +1005,25 @@ bool load_jump_table(IFile *file, CompressionFile::HeaderTrailer *pheader_traile
9941005
}
9951006
auto ibuf = std::unique_ptr<uint32_t[]>(new uint32_t[pht->index_size]);
9961007
LOG_DEBUG("index_offset: `", pht->index_offset);
997-
ret = file->pread((void *)(ibuf.get()), index_bytes, pht->index_offset);
998-
if (ret < (ssize_t)index_bytes) {
1008+
1009+
size_t delta = 1UL<<20;
1010+
std::vector<photon::join_handle*> ths;
1011+
ssize_t *r = new ssize_t[index_bytes / delta + 1]{};
1012+
DEFER(delete []r);
1013+
int idx = 0;
1014+
for (off_t offset = 0; offset < (off_t)index_bytes; offset += delta) {
1015+
size_t chunk_size = std::min(index_bytes - offset, delta);
1016+
auto th = photon::thread_create11(&ZFile::read_chunk, file, (char*)ibuf.get() + offset, pht->index_offset + offset, chunk_size, &r[idx++]);
1017+
ths.push_back(photon::thread_enable_join(th));
1018+
}
1019+
ret = 0;
1020+
for (int i =0; i<idx; i++){
1021+
photon::thread_join(ths[i]);
1022+
if (r[i]!=0) {
1023+
ret = -1;
1024+
}
1025+
}
1026+
if (ret != 0) {
9991027
LOG_ERRNO_RETURN(0, false, "failed to read index");
10001028
}
10011029
if (pht->is_digest_enabled()) {

0 commit comments

Comments
 (0)