Skip to content

Commit 5c6be8e

Browse files
lex007inblinkov
authored andcommitted
Add data cleanup starting in datashard (#14244)
1 parent f4e6b30 commit 5c6be8e

22 files changed

+522
-107
lines changed

ydb/core/blobstorage/dsproxy/mock/model.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ namespace NFake {
430430
return Blobs;
431431
}
432432

433+
TGroupId GetGroupId() const {
434+
return GroupId;
435+
}
436+
433437
private:
434438
// check if provided generation is blocked for specific tablet
435439
bool IsBlocked(TTabletId tabletId, TGeneration generation) const noexcept {

ydb/core/protos/counters_datashard.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,4 +529,6 @@ enum ETxTypes {
529529
TXTYPE_WRITE = 82 [(TxTypeOpts) = {Name: "TxWrite"}];
530530
TXTYPE_REMOVE_SCHEMA_SNAPSHOTS = 83 [(TxTypeOpts) = {Name: "TxRemoveSchemaSnapshots"}];
531531
TXTYPE_INIT_RESTORED = 84 [(TxTypeOpts) = {Name: "TxInitRestored"}];
532+
TXTYPE_DATA_CLEANUP = 85 [(TxTypeOpts) = {Name: "TxDataCleanup"}];
533+
TXTYPE_COMPLETE_DATA_CLEANUP = 86 [(TxTypeOpts) = {Name: "TxCompleteDataCleanup"}];
532534
}

ydb/core/tablet_flat/flat_executor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3904,8 +3904,8 @@ bool TExecutor::CompactTables() {
39043904
}
39053905
}
39063906

3907-
void TExecutor::CleanupData() {
3908-
if (DataCleanupLogic->TryStartCleanup()) {
3907+
void TExecutor::CleanupData(ui64 dataCleanupGeneration) {
3908+
if (DataCleanupLogic->TryStartCleanup(dataCleanupGeneration, OwnerCtx())) {
39093909
for (const auto& [tableId, _] : Scheme().Tables) {
39103910
auto compactionId = CompactionLogic->PrepareForceCompaction(tableId);
39113911
DataCleanupLogic->OnCompactionPrepared(tableId, compactionId);

ydb/core/tablet_flat/flat_executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ class TExecutor
640640
ui64 CompactTable(ui32 tableId) override;
641641
bool CompactTables() override;
642642

643-
void CleanupData() override;
643+
void CleanupData(ui64 dataCleanupGeneration) override;
644644

645645
void Handle(NMemory::TEvMemTableRegistered::TPtr &ev);
646646
void Handle(NMemory::TEvMemTableCompact::TPtr &ev);

ydb/core/tablet_flat/flat_executor_data_cleanup_logic.cpp

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,43 @@ TDataCleanupLogic::TDataCleanupLogic(IOps* ops, IExecutor* executor, ITablet* ow
1010
, GcLogic(gcLogic)
1111
{}
1212

13-
bool TDataCleanupLogic::TryStartCleanup() {
14-
if (State == EDataCleanupState::Idle) {
15-
if (auto logl = Logger->Log(ELnLev::Info)) {
16-
logl << "TDataCleanupLogic: Starting DataCleanup for tablet with id " << Owner->TabletID();
13+
bool TDataCleanupLogic::TryStartCleanup(ui64 dataCleanupGeneration, const TActorContext& ctx) {
14+
switch (State) {
15+
case EDataCleanupState::Idle: {
16+
if (CurrentDataCleanupGeneration >= dataCleanupGeneration) {
17+
if (auto logl = Logger->Log(ELnLev::Info)) {
18+
logl << "TDataCleanupLogic: DataCleanup for tablet with id " << Owner->TabletID()
19+
<< " had already completed for generation " << dataCleanupGeneration
20+
<< ", current DataCleanup generation: " << CurrentDataCleanupGeneration;
21+
}
22+
// repeat DataCleanupComplete callback
23+
CompleteDataCleanup(ctx);
24+
return false;
25+
} else {
26+
CurrentDataCleanupGeneration = dataCleanupGeneration;
27+
if (auto logl = Logger->Log(ELnLev::Info)) {
28+
logl << "TDataCleanupLogic: Starting DataCleanup for tablet with id " << Owner->TabletID()
29+
<< ", current DataCleanup generation: " << CurrentDataCleanupGeneration;
30+
}
31+
State = EDataCleanupState::PendingCompaction;
32+
return true;
33+
}
34+
break;
1735
}
18-
State = EDataCleanupState::PendingCompaction;
19-
return true;
20-
} else {
21-
if (auto logl = Logger->Log(ELnLev::Info)) {
22-
logl << "TDataCleanupLogic: schedule next DataCleanup for tablet with id " << Owner->TabletID();
36+
default: { // DataCleanup in progress
37+
if (dataCleanupGeneration > CurrentDataCleanupGeneration) {
38+
NextDataCleanupGeneration = Max(dataCleanupGeneration, NextDataCleanupGeneration);
39+
if (auto logl = Logger->Log(ELnLev::Info)) {
40+
logl << "TDataCleanupLogic: schedule next DataCleanup for tablet with id " << Owner->TabletID()
41+
<< ", current DataCleanup generation: " << CurrentDataCleanupGeneration
42+
<< ", next DataCleanup generation: " << NextDataCleanupGeneration;
43+
}
44+
return false;
45+
} else {
46+
// more recent DataCleanup in progress, so just ignore osolete generation
47+
return false;
48+
}
2349
}
24-
StartNextCleanup = true;
25-
return false;
2650
}
2751
}
2852

@@ -166,14 +190,16 @@ bool TDataCleanupLogic::NeedGC() {
166190
}
167191

168192
void TDataCleanupLogic::CompleteDataCleanup(const TActorContext& ctx) {
169-
Owner->DataCleanupComplete(ctx);
170-
if (auto logl = Logger->Log(ELnLev::Info)) {
171-
logl << "TDataCleanupLogic: DataCleanup finished for tablet with id " << Owner->TabletID();
172-
}
173193
State = EDataCleanupState::Idle;
174-
if (StartNextCleanup) {
175-
StartNextCleanup = false;
176-
Executor->CleanupData();
194+
if (NextDataCleanupGeneration) {
195+
Executor->CleanupData(std::exchange(NextDataCleanupGeneration, 0));
196+
} else {
197+
// report complete only if all planned cleanups completed
198+
Owner->DataCleanupComplete(CurrentDataCleanupGeneration, ctx);
199+
if (auto logl = Logger->Log(ELnLev::Info)) {
200+
logl << "TDataCleanupLogic: DataCleanup finished for tablet with id " << Owner->TabletID()
201+
<< ", current DataCleanup generation: " << CurrentDataCleanupGeneration;
202+
}
177203
}
178204
}
179205

ydb/core/tablet_flat/flat_executor_data_cleanup_logic.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TDataCleanupLogic {
3333

3434
TDataCleanupLogic(IOps* ops, IExecutor* executor, ITablet* owner, NUtil::ILogger* logger, TExecutorGCLogic* gcLogic);
3535

36-
bool TryStartCleanup();
36+
bool TryStartCleanup(ui64 dataCleanupGeneration, const TActorContext& ctx);
3737
void OnCompactionPrepared(ui32 tableId, ui64 compactionId);
3838
void WaitCompaction();
3939
void OnCompleteCompaction(ui32 tableId, const TFinishedCompactionInfo& finishedCompactionInfo);
@@ -54,8 +54,9 @@ class TDataCleanupLogic {
5454
NUtil::ILogger* const Logger;
5555
TExecutorGCLogic* const GcLogic;
5656

57+
ui64 CurrentDataCleanupGeneration = 0;
58+
ui64 NextDataCleanupGeneration = 0;
5759
EDataCleanupState State = EDataCleanupState::Idle;
58-
bool StartNextCleanup = false;
5960
THashMap<ui32, TCleanupTableInfo> CompactingTables; // tracks statuses of compaction
6061

6162
// two subsequent are snapshots required to force GC

ydb/core/tablet_flat/flat_executor_ut.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ class TTestFlatTablet : public TActor<TTestFlatTablet>, public TTabletExecutedFl
445445
Send(Sender, new NFake::TEvCompacted(table));
446446
}
447447

448-
void DataCleanupComplete(const TActorContext&) override {
449-
Send(Sender, new NFake::TEvDataCleaned());
448+
void DataCleanupComplete(ui64 dataCleanupComplete, const TActorContext&) override {
449+
Send(Sender, new NFake::TEvDataCleaned(dataCleanupComplete));
450450
}
451451

452452
void ScanComplete(NTable::EAbort, TAutoPtr<IDestructable>, ui64 cookie, const TActorContext&) override

ydb/core/tablet_flat/tablet_flat_executor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace NFlatExecutorSetup {
2020
Y_UNUSED(ctx);
2121
}
2222

23-
void ITablet::DataCleanupComplete(const TActorContext& ctx) {
23+
void ITablet::DataCleanupComplete(ui64 dataCleanupGeneration, const TActorContext& ctx) {
24+
Y_UNUSED(dataCleanupGeneration);
2425
Y_UNUSED(ctx);
2526
}
2627

ydb/core/tablet_flat/tablet_flat_executor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ namespace NFlatExecutorSetup {
499499
virtual void SnapshotComplete(TIntrusivePtr<TTableSnapshotContext> snapContext, const TActorContext &ctx); // would be FAIL in default implementation
500500
virtual void CompletedLoansChanged(const TActorContext &ctx); // would be no-op in default implementation
501501
virtual void CompactionComplete(ui32 tableId, const TActorContext &ctx); // would be no-op in default implementation
502-
virtual void DataCleanupComplete(const TActorContext& ctx);
502+
virtual void DataCleanupComplete(ui64 dataCleanupGeneration, const TActorContext& ctx);
503503

504504
virtual void ScanComplete(NTable::EAbort status, TAutoPtr<IDestructable> prod, ui64 cookie, const TActorContext &ctx);
505505

@@ -634,7 +634,7 @@ namespace NFlatExecutorSetup {
634634

635635
virtual void SetPreloadTablesData(THashSet<ui32> tables) = 0;
636636

637-
virtual void CleanupData() = 0;
637+
virtual void CleanupData(ui64 dataCleanupGeneration) = 0;
638638

639639
ui32 Generation() const { return Generation0; }
640640
ui32 Step() const { return Step0; }

ydb/core/tablet_flat/test/libs/exec/dummy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ namespace NFake {
124124
Send(Owner, new NFake::TEvCompacted(table));
125125
}
126126

127-
void DataCleanupComplete(const TActorContext&) override
127+
void DataCleanupComplete(ui64 dataCleanupGeneration, const TActorContext&) override
128128
{
129129
if (Flags & ui32(EFlg::Clean))
130-
Send(Owner, new NFake::TEvDataCleaned());
130+
Send(Owner, new NFake::TEvDataCleaned(dataCleanupGeneration));
131131
}
132132

133133
void SnapshotComplete(

0 commit comments

Comments
 (0)