Skip to content

Commit ad10367

Browse files
authored
Merge 2ac5a5a into d6a279e
2 parents d6a279e + 2ac5a5a commit ad10367

10 files changed

+188
-178
lines changed

ydb/core/tablet_flat/flat_executor.cpp

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ void TExecutor::Broken() {
208208
void TExecutor::RecreatePageCollectionsCache() noexcept
209209
{
210210
PrivatePageCache = MakeHolder<TPrivatePageCache>();
211+
StickyPagesMemory = 0;
211212

212213
Stats->PacksMetaBytes = 0;
213214

@@ -611,6 +612,10 @@ void TExecutor::AddSingleCache(const TIntrusivePtr<TPrivatePageCache::TInfo> &in
611612
{
612613
PrivatePageCache->RegisterPageCollection(info);
613614
Send(MakeSharedPageCacheId(), new NSharedCache::TEvAttach(info->PageCollection, SelfId()));
615+
616+
StickyPagesMemory += info->GetStickySize();
617+
618+
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_STICKY] = StickyPagesMemory;
614619
}
615620

616621
void TExecutor::DropCachesOfBundle(const NTable::TPart &part) noexcept
@@ -633,13 +638,20 @@ void TExecutor::DropCachesOfBundle(const NTable::TPart &part) noexcept
633638

634639
void TExecutor::DropSingleCache(const TLogoBlobID &label) noexcept
635640
{
636-
auto toActivate = PrivatePageCache->ForgetPageCollection(label);
641+
auto pageCollection = PrivatePageCache->GetPageCollection(label);
642+
643+
ui64 stickySize = pageCollection->GetStickySize();
644+
Y_ABORT_UNLESS(StickyPagesMemory >= stickySize);
645+
StickyPagesMemory -= stickySize;
646+
647+
auto toActivate = PrivatePageCache->ForgetPageCollection(pageCollection);
637648
ActivateWaitingTransactions(toActivate);
638649
if (!PrivatePageCache->Info(label))
639650
Send(MakeSharedPageCacheId(), new NSharedCache::TEvInvalidate(label));
640651

641652
Counters->Simple()[TExecutorCounters::CACHE_PINNED_SET] = PrivatePageCache->GetStats().PinnedSetSize;
642653
Counters->Simple()[TExecutorCounters::CACHE_PINNED_LOAD] = PrivatePageCache->GetStats().PinnedLoadSize;
654+
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_STICKY] = StickyPagesMemory;
643655
}
644656

645657
void TExecutor::TranslateCacheTouchesToSharedCache() {
@@ -667,6 +679,28 @@ void TExecutor::RequestInMemPagesForDatabase(bool pendingOnly) {
667679
}
668680
}
669681

682+
void TExecutor::StickInMemPages(NSharedCache::TEvResult *msg) {
683+
const auto& scheme = Scheme();
684+
for (auto& pr : scheme.Tables) {
685+
const ui32 tid = pr.first;
686+
auto subset = Database->Subset(tid, NTable::TEpoch::Max(), { } , { });
687+
for (auto &partView : subset->Flatten) {
688+
auto partStore = partView.As<NTable::TPartStore>();
689+
for (auto &pageCollection : partStore->PageCollections) {
690+
// Note: page collection search optimization seems useless
691+
if (pageCollection->PageCollection == msg->Origin) {
692+
ui64 stickySizeBefore = pageCollection->GetStickySize();
693+
for (auto& loaded : msg->Loaded) {
694+
pageCollection->AddSticky(loaded.PageId, loaded.Page);
695+
}
696+
StickyPagesMemory += pageCollection->GetStickySize() - stickySizeBefore;
697+
}
698+
}
699+
}
700+
}
701+
// Note: the next call of ProvideBlock will also fill pages bodies
702+
}
703+
670704
TExecutorCaches TExecutor::CleanupState() {
671705
TExecutorCaches caches;
672706

@@ -1347,14 +1381,8 @@ void TExecutor::RequestInMemPagesForPartStore(ui32 tableId, const NTable::TPartV
13471381

13481382
if (stickyGroup) {
13491383
auto req = partView.As<NTable::TPartStore>()->GetPages(groupIndex);
1350-
1351-
TPrivatePageCache::TInfo *info = PrivatePageCache->Info(req->PageCollection->Label());
1352-
Y_ABORT_UNLESS(info);
1353-
for (ui32 pageId : req->Pages)
1354-
PrivatePageCache->MarkSticky(pageId, info);
1355-
13561384
// TODO: only request missing pages
1357-
RequestFromSharedCache(req, NBlockIO::EPriority::Bkgr, EPageCollectionRequest::CacheSync);
1385+
RequestFromSharedCache(req, NBlockIO::EPriority::Bkgr, EPageCollectionRequest::InMemPages);
13581386
}
13591387
}
13601388
}
@@ -1792,13 +1820,17 @@ void TExecutor::ExecuteTransaction(TAutoPtr<TSeat> seat, const TActorContext &ct
17921820
}
17931821

17941822
void TExecutor::UnpinTransactionPages(TSeat &seat) {
1823+
Y_ABORT_UNLESS(TransactionPagesMemory >= seat.MemoryTouched);
1824+
TransactionPagesMemory -= seat.MemoryTouched;
1825+
17951826
size_t unpinnedPages = 0;
17961827
PrivatePageCache->UnpinPages(seat.Pinned, unpinnedPages);
17971828
seat.Pinned.clear();
17981829
seat.MemoryTouched = 0;
17991830

18001831
Counters->Simple()[TExecutorCounters::CACHE_PINNED_SET] = PrivatePageCache->GetStats().PinnedSetSize;
18011832
Counters->Simple()[TExecutorCounters::CACHE_PINNED_LOAD] = PrivatePageCache->GetStats().PinnedLoadSize;
1833+
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_USED] = TransactionPagesMemory;
18021834
}
18031835

