Skip to content

Commit 9e31f54

Browse files
authored
Merge fee94af into c52270b
2 parents c52270b + fee94af commit 9e31f54

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
@@ -97,6 +97,9 @@ bool TSchemeModifier::Apply(const TAlterRecord &delta)
9797
ui32 large = delta.HasLarge() ? delta.GetLarge() : family.Large;
9898

9999
Y_ABORT_UNLESS(ui32(cache) <= 2, "Invalid pages cache policy value");
100+
if (family.Cache != cache && cache == ECache::Ever) {
101+
ChangeTableSetting(table, tableInfo.PendingCacheEnable, true);
102+
}
100103
changes |= ChangeTableSetting(table, family.Cache, cache);
101104
changes |= ChangeTableSetting(table, family.Codec, codec);
102105
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
@@ -85,6 +85,9 @@ class TScheme {
8585
bool EraseCacheEnabled = false;
8686
ui32 EraseCacheMinRows = 0; // 0 means use default
8787
ui32 EraseCacheMaxBytes = 0; // 0 means use default
88+
89+
// When true this table has an in-memory caching enabled that has not been processed yet
90+
mutable bool PendingCacheEnable = false;
8891
};
8992

9093
struct TRedo {

ydb/core/tablet_flat/flat_executor.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ void TExecutor::ActivateFollower(const TActorContext &ctx) {
408408
RecreatePageCollectionsCache();
409409
ReflectSchemeSettings();
410410

411+
RequestInMemPagesForDatabase();
412+
411413
Become(&TThis::StateFollower);
412414
Stats->IsActive = true;
413415
Stats->FollowerId = FollowerId;
@@ -647,16 +649,21 @@ void TExecutor::TranslateCacheTouchesToSharedCache() {
647649
Send(MakeSharedPageCacheId(), new NSharedCache::TEvTouch(std::move(touches)));
648650
}
649651

650-
void TExecutor::RequestInMemPagesForDatabase() {
651-
const auto &scheme = Scheme();
652-
for (auto &sxpair : scheme.Tables) {
653-
auto stickyColumns = GetStickyColumns(sxpair.first);
652+
void TExecutor::RequestInMemPagesForDatabase(bool pendingOnly) {
653+
const auto& scheme = Scheme();
654+
for (auto& pr : scheme.Tables) {
655+
const ui32 tid = pr.first;
656+
if (pendingOnly && !pr.second.PendingCacheEnable) {
657+
continue;
658+
}
659+
auto stickyColumns = GetStickyColumns(tid);
654660
if (stickyColumns) {
655-
auto subset = Database->Subset(sxpair.first, NTable::TEpoch::Max(), { } , { });
661+
auto subset = Database->Subset(tid, NTable::TEpoch::Max(), { } , { });
656662

657663
for (auto &partView: subset->Flatten)
658-
RequestInMemPagesForPartStore(sxpair.first, partView, stickyColumns);
664+
RequestInMemPagesForPartStore(tid, partView, stickyColumns);
659665
}
666+
pr.second.PendingCacheEnable = false;
660667
}
661668
}
662669

@@ -968,6 +975,7 @@ void TExecutor::ApplyFollowerUpdate(THolder<TEvTablet::TFUpdateBody> update) {
968975
if (schemeUpdate) {
969976
ReadResourceProfile();
970977
ReflectSchemeSettings();
978+
RequestInMemPagesForDatabase(/* pendingOnly */ true);
971979
Owner->OnFollowerSchemaUpdated();
972980
}
973981

@@ -1345,6 +1353,7 @@ void TExecutor::RequestInMemPagesForPartStore(ui32 tableId, const NTable::TPartV
13451353
for (ui32 pageId : req->Pages)
13461354
PrivatePageCache->MarkSticky(pageId, info);
13471355

1356+
// TODO: only request missing pages
13481357
RequestFromSharedCache(req, NBlockIO::EPriority::Bkgr, EPageCollectionRequest::CacheSync);
13491358
}
13501359
}
@@ -2065,6 +2074,7 @@ void TExecutor::CommitTransactionLog(TAutoPtr<TSeat> seat, TPageCollectionTxEnv
20652074

20662075
ReadResourceProfile();
20672076
ReflectSchemeSettings();
2077+
RequestInMemPagesForDatabase(/* pendingOnly */ true);
20682078

20692079
// For every table that changed strategy we need to generate a
20702080
// 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
@@ -513,7 +513,7 @@ class TExecutor
513513
void DropSingleCache(const TLogoBlobID&) noexcept;
514514

515515
void TranslateCacheTouchesToSharedCache();
516-
void RequestInMemPagesForDatabase();
516+
void RequestInMemPagesForDatabase(bool pendingOnly = false);
517517
void RequestInMemPagesForPartStore(ui32 tableId, const NTable::TPartView &partView, const THashSet<NTable::TTag> &stickyColumns);
518518
THashSet<NTable::TTag> GetStickyColumns(ui32 tableId);
519519
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
@@ -6140,7 +6140,7 @@ Y_UNIT_TEST_SUITE(TFlatTableExecutorStickyPages) {
61406140

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

61456145
// restart tablet
61466146
env.SendSync(new TEvents::TEvPoison, false, true);
@@ -6174,7 +6174,7 @@ Y_UNIT_TEST_SUITE(TFlatTableExecutorStickyPages) {
61746174

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

61796179
// restart tablet
61806180
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
@@ -183,13 +183,15 @@ namespace NKikimr {
183183
case Ydb::FeatureFlag::STATUS_UNSPECIFIED:
184184
break;
185185
case Ydb::FeatureFlag::ENABLED:
186-
*code = Ydb::StatusIds::BAD_REQUEST;
187-
*error = TStringBuilder()
188-
<< "Setting keep_in_memory to ENABLED is not supported in column family '"
189-
<< familySettings.name() << "'";
190-
return false;
186+
if (!AppData()->FeatureFlags.GetEnablePublicApiKeepInMemory()) {
187+
*code = Ydb::StatusIds::BAD_REQUEST;
188+
*error = "Setting keep_in_memory to ENABLED is not allowed";
189+
return false;
190+
}
191+
family->SetColumnCache(NKikimrSchemeOp::ColumnCacheEver);
192+
break;
191193
case Ydb::FeatureFlag::DISABLED:
192-
family->ClearColumnCache();
194+
family->SetColumnCache(NKikimrSchemeOp::ColumnCacheNone);
193195
break;
194196
default:
195197
*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
@@ -1115,6 +1115,11 @@ TColumnFamilyBuilder& TColumnFamilyBuilder::SetCompression(EColumnFamilyCompress
11151115
return *this;
11161116
}
11171117

1118+
TColumnFamilyBuilder& TColumnFamilyBuilder::SetKeepInMemory(bool enabled) {
1119+
Impl_->Proto.set_keep_in_memory(enabled ? Ydb::FeatureFlag::ENABLED : Ydb::FeatureFlag::DISABLED);
1120+
return *this;
1121+
}
1122+
11181123
TColumnFamilyDescription TColumnFamilyBuilder::Build() const {
11191124
return TColumnFamilyDescription(Impl_->Proto);
11201125
}

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

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

807807
TColumnFamilyBuilder& SetData(const TString& media);
808808
TColumnFamilyBuilder& SetCompression(EColumnFamilyCompression compression);
809+
TColumnFamilyBuilder& SetKeepInMemory(bool enabled);
809810

810811
TColumnFamilyDescription Build() const;
811812

@@ -868,6 +869,11 @@ class TTableColumnFamilyBuilder {
868869
return *this;
869870
}
870871

872+
TTableColumnFamilyBuilder& SetKeepInMemory(bool enabled) {
873+
Builder_.SetKeepInMemory(enabled);
874+
return *this;
875+
}
876+
871877
TTableBuilder& EndColumnFamily();
872878

873879
private:
@@ -1486,6 +1492,11 @@ class TAlterColumnFamilyBuilder {
14861492
return *this;
14871493
}
14881494

1495+
TAlterColumnFamilyBuilder& SetKeepInMemory(bool enabled) {
1496+
Builder_.SetKeepInMemory(enabled);
1497+
return *this;
1498+
}
1499+
14891500
TAlterTableSettings& EndAddColumnFamily();
14901501
TAlterTableSettings& EndAlterColumnFamily();
14911502

ydb/services/ydb/ydb_ut.cpp

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

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

0 commit comments

Comments
 (0)