Skip to content

Commit bd8a859

Browse files
authored
Merge pull request #18028 from swalrus1/integrate-426e0d8-1
separate schema caches by tenant (#17729)
2 parents f7ebefc + 2eb339d commit bd8a859

File tree

11 files changed

+86
-43
lines changed

11 files changed

+86
-43
lines changed

ydb/core/tx/columnshard/columnshard_impl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ TColumnShard::TColumnShard(TTabletStorageInfo* info, const TActorId& tablet)
8585
, PeriodicWakeupActivationPeriod(NYDBTest::TControllers::GetColumnShardController()->GetPeriodicWakeupActivationPeriod())
8686
, StatsReportInterval(NYDBTest::TControllers::GetColumnShardController()->GetStatsReportInterval())
8787
, InFlightReadsTracker(StoragesManager, Counters.GetRequestsTracingCounters())
88-
, TablesManager(StoragesManager, std::make_shared<NOlap::NDataAccessorControl::TLocalManager>(nullptr),
89-
std::make_shared<NOlap::TSchemaObjectsCache>(), Counters.GetPortionIndexCounters(), info->TabletID)
88+
, TablesManager(StoragesManager, std::make_shared<NOlap::NDataAccessorControl::TLocalManager>(nullptr), nullptr,
89+
Counters.GetPortionIndexCounters(), info->TabletID)
9090
, Subscribers(std::make_shared<NSubscriber::TManager>(*this))
9191
, PipeClientCache(NTabletPipe::CreateBoundedClientCache(new NTabletPipe::TBoundedClientCacheConfig(), GetPipeClientConfig()))
9292
, InsertTable(std::make_unique<NOlap::TInsertTable>())
@@ -360,6 +360,7 @@ void TColumnShard::RunInit(const NKikimrTxColumnShard::TInitShard& proto, const
360360
if (proto.HasOwnerPathId()) {
361361
OwnerPathId = proto.GetOwnerPathId();
362362
Schema::SaveSpecialValue(db, Schema::EValueIds::OwnerPathId, OwnerPathId);
363+
TablesManager.SetSchemaObjectsCache(NOlap::TSchemaCachesManager::GetCache(OwnerPathId, Info()->TenantPathId));
363364
}
364365

365366
if (proto.HasOwnerPath()) {

ydb/core/tx/columnshard/engines/column_engine_logs.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ TColumnEngineForLogs::TColumnEngineForLogs(const ui64 tabletId, const std::share
3737
, Counters(counters)
3838
, LastPortion(0)
3939
, LastGranule(0) {
40+
AFL_VERIFY(SchemaObjectsCache);
4041
ActualizationController = std::make_shared<NActualizer::TController>();
4142
RegisterSchemaVersion(snapshot, presetId, schema);
4243
}
@@ -52,6 +53,7 @@ TColumnEngineForLogs::TColumnEngineForLogs(const ui64 tabletId, const std::share
5253
, Counters(counters)
5354
, LastPortion(0)
5455
, LastGranule(0) {
56+
AFL_VERIFY(SchemaObjectsCache);
5557
ActualizationController = std::make_shared<NActualizer::TController>();
5658
RegisterSchemaVersion(snapshot, presetId, std::move(schema));
5759
}
@@ -62,7 +64,7 @@ void TColumnEngineForLogs::RegisterSchemaVersion(const TSnapshot& snapshot, cons
6264
bool switchAccessorsManager = false;
6365
if (!VersionedIndex.IsEmpty()) {
6466
const NOlap::TIndexInfo& lastIndexInfo = VersionedIndex.GetLastSchema()->GetIndexInfo();
65-
Y_ABORT_UNLESS(lastIndexInfo.CheckCompatible(indexInfo));
67+
lastIndexInfo.CheckCompatible(indexInfo).Validate();
6668
switchOptimizer = !indexInfo.GetCompactionPlannerConstructor()->IsEqualTo(lastIndexInfo.GetCompactionPlannerConstructor());
6769
switchAccessorsManager = !indexInfo.GetMetadataManagerConstructor()->IsEqualTo(*lastIndexInfo.GetMetadataManagerConstructor());
6870
}

ydb/core/tx/columnshard/engines/scheme/index_info.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
namespace NKikimr::NOlap {
1818

19-
bool TIndexInfo::CheckCompatible(const TIndexInfo& other) const {
19+
TConclusionStatus TIndexInfo::CheckCompatible(const TIndexInfo& other) const {
2020
if (!other.GetPrimaryKey()->Equals(PrimaryKey)) {
21-
return false;
21+
return TConclusionStatus::Fail(
22+
TStringBuilder() << "PK mismatch: this=" << PrimaryKey->ToString() << " other=" << other.GetPrimaryKey()->ToString());
2223
}
23-
return true;
24+
return TConclusionStatus::Success();
2425
}
2526

2627
ui32 TIndexInfo::GetColumnIdVerified(const std::string& name) const {

ydb/core/tx/columnshard/engines/scheme/index_info.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ struct TIndexInfo: public IIndexInfo {
398398
return Version;
399399
}
400400

401-
bool CheckCompatible(const TIndexInfo& other) const;
401+
TConclusionStatus CheckCompatible(const TIndexInfo& other) const;
402402
NArrow::NSerialization::TSerializerContainer GetDefaultSerializer() const {
403403
return DefaultSerializer;
404404
}

ydb/core/tx/columnshard/engines/scheme/objects_cache.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,36 @@ class TSchemaObjectsCache {
7979

8080
class TSchemaCachesManager {
8181
private:
82-
THashMap<ui64, std::shared_ptr<TSchemaObjectsCache>> CacheByTableOwner;
83-
TMutex Mutex;
82+
class TColumnOwnerId {
83+
private:
84+
TPathId Tenant;
85+
TLocalPathId Owner;
86+
87+
public:
88+
TColumnOwnerId(const TPathId& tenant, const TLocalPathId owner)
89+
: Tenant(tenant)
90+
, Owner(owner) {
91+
AFL_VERIFY(!!Owner);
92+
}
8493

85-
std::shared_ptr<TSchemaObjectsCache> GetCacheImpl(const ui64 ownerPathId) {
86-
if (!ownerPathId) {
87-
return std::make_shared<TSchemaObjectsCache>();
94+
operator size_t() const {
95+
return CombineHashes(Owner, Tenant.Hash());
96+
}
97+
bool operator==(const TColumnOwnerId& other) const {
98+
return Tenant == other.Tenant && Owner == other.Owner;
8899
}
100+
};
101+
102+
THashMap<TColumnOwnerId, std::shared_ptr<TSchemaObjectsCache>> CacheByTableOwner;
103+
TMutex Mutex;
104+
105+
std::shared_ptr<TSchemaObjectsCache> GetCacheImpl(const TColumnOwnerId& owner) {
89106
TGuard lock(Mutex);
90-
auto findCache = CacheByTableOwner.FindPtr(ownerPathId);
107+
auto findCache = CacheByTableOwner.FindPtr(owner);
91108
if (findCache) {
92109
return *findCache;
93110
}
94-
return CacheByTableOwner.emplace(ownerPathId, std::make_shared<TSchemaObjectsCache>()).first->second;
111+
return CacheByTableOwner.emplace(owner, std::make_shared<TSchemaObjectsCache>()).first->second;
95112
}
96113

97114
void DropCachesImpl() {
@@ -100,8 +117,8 @@ class TSchemaCachesManager {
100117
}
101118

102119
public:
103-
static std::shared_ptr<TSchemaObjectsCache> GetCache(const ui64 ownerPathId) {
104-
return Singleton<TSchemaCachesManager>()->GetCacheImpl(ownerPathId);
120+
static std::shared_ptr<TSchemaObjectsCache> GetCache(const ui64 ownerPathId, const TPathId& tenantPathId) {
121+
return Singleton<TSchemaCachesManager>()->GetCacheImpl(TColumnOwnerId(tenantPathId, ownerPathId));
105122
}
106123

107124
static void DropCaches() {

ydb/core/tx/columnshard/loading/stages.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ bool TSpecialValuesInitializer::DoPrecharge(NTabletFlatExecutor::TTransactionCon
196196
bool TTablesManagerInitializer::DoExecute(NTabletFlatExecutor::TTransactionContext& txc, const TActorContext& /*ctx*/) {
197197
NIceDb::TNiceDb db(txc.DB);
198198
TTablesManager tablesManagerLocal(Self->StoragesManager, Self->DataAccessorsManager.GetObjectPtrVerified(),
199-
NOlap::TSchemaCachesManager::GetCache(Self->OwnerPathId), Self->Counters.GetPortionIndexCounters(), Self->TabletID());
199+
Self->OwnerPathId ? NOlap::TSchemaCachesManager::GetCache(Self->OwnerPathId, Self->Info()->TenantPathId)
200+
: std::shared_ptr<NOlap::TSchemaObjectsCache>(),
201+
Self->Counters.GetPortionIndexCounters(), Self->TabletID());
200202
{
201203
TMemoryProfileGuard g("TTxInit/TTablesManager");
202204
if (!tablesManagerLocal.InitFromDB(db)) {

ydb/core/tx/columnshard/tables_manager.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ bool TTablesManager::InitFromDB(NIceDb::TNiceDb& db) {
163163
"version", info.GetSchema().GetVersion());
164164
NOlap::IColumnEngine::TSchemaInitializationData schemaInitializationData(info);
165165
if (!PrimaryIndex) {
166-
PrimaryIndex = std::make_unique<NOlap::TColumnEngineForLogs>(TabletId, SchemaObjectsCache, DataAccessorsManager, StoragesManager,
167-
version, preset->Id, schemaInitializationData, PortionsStats);
166+
PrimaryIndex = std::make_unique<NOlap::TColumnEngineForLogs>(TabletId, SchemaObjectsCache.GetObjectPtrVerified(),
167+
DataAccessorsManager, StoragesManager, version, preset->Id, schemaInitializationData, PortionsStats);
168168
} else if (PrimaryIndex->GetVersionedIndex().IsEmpty() ||
169169
info.GetSchema().GetVersion() > PrimaryIndex->GetVersionedIndex().GetLastSchema()->GetVersion()) {
170170
PrimaryIndex->RegisterSchemaVersion(version, preset->Id, schemaInitializationData);
@@ -279,8 +279,8 @@ void TTablesManager::AddSchemaVersion(
279279
versionInfo.MutableSchema()->SetEngine(NKikimrSchemeOp::COLUMN_ENGINE_REPLACING_TIMESERIES);
280280
Schema::SaveSchemaPresetVersionInfo(db, presetId, version, versionInfo);
281281
if (!PrimaryIndex) {
282-
PrimaryIndex = std::make_unique<NOlap::TColumnEngineForLogs>(TabletId, SchemaObjectsCache, DataAccessorsManager, StoragesManager,
283-
version, presetId, NOlap::IColumnEngine::TSchemaInitializationData(versionInfo), PortionsStats);
282+
PrimaryIndex = std::make_unique<NOlap::TColumnEngineForLogs>(TabletId, SchemaObjectsCache.GetObjectPtrVerified(), DataAccessorsManager,
283+
StoragesManager, version, presetId, NOlap::IColumnEngine::TSchemaInitializationData(versionInfo), PortionsStats);
284284
for (auto&& i : Tables) {
285285
PrimaryIndex->RegisterTable(i.first);
286286
}
@@ -339,7 +339,6 @@ TTablesManager::TTablesManager(const std::shared_ptr<NOlap::IStoragesManager>& s
339339
, SchemaObjectsCache(schemaCache)
340340
, PortionsStats(portionsStats)
341341
, TabletId(tabletId) {
342-
AFL_VERIFY(SchemaObjectsCache);
343342
}
344343

345344
bool TTablesManager::TryFinalizeDropPathOnExecute(NTable::TDatabase& dbTable, const ui64 pathId) const {

ydb/core/tx/columnshard/tables_manager.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class TTablesManager {
198198
std::shared_ptr<NOlap::IStoragesManager> StoragesManager;
199199
std::shared_ptr<NOlap::NDataAccessorControl::IDataAccessorsManager> DataAccessorsManager;
200200
std::unique_ptr<TTableLoadTimeCounters> LoadTimeCounters;
201-
std::shared_ptr<NOlap::TSchemaObjectsCache> SchemaObjectsCache;
201+
NBackgroundTasks::TControlInterfaceContainer<NOlap::TSchemaObjectsCache> SchemaObjectsCache;
202202
std::shared_ptr<TPortionIndexStats> PortionsStats;
203203
ui64 TabletId = 0;
204204

@@ -330,6 +330,12 @@ class TTablesManager {
330330

331331
[[nodiscard]] std::unique_ptr<NTabletFlatExecutor::ITransaction> CreateAddShardingInfoTx(TColumnShard& owner, const ui64 pathId,
332332
const ui64 versionId, const NSharding::TGranuleShardingLogicContainer& tabletShardingLogic) const;
333+
334+
void SetSchemaObjectsCache(const std::shared_ptr<NOlap::TSchemaObjectsCache>& cache) {
335+
AFL_VERIFY(cache);
336+
AFL_VERIFY(!SchemaObjectsCache);
337+
SchemaObjectsCache = cache;
338+
}
333339
};
334340

335341
} // namespace NKikimr::NColumnShard

ydb/core/tx/columnshard/test_helper/columnshard_ut_common.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void TTester::Setup(TTestActorRuntime& runtime) {
2626
runtime.SetLogPriority(NKikimrServices::TX_COLUMNSHARD_SCAN, NActors::NLog::PRI_DEBUG);
2727
runtime.SetLogPriority(NKikimrServices::S3_WRAPPER, NLog::PRI_DEBUG);
2828

29+
NOlap::TSchemaCachesManager::DropCaches();
30+
2931
ui32 domainId = 0;
3032
ui32 planResolution = 500;
3133

@@ -449,7 +451,6 @@ namespace NKikimr::NColumnShard {
449451
}
450452

451453
void SetupSchema(TTestBasicRuntime& runtime, TActorId& sender, const TString& txBody, const NOlap::TSnapshot& snapshot, bool succeed) {
452-
453454
auto controller = NYDBTest::TControllers::GetControllerAs<NYDBTest::NColumnShard::TController>();
454455
while (controller && !controller->IsActiveTablet(TTestTxConfig::TxTablet0)) {
455456
runtime.SimulateSleep(TDuration::Seconds(1));
@@ -463,21 +464,19 @@ namespace NKikimr::NColumnShard {
463464
}
464465
}
465466

466-
void SetupSchema(TTestBasicRuntime& runtime, TActorId& sender, ui64 pathId,
467-
const TestTableDescription& table, TString codec) {
467+
void SetupSchema(TTestBasicRuntime& runtime, TActorId& sender, ui64 pathId, const TestTableDescription& table, TString codec) {
468468
using namespace NTxUT;
469469
NOlap::TSnapshot snapshot(10, 10);
470470
TString txBody;
471471
auto specials = TTestSchema::TTableSpecials().WithCodec(codec);
472472
if (table.InStore) {
473-
txBody = TTestSchema::CreateTableTxBody(pathId, table.Schema, table.Pk, specials);
473+
txBody = TTestSchema::CreateInitShardTxBody(pathId, table.Schema, table.Pk, specials);
474474
} else {
475475
txBody = TTestSchema::CreateStandaloneTableTxBody(pathId, table.Schema, table.Pk, specials);
476476
}
477477
SetupSchema(runtime, sender, txBody, snapshot, true);
478478
}
479479

480-
481480
void PrepareTablet(TTestBasicRuntime& runtime, const ui64 tableId, const std::vector<NArrow::NTest::TTestColumn>& schema, const ui32 keySize) {
482481
using namespace NTxUT;
483482
CreateTestBootstrapper(runtime, CreateTestTabletInfo(TTestTxConfig::TxTablet0, TTabletTypes::ColumnShard), &CreateColumnShard);

ydb/core/tx/columnshard/test_helper/columnshard_ut_common.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,18 @@ struct TTestSchema {
295295
NKikimrTxColumnShard::TSchemaTxBody tx;
296296
auto* table = tx.MutableInitShard()->AddTables();
297297
tx.MutableInitShard()->SetOwnerPath(ownerPath);
298+
tx.MutableInitShard()->SetOwnerPathId(pathId);
298299
table->SetPathId(pathId);
299300

300-
InitSchema(columns, pk, specials, table->MutableSchema());
301+
{ // preset
302+
auto* preset = table->MutableSchemaPreset();
303+
preset->SetId(1);
304+
preset->SetName("default");
305+
306+
// schema
307+
InitSchema(columns, pk, specials, preset->MutableSchema());
308+
}
309+
301310
InitTiersAndTtl(specials, table->MutableTtlSettings());
302311

303312
Cerr << "CreateInitShard: " << tx << "\n";
@@ -308,9 +317,11 @@ struct TTestSchema {
308317
}
309318

310319
static TString CreateStandaloneTableTxBody(ui64 pathId, const std::vector<NArrow::NTest::TTestColumn>& columns,
311-
const std::vector<NArrow::NTest::TTestColumn>& pk, const TTableSpecials& specials = {}) {
320+
const std::vector<NArrow::NTest::TTestColumn>& pk, const TTableSpecials& specials = {}, const TString& path = "/Root/olap") {
312321
NKikimrTxColumnShard::TSchemaTxBody tx;
313-
auto* table = tx.MutableEnsureTables()->AddTables();
322+
auto* table = tx.MutableInitShard()->AddTables();
323+
tx.MutableInitShard()->SetOwnerPath(path);
324+
tx.MutableInitShard()->SetOwnerPathId(pathId);
314325
table->SetPathId(pathId);
315326

316327
InitSchema(columns, pk, specials, table->MutableSchema());
@@ -323,12 +334,18 @@ struct TTestSchema {
323334
return out;
324335
}
325336

326-
static TString AlterTableTxBody(ui64 pathId, ui32 version, const TTableSpecials& specials) {
337+
static TString AlterTableTxBody(ui64 pathId, ui32 version, const std::vector<NArrow::NTest::TTestColumn>& columns,
338+
const std::vector<NArrow::NTest::TTestColumn>& pk, const TTableSpecials& specials) {
327339
NKikimrTxColumnShard::TSchemaTxBody tx;
328340
auto* table = tx.MutableAlterTable();
329341
table->SetPathId(pathId);
330342
tx.MutableSeqNo()->SetRound(version);
331343

344+
auto* preset = table->MutableSchemaPreset();
345+
preset->SetId(1);
346+
preset->SetName("default");
347+
InitSchema(columns, pk, specials, preset->MutableSchema());
348+
332349
auto* ttlSettings = table->MutableTtlSettings();
333350
if (!InitTiersAndTtl(specials, ttlSettings)) {
334351
ttlSettings->MutableDisabled();

0 commit comments

Comments
 (0)