Skip to content

Commit edeb17d

Browse files
authored
Request DataShard compaction if scheme has been changeed (#11147)
1 parent 91cc642 commit edeb17d

25 files changed

+527
-149
lines changed

ydb/core/protos/table_stats.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ message TTableStats {
6868
optional TStoragePoolsStats StoragePools = 31;
6969

7070
optional uint64 ByKeyFilterSize = 32;
71+
72+
// denotes that datashard should be background compacted
73+
// even if it is single parted
74+
optional bool HasSchemaChanges = 33;
7175
}

ydb/core/tablet_flat/flat_comp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace NTable {
166166
/**
167167
* Returns row schema of the specified table
168168
*/
169-
virtual TIntrusiveConstPtr<TRowScheme> RowScheme(ui32 table) = 0;
169+
virtual TIntrusiveConstPtr<TRowScheme> RowScheme(ui32 table) const = 0;
170170

171171
/**
172172
* Returns schema of the specified table

ydb/core/tablet_flat/flat_executor.cpp

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4293,7 +4293,7 @@ const NTable::TScheme& TExecutor::DatabaseScheme()
42934293
return Scheme();
42944294
}
42954295

4296-
TIntrusiveConstPtr<NTable::TRowScheme> TExecutor::RowScheme(ui32 table)
4296+
TIntrusiveConstPtr<NTable::TRowScheme> TExecutor::RowScheme(ui32 table) const
42974297
{
42984298
return Database->GetRowScheme(table);
42994299
}
@@ -4334,6 +4334,80 @@ const NTable::TRowVersionRanges& TExecutor::TableRemovedRowVersions(ui32 table)
43344334
return Database->GetRemovedRowVersions(table);
43354335
}
43364336

4337+
bool TExecutor::HasSchemaChanges(ui32 table) const {
4338+
auto *tableInfo = Scheme().GetTableInfo(table);
4339+
auto rowScheme = RowScheme(table);
4340+
if (!tableInfo || !rowScheme) {
4341+
return false;
4342+
}
4343+
4344+
auto subset = Database->Subset(table, NTable::TEpoch::Max(), { } , { });
4345+
for (const auto& partView : subset->Flatten) {
4346+
if (HasSchemaChanges(partView, *tableInfo, *rowScheme)) {
4347+
return true;
4348+
}
4349+
}
4350+
4351+
return false;
4352+
}
4353+
4354+
bool TExecutor::HasSchemaChanges(const NTable::TPartView& partView, const NTable::TScheme::TTableInfo& tableInfo, const NTable::TRowScheme& rowScheme) const {
4355+
if (partView.Part->Stat.Rows == 0) {
4356+
return false;
4357+
}
4358+
4359+
{ // Check by key filter existence
4360+
bool partByKeyFilter = bool(partView->ByKey);
4361+
bool schemeByKeyFilter = tableInfo.ByKeyFilter;
4362+
if (partByKeyFilter != schemeByKeyFilter) {
4363+
return true;
4364+
}
4365+
}
4366+
4367+
{ // Check B-Tree index existence
4368+
if (AppData()->FeatureFlags.GetEnableLocalDBBtreeIndex() && !partView->IndexPages.HasBTree()) {
4369+
return true;
4370+
}
4371+
}
4372+
4373+
{ // Check families
4374+
size_t partFamiliesCount = partView->GroupsCount;
4375+
size_t schemeFamiliesCount = rowScheme.Families.size();
4376+
if (partFamiliesCount != schemeFamiliesCount) {
4377+
return true;
4378+
}
4379+
4380+
for (size_t index : xrange(rowScheme.Families.size())) {
4381+
auto familyId = rowScheme.Families[index];
4382+
static const NTable::TScheme::TFamily defaultFamilySettings;
4383+
const auto& family = tableInfo.Families.ValueRef(familyId, defaultFamilySettings); // Workaround for KIKIMR-17222
4384+
4385+
const auto* schemeGroupRoom = tableInfo.Rooms.FindPtr(family.Room);
4386+
Y_ABORT_UNLESS(schemeGroupRoom, "Cannot find room %" PRIu32 " in table %" PRIu32, family.Room, tableInfo.Id);
4387+
4388+
ui32 partGroupChannel = partView.Part->GetGroupChannel(NTable::NPage::TGroupId(index));
4389+
if (partGroupChannel != schemeGroupRoom->Main) {
4390+
return true;
4391+
}
4392+
}
4393+
}
4394+
4395+
{ // Check columns
4396+
THashMap<NTable::TTag, ui32> partColumnGroups, schemeColumnGroups;
4397+
for (const auto& column : partView->Scheme->AllColumns) {
4398+
partColumnGroups[column.Tag] = column.Group;
4399+
}
4400+
for (const auto& col : rowScheme.Cols) {
4401+
schemeColumnGroups[col.Tag] = col.Group;
4402+
}
4403+
if (partColumnGroups != schemeColumnGroups) {
4404+
return true;
4405+
}
4406+
}
4407+
4408+
return false;
4409+
}
4410+
43374411
ui64 TExecutor::BeginCompaction(THolder<NTable::TCompactionParams> params)
43384412
{
43394413
if (auto logl = Logger->Log(ELnLev::Info))
@@ -4379,37 +4453,29 @@ ui64 TExecutor::BeginCompaction(THolder<NTable::TCompactionParams> params)
43794453

43804454
for (size_t group : xrange(rowScheme->Families.size())) {
43814455
auto familyId = rowScheme->Families[group];
4382-
const auto* family = tableInfo->Families.FindPtr(familyId);
4383-
if (Y_UNLIKELY(!family)) {
4384-
// FIXME: workaround for KIKIMR-17222
4385-
// Column families with default settings may be missing in schema,
4386-
// so we have to use a static variable as a substitute
4387-
static const NTable::TScheme::TFamily defaultFamilySettings;
4388-
family = &defaultFamilySettings;
4389-
}
4390-
Y_ABORT_UNLESS(family, "Cannot find family %" PRIu32 " in table %" PRIu32, familyId, table);
4456+
static const NTable::TScheme::TFamily defaultFamilySettings;
4457+
const auto& family = tableInfo->Families.ValueRef(familyId, defaultFamilySettings); // Workaround for KIKIMR-17222
43914458

4392-
auto roomId = family->Room;
4393-
auto* room = tableInfo->Rooms.FindPtr(roomId);
4394-
Y_ABORT_UNLESS(room, "Cannot find room %" PRIu32 " in table %" PRIu32, roomId, table);
4459+
auto* room = tableInfo->Rooms.FindPtr(family.Room);
4460+
Y_ABORT_UNLESS(room, "Cannot find room %" PRIu32 " in table %" PRIu32, family.Room, table);
43954461

43964462
auto& pageGroup = comp->Layout.Groups.at(group);
43974463
auto& writeGroup = comp->Writer.Groups.at(group);
43984464

4399-
pageGroup.Codec = family->Codec;
4465+
pageGroup.Codec = family.Codec;
44004466
pageGroup.PageSize = policy->MinDataPageSize;
44014467
pageGroup.BTreeIndexNodeTargetSize = policy->MinBTreeIndexNodeSize;
44024468
pageGroup.BTreeIndexNodeKeysMin = policy->MinBTreeIndexNodeKeys;
44034469

4404-
writeGroup.Cache = Max(family->Cache, cache);
4470+
writeGroup.Cache = Max(family.Cache, cache);
44054471
writeGroup.MaxBlobSize = NBlockIO::BlockSize;
44064472
writeGroup.Channel = room->Main;
44074473
addChannel(room->Main);
44084474

44094475
if (group == 0) {
44104476
// Small/Large edges are taken from the leader family
4411-
comp->Layout.SmallEdge = family->Small;
4412-
comp->Layout.LargeEdge = family->Large;
4477+
comp->Layout.SmallEdge = family.Small;
4478+
comp->Layout.LargeEdge = family.Large;
44134479

44144480
// Small/Large channels are taken from the leader family
44154481
comp->Writer.BlobsChannels = room->Blobs;

ydb/core/tablet_flat/flat_executor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ class TExecutor
591591

592592
ui64 OwnerTabletId() const override;
593593
const NTable::TScheme& DatabaseScheme() override;
594-
TIntrusiveConstPtr<NTable::TRowScheme> RowScheme(ui32 table) override;
594+
TIntrusiveConstPtr<NTable::TRowScheme> RowScheme(ui32 table) const override;
595595
const NTable::TScheme::TTableInfo* TableScheme(ui32 table) override;
596596
ui64 TableMemSize(ui32 table, NTable::TEpoch epoch) override;
597597
NTable::TPartView TablePart(ui32 table, const TLogoBlobID& label) override;
@@ -652,6 +652,8 @@ class TExecutor
652652
bool CancelScan(ui32 tableId, ui64 taskId) override;
653653

654654
TFinishedCompactionInfo GetFinishedCompactionInfo(ui32 tableId) const override;
655+
bool HasSchemaChanges(ui32 table) const override;
656+
bool HasSchemaChanges(const NTable::TPartView& partView, const NTable::TScheme::TTableInfo& tableInfo, const NTable::TRowScheme& rowScheme) const;
655657
ui64 CompactBorrowed(ui32 tableId) override;
656658
ui64 CompactMemTable(ui32 tableId) override;
657659
ui64 CompactTable(ui32 tableId) override;

ydb/core/tablet_flat/flat_stat_table.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ bool BuildStats(const TSubset& subset, TStats& stats, ui64 rowCountResolution, u
2323
}
2424

2525
void GetPartOwners(const TSubset& subset, THashSet<ui64>& partOwners) {
26-
for (auto& pi : subset.Flatten) {
27-
partOwners.insert(pi->Label.TabletID());
26+
for (const auto& partView : subset.Flatten) {
27+
partOwners.insert(partView->Label.TabletID());
2828
}
29-
for (auto& pi : subset.ColdParts) {
30-
partOwners.insert(pi->Label.TabletID());
29+
for (const auto& coldPart : subset.ColdParts) {
30+
partOwners.insert(coldPart->Label.TabletID());
3131
}
32-
for (auto& pi : subset.TxStatus) {
33-
partOwners.insert(pi->Label.TabletID());
32+
for (const auto& txStatus : subset.TxStatus) {
33+
partOwners.insert(txStatus->Label.TabletID());
3434
}
3535
}
3636

ydb/core/tablet_flat/flat_table.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,13 +1398,12 @@ bool TTable::RemoveRowVersions(const TRowVersion& lower, const TRowVersion& uppe
13981398

13991399
TCompactionStats TTable::GetCompactionStats() const
14001400
{
1401-
TCompactionStats stats;
1402-
stats.MemRowCount = GetMemRowCount();
1403-
stats.MemDataSize = GetMemSize();
1404-
stats.MemDataWaste = GetMemWaste();
1405-
stats.PartCount = Flatten.size() + ColdParts.size();
1406-
1407-
return stats;
1401+
return {
1402+
.PartCount = Flatten.size() + ColdParts.size(),
1403+
.MemRowCount = GetMemRowCount(),
1404+
.MemDataSize = GetMemSize(),
1405+
.MemDataWaste = GetMemWaste(),
1406+
};
14081407
}
14091408

14101409
void TTable::SetTableObserver(TIntrusivePtr<ITableObserver> ptr) noexcept

ydb/core/tablet_flat/tablet_flat_executor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ namespace NFlatExecutorSetup {
585585

586586
// edge and ts of last full compaction
587587
virtual TFinishedCompactionInfo GetFinishedCompactionInfo(ui32 tableId) const = 0;
588+
virtual bool HasSchemaChanges(ui32 table) const = 0;
588589

589590
// Forces full compaction of the specified table in the near future
590591
// Returns 0 if can't compact, otherwise compaction ID

ydb/core/tablet_flat/ut/flat_comp_ut_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TSimpleBackend : public ICompactionBackend {
4747
return DB.GetScheme();
4848
}
4949

50-
TIntrusiveConstPtr<NKikimr::NTable::TRowScheme> RowScheme(ui32 table) override {
50+
TIntrusiveConstPtr<NKikimr::NTable::TRowScheme> RowScheme(ui32 table) const override {
5151
return DB.GetRowScheme(table);
5252
}
5353

ydb/core/tx/datashard/datashard.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ TDataShard::TDataShard(const TActorId &tablet, TTabletStorageInfo *info)
138138
, MaxTxLagMilliseconds(5*60*1000, 0, 30*24*3600*1000ll)
139139
, CanCancelROWithReadSets(0, 0, 1)
140140
, PerShardReadSizeLimit(5368709120, 0, 107374182400)
141-
, CpuUsageReportThreshlodPercent(60, -1, 146)
141+
, CpuUsageReportThresholdPercent(60, -1, 146)
142142
, CpuUsageReportIntervalSeconds(60, 0, 365*86400)
143-
, HighDataSizeReportThreshlodBytes(10ull<<30, -1, Max<i64>())
143+
, HighDataSizeReportThresholdBytes(10ull<<30, -1, Max<i64>())
144144
, HighDataSizeReportIntervalSeconds(60, 0, 365*86400)
145145
, DataTxProfileLogThresholdMs(0, 0, 86400000)
146146
, DataTxProfileBufferThresholdMs(0, 0, 86400000)
@@ -308,9 +308,9 @@ void TDataShard::IcbRegister() {
308308

309309
appData->Icb->RegisterSharedControl(CanCancelROWithReadSets, "DataShardControls.CanCancelROWithReadSets");
310310
appData->Icb->RegisterSharedControl(PerShardReadSizeLimit, "TxLimitControls.PerShardReadSizeLimit");
311-
appData->Icb->RegisterSharedControl(CpuUsageReportThreshlodPercent, "DataShardControls.CpuUsageReportThreshlodPercent");
311+
appData->Icb->RegisterSharedControl(CpuUsageReportThresholdPercent, "DataShardControls.CpuUsageReportThreshlodPercent");
312312
appData->Icb->RegisterSharedControl(CpuUsageReportIntervalSeconds, "DataShardControls.CpuUsageReportIntervalSeconds");
313-
appData->Icb->RegisterSharedControl(HighDataSizeReportThreshlodBytes, "DataShardControls.HighDataSizeReportThreshlodBytes");
313+
appData->Icb->RegisterSharedControl(HighDataSizeReportThresholdBytes, "DataShardControls.HighDataSizeReportThreshlodBytes");
314314
appData->Icb->RegisterSharedControl(HighDataSizeReportIntervalSeconds, "DataShardControls.HighDataSizeReportIntervalSeconds");
315315

316316
appData->Icb->RegisterSharedControl(BackupReadAheadLo, "DataShardControls.BackupReadAheadLo");

ydb/core/tx/datashard/datashard__compaction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class TDataShard::TTxCompactTable : public NTabletFlatExecutor::TTransactionBase
108108
auto stats = txc.DB.GetCompactionStats(localTid);
109109
bool isEmpty = stats.PartCount == 0 && stats.MemDataSize == 0;
110110
bool isSingleParted = stats.PartCount == 1 && stats.MemDataSize == 0;
111-
if (isEmpty || isSingleParted && !hasBorrowed && !record.HasCompactSinglePartedShards()) {
111+
bool hasSchemaChanges = Self->Executor()->HasSchemaChanges(tableInfo.LocalTid);
112+
if (isEmpty || isSingleParted && !hasBorrowed && !hasSchemaChanges && !record.GetCompactSinglePartedShards()) {
112113
// nothing to compact
113114
LOG_DEBUG_S(ctx, NKikimrServices::TX_DATASHARD,
114115
"Background compaction of tablet# " << Self->TabletID()

0 commit comments

Comments
 (0)