google / google/leveldb

TableBuilder::Finish() dose not call rep_->file->Sync() at the end

Open
#693 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
39.4k
Forks
8.2k
PR merge metrics
No merged PRs in 30d

Description

When I used TableBuilder to manually build an SSTable, I found that TableBuilder::FileSize() was not equal to the size of rep_->file after I called TableBuilder::Finish(). It was very confusing to me.

The reason is that TableBuilder::Finish() does not call rep_->file->Sync() at the end. So, some data(meta index block, index block, and footer block ) in rep_->file may be still buffered and not flushed to persistent storage.

**But semantically, "Finish" means that after we called this function successfully, the SSTable should be completely built.**

So, I think that rep_->file->Sync() should be called at the end of TableBuilder::Finish().

[https://github.com/google/leveldb/blob/master/table/table_builder.cc#L240](https://github.com/google/leveldb/blob/master/table/table_builder.cc#L240)
```cpp
// Write footer
if (ok()) {
Footer footer;
footer.set_metaindex_handle(metaindex_block_handle);
footer.set_index_handle(index_block_handle);
std::string footer_encoding;
footer.EncodeTo(&footer_encoding);
r->status = r->file->Append(footer_encoding);
if (r->status.ok()) {
r->offset += footer_encoding.size();
}
}

if (ok()) {
r->status = r->file->Sync();
}
return r->status;
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.