Fix data racing of BlockBasedTableBuilder::ParallelCompressionRep::first_block (#6640)

Summary:
BlockBasedTableBuilder::ParallelCompressionRep::first_block can be read in
Flush() and written in BGWorkWriteRawBlock() concurrently. This commit fixes
the issue by reading first_block out before pushing the block to compression
and write.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/6640

Test Plan: Run all tests concurrently with TSAN.

Reviewed By: cheng-chang

Differential Revision: D20851370

fbshipit-source-id: 6f039222e8319d31e15f1b45e05c106527253f72
main
Ziyue Yang 5 years ago committed by Facebook GitHub Bot
parent d9cad3a526
commit 41563b61db
  1. 7
      table/block_based/block_based_table_builder.cc

@ -818,6 +818,9 @@ void BlockBasedTableBuilder::Flush() {
new_blocks_inflight * kBlockTrailerSize,
std::memory_order_relaxed);
// Read out first_block here to avoid data race with BGWorkWriteRawBlock
bool first_block = r->pc_rep->first_block;
assert(block_rep->status.ok());
if (!r->pc_rep->write_queue.push(block_rep->slot.get())) {
return;
@ -826,7 +829,7 @@ void BlockBasedTableBuilder::Flush() {
return;
}
if (r->pc_rep->first_block) {
if (first_block) {
std::unique_lock<std::mutex> lock(r->pc_rep->first_block_mutex);
r->pc_rep->first_block_cond.wait(lock,
[=] { return !r->pc_rep->first_block; });
@ -1140,7 +1143,7 @@ void BlockBasedTableBuilder::BGWorkWriteRawBlock() {
}
if (r->pc_rep->first_block) {
std::unique_lock<std::mutex> lock(r->pc_rep->first_block_mutex);
std::lock_guard<std::mutex> lock(r->pc_rep->first_block_mutex);
r->pc_rep->first_block = false;
r->pc_rep->first_block_cond.notify_one();
}

Loading…
Cancel
Save