Skip to content

Commit 985ff54

Browse files
authored
24-3: Make it possible to change in-memory setting for tables (#12139)
1 parent c3d51bf commit 985ff54

File tree

9 files changed

+130
-15
lines changed

9 files changed

+130
-15
lines changed

ydb/core/tablet_flat/flat_dbase_apply.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ bool TSchemeModifier::Apply(const TAlterRecord &delta)
9393
ui32 large = delta.HasLarge() ? delta.GetLarge() : family.Large;
9494

9595
Y_ABORT_UNLESS(ui32(cache) <= 2, "Invalid pages cache policy value");
96+
if (family.Cache != cache && cache == ECache::Ever) {
97+
ChangeTableSetting(table, tableInfo.PendingCacheEnable, true);
98+
}
9699
changes |= ChangeTableSetting(table, family.Cache, cache);
97100
changes |= ChangeTableSetting(table, family.Codec, codec);
98101
changes |= ChangeTableSetting(table, family.Small, small);

ydb/core/tablet_flat/flat_dbase_scheme.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class TScheme {
103103
bool EraseCacheEnabled = false;
104104
ui32 EraseCacheMinRows = 0; // 0 means use default
105105
ui32 EraseCacheMaxBytes = 0; // 0 means use default
106+
107+
// When true this table has an in-memory caching enabled that has not been processed yet
108+
mutable bool PendingCacheEnable = false;
106109
};
107110

108111
struct TRedo {

ydb/core/tablet_flat/flat_executor.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ void TExecutor::ActivateFollower(const TActorContext &ctx) {
414414
RecreatePageCollectionsCache();
415415
ReflectSchemeSettings();
416416

417+
RequestInMemPagesForDatabase();
418+
417419
Become(&TThis::StateFollower);
418420
Stats->IsActive = true;
419421
Stats->IsFollower = true;
@@ -663,16 +665,21 @@ void TExecutor::TranslateCacheTouchesToSharedCache() {
663665
Send(MakeSharedPageCacheId(), new NSharedCache::TEvTouch(std::move(touches)));
664666
}
665667

666-
void TExecutor::RequestInMemPagesForDatabase() {
667-
const auto &scheme = Scheme();
668-
for (auto &sxpair : scheme.Tables) {
669-
auto stickyColumns = GetStickyColumns(sxpair.first);
668+
void TExecutor::RequestInMemPagesForDatabase(bool pendingOnly) {
669+
const auto& scheme = Scheme();
670+
for (auto& pr : scheme.Tables) {
671+
const ui32 tid = pr.first;
672+
if (pendingOnly && !pr.second.PendingCacheEnable) {
673+
continue;
674+
}
675+
auto stickyColumns = GetStickyColumns(tid);
670676
if (stickyColumns) {
671-
auto subset = Database->Subset(sxpair.first, NTable::TEpoch::Max(), { } , { });
677+
auto subset = Database->Subset(tid, NTable::TEpoch::Max(), { } , { });
672678

673679
for (auto &partView: subset->Flatten)
674-
RequestInMemPagesForPartStore(sxpair.first, partView, stickyColumns);
680+
RequestInMemPagesForPartStore(tid, partView, stickyColumns);
675681
}
682+
pr.second.PendingCacheEnable = false;
676683
}
677684
}
678685

@@ -983,6 +990,7 @@ void TExecutor::ApplyFollowerUpdate(THolder<TEvTablet::TFUpdateBody> update) {
983990
if (schemeUpdate) {
984991
ReadResourceProfile();
985992
ReflectSchemeSettings();
993+
RequestInMemPagesForDatabase(/* pendingOnly */ true);
986994
Owner->OnFollowerSchemaUpdated();
987995
}
988996

@@ -1360,6 +1368,7 @@ void TExecutor::RequestInMemPagesForPartStore(ui32 tableId, const NTable::TPartV
13601368
for (ui32 pageId : req->Pages)
13611369
PrivatePageCache->MarkSticky(pageId, info);
13621370

1371+
// TODO: only request missing pages
13631372
RequestFromSharedCache(req, NBlockIO::EPriority::Bkgr, EPageCollectionRequest::CacheSync);
13641373
}
13651374
}
@@ -2080,6 +2089,7 @@ void TExecutor::CommitTransactionLog(TAutoPtr<TSeat> seat, TPageCollectionTxEnv
20802089

20812090
ReadResourceProfile();
20822091
ReflectSchemeSettings();
2092+
RequestInMemPagesForDatabase(/* pendingOnly */ true);
20832093

20842094
// For every table that changed strategy we need to generate a
20852095
// special part switch that notifies bootlogic about new strategy

ydb/core/tablet_flat/flat_executor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ class TExecutor
528528
void DropSingleCache(const TLogoBlobID&) noexcept;
529529

530530
void TranslateCacheTouchesToSharedCache();
531-
void RequestInMemPagesForDatabase();
531+
void RequestInMemPagesForDatabase(bool pendingOnly = false);
532532
void RequestInMemPagesForPartStore(ui32 tableId, const NTable::TPartView &partView, const THashSet<NTable::TTag> &stickyColumns);
533533
THashSet<NTable::TTag> GetStickyColumns(ui32 tableId);
534534
void RequestFromSharedCache(TAutoPtr<NPageCollection::TFetch> fetch,

ydb/core/tablet_flat/flat_executor_ut.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6142,7 +6142,7 @@ Y_UNIT_TEST_SUITE(TFlatTableExecutorStickyPages) {
61426142

61436143
int failedAttempts = 0;
61446144
env.SendSync(new NFake::TEvExecute{ new TTxFullScan(failedAttempts) });
6145-
UNIT_ASSERT_GE(failedAttempts, 20); // old parts aren't sticky before restart
6145+
UNIT_ASSERT_VALUES_EQUAL(failedAttempts, 0); // parts become sticky soon after it's enabled
61466146

61476147
// restart tablet
61486148
env.SendSync(new TEvents::TEvPoison, false, true);
@@ -6176,7 +6176,7 @@ Y_UNIT_TEST_SUITE(TFlatTableExecutorStickyPages) {
61766176

61776177
int failedAttempts = 0;
61786178
env.SendSync(new NFake::TEvExecute{ new TTxFullScan(failedAttempts) });
6179-
UNIT_ASSERT_GE(failedAttempts, 20); // old parts aren't sticky before restart
6179+
UNIT_ASSERT_VALUES_EQUAL(failedAttempts, 0); // parts become sticky soon after it's enabled
61806180

61816181
// restart tablet
61826182
env.SendSync(new TEvents::TEvPoison, false, true);

ydb/core/ydb_convert/column_families.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ namespace NKikimr {
177177
case Ydb::FeatureFlag::STATUS_UNSPECIFIED:
178178
break;
179179
case Ydb::FeatureFlag::ENABLED:
180-
*code = Ydb::StatusIds::BAD_REQUEST;
181-
*error = TStringBuilder()
182-
<< "Setting keep_in_memory to ENABLED is not supported in column family '"
183-
<< familySettings.name() << "'";
184-
return false;
180+
if (!AppData()->FeatureFlags.GetEnablePublicApiKeepInMemory()) {
181+
*code = Ydb::StatusIds::BAD_REQUEST;
182+
*error = "Setting keep_in_memory to ENABLED is not allowed";
183+
return false;
184+
}
185+
family->SetColumnCache(NKikimrSchemeOp::ColumnCacheEver);
186+
break;
185187
case Ydb::FeatureFlag::DISABLED:
186-
family->ClearColumnCache();
188+
family->SetColumnCache(NKikimrSchemeOp::ColumnCacheNone);
187189
break;
188190
default:
189191
*code = Ydb::StatusIds::BAD_REQUEST;

ydb/public/sdk/cpp/client/ydb_table/table.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,11 @@ TColumnFamilyBuilder& TColumnFamilyBuilder::SetCompression(EColumnFamilyCompress
11111111
return *this;
11121112
}
11131113

1114+
TColumnFamilyBuilder& TColumnFamilyBuilder::SetKeepInMemory(bool enabled) {
1115+
Impl_->Proto.set_keep_in_memory(enabled ? Ydb::FeatureFlag::ENABLED : Ydb::FeatureFlag::DISABLED);
1116+
return *this;
1117+
}
1118+
11141119
TColumnFamilyDescription TColumnFamilyBuilder::Build() const {
11151120
return TColumnFamilyDescription(Impl_->Proto);
11161121
}

ydb/public/sdk/cpp/client/ydb_table/table.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ class TColumnFamilyBuilder {
689689

690690
TColumnFamilyBuilder& SetData(const TString& media);
691691
TColumnFamilyBuilder& SetCompression(EColumnFamilyCompression compression);
692+
TColumnFamilyBuilder& SetKeepInMemory(bool enabled);
692693

693694
TColumnFamilyDescription Build() const;
694695

@@ -751,6 +752,11 @@ class TTableColumnFamilyBuilder {
751752
return *this;
752753
}
753754

755+
TTableColumnFamilyBuilder& SetKeepInMemory(bool enabled) {
756+
Builder_.SetKeepInMemory(enabled);
757+
return *this;
758+
}
759+
754760
TTableBuilder& EndColumnFamily();
755761

756762
private:
@@ -1385,6 +1391,11 @@ class TAlterColumnFamilyBuilder {
13851391
return *this;
13861392
}
13871393

1394+
TAlterColumnFamilyBuilder& SetKeepInMemory(bool enabled) {
1395+
Builder_.SetKeepInMemory(enabled);
1396+
return *this;
1397+
}
1398+
13881399
TAlterTableSettings& EndAddColumnFamily();
13891400
TAlterTableSettings& EndAlterColumnFamily();
13901401

ydb/services/ydb/ydb_ut.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,87 @@ Y_UNIT_TEST_SUITE(TGRpcNewClient) {
822822
client.CreateSession().Apply(createSessionHandler).Wait();
823823
UNIT_ASSERT(done);
824824
}
825+
826+
Y_UNIT_TEST(InMemoryTables) {
827+
TKikimrWithGrpcAndRootSchemaNoSystemViews server;
828+
server.Server_->GetRuntime()->GetAppData().FeatureFlags.SetEnablePublicApiKeepInMemory(true);
829+
830+
ui16 grpc = server.GetPort();
831+
TString location = TStringBuilder() << "localhost:" << grpc;
832+
833+
auto connection = NYdb::TDriver(
834+
TDriverConfig()
835+
.SetEndpoint(location));
836+
837+
auto client = NYdb::NTable::TTableClient(connection);
838+
auto createSessionResult = client.CreateSession().ExtractValueSync();
839+
UNIT_ASSERT(!createSessionResult.IsTransportError());
840+
auto session = createSessionResult.GetSession();
841+
842+
auto createTableResult = session.CreateTable("/Root/Table", client.GetTableBuilder()
843+
.AddNullableColumn("Key", EPrimitiveType::Int32)
844+
.AddNullableColumn("Value", EPrimitiveType::String)
845+
.SetPrimaryKeyColumn("Key")
846+
// Note: only needed because this test doesn't initial table profiles
847+
.BeginStorageSettings()
848+
.SetTabletCommitLog0("ssd")
849+
.SetTabletCommitLog1("ssd")
850+
.EndStorageSettings()
851+
.BeginColumnFamily("default")
852+
.SetData("ssd")
853+
.SetKeepInMemory(true)
854+
.EndColumnFamily()
855+
.Build()).ExtractValueSync();
856+
UNIT_ASSERT_C(createTableResult.IsSuccess(), (NYdb::TStatus&)createTableResult);
857+
858+
{
859+
auto describeTableResult = session.DescribeTable("/Root/Table").ExtractValueSync();
860+
UNIT_ASSERT_C(describeTableResult.IsSuccess(), (NYdb::TStatus&)describeTableResult);
861+
auto desc = describeTableResult.GetTableDescription();
862+
auto families = desc.GetColumnFamilies();
863+
UNIT_ASSERT_VALUES_EQUAL(families.size(), 1u);
864+
auto family = families.at(0);
865+
UNIT_ASSERT_VALUES_EQUAL(family.GetKeepInMemory(), true);
866+
}
867+
868+
{
869+
auto alterTableResult = session.AlterTable("/Root/Table", NYdb::NTable::TAlterTableSettings()
870+
.BeginAlterColumnFamily("default")
871+
.SetKeepInMemory(false)
872+
.EndAlterColumnFamily()).ExtractValueSync();
873+
UNIT_ASSERT_C(alterTableResult.IsSuccess(), (NYdb::TStatus&)alterTableResult);
874+
}
875+
876+
{
877+
auto describeTableResult = session.DescribeTable("/Root/Table").ExtractValueSync();
878+
UNIT_ASSERT_C(describeTableResult.IsSuccess(), (NYdb::TStatus&)describeTableResult);
879+
auto desc = describeTableResult.GetTableDescription();
880+
auto families = desc.GetColumnFamilies();
881+
UNIT_ASSERT_VALUES_EQUAL(families.size(), 1u);
882+
auto family = families.at(0);
883+
// Note: server cannot currently distinguish between implicitly
884+
// unset and explicitly disabled, so it returns the former.
885+
UNIT_ASSERT_VALUES_EQUAL(family.GetKeepInMemory(), Nothing());
886+
}
887+
888+
{
889+
auto alterTableResult = session.AlterTable("/Root/Table", NYdb::NTable::TAlterTableSettings()
890+
.BeginAlterColumnFamily("default")
891+
.SetKeepInMemory(true)
892+
.EndAlterColumnFamily()).ExtractValueSync();
893+
UNIT_ASSERT_C(alterTableResult.IsSuccess(), (NYdb::TStatus&)alterTableResult);
894+
}
895+
896+
{
897+
auto describeTableResult = session.DescribeTable("/Root/Table").ExtractValueSync();
898+
UNIT_ASSERT_C(describeTableResult.IsSuccess(), (NYdb::TStatus&)describeTableResult);
899+
auto desc = describeTableResult.GetTableDescription();
900+
auto families = desc.GetColumnFamilies();
901+
UNIT_ASSERT_VALUES_EQUAL(families.size(), 1u);
902+
auto family = families.at(0);
903+
UNIT_ASSERT_VALUES_EQUAL(family.GetKeepInMemory(), true);
904+
}
905+
}
825906
}
826907

827908
static TString CreateSession(std::shared_ptr<grpc::Channel> channel) {

0 commit comments

Comments
 (0)