18041836
void TExecutor::ReleaseTxData(TSeat &seat, ui64 requested, const TActorContext &ctx)
@@ -1830,12 +1862,14 @@ void TExecutor::PostponeTransaction(TAutoPtr<TSeat> seat, TPageCollectionTxEnv &
18301862
ui64 prevTouched = seat->MemoryTouched;
18311863

18321864
PrivatePageCache->PinTouches(seat->Pinned, touchedPages, newPinnedPages, seat->MemoryTouched);
1865+
TransactionPagesMemory += seat->MemoryTouched - prevTouched;
18331866

18341867
ui32 newTouchedPages = newPinnedPages;
18351868
ui64 newTouchedBytes = seat->MemoryTouched - prevTouched;
18361869
prevTouched = seat->MemoryTouched;
18371870

18381871
PrivatePageCache->PinToLoad(seat->Pinned, newPinnedPages, seat->MemoryTouched);
1872+
TransactionPagesMemory += seat->MemoryTouched - prevTouched;
18391873

18401874
if (seat->AttachedMemory)
18411875
Memory->AttachMemory(*seat);
@@ -1978,6 +2012,7 @@ void TExecutor::PostponeTransaction(TAutoPtr<TSeat> seat, TPageCollectionTxEnv &
19782012

19792013
Counters->Simple()[TExecutorCounters::CACHE_PINNED_SET] = PrivatePageCache->GetStats().PinnedSetSize;
19802014
Counters->Simple()[TExecutorCounters::CACHE_PINNED_LOAD] = PrivatePageCache->GetStats().PinnedLoadSize;
2015+
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_USED] = TransactionPagesMemory;
19812016
}
19822017

