Skip to content

Commit 694286b

Browse files
authored
KIKIMR-19522 BTreeIndex Charge Items Limit (#1338)
1 parent ef309ff commit 694286b

File tree

7 files changed

+373
-240
lines changed

7 files changed

+373
-240
lines changed

ydb/core/tablet_flat/flat_page_btree_index.h

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ namespace NKikimr::NTable::NPage {
101101

102102
auto operator<=>(const TChild&) const = default;
103103

104-
TString ToString() const noexcept
105-
{
104+
TRowId GetNonErasedRowCount() const noexcept {
105+
return RowCount - ErasedRowCount;
106+
}
107+
108+
TString ToString() const noexcept {
106109
return TStringBuilder() << "PageId: " << PageId << " RowCount: " << RowCount << " DataSize: " << DataSize << " ErasedRowCount: " << ErasedRowCount;
107110
}
108111
} Y_PACKED;
@@ -268,6 +271,11 @@ namespace NKikimr::NTable::NPage {
268271
return ReadUnaligned<NPage::TLabel>(Raw.data());
269272
}
270273

274+
bool IsShortChildFormat() const noexcept
275+
{
276+
return Header->IsShortChildFormat;
277+
}
278+
271279
bool IsFixedFormat() const noexcept
272280
{
273281
return Header->FixedKeySize != Header->MaxFixedKeySize;
@@ -293,23 +301,26 @@ namespace NKikimr::NTable::NPage {
293301
return GetCells<TCellsIter>(pos, columns);
294302
}
295303

304+
const TShortChild* GetShortChildRef(TRecIdx pos) const noexcept
305+
{
306+
return TDeref<const TShortChild>::At(Children,
307+
pos * (Header->IsShortChildFormat ? sizeof(TShortChild) : sizeof(TChild)));
308+
}
309+
296310
const TShortChild& GetShortChild(TRecIdx pos) const noexcept
297311
{
298-
if (Header->IsShortChildFormat) {
299-
return *TDeref<const TShortChild>::At(Children, pos * sizeof(TShortChild));
300-
} else {
301-
return *TDeref<const TShortChild>::At(Children, pos * sizeof(TChild));
302-
}
312+
return *GetShortChildRef(pos);
303313
}
304314

305-
TChild GetChild(TRecIdx pos) const noexcept
315+
const TChild* GetChildRef(TRecIdx pos) const noexcept
306316
{
307-
if (Header->IsShortChildFormat) {
308-
const TShortChild* const shortChild = TDeref<const TShortChild>::At(Children, pos * sizeof(TShortChild));
309-
return { shortChild->PageId, shortChild->RowCount, shortChild->DataSize, 0 };
310-
} else {
311-
return *TDeref<const TChild>::At(Children, pos * sizeof(TChild));
312-
}
317+
Y_DEBUG_ABORT_UNLESS(!Header->IsShortChildFormat, "GetShortChildRef should be used instead");
318+
return TDeref<const TChild>::At(Children, pos * sizeof(TChild));
319+
}
320+
321+
const TChild& GetChild(TRecIdx pos) const noexcept
322+
{
323+
return *GetChildRef(pos);
313324
}
314325

315326
static bool Has(TRowId rowId, TRowId beginRowId, TRowId endRowId) noexcept {

ydb/core/tablet_flat/flat_part_btree_index_iter.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ class TPartBtreeIndexIt : public IIndexIter {
1818
using TBtreeIndexMeta = NPage::TBtreeIndexMeta;
1919

2020
struct TNodeState {
21-
TChild Meta;
21+
TPageId PageId;
2222
TRowId BeginRowId;
2323
TRowId EndRowId;
2424
TCellsIterable BeginKey;
2525
TCellsIterable EndKey;
2626
std::optional<TBtreeIndexNode> Node;
2727
std::optional<TRecIdx> Pos;
2828

29-
TNodeState(TChild meta, TRowId beginRowId, TRowId endRowId, TCellsIterable beginKey, TCellsIterable endKey)
30-
: Meta(meta)
29+
TNodeState(TPageId pageId, TRowId beginRowId, TRowId endRowId, TCellsIterable beginKey, TCellsIterable endKey)
30+
: PageId(pageId)
3131
, BeginRowId(beginRowId)
3232
, EndRowId(endRowId)
3333
, BeginKey(beginKey)
@@ -118,7 +118,7 @@ class TPartBtreeIndexIt : public IIndexIter {
118118
, State(Reserve(Meta.LevelCount + 1))
119119
{
120120
const static TCellsIterable EmptyKey(static_cast<const char*>(nullptr), TColumns());
121-
State.emplace_back(Meta, 0, GetEndRowId(), EmptyKey, EmptyKey);
121+
State.emplace_back(Meta.PageId, 0, GetEndRowId(), EmptyKey, EmptyKey);
122122
}
123123

124124
EReady Seek(TRowId rowId) override {
@@ -251,7 +251,7 @@ class TPartBtreeIndexIt : public IIndexIter {
251251

252252
TPageId GetPageId() const override {
253253
Y_ABORT_UNLESS(IsLeaf());
254-
return State.back().Meta.PageId;
254+
return State.back().PageId;
255255
}
256256

257257
TRowId GetRowId() const override {
@@ -332,23 +332,23 @@ class TPartBtreeIndexIt : public IIndexIter {
332332
Y_ABORT_UNLESS(pos < current.Node->GetChildrenCount(), "Should point to some child");
333333
current.Pos.emplace(pos);
334334

335-
auto child = current.Node->GetChild(pos);
335+
auto& child = current.Node->GetShortChild(pos);
336336

337-
TRowId beginRowId = pos ? current.Node->GetChild(pos - 1).RowCount : current.BeginRowId;
337+
TRowId beginRowId = pos ? current.Node->GetShortChild(pos - 1).RowCount : current.BeginRowId;
338338
TRowId endRowId = child.RowCount;
339339

340340
TCellsIterable beginKey = pos ? current.Node->GetKeyCellsIterable(pos - 1, GroupInfo.ColsKeyIdx) : current.BeginKey;
341341
TCellsIterable endKey = pos < current.Node->GetKeysCount() ? current.Node->GetKeyCellsIterable(pos, GroupInfo.ColsKeyIdx) : current.EndKey;
342342

343-
State.emplace_back(child, beginRowId, endRowId, beginKey, endKey);
343+
State.emplace_back(child.PageId, beginRowId, endRowId, beginKey, endKey);
344344
}
345345

346346
bool TryLoad(TNodeState& state) {
347347
if (state.Node) {
348348
return true;
349349
}
350350

351-
auto page = Env->TryGetPage(Part, state.Meta.PageId);
351+
auto page = Env->TryGetPage(Part, state.PageId);
352352
if (page) {
353353
state.Node.emplace(*page);
354354
return true;

ydb/core/tablet_flat/flat_part_charge.h

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace NTable {
208208
ui64 items = 0;
209209
ui64 bytes = 0;
210210

211-
TRowId prechargedFirstRowId, prechargedLastRowId;
211+
std::optional<std::pair<TRowId, TRowId>> prechargedRowsRange;
212212
bool needExactBounds = Groups || HistoryIndex;
213213

214214
for (auto current = first;
@@ -235,6 +235,15 @@ namespace NTable {
235235
prechargeCurrentFirstRowId = Max<TRowId>(); // no precharge
236236
}
237237
}
238+
if (itemsLimit && prechargeCurrentFirstRowId <= prechargeCurrentLastRowId) {
239+
ui64 left = itemsLimit - items; // we count only foolproof taken rows, so here we may precharge some extra rows
240+
if (prechargeCurrentLastRowId - prechargeCurrentFirstRowId > left) {
241+
prechargeCurrentLastRowId = prechargeCurrentFirstRowId + left;
242+
}
243+
}
244+
if (prechargeCurrentFirstRowId <= prechargeCurrentLastRowId) {
245+
items += prechargeCurrentLastRowId - prechargeCurrentFirstRowId + 1;
246+
}
238247
if (key2Page && key2Page <= current) {
239248
if (key2Page == current) {
240249
if (needExactBounds && page) {
@@ -251,29 +260,22 @@ namespace NTable {
251260
prechargeCurrentFirstRowId = Max<TRowId>(); // no precharge
252261
}
253262
}
254-
if (itemsLimit && prechargeCurrentFirstRowId <= prechargeCurrentLastRowId) {
255-
ui64 left = itemsLimit - items; // we count only foolproof taken rows, so here we may precharge some extra rows
256-
if (prechargeCurrentLastRowId - prechargeCurrentFirstRowId > left) {
257-
prechargeCurrentLastRowId = prechargeCurrentFirstRowId + left;
258-
}
259-
}
260-
261263
if (prechargeCurrentFirstRowId <= prechargeCurrentLastRowId) {
262-
if (!items) {
263-
prechargedFirstRowId = prechargeCurrentFirstRowId;
264+
if (prechargedRowsRange) {
265+
prechargedRowsRange->second = prechargeCurrentLastRowId;
266+
} else {
267+
prechargedRowsRange.emplace(prechargeCurrentFirstRowId, prechargeCurrentLastRowId);
264268
}
265-
prechargedLastRowId = prechargeCurrentLastRowId;
266269
if (Groups) {
267270
for (auto& g : Groups) {
268271
ready &= DoPrechargeGroup(g, prechargeCurrentFirstRowId, prechargeCurrentLastRowId, bytes);
269272
}
270273
}
271-
items += prechargeCurrentLastRowId - prechargeCurrentFirstRowId + 1;
272274
}
273275
}
274276

275-
if (items && HistoryIndex) {
276-
ready &= DoPrechargeHistory(prechargedFirstRowId, prechargedLastRowId);
277+
if (prechargedRowsRange && HistoryIndex) {
278+
ready &= DoPrechargeHistory(prechargedRowsRange->first, prechargedRowsRange->second);
277279
}
278280
}
279281

@@ -304,7 +306,7 @@ namespace NTable {
304306
ui64 items = 0;
305307
ui64 bytes = 0;
306308

307-
TRowId prechargedFirstRowId, prechargedLastRowId;
309+
std::optional<std::pair<TRowId, TRowId>> prechargedRowsRange;
308310
bool needExactBounds = Groups || HistoryIndex;
309311

310312
for (auto current = first;
@@ -335,6 +337,15 @@ namespace NTable {
335337
prechargeCurrentLastRowId = Max<TRowId>(); // no precharge
336338
}
337339
}
340+
if (itemsLimit && prechargeCurrentFirstRowId >= prechargeCurrentLastRowId) {
341+
ui64 left = itemsLimit - items; // we count only foolproof taken rows, so here we may precharge some extra rows
342+
if (prechargeCurrentFirstRowId - prechargeCurrentLastRowId > left) {
343+
prechargeCurrentLastRowId = prechargeCurrentFirstRowId - left;
344+
}
345+
}
346+
if (prechargeCurrentFirstRowId >= prechargeCurrentLastRowId) {
347+
items += prechargeCurrentFirstRowId - prechargeCurrentLastRowId + 1;
348+
}
338349
if (key2Page && key2Page >= current) {
339350
if (key2Page == current) {
340351
if (needExactBounds && page) {
@@ -349,34 +360,26 @@ namespace NTable {
349360
prechargeCurrentLastRowId = Max<TRowId>(); // no precharge
350361
}
351362
}
352-
353-
if (itemsLimit && prechargeCurrentFirstRowId >= prechargeCurrentLastRowId) {
354-
ui64 left = itemsLimit - items; // we count only foolproof taken rows, so here we may precharge some extra rows
355-
if (prechargeCurrentFirstRowId - prechargeCurrentLastRowId > left) {
356-
prechargeCurrentLastRowId = prechargeCurrentFirstRowId - left;
357-
}
358-
}
359-
360363
if (prechargeCurrentFirstRowId >= prechargeCurrentLastRowId) {
361-
if (!items) {
362-
prechargedFirstRowId = prechargeCurrentFirstRowId;
364+
if (prechargedRowsRange) {
365+
prechargedRowsRange->second = prechargeCurrentLastRowId;
366+
} else {
367+
prechargedRowsRange.emplace(prechargeCurrentFirstRowId, prechargeCurrentLastRowId);
363368
}
364-
prechargedLastRowId = prechargeCurrentLastRowId;
365369
if (Groups) {
366370
for (auto& g : Groups) {
367371
ready &= DoPrechargeGroupReverse(g, prechargeCurrentFirstRowId, prechargeCurrentLastRowId, bytes);
368372
}
369373
}
370-
items += prechargeCurrentFirstRowId - prechargeCurrentLastRowId + 1;
371374
}
372375

373376
if (current.Off() == 0) {
374377
break;
375378
}
376379
}
377380

378-
if (items && HistoryIndex) {
379-
ready &= DoPrechargeHistory(prechargedFirstRowId, prechargedLastRowId);
381+
if (prechargedRowsRange && HistoryIndex) {
382+
ready &= DoPrechargeHistory(prechargedRowsRange->first, prechargedRowsRange->second);
380383
}
381384
}
382385

0 commit comments

Comments
 (0)