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

issue-2137: calculating GarbageBlocksCount stats in CompactionMap #2311

Merged
merged 3 commits into from
Oct 18, 2024
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
1 change: 1 addition & 0 deletions cloud/filestore/libs/diagnostics/events/profile_events.ev
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ message TProfileLogCompactionRangeInfo {
optional uint64 RangeId = 2;
optional uint64 BlobsCount = 3;
optional uint64 DeletionsCount = 4;
optional uint64 GarbageBlocksCount = 5;
};

message TProfileLogRequestInfo {
Expand Down
24 changes: 24 additions & 0 deletions cloud/filestore/libs/storage/tablet/model/mixed_blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,28 @@ TMixedBlobMeta TMixedBlocks::FindBlob(ui32 rangeId, TPartialBlobId blobId) const
return {it->BlobId, std::move(blocks)};
}

ui32 TMixedBlocks::CalculateGarbageBlockCount(ui32 rangeId) const
{
const auto* range = Impl->Ranges.FindPtr(rangeId);
Y_DEBUG_ABORT_UNLESS(range);
if (!range) {
return 0;
}

ui32 result = 0;
for (const auto& blob: range->Blobs) {
auto blocks = blob.BlockList.DecodeBlocks();

range->DeletionMarkers.Apply(MakeArrayRef(blocks));
for (const auto& block: blocks) {
// TODO(#1923): take checkpoints into account
if (block.MaxCommitId != InvalidCommitId) {
++result;
}
}
}

return result;
}

} // namespace NCloud::NFileStore::NStorage
2 changes: 2 additions & 0 deletions cloud/filestore/libs/storage/tablet/model/mixed_blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class TMixedBlocks
TVector<TMixedBlobMeta> GetBlobsForCompaction(ui32 rangeId) const;

TMixedBlobMeta FindBlob(ui32 rangeId, TPartialBlobId blobId) const;

ui32 CalculateGarbageBlockCount(ui32 rangeId) const;
};

} // namespace NCloud::NFileStore::NStorage
2 changes: 2 additions & 0 deletions cloud/filestore/libs/storage/tablet/profile_log_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ void AddCompactionRange(
ui32 rangeId,
ui32 blobsCount,
ui32 deletionsCount,
ui32 garbageBlocksCount,
NProto::TProfileLogRequestInfo& profileLogRequest)
{
auto* range = profileLogRequest.AddCompactionRanges();
range->SetCommitId(commitId);
range->SetRangeId(rangeId);
range->SetBlobsCount(blobsCount);
range->SetDeletionsCount(deletionsCount);
range->SetGarbageBlocksCount(garbageBlocksCount);
}

template <typename T>
Expand Down
1 change: 1 addition & 0 deletions cloud/filestore/libs/storage/tablet/profile_log_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void AddCompactionRange(
ui32 rangeId,
ui32 blobsCount,
ui32 deletionsCount,
ui32 garbageBlocksCount,
NProto::TProfileLogRequestInfo& profileLogRequest);

template <typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ Y_UNIT_TEST_SUITE(TProfileLogEvent)
Y_UNIT_TEST(ShouldAddCompactionRange)
{
NProto::TProfileLogRequestInfo profileLogRequest;
AddCompactionRange(100500, 500100, 100, 500, profileLogRequest);
AddCompactionRange(100500, 500100, 100, 500, 1000, profileLogRequest);

UNIT_ASSERT(!profileLogRequest.HasTimestampMcs());
UNIT_ASSERT(!profileLogRequest.HasDurationMcs());
Expand All @@ -429,6 +429,9 @@ Y_UNIT_TEST_SUITE(TProfileLogEvent)
UNIT_ASSERT_VALUES_EQUAL(
500,
profileLogRequest.GetCompactionRanges().at(0).GetDeletionsCount());
UNIT_ASSERT_VALUES_EQUAL(
1000,
profileLogRequest.GetCompactionRanges().at(0).GetGarbageBlocksCount());

UNIT_ASSERT(!profileLogRequest.HasNodeInfo());
UNIT_ASSERT(!profileLogRequest.HasLockInfo());
Expand Down
25 changes: 24 additions & 1 deletion cloud/filestore/libs/storage/tablet/tablet_actor_addblob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class TAddBlobsExecutor
block.BlockIndex,
blob.BlocksCount);
AccessCompactionStats(rangeId).BlobsCount += 1;
// conservative estimate
AccessCompactionStats(rangeId).GarbageBlocksCount +=
blob.BlocksCount;
}

