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
This commit is contained in:
Ziyue Yang 2020-04-13 16:22:19 -07:00 committed by Facebook GitHub Bot
parent d9cad3a526
commit 41563b61db

View File

@ -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();
}