19832018
void TExecutor::CommitTransactionLog(TAutoPtr<TSeat> seat, TPageCollectionTxEnv &env,
@@ -2665,20 +2700,19 @@ void TExecutor::Handle(TEvents::TEvFlushLog::TPtr &ev) {
26652700
}
26662701

26672702
void TExecutor::Handle(NSharedCache::TEvResult::TPtr &ev) {
2668-
const bool failed = (ev->Get()->Status != NKikimrProto::OK);
2703+
NSharedCache::TEvResult *msg = ev->Get();
2704+
const bool failed = (msg->Status != NKikimrProto::OK);
26692705

26702706
if (auto logl = Logger->Log(failed ? ELnLev::Info : ELnLev::Debug)) {
26712707
logl
26722708
<< NFmt::Do(*this) << " got result " << NFmt::Do(*ev->Get())
26732709
<< ", category " << ev->Cookie;
26742710
}
26752711

2676-
switch (EPageCollectionRequest(ev->Cookie)) {
2712+
switch (auto requestType = EPageCollectionRequest(ev->Cookie)) {
26772713
case EPageCollectionRequest::Cache:
2678-
case EPageCollectionRequest::CacheSync:
2714+
case EPageCollectionRequest::InMemPages:
26792715
{
2680-
auto *msg = ev->CastAsLocal<NSharedCache::TEvResult>();
2681-
26822716
TPrivatePageCache::TInfo *collectionInfo = PrivatePageCache->Info(msg->Origin->Label());
26832717
if (!collectionInfo) // collection could be outdated
26842718
return;
@@ -2695,6 +2729,9 @@ void TExecutor::Handle(NSharedCache::TEvResult::TPtr &ev) {
26952729
return Broken();
26962730
}
26972731

2732+
if (requestType == EPageCollectionRequest::InMemPages) {
2733+
StickInMemPages(msg);
2734+
}
26982735
for (auto& loaded : msg->Loaded) {
26992736
TPrivatePageCache::TPage::TWaitQueuePtr transactionsToActivate = PrivatePageCache->ProvideBlock(std::move(loaded), collectionInfo);
27002737
ActivateWaitingTransactions(transactionsToActivate);
@@ -2704,8 +2741,6 @@ void TExecutor::Handle(NSharedCache::TEvResult::TPtr &ev) {
27042741

27052742
case EPageCollectionRequest::PendingInit:
27062743
{
2707-
auto *msg = ev->CastAsLocal<NSharedCache::TEvResult>();
2708-
27092744
const auto *pageCollection = msg->Origin.Get();
27102745
TPendingPartSwitch *foundSwitch = nullptr;
27112746
TPendingPartSwitch::TNewBundle *foundBundle = nullptr;
@@ -2756,6 +2791,7 @@ void TExecutor::Handle(NSharedCache::TEvResult::TPtr &ev) {
27562791
return;
27572792

27582793
default:
2794+
Y_DEBUG_ABORT_S("Unexpected request " << ev->Cookie);
27592795
break;
27602796
}
27612797
}
@@ -3460,8 +3496,8 @@ void TExecutor::Handle(NOps::TEvResult *ops, TProdCompact *msg, bool cancelled)
34603496

34613497
CompactionLogic->UpdateLogUsage(LogicRedo->GrabLogUsage());
34623498

3463-
const ui64 partSwitchCpuuS = ui64(1000000. * partSwitchCpuTimer.Passed());
3464-
Counters->Percentile()[TExecutorCounters::TX_PERCENTILE_PARTSWITCH_CPUTIME].IncrementFor(partSwitchCpuuS);
3499+
const ui64 partSwitchCpuUs = ui64(1000000. * partSwitchCpuTimer.Passed());
3500+
Counters->Percentile()[TExecutorCounters::TX_PERCENTILE_PARTSWITCH_CPUTIME].IncrementFor(partSwitchCpuUs);
34653501

34663502
if (msg->YellowMoveChannels || msg->YellowStopChannels) {
34673503
CheckYellow(std::move(msg->YellowMoveChannels), std::move(msg->YellowStopChannels));
@@ -3490,13 +3526,11 @@ void TExecutor::Handle(NOps::TEvResult *ops, TProdCompact *msg, bool cancelled)
34903526

34913527
void TExecutor::UpdateUsedTabletMemory() {
34923528
// Estimate memory usage for internal executor structures:
3493-
UsedTabletMemory = 50 << 10; // 50kb
3529+
UsedTabletMemory = 50_KB;
34943530

3495-
// Count the number of bytes kept in private cache (can't be offloaded right now):
3496-
if (PrivatePageCache) {
3497-
UsedTabletMemory += PrivatePageCache->GetStats().TotalPinnedBody;
3498-
UsedTabletMemory += PrivatePageCache->GetStats().PinnedLoadSize;
3499-
}
3531+
// Count the number of bytes that can't be offloaded right now:
3532+
UsedTabletMemory += StickyPagesMemory;
3533+
UsedTabletMemory += TransactionPagesMemory;
35003534

35013535
// Estimate memory used by internal database structures:
35023536
auto &counters = Database->Counters();
@@ -3589,8 +3623,9 @@ void TExecutor::UpdateCounters(const TActorContext &ctx) {
35893623
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_SHARED_BODY].Set(stats.TotalSharedBody);
35903624
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_PINNED_BODY].Set(stats.TotalPinnedBody);
35913625
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_EXCLUSIVE].Set(stats.TotalExclusive);
3592-
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_STICKY].Set(stats.TotalSticky);
35933626
}
3627+
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_STICKY].Set(StickyPagesMemory);
3628+
Counters->Simple()[TExecutorCounters::CACHE_TOTAL_USED].Set(TransactionPagesMemory);
35943629

35953630
const auto &memory = Memory->Stats();
35963631

@@ -4108,7 +4143,8 @@ void TExecutor::RenderHtmlPage(NMon::TEvRemoteHttpInfo::TPtr &ev) const {
41084143
DIV_CLASS("row") {str << "Total bytes in shared cache: " << PrivatePageCache->GetStats().TotalSharedBody; }
41094144
DIV_CLASS("row") {str << "Total bytes in local cache: " << PrivatePageCache->GetStats().TotalPinnedBody; }
41104145
DIV_CLASS("row") {str << "Total bytes exclusive to local cache: " << PrivatePageCache->GetStats().TotalExclusive; }
4111-
DIV_CLASS("row") {str << "Total bytes marked as sticky: " << PrivatePageCache->GetStats().TotalSticky; }
4146+
DIV_CLASS("row") {str << "Total bytes marked as sticky: " << StickyPagesMemory; }
4147+
DIV_CLASS("row") {str << "Total bytes currently in use: " << TransactionPagesMemory; }
41124148

41134149
if (GcLogic) {
41144150
TAG(TH3) {str << "Gc logic:";}

ydb/core/tablet_flat/flat_executor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ struct TPendingPartSwitch {
281281
enum class EPageCollectionRequest : ui64 {
282282
Undefined = 0,
283283
Cache = 1,
284-
CacheSync,
284+
InMemPages,
285285
PendingInit,
286286
BootLogic,
287287
};
@@ -465,6 +465,8 @@ class TExecutor
465465
size_t ReadyPartSwitches = 0;
466466

467467
ui64 UsedTabletMemory = 0;
468+
ui64 StickyPagesMemory = 0;
469+
ui64 TransactionPagesMemory = 0;
468470

469471
TActorContext OwnerCtx() const;
470472

@@ -515,6 +517,7 @@ class TExecutor
515517
void TranslateCacheTouchesToSharedCache();
516518
void RequestInMemPagesForDatabase(bool pendingOnly = false);
517519
void RequestInMemPagesForPartStore(ui32 tableId, const NTable::TPartView &partView, const THashSet<NTable::TTag> &stickyColumns);
520+
void StickInMemPages(NSharedCache::TEvResult *msg);
518521
THashSet<NTable::TTag> GetStickyColumns(ui32 tableId);
519522
void RequestFromSharedCache(TAutoPtr<NPageCollection::TFetch> fetch,
520523
NBlockIO::EPriority way, EPageCollectionRequest requestCategory);

ydb/core/tablet_flat/flat_executor_counters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace NTabletFlatExecutor {
6464
XX(COMPACTION_READ_IN_FLY, "CompactionReadInFly") \
6565
XX(DB_FLAT_INDEX_BYTES, "DbFlatIndexBytes") \
6666
XX(DB_B_TREE_INDEX_BYTES, "DbBTreeIndexBytes") \
67+
XX(CACHE_TOTAL_USED, "CacheTotalUsed") \
6768

6869
// don't change order!
6970
#define FLAT_EXECUTOR_CUMULATIVE_COUNTERS_MAP(XX) \

0 commit comments

Comments
 (0)