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

Fix deadlock happen in mvcc. #3911

Merged
merged 5 commits into from
Jan 20, 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
16 changes: 13 additions & 3 deletions dbms/src/Storages/Page/V3/PageDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ namespace PS::V3
* VersionedPageEntries methods *
********************************/

std::optional<PageEntryV3> VersionedPageEntries::getEntry(UInt64 seq) const
std::optional<PageEntryV3> VersionedPageEntries::getEntryNotSafe(UInt64 seq) const
{
auto page_lock = acquireLock();
// entries are sorted by <ver, epoch>, find the first one less than <ver+1, 0>
if (auto iter = MapUtils::findLess(entries, PageVersionType(seq + 1));
iter != entries.end())
Expand All @@ -36,6 +35,12 @@ std::optional<PageEntryV3> VersionedPageEntries::getEntry(UInt64 seq) const
return std::nullopt;
}

std::optional<PageEntryV3> VersionedPageEntries::getEntry(UInt64 seq) const
{
auto page_lock = acquireLock();
return getEntryNotSafe(seq);
}

std::pair<VersionedEntries, PageSize> VersionedPageEntries::getEntriesByBlobId(BlobFileId blob_id)
{
PageSize single_page_size = 0;
Expand Down Expand Up @@ -243,7 +248,12 @@ void PageDirectory::apply(PageEntriesEdit && edit)
break;
}

if (auto entry = iter->second->getEntry(last_sequence); entry)
// If we already hold the lock from `r.ori_page_id`, Then we should not request it again.
// This can happen when r.ori_page_id have other operating in current writebatch
if (auto entry = (updating_locks.find(r.ori_page_id) != updating_locks.end()
? iter->second->getEntryNotSafe(last_sequence)
: iter->second->getEntry(last_sequence));
entry)
{
// copy the entry to be ref
updating_pages[idx]->createNewVersion(last_sequence + 1, *entry);
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Storages/Page/V3/PageDirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class VersionedPageEntries

std::optional<PageEntryV3> getEntry(UInt64 seq) const;

std::optional<PageEntryV3> getEntryNotSafe(UInt64 seq) const;

/**
* Take out the `VersionedEntries` which exist in the `BlobFileId`.
* Also return the total size of entries.
Expand Down
26 changes: 26 additions & 0 deletions dbms/src/Storages/Page/V3/tests/gtest_page_directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,32 @@ try
}
CATCH

TEST_F(PageDirectoryTest, TestRefWontDeadLock)
{
PageEntriesEdit edit;
{
// 1. batch.putExternal(0, 0);
PageEntryV3 entry1;
edit.put(0, entry1);

// 2. batch.putRefPage(1, 0);
edit.ref(1, 0);
}

dir.apply(std::move(edit));

PageEntriesEdit edit2;
{
// 1. batch.putRefPage(2, 1); // ref 2 -> 1 -> 0
edit2.ref(2, 1);

// 2. batch.delPage(1); // free ref 1 -> 0
edit2.del(1);
}

dir.apply(std::move(edit2));
}

#define INSERT_BLOBID_ENTRY(BLOBID, VERSION) \
PageEntryV3 entry_v##VERSION{.file_id = (BLOBID), .size = (VERSION), .offset = 0x123, .checksum = 0x4567}; \
entries.createNewVersion((VERSION), entry_v##VERSION);
Expand Down