Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush segment cache when doing the compaction #5284

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dbms/src/Storages/DeltaMerge/DeltaMergeStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,13 @@ std::optional<DM::RowKeyRange> DeltaMergeStore::mergeDeltaBySegment(const Contex
segment = segment_it->second;
}

if (!segment->flushCache(*dm_context))
{
// If the flush failed, it means there are parallel updates to the segment in the background.
// In this case, we try again.
continue;
}

const auto new_segment = segmentMergeDelta(*dm_context, segment, run_thread);
if (new_segment)
{
Expand Down
49 changes: 49 additions & 0 deletions dbms/src/Storages/DeltaMerge/tests/gtest_dm_delta_merge_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3762,6 +3762,55 @@ try
CATCH


// Verify that unflushed data will also be compacted.
TEST_P(DeltaMergeStoreMergeDeltaBySegmentTest, Flush)
try
{
{
// Write data to first 3 segments and flush.
auto newly_written_rows = helper->rows_by_segments[0] + helper->rows_by_segments[1] + helper->rows_by_segments[2];
Block block = DMTestEnv::prepareSimpleWriteBlock(0, newly_written_rows, false, pk_type, 5 /* new tso */);
store->write(*db_context, db_context->getSettingsRef(), block);
store->flushCache(dm_context, RowKeyRange::newAll(store->isCommonHandle(), store->getRowKeyColumnSize()));

helper->expected_delta_rows[0] += helper->rows_by_segments[0];
helper->expected_delta_rows[1] += helper->rows_by_segments[1];
helper->expected_delta_rows[2] += helper->rows_by_segments[2];
helper->verifyExpectedRowsForAllSegments();

auto segment1 = std::next(store->segments.begin())->second;
ASSERT_EQ(segment1->getDelta()->getUnsavedRows(), 0);
}
{
// Write new data to segment[1] without flush.
auto newly_written_rows = helper->rows_by_segments[1];
Block block = DMTestEnv::prepareSimpleWriteBlock(helper->rows_by_segments[0], helper->rows_by_segments[0] + newly_written_rows, false, pk_type, 10 /* new tso */);
store->write(*db_context, db_context->getSettingsRef(), block);

helper->expected_delta_rows[1] += helper->rows_by_segments[1];
helper->verifyExpectedRowsForAllSegments();

auto segment1 = std::next(store->segments.begin())->second;
ASSERT_GT(segment1->getDelta()->getUnsavedRows(), 0);
}
{
auto segment1 = std::next(store->segments.begin())->second;
auto result = store->mergeDeltaBySegment(*db_context, segment1->getRowKeyRange().start, DeltaMergeStore::TaskRunThread::Foreground);
ASSERT_NE(result, std::nullopt);

segment1 = std::next(store->segments.begin())->second;
ASSERT_EQ(*result, segment1->getRowKeyRange());

helper->expected_stable_rows[1] += helper->expected_delta_rows[1];
helper->expected_delta_rows[1] = 0;
helper->verifyExpectedRowsForAllSegments();

ASSERT_EQ(segment1->getDelta()->getUnsavedRows(), 0);
}
}
CATCH


} // namespace tests
} // namespace DM
} // namespace DB