Skip to content

Commit 4c43d1f

Browse files
committed
System views creation in SchemeShard under FeatureFlag
1 parent 6be8671 commit 4c43d1f

File tree

16 files changed

+628
-52
lines changed

16 files changed

+628
-52
lines changed

ydb/core/protos/feature_flags.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,5 @@ message TFeatureFlags {
211211
optional bool EnableNodeBrokerDeltaProtocol = 185 [default = false];
212212
optional bool EnableAccessToIndexImplTables = 186 [default = false];
213213
optional bool EnableAddUniqueIndex = 187 [default = false];
214+
optional bool EnableMaterializedSystemViewPaths = 188 [default = false, (RequireRestart) = true];
214215
}

ydb/core/sys_view/common/events.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class IDbCounters : public virtual TThrRefBase {
2222

2323
struct TEvSysView {
2424
enum EEv {
25-
EvSendPartitionStats = EventSpaceBegin(TKikimrEvents::ES_SYSTEM_VIEW),
25+
EvUpdateFinished = EventSpaceBegin(TKikimrEvents::ES_SYSTEM_VIEW),
26+
EvSendPartitionStats,
2627
EvSetPartitioning,
2728
EvRemoveTable,
2829
EvGetPartitionStats,
@@ -81,6 +82,12 @@ struct TEvSysView {
8182
EvEnd,
8283
};
8384

85+
struct TEvUpdateFinished : public TEventLocal<
86+
TEvUpdateFinished,
87+
EvUpdateFinished>
88+
{
89+
};
90+
8491
struct TEvSendPartitionStats : public TEventLocal<
8592
TEvSendPartitionStats,
8693
EvSendPartitionStats>

ydb/core/sys_view/common/schema.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ class TSystemViewResolver : public ISystemViewResolver {
210210
return result;
211211
}
212212

213+
const THashMap<TString, ESysViewType>& GetSystemViewsTypes(ETarget target) const override {
214+
switch (target) {
215+
case ETarget::Domain:
216+
return DomainSystemViewTypes;
217+
case ETarget::SubDomain:
218+
return SubDomainSystemViewTypes;
219+
case ETarget::OlapStore:
220+
return OlapStoreSystemViewTypes;
221+
case ETarget::ColumnTable:
222+
return ColumnTableSystemViewTypes;
223+
}
224+
}
225+
213226
bool IsSystemView(const TStringBuf viewName) const override final {
214227
return DomainSystemViews.contains(viewName) ||
215228
SubDomainSystemViews.contains(viewName) ||
@@ -255,13 +268,16 @@ class TSystemViewResolver : public ISystemViewResolver {
255268
template <typename Table>
256269
void RegisterSystemView(const TStringBuf& name, ESysViewType type) {
257270
TSchemaFiller<Table>::Fill(DomainSystemViews[name]);
271+
DomainSystemViewTypes[name] = type;
258272
TSchemaFiller<Table>::Fill(SubDomainSystemViews[name]);
273+
SubDomainSystemViewTypes[name] = type;
259274
TSchemaFiller<Table>::Fill(SystemViews[type]);
260275
}
261276

262277
template <typename Table>
263278
void RegisterDomainSystemView(const TStringBuf& name, ESysViewType type) {
264279
TSchemaFiller<Table>::Fill(DomainSystemViews[name]);
280+
DomainSystemViewTypes[name] = type;
265281
TSchemaFiller<Table>::Fill(SystemViews[type]);
266282
}
267283

@@ -332,9 +348,13 @@ class TSystemViewResolver : public ISystemViewResolver {
332348

333349
private:
334350
THashMap<TString, TSchema> DomainSystemViews;
351+
THashMap<TString, ESysViewType> DomainSystemViewTypes;
335352
THashMap<TString, TSchema> SubDomainSystemViews;
353+
THashMap<TString, ESysViewType> SubDomainSystemViewTypes;
336354
THashMap<TString, TSchema> OlapStoreSystemViews;
355+
THashMap<TString, ESysViewType> OlapStoreSystemViewTypes;
337356
THashMap<TString, TSchema> ColumnTableSystemViews;
357+
THashMap<TString, ESysViewType> ColumnTableSystemViewTypes;
338358
THashMap<ESysViewType, TSchema> SystemViews;
339359
};
340360

@@ -375,12 +395,18 @@ class TSystemViewRewrittenResolver : public ISystemViewResolver {
375395
return {};
376396
}
377397

398+
const THashMap<TString, ESysViewType>& GetSystemViewsTypes(ETarget target) const override {
399+
Y_UNUSED(target);
400+
return SystemViewTypes;
401+
}
402+
378403
bool IsSystemView(const TStringBuf viewName) const override final {
379404
return SystemViews.contains(viewName);
380405
}
381406

382407
private:
383408
THashMap<TString, TSchema> SystemViews;
409+
THashMap<TString, ESysViewType> SystemViewTypes;
384410
};
385411

386412
ISystemViewResolver* CreateSystemViewResolver() {

ydb/core/sys_view/common/schema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ class ISystemViewResolver {
837837
virtual bool IsSystemView(const TStringBuf viewName) const = 0;
838838

839839
virtual TVector<TString> GetSystemViewNames(ETarget target) const = 0;
840+
841+
virtual const THashMap<TString, NKikimrSysView::ESysViewType>& GetSystemViewsTypes(ETarget target) const = 0;
840842
};
841843

842844
ISystemViewResolver* CreateSystemViewResolver();

ydb/core/testlib/basics/feature_flags.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class TTestFeatureFlagsHolder {
7777
FEATURE_FLAG_SETTER(EnableDatabaseAdmin)
7878
FEATURE_FLAG_SETTER(EnablePermissionsExport)
7979
FEATURE_FLAG_SETTER(EnableShowCreate)
80+
FEATURE_FLAG_SETTER(EnableMaterializedSystemViewPaths)
8081

8182
#undef FEATURE_FLAG_SETTER
8283
};

ydb/core/tx/schemeshard/schemeshard__operation_create_sysview.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class TCreateSysView : public TSubOperation {
105105
THolder<TProposeResponse> Propose(const TString& owner, TOperationContext& context) override {
106106
const TTabletId ssId = context.SS->SelfTabletId();
107107

108+
const auto acceptExisting = !Transaction.GetFailOnExist();
108109
const TString& parentPathStr = Transaction.GetWorkingDir();
109110
const auto& sysViewDescription = Transaction.GetCreateSysView();
110111

@@ -150,7 +151,7 @@ class TCreateSysView : public TSubOperation {
150151
if (dstPath.IsResolved()) {
151152
checks
152153
.NotUnderDeleting()
153-
.FailOnExist(TPathElement::EPathType::EPathTypeSysView, /* acceptAlreadyExist */ false);
154+
.FailOnExist(TPathElement::EPathType::EPathTypeSysView, acceptExisting);
154155
} else {
155156
checks
156157
.NotEmpty();

ydb/core/tx/schemeshard/schemeshard_impl.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "olap/bg_tasks/events/global.h"
66
#include "schemeshard__data_erasure_manager.h"
77

8+
#include <ydb/core/sys_view/common/path.h>
9+
#include <ydb/core/sys_view/common/schema.h>
810
#include <ydb/core/tablet_flat/tablet_flat_executed.h>
911
#include <ydb/core/tablet/tablet_counters_aggregator.h>
1012
#include <ydb/core/tablet/tablet_counters_protobuf.h>
@@ -22,6 +24,8 @@
2224
#include <ydb/core/scheme/scheme_types_proto.h>
2325
#include <ydb/core/tx/columnshard/bg_tasks/events/events.h>
2426
#include <ydb/core/tx/scheme_board/events_schemeshard.h>
27+
#include <ydb/core/tx/schemeshard/schemeshard_path.h>
28+
#include <ydb/core/tx/schemeshard/schemeshard_sysviews_update.h>
2529
#include <ydb/core/keyvalue/keyvalue_events.h>
2630
#include <ydb/library/login/password_checker/password_checker.h>
2731
#include <ydb/library/login/account_lockout/account_lockout.h>
@@ -71,6 +75,12 @@ bool ResolvePoolNames(
7175
return true;
7276
}
7377

78+
struct TDirectoryEntry {
79+
NKikimrSchemeOp::EPathType Type;
80+
TString Owner;
81+
TMaybe<NKikimrSysView::ESysViewType> SysViewType;
82+
};
83+
7484
} // anonymous namespace
7585

7686
const TSchemeLimits TSchemeShard::DefaultLimits = {};
@@ -85,6 +95,88 @@ void TSchemeShard::SubscribeToTempTableOwners() {
8595
}
8696
}
8797

98+
void TSchemeShard::CollectSysViewUpdates(const TActorContext& ctx) {
99+
THashMap<uint64_t ,TModifySysViewRequestInfo> sysViewUpdates;
100+
101+
const TPath rootPath = TPath::Root(this);
102+
const TPath sysViewDirPath = rootPath.Child(TString(NSysView::SysPathName));
103+
bool needToMakeSysViewDir = false;
104+
105+
// make system view dir
106+
if (!sysViewDirPath.Check().IsResolved().NotDeleted().NotUnderDeleting()) {
107+
needToMakeSysViewDir = true;
108+
TModifySysViewRequestInfo makeSysViewDirRequest;
109+
makeSysViewDirRequest.OperationType = NKikimrSchemeOp::ESchemeOpMkDir;
110+
makeSysViewDirRequest.WorkingDir = rootPath.PathString();
111+
makeSysViewDirRequest.TargetName = sysViewDirPath.LeafName();
112+
113+
sysViewUpdates.emplace(GetCachedTxId(ctx), std::move(makeSysViewDirRequest));
114+
}
115+
116+
const auto sysViewDirType = IsDomainSchemeShard
117+
? NSysView::ISystemViewResolver::ETarget::Domain
118+
: NSysView::ISystemViewResolver::ETarget::SubDomain;
119+
const auto sysViewsRegister = NSysView::CreateSystemViewResolver()->GetSystemViewsTypes(sysViewDirType);
120+
121+
TMap<TString, TDirectoryEntry> sysViewDirContents;
122+
if (sysViewDirPath.Check().IsResolved().NotDeleted().NotUnderDeleting().IsDirectory()) {
123+
for (const auto& [name, pathId] : sysViewDirPath->GetChildren()) {
124+
const TPath dirEntryPath = TPath::Init(pathId, this);
125+
const auto checks = dirEntryPath.Check();
126+
if (checks.IsResolved().NotDeleted().NotUnderDeleting()) {
127+
TDirectoryEntry dirEntry;
128+
dirEntry.Type = dirEntryPath->PathType;
129+
dirEntry.Owner = dirEntryPath->Owner;
130+
if (checks.IsSysView()) {
131+
dirEntry.SysViewType = SysViews.at(pathId)->Type;
132+
}
133+
134+
sysViewDirContents.emplace(name, std::move(dirEntry));
135+
}
136+
}
137+
}
138+
139+
// create absent system views
140+
if (needToMakeSysViewDir || sysViewDirPath.Check().IsResolved().NotDeleted().NotUnderDeleting().IsDirectory()) {
141+
for (const auto& [name, type] : sysViewsRegister) {
142+
if (!sysViewDirContents.contains(name)) {
143+
TModifySysViewRequestInfo createSysViewRequest;
144+
createSysViewRequest.OperationType = NKikimrSchemeOp::ESchemeOpCreateSysView;
145+
createSysViewRequest.WorkingDir = sysViewDirPath.PathString();
146+
createSysViewRequest.TargetName = name;
147+
createSysViewRequest.SysViewType = type;
148+
149+
sysViewUpdates.emplace(GetCachedTxId(ctx), std::move(createSysViewRequest));
150+
}
151+
}
152+
}
153+
154+
THashSet<NKikimrSysView::ESysViewType> availableSysViewTypes;
155+
for (const auto& type : std::views::values(sysViewsRegister)) {
156+
availableSysViewTypes.insert(type);
157+
}
158+
159+
// drop obsolete system views
160+
for (const auto& [name, dirEntry] : sysViewDirContents) {
161+
if (dirEntry.Type == NKikimrSchemeOp::EPathTypeSysView) {
162+
if (!dirEntry.SysViewType || !availableSysViewTypes.contains(*dirEntry.SysViewType) ||
163+
(dirEntry.Owner == BUILTIN_ACL_METADATA && !sysViewsRegister.contains(name))) {
164+
TModifySysViewRequestInfo dropSysViewRequest;
165+
dropSysViewRequest.OperationType = NKikimrSchemeOp::ESchemeOpDropSysView;
166+
dropSysViewRequest.WorkingDir = sysViewDirPath.PathString();
167+
dropSysViewRequest.TargetName = name;
168+
169+
sysViewUpdates.emplace(GetCachedTxId(ctx), std::move(dropSysViewRequest));
170+
}
171+
}
172+
}
173+
174+
if (!sysViewUpdates.empty()) {
175+
LOG_DEBUG_S(ctx, NKikimrServices::FLAT_TX_SCHEMESHARD, "Start update system views cnt: " << sysViewUpdates.size());
176+
Register(CreateSysViewsUpdate(TabletID(), SelfId(), std::move(sysViewUpdates)).Release());
177+
}
178+
}
179+
88180
void TSchemeShard::ActivateAfterInitialization(const TActorContext& ctx, TActivationOpts&& opts) {
89181
TPathId subDomainPathId = GetCurrentSubDomainPathId();
90182
TSubDomainInfo::TPtr domainPtr = ResolveDomainInfo(subDomainPathId);
@@ -6680,6 +6772,12 @@ void TSchemeShard::Handle(TEvTxAllocatorClient::TEvAllocateResult::TPtr& ev, con
66806772
for (auto txId: ev->Get()->TxIds) {
66816773
CachedTxIds.push_back(TTxId(txId));
66826774
}
6775+
6776+
if (AppData()->FeatureFlags.GetEnableMaterializedSystemViewPaths() && !SysViewsUpdateStarted) {
6777+
SysViewsUpdateStarted = true;
6778+
CollectSysViewUpdates(ctx);
6779+
}
6780+
66836781
return;
66846782
} else if (Exports.contains(id)) {
66856783
return Execute(CreateTxProgressExport(ev), ctx);

ydb/core/tx/schemeshard/schemeshard_impl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ class TSchemeShard
380380
bool TopicPersistStatsPending = false;
381381
TStatsQueue<TEvPersQueue::TEvPeriodicTopicStats> TopicStatsQueue;
382382

383+
bool SysViewsUpdateStarted = false;
384+
383385
TSet<TPathId> CleanDroppedPathsCandidates;
384386
TSet<TPathId> CleanDroppedSubDomainsCandidates;
385387
bool CleanDroppedPathsInFly = false;
@@ -915,6 +917,8 @@ class TSchemeShard
915917

916918
void SubscribeToTempTableOwners();
917919

920+
void CollectSysViewUpdates(const TActorContext& ctx);
921+
918922
void ActivateAfterInitialization(const TActorContext& ctx, TActivationOpts&& opts);
919923

920924
struct TTxInitPopulator;

0 commit comments

Comments
 (0)