for (const auto& part: args.UnalignedDataParts) {
Expand Down Expand Up @@ -221,6 +224,9 @@ class TAddBlobsExecutor
if (written) {
ui32 rangeId = Tablet.GetMixedRangeIndex(blob.Blocks);
AccessCompactionStats(rangeId).BlobsCount += 1;
// conservative estimate
AccessCompactionStats(rangeId).GarbageBlocksCount +=
blob.Blocks.size();
}
}

Expand Down Expand Up @@ -257,6 +263,8 @@ class TAddBlobsExecutor
auto& stats = AccessCompactionStats(rangeId);
if (Tablet.WriteMixedBlocks(db, blob.BlobId, blob.Blocks)) {
stats.BlobsCount += 1;
// conservative estimate
stats.GarbageBlocksCount += blob.Blocks.size();
}
}
}
Expand All @@ -273,6 +281,7 @@ class TAddBlobsExecutor
auto& stats = AccessCompactionStats(rangeId);
if (!Tablet.UpdateBlockLists(db, blob)) {
stats.BlobsCount = Max(stats.BlobsCount, 1U) - 1;
// no proper way to reliably decrement stats.GarbageBlocksCount
}
}

Expand All @@ -293,6 +302,8 @@ class TAddBlobsExecutor
auto& stats = AccessCompactionStats(rangeId);
if (Tablet.WriteMixedBlocks(db, blob.BlobId, blob.Blocks)) {
stats.BlobsCount += 1;
// conservative estimate
stats.GarbageBlocksCount += blob.Blocks.size();
}
}
}
Expand All @@ -304,6 +315,8 @@ class TAddBlobsExecutor
TABLET_VERIFY(!args.MergedBlobs);
TABLET_VERIFY(!args.UnalignedDataParts);

THashSet<ui32> rangeIds;

for (const auto& blob: args.SrcBlobs) {
const auto rangeId = Tablet.GetMixedRangeIndex(blob.Blocks);
auto& stats = AccessCompactionStats(rangeId);
Expand All @@ -313,6 +326,8 @@ class TAddBlobsExecutor
// needed.
stats.BlobsCount = Max(1U, stats.BlobsCount) - 1;
Tablet.DeleteMixedBlocks(db, blob.BlobId, blob.Blocks);

rangeIds.insert(rangeId);
}

THashMap<ui32, ui32> rangeId2AddedBlobsCount;
Expand All @@ -323,6 +338,8 @@ class TAddBlobsExecutor
if (Tablet.WriteMixedBlocks(db, blob.BlobId, blob.Blocks)) {
++rangeId2AddedBlobsCount[rangeId];
}

rangeIds.insert(rangeId);
}

for (const auto& [rangeId, addedBlobsCount]: rangeId2AddedBlobsCount) {
Expand All @@ -337,8 +354,13 @@ class TAddBlobsExecutor
}
stats.BlobsCount += increment;
}
}

// recalculating GarbageBlocksCount for each of the affected ranges
for (const auto rangeId: rangeIds) {
AccessCompactionStats(rangeId).GarbageBlocksCount =
Tablet.CalculateMixedIndexRangeGarbageBlockCount(rangeId);
}
}

