Skip to content

Commit

Permalink
Optimize blobstore gc when current stat total size is 0 (#5015)
Browse files Browse the repository at this point in the history
close #5016
  • Loading branch information
jiaqizho authored May 27, 2022
1 parent f10b6d2 commit d84f273
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions dbms/src/Storages/Page/V3/BlobStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,22 @@ std::vector<BlobFileId> BlobStore::getGCStats()
// Avoid divide by zero
if (right_margin == 0)
{
LOG_FMT_TRACE(log, "Current blob is empty [blob_id={}, total size(all invalid)={}].", stat->id, stat->sm_total_size);
if (unlikely(stat->sm_valid_rate != 0))
{
throw Exception(fmt::format("Current blob is empty, but valid rate is not 0. [blob_id={}][valid_size={}][valid_rate={}]",
stat->id,
stat->sm_valid_size,
stat->sm_valid_rate));
}

LOG_FMT_TRACE(log, "Current blob is empty [blob_id={}, total size(all invalid)={}] [valid_rate={}].", stat->id, stat->sm_total_size, stat->sm_valid_rate);

// If current blob empty, the size of in disk blob may not empty
// So we need truncate current blob, and let it be reused.
auto blobfile = getBlobFile(stat->id);
LOG_FMT_TRACE(log, "Truncate empty blob file [blob_id={}] to 0.", stat->id);
blobfile->truncate(right_margin);
blobstore_gc_info.appendToTruncatedBlob(stat->id, stat->sm_valid_rate);
continue;
}

Expand Down Expand Up @@ -1468,7 +1483,7 @@ void BlobStore::BlobStats::BlobStat::recalculateSpaceMap()
const auto & [total_size, valid_size] = smap->getSizes();
sm_total_size = total_size;
sm_valid_size = valid_size;
sm_valid_rate = valid_size * 1.0 / total_size;
sm_valid_rate = total_size == 0 ? 0.0 : valid_size * 1.0 / total_size;
recalculateCapacity();
}

Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Storages/Page/V3/BlobStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class BlobStore : private Allocator<false>
UInt64 sm_max_caps = 0;
UInt64 sm_total_size = 0;
UInt64 sm_valid_size = 0;
double sm_valid_rate = 1.0;
double sm_valid_rate = 0.0;

public:
BlobStat(BlobFileId id_, SpaceMap::SpaceMapType sm_type, UInt64 sm_max_caps_, BlobStatType type_ = BlobStatType::NORMAL)
Expand Down

0 comments on commit d84f273

Please sign in to comment.