Skip to content

Commit

Permalink
Fix bug for restoring entries to BlobStats
Browse files Browse the repository at this point in the history
  • Loading branch information
JaySon-Huang committed Mar 14, 2022
1 parent c9904eb commit aef7c37
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
29 changes: 20 additions & 9 deletions dbms/src/Storages/Page/V3/PageDirectoryFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@ PageDirectoryPtr PageDirectoryFactory::create(FileProviderPtr & file_provider, P
auto [wal, reader] = WALStore::create(file_provider, delegator);
PageDirectoryPtr dir = std::make_unique<PageDirectory>(std::move(wal));
loadFromDisk(dir, std::move(reader));
// TODO: After restored ends, set the last offset of log file for `wal`
if (blob_stats)
blob_stats->restore();
// Reset the `sequence` to the maximum of persisted.
dir->sequence = max_applied_ver.sequence;

if (blob_stats)
{
// After all entries restored to `mvcc_table_directory`, only apply
// the latest entry to `blob_stats`, or we may meet error since
// some entries may be removed in memory but not get compacted
// in the log file.
for (const auto & [page_id, entries] : dir->mvcc_table_directory)
{
(void)page_id;
if (auto entry = entries->getEntry(max_applied_ver.sequence); entry)
{
blob_stats->restoreByEntry(*entry);
}
}

blob_stats->restore();
}

// TODO: After restored ends, set the last offset of log file for `wal`
return dir;
}

Expand Down Expand Up @@ -67,8 +84,6 @@ void PageDirectoryFactory::loadEdit(const PageDirectoryPtr & dir, const PageEntr
}
case EditRecordType::VAR_ENTRY:
version_list->fromRestored(r);
if (blob_stats)
blob_stats->restoreByEntry(r.entry);
break;
case EditRecordType::PUT_EXTERNAL:
{
Expand All @@ -82,8 +97,6 @@ void PageDirectoryFactory::loadEdit(const PageDirectoryPtr & dir, const PageEntr
}
case EditRecordType::PUT:
version_list->createNewEntry(restored_version, r.entry);
if (blob_stats)
blob_stats->restoreByEntry(r.entry);
break;
case EditRecordType::DEL:
case EditRecordType::VAR_DELETE: // nothing different from `DEL`
Expand All @@ -94,8 +107,6 @@ void PageDirectoryFactory::loadEdit(const PageDirectoryPtr & dir, const PageEntr
break;
case EditRecordType::UPSERT:
version_list->createNewEntry(restored_version, r.entry);
if (blob_stats)
blob_stats->restoreByEntry(r.entry);
break;
}
}
Expand Down
45 changes: 44 additions & 1 deletion dbms/src/Storages/Page/V3/PageEntriesEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ enum class EditRecordType
VAR_DELETE,
};

inline const char * typeToString(EditRecordType t)
{
switch (t)
{
case EditRecordType::PUT:
return "PUT ";
case EditRecordType::PUT_EXTERNAL:
return "EXT ";
case EditRecordType::REF:
return "REF ";
case EditRecordType::DEL:
return "DEL ";
case EditRecordType::UPSERT:
return "UPSERT ";
case EditRecordType::VAR_ENTRY:
return "VAR_ENT";
case EditRecordType::VAR_REF:
return "VAR_REF";
case EditRecordType::VAR_EXTERNAL:
return "VAR_EXT";
case EditRecordType::VAR_DELETE:
return "VAR_DEL";
}
}

/// Page entries change to apply to PageDirectory
class PageEntriesEdit
{
Expand Down Expand Up @@ -176,10 +201,28 @@ class PageEntriesEdit
PageIdV3Internal ori_page_id;
PageVersionType version;
PageEntryV3 entry;
Int64 being_ref_count = 1;
Int64 being_ref_count;

EditRecord()
: page_id(0)
, ori_page_id(0)
, being_ref_count(1)
{}
};
using EditRecords = std::vector<EditRecord>;

static String toDebugString(const EditRecord & rec)
{
return fmt::format(
"{{type:{}, page_id:{}, ori_id:{}, version:{}, entry:{}, being_ref_count:{}}}",
typeToString(rec.type),
rec.page_id,
rec.ori_page_id,
rec.version,
DB::PS::V3::toDebugString(rec.entry),
rec.being_ref_count);
}

void appendRecord(const EditRecord & rec)
{
records.emplace_back(rec);
Expand Down

0 comments on commit aef7c37

Please sign in to comment.