void UpdateCompactionMap(
TIndexTabletDatabase& db,
Expand All @@ -361,6 +383,7 @@ class TAddBlobsExecutor
x.first,
x.second.BlobsCount,
x.second.DeletionsCount,
x.second.GarbageBlocksCount,
args.ProfileLogRequest);
}
}
Expand Down
34 changes: 20 additions & 14 deletions cloud/filestore/libs/storage/tablet/tablet_actor_counters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,26 +631,32 @@ void TIndexTabletActor::HandleGetStorageStats(

auto& req = ev->Get()->Record;

TVector<TCompactionRangeInfo> topRanges;

if (req.GetCompactionRangeCountByCompactionScore()) {
const auto topRanges = GetTopRangesByCompactionScore(
const auto r = GetTopRangesByCompactionScore(
req.GetCompactionRangeCountByCompactionScore());
for (const auto& r: topRanges) {
auto* out = stats->AddCompactionRangeStats();
out->SetRangeId(r.RangeId);
out->SetBlobCount(r.Stats.BlobsCount);
out->SetDeletionCount(r.Stats.DeletionsCount);
}
topRanges.insert(topRanges.end(), r.begin(), r.end());
}

if (req.GetCompactionRangeCountByCleanupScore()) {
const auto topRanges = GetTopRangesByCleanupScore(
const auto r = GetTopRangesByCleanupScore(
req.GetCompactionRangeCountByCleanupScore());
for (const auto& r: topRanges) {
auto* out = stats->AddCompactionRangeStats();
out->SetRangeId(r.RangeId);
out->SetBlobCount(r.Stats.BlobsCount);
out->SetDeletionCount(r.Stats.DeletionsCount);
}
topRanges.insert(topRanges.end(), r.begin(), r.end());
}

if (req.GetCompactionRangeCountByGarbageScore()) {
const auto r = GetTopRangesByGarbageScore(
req.GetCompactionRangeCountByGarbageScore());
topRanges.insert(topRanges.end(), r.begin(), r.end());
}

for (const auto& r: topRanges) {
auto* out = stats->AddCompactionRangeStats();
out->SetRangeId(r.RangeId);
out->SetBlobCount(r.Stats.BlobsCount);
out->SetDeletionCount(r.Stats.DeletionsCount);
out->SetGarbageBlockCount(r.Stats.GarbageBlocksCount);
}

stats->SetFlushState(static_cast<ui32>(FlushState.GetOperationState()));
Expand Down
2 changes: 2 additions & 0 deletions cloud/filestore/libs/storage/tablet/tablet_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ FILESTORE_DUPCACHE_REQUESTS(FILESTORE_DECLARE_DUPCACHE)
ui32 GetMixedRangeIndex(const TVector<TBlock>& blocks) const;
const IBlockLocation2RangeIndex& GetRangeIdHasher() const;

ui32 CalculateMixedIndexRangeGarbageBlockCount(ui32 rangeId) const;

private:
bool WriteMixedBlocks(
TIndexTabletDatabase& db,
Expand Down
12 changes: 10 additions & 2 deletions cloud/filestore/libs/storage/tablet/tablet_state_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,9 @@ ui32 TIndexTabletState::CleanupBlockDeletions(
stats.BlobsCount = (stats.BlobsCount > removedBlobs)
? (stats.BlobsCount - removedBlobs) : 0;
stats.DeletionsCount = 0;
// TODO: calculate GarbageBlocksCount
stats.GarbageBlocksCount = 0;
if (stats.BlobsCount == 0) {
stats.GarbageBlocksCount = 0;
}

db.WriteCompactionMap(
rangeId,
Expand All @@ -973,6 +974,7 @@ ui32 TIndexTabletState::CleanupBlockDeletions(
rangeId,
stats.BlobsCount,
stats.DeletionsCount,
stats.GarbageBlocksCount,
profileLogRequest);

return deletionMarkerCount;
Expand Down Expand Up @@ -1078,6 +1080,12 @@ const IBlockLocation2RangeIndex& TIndexTabletState::GetRangeIdHasher() const
return *Impl->RangeIdHasher;
}

ui32 TIndexTabletState::CalculateMixedIndexRangeGarbageBlockCount(
ui32 rangeId) const
{
return Impl->MixedBlocks.CalculateGarbageBlockCount(rangeId);
}

////////////////////////////////////////////////////////////////////////////////
// LargeBlocks

Expand Down
16 changes: 8 additions & 8 deletions cloud/filestore/libs/storage/tablet/tablet_ut_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3566,28 +3566,28 @@ Y_UNIT_TEST_SUITE(TIndexTabletTest_Data)
stats.GetAllocatedCompactionRanges());
UNIT_ASSERT_VALUES_EQUAL(8, stats.CompactionRangeStatsSize());
UNIT_ASSERT_VALUES_EQUAL(
"r=1656356864 b=16 d=1024",
"r=1656356864 b=16 d=1024 g=1024",
CompactionRangeToString(stats.GetCompactionRangeStats(0)));
UNIT_ASSERT_VALUES_EQUAL(
"r=1656356865 b=16 d=1024",
"r=1656356865 b=16 d=1024 g=1024",
CompactionRangeToString(stats.GetCompactionRangeStats(1)));
UNIT_ASSERT_VALUES_EQUAL(
"r=4283236352 b=16 d=1024",
"r=4283236352 b=16 d=1024 g=1024",
CompactionRangeToString(stats.GetCompactionRangeStats(2)));
UNIT_ASSERT_VALUES_EQUAL(
"r=4283236353 b=16 d=1024",
"r=4283236353 b=16 d=1024 g=1024",
CompactionRangeToString(stats.GetCompactionRangeStats(3)));
UNIT_ASSERT_VALUES_EQUAL(
"r=1177944064 b=14 d=833",
"r=1177944064 b=14 d=833 g=832",
CompactionRangeToString(stats.GetCompactionRangeStats(4)));
UNIT_ASSERT_VALUES_EQUAL(
"r=1177944065 b=13 d=832",
"r=1177944065 b=13 d=832 g=832",
CompactionRangeToString(stats.GetCompactionRangeStats(5)));
UNIT_ASSERT_VALUES_EQUAL(
"r=737148928 b=3 d=192",
"r=737148928 b=3 d=192 g=192",
CompactionRangeToString(stats.GetCompactionRangeStats(6)));
UNIT_ASSERT_VALUES_EQUAL(
"r=737148929 b=3 d=192",
"r=737148929 b=3 d=192 g=192",
CompactionRangeToString(stats.GetCompactionRangeStats(7)));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ Y_UNIT_TEST_SUITE(TIndexTabletTest_Data_Stress)
UNIT_ASSERT_VALUES_EQUAL(1, stats.CompactionRangeStatsSize());
UNIT_ASSERT_VALUES_EQUAL(
Sprintf(
"r=1177944064 b=%u d=%u",
"r=1177944064 b=%u d=%u g=0",
(compactionThreshold - 1),
deletionMarkers),
CompactionRangeToString(stats.GetCompactionRangeStats(0)));
Expand Down
5 changes: 3 additions & 2 deletions cloud/filestore/libs/storage/testlib/test_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ inline auto CompactionRangeToString(
const NProtoPrivate::TCompactionRangeStats& rs)
{
return Sprintf(
"r=%u b=%u d=%u",
"r=%u b=%u d=%u g=%u",
rs.GetRangeId(),
rs.GetBlobCount(),
rs.GetDeletionCount());
rs.GetDeletionCount(),
rs.GetGarbageBlockCount());
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 2 additions & 0 deletions cloud/filestore/private/api/protos/tablet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ message TGetStorageStatsRequest
uint32 CompactionRangeCountByCompactionScore = 3;
// The same but for cleanup score.
uint32 CompactionRangeCountByCleanupScore = 4;
// The same but for garbage score.
uint32 CompactionRangeCountByGarbageScore = 5;
}

message TGetStorageStatsResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,14 @@ TString PrintCompactionRanges(
currentRange << PrintValue("blobsCount", range.GetBlobsCount()) << ", ";
}
if (range.HasDeletionsCount()) {
currentRange << PrintValue("deletionsCount", range.GetDeletionsCount()) << ", ";
currentRange << PrintValue(
"deletionsCount",
range.GetDeletionsCount()) << ", ";
}
if (range.HasGarbageBlocksCount()) {
currentRange << PrintValue(
"garbageBlocksCount",
range.GetGarbageBlocksCount()) << ", ";
}

if (currentRange.empty()) {
Expand Down
Loading