Clarify status-handling logic in BlockBasedTableBuilder::WriteRawBlock (#9393)

Summary:
**Context:**
Inside `BlockBasedTableBuilder::WriteRawBlock`, there are multiple places that change local variables `io_s` and `s` while
depend on them. This PR attempts to clarify the relevant logics so that it's easier to read and add places of changing these local variables later (like https://github.com/facebook/rocksdb/pull/9342.) without changing the current behavior.

**Summary:**
- Shorten the lifetime of local var `io_s` and `s` as much as possible to avoid if-else branches by early return

**Test**
- Reasoned against original behavior to verify new changes do not break existing behaviors.
- Rely on CI tests since we are not changing current behavior.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/9393

Reviewed By: pdillinger

Differential Revision: D33626095

Pulled By: hx235

fbshipit-source-id: 6184d1e1d85d2650d16617c449971988d062ed3f
main
Hui Xiao 3 years ago committed by Facebook GitHub Bot
parent 961d8dacf2
commit 11d7329503
  1. 57
      table/block_based/block_based_table_builder.cc

@ -386,6 +386,7 @@ struct BlockBasedTableBuilder::Rep {
} }
// Never erase an existing I/O status that is not OK. // Never erase an existing I/O status that is not OK.
// Calling this will also SetStatus(ios)
void SetIOStatus(IOStatus ios) { void SetIOStatus(IOStatus ios) {
if (!ios.ok() && io_status_ok.load(std::memory_order_relaxed)) { if (!ios.ok() && io_status_ok.load(std::memory_order_relaxed)) {
// Locking is an overkill for non compression_opts.parallel_threads // Locking is an overkill for non compression_opts.parallel_threads
@ -395,6 +396,7 @@ struct BlockBasedTableBuilder::Rep {
io_status = ios; io_status = ios;
io_status_ok.store(false, std::memory_order_relaxed); io_status_ok.store(false, std::memory_order_relaxed);
} }
SetStatus(ios);
} }
Rep(const BlockBasedTableOptions& table_opt, const TableBuilderOptions& tbo, Rep(const BlockBasedTableOptions& table_opt, const TableBuilderOptions& tbo,
@ -1231,29 +1233,39 @@ void BlockBasedTableBuilder::WriteRawBlock(const Slice& block_contents,
bool is_top_level_filter_block) { bool is_top_level_filter_block) {
Rep* r = rep_; Rep* r = rep_;
bool is_data_block = block_type == BlockType::kData; bool is_data_block = block_type == BlockType::kData;
Status s = Status::OK();
IOStatus io_s = IOStatus::OK();
StopWatch sw(r->ioptions.clock, r->ioptions.stats, WRITE_RAW_BLOCK_MICROS); StopWatch sw(r->ioptions.clock, r->ioptions.stats, WRITE_RAW_BLOCK_MICROS);
handle->set_offset(r->get_offset()); handle->set_offset(r->get_offset());
handle->set_size(block_contents.size()); handle->set_size(block_contents.size());
assert(status().ok()); assert(status().ok());
assert(io_status().ok()); assert(io_status().ok());
io_s = r->file->Append(block_contents);
if (io_s.ok()) { {
IOStatus io_s = r->file->Append(block_contents);
if (!io_s.ok()) {
r->SetIOStatus(io_s);
return;
}
}
std::array<char, kBlockTrailerSize> trailer; std::array<char, kBlockTrailerSize> trailer;
trailer[0] = type; trailer[0] = type;
uint32_t checksum = ComputeBuiltinChecksumWithLastByte( uint32_t checksum = ComputeBuiltinChecksumWithLastByte(
r->table_options.checksum, block_contents.data(), block_contents.size(), r->table_options.checksum, block_contents.data(), block_contents.size(),
/*last_byte*/ type); /*last_byte*/ type);
EncodeFixed32(trailer.data() + 1, checksum); EncodeFixed32(trailer.data() + 1, checksum);
assert(io_s.ok());
TEST_SYNC_POINT_CALLBACK( TEST_SYNC_POINT_CALLBACK(
"BlockBasedTableBuilder::WriteRawBlock:TamperWithChecksum", "BlockBasedTableBuilder::WriteRawBlock:TamperWithChecksum",
trailer.data()); trailer.data());
io_s = r->file->Append(Slice(trailer.data(), trailer.size())); {
if (io_s.ok()) { IOStatus io_s = r->file->Append(Slice(trailer.data(), trailer.size()));
assert(s.ok()); if (!io_s.ok()) {
r->SetIOStatus(io_s);
return;
}
}
{
Status s = Status::OK();
bool warm_cache; bool warm_cache;
switch (r->table_options.prepopulate_block_cache) { switch (r->table_options.prepopulate_block_cache) {
case BlockBasedTableOptions::PrepopulateBlockCache::kFlushOnly: case BlockBasedTableOptions::PrepopulateBlockCache::kFlushOnly:
@ -1277,32 +1289,31 @@ void BlockBasedTableBuilder::WriteRawBlock(const Slice& block_contents,
} }
if (!s.ok()) { if (!s.ok()) {
r->SetStatus(s); r->SetStatus(s);
return;
} }
} }
// TODO:: Should InsertBlockInCompressedCache take into account error from
// InsertBlockInCache or ignore and overwrite it.
s = InsertBlockInCompressedCache(block_contents, type, handle); s = InsertBlockInCompressedCache(block_contents, type, handle);
if (!s.ok()) { if (!s.ok()) {
r->SetStatus(s); r->SetStatus(s);
return;
} }
} else {
r->SetIOStatus(io_s);
} }
if (s.ok() && io_s.ok()) {
r->set_offset(r->get_offset() + block_contents.size() + r->set_offset(r->get_offset() + block_contents.size() + kBlockTrailerSize);
kBlockTrailerSize);
if (r->table_options.block_align && is_data_block) { if (r->table_options.block_align && is_data_block) {
size_t pad_bytes = size_t pad_bytes =
(r->alignment - ((block_contents.size() + kBlockTrailerSize) & (r->alignment -
(r->alignment - 1))) & ((block_contents.size() + kBlockTrailerSize) & (r->alignment - 1))) &
(r->alignment - 1); (r->alignment - 1);
io_s = r->file->Pad(pad_bytes); IOStatus io_s = r->file->Pad(pad_bytes);
if (io_s.ok()) { if (io_s.ok()) {
r->set_offset(r->get_offset() + pad_bytes); r->set_offset(r->get_offset() + pad_bytes);
} else { } else {
r->SetIOStatus(io_s); r->SetIOStatus(io_s);
return;
} }
} }
if (r->IsParallelCompressionEnabled()) { if (r->IsParallelCompressionEnabled()) {
if (is_data_block) { if (is_data_block) {
r->pc_rep->file_size_estimator.ReapBlock(block_contents.size(), r->pc_rep->file_size_estimator.ReapBlock(block_contents.size(),
@ -1312,13 +1323,6 @@ void BlockBasedTableBuilder::WriteRawBlock(const Slice& block_contents,
} }
} }
} }
} else {
r->SetIOStatus(io_s);
}
if (!io_s.ok() && s.ok()) {
r->SetStatus(io_s);
}
}
void BlockBasedTableBuilder::BGWorkWriteRawBlock() { void BlockBasedTableBuilder::BGWorkWriteRawBlock() {
Rep* r = rep_; Rep* r = rep_;
@ -1800,7 +1804,6 @@ void BlockBasedTableBuilder::WriteFooter(BlockHandle& metaindex_block_handle,
r->set_offset(r->get_offset() + footer.GetSlice().size()); r->set_offset(r->get_offset() + footer.GetSlice().size());
} else { } else {
r->SetIOStatus(ios); r->SetIOStatus(ios);
r->SetStatus(ios);
} }
} }

Loading…
Cancel
Save