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

apply merge version edits atomically #331

Open
wants to merge 2 commits into
base: 6.29.tikv
Choose a base branch
from
Open
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 db/db_impl/db_impl_merge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ Status DBImpl::MergeDisjointInstances(const MergeInstanceOptions& merge_options,
{
autovector<autovector<VersionEdit*>> edit_ptrs;
autovector<const MutableCFOptions*> cf_mopts;
// Mark the version edits as an atomic group if the number of version edits
// exceeds 1.
if (num_cfs > 1) {
for (size_t i = 0; i < num_cfs; i++) {
cf_edits[i].MarkAtomicGroup(num_cfs - i - 1 /*remaining_entries*/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the edit is empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine. It will still be encoded with a special trailing field.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps we can remove all those empty records before proceed. For example, if all data are in writecf, then only one edit is needed. For rawkv, all data are in default cf.

And if the edit contains too many files, will it be marked as atomic group by itself?

}
}
for (size_t i = 0; i < num_cfs; i++) {
edit_ptrs.push_back({&cf_edits[i]});
cf_mopts.push_back(this_cfds[i]->GetLatestMutableCFOptions());
Expand Down
16 changes: 12 additions & 4 deletions db/db_write_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ TEST_P(DBWriteTest, PostWriteCallback) {
the_first_can_exit_write_mutex.Lock();
port::Mutex can_flush_mutex;
can_flush_mutex.Lock();
port::Mutex the_second_can_enter_write_mutex;
the_second_can_enter_write_mutex.Lock();
port::Mutex the_second_can_exit_write_mutex;
the_second_can_exit_write_mutex.Lock();

Expand All @@ -599,13 +601,14 @@ TEST_P(DBWriteTest, PostWriteCallback) {
opts.disableWAL = true;
SimpleCallback callback([&](SequenceNumber seq) {
ASSERT_NE(seq, 0);
the_second_can_enter_write_mutex.Unlock();
can_flush_mutex.Unlock();
the_first_can_exit_write_mutex.Lock();
the_second_can_exit_write_mutex.Unlock();
written.fetch_add(1, std::memory_order_relaxed);
});
batch.Put("key", "value");
ASSERT_OK(dbfull()->Write(opts, &batch, &callback));
written.fetch_add(1, std::memory_order_relaxed);
}));
threads.push_back(port::Thread([&] {
WriteBatch batch;
Expand All @@ -615,10 +618,11 @@ TEST_P(DBWriteTest, PostWriteCallback) {
SimpleCallback callback([&](SequenceNumber seq) {
ASSERT_NE(seq, 0);
the_second_can_exit_write_mutex.Lock();
written.fetch_add(1, std::memory_order_relaxed);
});
batch.Put("key", "value");
the_second_can_enter_write_mutex.Lock();
ASSERT_OK(dbfull()->Write(opts, &batch, &callback));
written.fetch_add(1, std::memory_order_relaxed);
}));
// Flush will enter write thread and wait for pending writes.
threads.push_back(port::Thread([&] {
Expand All @@ -634,9 +638,13 @@ TEST_P(DBWriteTest, PostWriteCallback) {
ASSERT_EQ(flushed.load(std::memory_order_relaxed), false);

the_first_can_exit_write_mutex.Unlock();
std::this_thread::sleep_for(std::chrono::milliseconds{100});
size_t wait = 0;
while (!flushed.load(std::memory_order_relaxed)) {
std::this_thread::sleep_for(std::chrono::milliseconds{10});
wait += 1;
ASSERT_LE(wait, 100);
}
ASSERT_EQ(written.load(std::memory_order_relaxed), 2);
ASSERT_EQ(flushed.load(std::memory_order_relaxed), true);

for (auto& t : threads) {
t.join();
Expand Down