Skip to content

Commit 178cf6e

Browse files
authored
24-1 Fix DB.CalculateReadSize (#4728) (#4977)
1 parent c165555 commit 178cf6e

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

ydb/core/engine/minikql/minikql_engine_host.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool TEngineHost::IsValidKey(TKeyDesc& key) const {
7373
return NMiniKQL::IsValidKey(Scheme, localTableId, key);
7474
}
7575
ui64 TEngineHost::CalculateReadSize(const TVector<const TKeyDesc*>& keys) const {
76-
NTable::TSizeEnv env;
76+
auto env = Db.CreateSizeEnv();
7777

7878
for (const TKeyDesc* ki : keys) {
7979
DoCalculateReadSize(*ki, env);
@@ -120,7 +120,7 @@ ui64 TEngineHost::CalculateResultSize(const TKeyDesc& key) const {
120120
if (key.Range.Point) {
121121
return Db.EstimateRowSize(localTid);
122122
} else {
123-
NTable::TSizeEnv env;
123+
auto env = Db.CreateSizeEnv();
124124
DoCalculateReadSize(key, env);
125125
ui64 size = env.GetSize();
126126

ydb/core/tablet_flat/flat_database.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ TSelectRowVersionResult TDatabase::SelectRowVersion(
266266
return Require(table)->SelectRowVersion(key, Env, readFlags, visible, observer);
267267
}
268268

269+
TSizeEnv TDatabase::CreateSizeEnv()
270+
{
271+
return TSizeEnv(Env);
272+
}
273+
274+
269275
void TDatabase::CalculateReadSize(TSizeEnv& env, ui32 table, TRawVals minKey, TRawVals maxKey,
270276
TTagsRef tags, ui64 flg, ui64 items, ui64 bytes,
271277
EDirection direction, TRowVersion snapshot)

ydb/core/tablet_flat/flat_database.h

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class TDatabase {
141141
EDirection direction = EDirection::Forward,
142142
TRowVersion snapshot = TRowVersion::Max());
143143

144+
TSizeEnv CreateSizeEnv();
144145
void CalculateReadSize(TSizeEnv& env, ui32 table, TRawVals minKey, TRawVals maxKey,
145146
TTagsRef tags, ui64 readFlags, ui64 itemsLimit, ui64 bytesLimit,
146147
EDirection direction = EDirection::Forward,

ydb/core/tablet_flat/flat_dbase_sz_env.h

+25-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ namespace NTable {
1111
struct TSizeEnv : public IPages {
1212
using TInfo = NTabletFlatExecutor::TPrivatePageCache::TInfo;
1313

14+
TSizeEnv(IPages* env)
15+
: Env(env)
16+
{
17+
}
18+
1419
TResult Locate(const TMemTable*, ui64, ui32) noexcept override
1520
{
1621
Y_ABORT("IPages::Locate(TMemTable*, ...) shouldn't be used here");
@@ -20,32 +25,46 @@ namespace NTable {
2025
{
2126
auto *partStore = CheckedCast<const NTable::TPartStore*>(part);
2227

23-
return { true, Touch(partStore->Locate(lob, ref), ref) };
28+
AddPageSize(partStore->Locate(lob, ref), ref);
29+
30+
return { true, nullptr };
2431
}
2532

26-
const TSharedData* TryGetPage(const TPart* part, TPageId page, TGroupId groupId) override
33+
const TSharedData* TryGetPage(const TPart* part, TPageId pageId, TGroupId groupId) override
2734
{
2835
auto *partStore = CheckedCast<const NTable::TPartStore*>(part);
2936

30-
return Touch(partStore->PageCollections.at(groupId.Index).Get(), page);
37+
auto info = partStore->PageCollections.at(groupId.Index).Get();
38+
auto type = EPage(info->PageCollection->Page(pageId).Type);
39+
40+
switch (type) {
41+
case EPage::Index:
42+
case EPage::BTreeIndex:
43+
// need index pages to continue counting
44+
// do not count index
45+
// if these pages are not in memory, data won't be counted in precharge
46+
return Env->TryGetPage(part, pageId, groupId);
47+
default:
48+
AddPageSize(partStore->PageCollections.at(groupId.Index).Get(), pageId);
49+
return nullptr;
50+
}
3151
}
3252

3353
ui64 GetSize() const {
3454
return Bytes;
3555
}
3656

3757
private:
38-
const TSharedData* Touch(TInfo *info, TPageId page) noexcept
58+
void AddPageSize(TInfo *info, TPageId page) noexcept
3959
{
4060
if (Touched[info].insert(page).second) {
4161
Pages++;
4262
Bytes += info->PageCollection->Page(page).Size;
4363
}
44-
45-
return nullptr;
4664
}
4765

4866
private:
67+
IPages* Env;
4968
THashMap<const void*, THashSet<TPageId>> Touched;
5069
ui64 Pages = 0;
5170
ui64 Bytes = 0;

ydb/core/tablet_flat/flat_executor_ut.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "flat_dbase_sz_env.h"
12
#include "flat_executor_ut_common.h"
23

34
namespace NKikimr {
@@ -4979,6 +4980,41 @@ Y_UNIT_TEST_SUITE(TFlatTableSnapshotWithCommits) {
49794980

49804981
Y_UNIT_TEST_SUITE(TFlatTableExecutorIndexLoading) {
49814982

4983+
struct TTxCalculateReadSize : public ITransaction {
4984+
ui32 Attempt = 0;
4985+
TVector<ui64>& ReadSizes;
4986+
ui64 MinKey, MaxKey;
4987+
4988+
TTxCalculateReadSize(TVector<ui64>& readSizes, ui64 minKey, ui64 maxKey)
4989+
: ReadSizes(readSizes)
4990+
, MinKey(minKey)
4991+
, MaxKey(maxKey)
4992+
{
4993+
ReadSizes.clear();
4994+
}
4995+
4996+
4997+
bool Execute(TTransactionContext &txc, const TActorContext &) override
4998+
{
4999+
UNIT_ASSERT_LE(++Attempt, 10);
5000+
5001+
const auto minKey = NScheme::TInt64::TInstance(MinKey);
5002+
const auto maxKey = NScheme::TInt64::TInstance(MaxKey);
5003+
const TVector<NTable::TTag> tags{ { TRowsModel::ColumnKeyId, TRowsModel::ColumnValueId } };
5004+
5005+
auto sizeEnv = txc.DB.CreateSizeEnv();
5006+
txc.DB.CalculateReadSize(sizeEnv, TRowsModel::TableId, { minKey }, { maxKey }, tags, 0, 0, 0);
5007+
ReadSizes.push_back(sizeEnv.GetSize());
5008+
5009+
return txc.DB.Precharge(TRowsModel::TableId, { minKey }, { maxKey }, tags, 0, 0, 0);
5010+
}
5011+
5012+
void Complete(const TActorContext &ctx) override
5013+
{
5014+
ctx.Send(ctx.SelfID, new NFake::TEvReturn);
5015+
}
5016+
};
5017+
49825018
struct TTxPrechargeAndSeek : public ITransaction {
49835019
ui32 Attempt = 0;
49845020
bool Pinned = false;
@@ -5020,6 +5056,43 @@ Y_UNIT_TEST_SUITE(TFlatTableExecutorIndexLoading) {
50205056
}
50215057
};
50225058

5059+
void ZeroSharedCache(TMyEnvBase &env) {
5060+
env.Env.GetMemObserver()->NotifyStat({1, 1, 1});
5061+
TDispatchOptions options;
5062+
options.FinalEvents.push_back(TDispatchOptions::TFinalEventCondition(NSharedCache::EvMem, 1));
5063+
env->DispatchEvents(options);
5064+
}
5065+
5066+
Y_UNIT_TEST(CalculateReadSize_FlatIndex) {
5067+
TMyEnvBase env;
5068+
TRowsModel rows;
5069+
const ui32 rowsCount = 1024;
5070+
5071+
env.FireTablet(env.Edge, env.Tablet, [&env](const TActorId &tablet, TTabletStorageInfo *info) {
5072+
return new TTestFlatTablet(env.Edge, tablet, info);
5073+
});
5074+
env.WaitForWakeUp();
5075+
ZeroSharedCache(env);
5076+
5077+
env.SendSync(rows.MakeScheme(new TCompactionPolicy(), false));
5078+
5079+
env.SendSync(rows.MakeRows(rowsCount, 10*1024));
5080+
5081+
env.SendSync(new NFake::TEvCompact(TRowsModel::TableId));
5082+
env.WaitFor<NFake::TEvCompacted>();
5083+
5084+
TVector<ui64> sizes;
5085+
5086+
env.SendSync(new NFake::TEvExecute{ new TTxCalculateReadSize(sizes, 0, 1) });
5087+
UNIT_ASSERT_VALUES_EQUAL(sizes, (TVector<ui64>{20566, 20566}));
5088+
5089+
env.SendSync(new NFake::TEvExecute{ new TTxCalculateReadSize(sizes, 100, 200) });
5090+
UNIT_ASSERT_VALUES_EQUAL(sizes, (TVector<ui64>{1048866, 1048866}));
5091+
5092+
env.SendSync(new NFake::TEvExecute{ new TTxCalculateReadSize(sizes, 300, 700) });
5093+
UNIT_ASSERT_VALUES_EQUAL(sizes, (TVector<ui64>{4133766, 4133766}));
5094+
}
5095+
50235096
Y_UNIT_TEST(TestPrechargeAndSeek) {
50245097
TMyEnvBase env;
50255098
TRowsModel rows;

0 commit comments

Comments
 (0)