Skip to content

Commit

Permalink
Fixed the race on free with last_append_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
hkadayam committed May 9, 2024
1 parent 9f2c140 commit 17d695b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1,371 deletions.
21 changes: 16 additions & 5 deletions src/lib/blkalloc/append_blk_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,24 @@ void AppendBlkAllocator::cp_flush(CP* cp) {
//
void AppendBlkAllocator::free(const BlkId& bid) {
// If we are freeing the last block, just move the offset back
auto const nblks = bid.blk_count();
auto exp_last_offset = bid.blk_num() + nblks;
auto const new_offset = m_last_append_offset - nblks;
// If we are freeing the last block, just move the offset back
blk_num_t cur_last_offset = m_last_append_offset.load();
auto const input_last_offset = bid.blk_num() + bid.blk_count();
blk_num_t new_last_offset;
bool freeing_in_middle{false};
do {
if (input_last_offset == cur_last_offset) {
new_last_offset = bid.blk_num();
freeing_in_middle = false;
} else {
new_last_offset = cur_last_offset;
freeing_in_middle = true;
}
} while (!m_last_append_offset.compare_exchange_weak(cur_last_offset, new_last_offset));

if (!m_last_append_offset.compare_exchange_strong(exp_last_offset, new_offset)) {
if (freeing_in_middle) {
// Freeing something in the middle, increment the count
m_freeable_nblks.fetch_add(nblks);
m_freeable_nblks.fetch_add(bid.blk_count());
}
}

Expand Down
Loading

0 comments on commit 17d695b

Please sign in to comment.