diff options
author | spuchin <spuchin@ydb.tech> | 2023-08-07 14:45:36 +0300 |
---|---|---|
committer | spuchin <spuchin@ydb.tech> | 2023-08-07 15:47:11 +0300 |
commit | 9b54275e5faba25458f3fe5c39b07b9ff3fc0fde (patch) | |
tree | 343d8fe9f71af2b38f0e7eae952590ccda12d826 | |
parent | 7640a0d4ec7e1d4c9a4e18a5881629d65fd21c4d (diff) | |
download | ydb-9b54275e5faba25458f3fe5c39b07b9ff3fc0fde.tar.gz |
Restrict query cache for scheme operations. (KIKIMR-18956)
-rw-r--r-- | ydb/core/grpc_services/query/rpc_execute_query.cpp | 6 | ||||
-rw-r--r-- | ydb/core/kqp/common/compilation/result.h | 1 | ||||
-rw-r--r-- | ydb/core/kqp/compile_service/kqp_compile_actor.cpp | 1 | ||||
-rw-r--r-- | ydb/core/kqp/compile_service/kqp_compile_service.cpp | 4 | ||||
-rw-r--r-- | ydb/core/kqp/session_actor/kqp_worker_common.cpp | 10 | ||||
-rw-r--r-- | ydb/core/kqp/session_actor/kqp_worker_common.h | 2 | ||||
-rw-r--r-- | ydb/core/kqp/ut/service/kqp_query_service_ut.cpp | 48 |
7 files changed, 70 insertions, 2 deletions
diff --git a/ydb/core/grpc_services/query/rpc_execute_query.cpp b/ydb/core/grpc_services/query/rpc_execute_query.cpp index 2a7b1818bf5..9dbe4f9aab1 100644 --- a/ydb/core/grpc_services/query/rpc_execute_query.cpp +++ b/ydb/core/grpc_services/query/rpc_execute_query.cpp @@ -246,6 +246,10 @@ private: ? NKikimrKqp::QUERY_TYPE_SQL_GENERIC_CONCURRENT_QUERY : NKikimrKqp::QUERY_TYPE_SQL_GENERIC_QUERY; + + auto cachePolicy = google::protobuf::Arena::CreateMessage<Ydb::Table::QueryCachePolicy>(Request_->GetArena()); + cachePolicy->set_keep_in_cache(true); + auto ev = MakeHolder<NKqp::TEvKqp::TEvQueryRequest>( queryAction, queryType, @@ -257,7 +261,7 @@ private: txControl, &req->parameters(), GetCollectStatsMode(req->stats_mode()), - nullptr, // queryCachePolicy + cachePolicy, nullptr, // operationParams false, // keepSession false, // useCancelAfter diff --git a/ydb/core/kqp/common/compilation/result.h b/ydb/core/kqp/common/compilation/result.h index ea26f9ff23e..b9178a01bc6 100644 --- a/ydb/core/kqp/common/compilation/result.h +++ b/ydb/core/kqp/common/compilation/result.h @@ -45,6 +45,7 @@ struct TKqpCompileResult { TString Uid; ETableReadType MaxReadType; + bool AllowCache = true; std::shared_ptr<const TPreparedQueryHolder> PreparedQuery; }; diff --git a/ydb/core/kqp/compile_service/kqp_compile_actor.cpp b/ydb/core/kqp/compile_service/kqp_compile_actor.cpp index cd5288ef90f..d3f00531968 100644 --- a/ydb/core/kqp/compile_service/kqp_compile_actor.cpp +++ b/ydb/core/kqp/compile_service/kqp_compile_actor.cpp @@ -332,6 +332,7 @@ private: kqpResult.PreparingQuery.release(), AppData()->FunctionRegistry); preparedQueryHolder->MutableLlvmSettings().Fill(Config, queryType); KqpCompileResult->PreparedQuery = preparedQueryHolder; + KqpCompileResult->AllowCache = CanCacheQuery(KqpCompileResult->PreparedQuery->GetPhysicalQuery()); } auto now = TInstant::Now(); diff --git a/ydb/core/kqp/compile_service/kqp_compile_service.cpp b/ydb/core/kqp/compile_service/kqp_compile_service.cpp index c86c9d3fc33..77bbadbe2c4 100644 --- a/ydb/core/kqp/compile_service/kqp_compile_service.cpp +++ b/ydb/core/kqp/compile_service/kqp_compile_service.cpp @@ -630,11 +630,13 @@ private: << ", status: " << compileResult->Status << ", compileActor: " << ev->Sender); + bool keepInCache = compileRequest.KeepInCache && compileResult->AllowCache; + try { if (compileResult->Status == Ydb::StatusIds::SUCCESS) { if (QueryCache.FindByUid(compileResult->Uid, false)) { QueryCache.Replace(compileResult); - } else if (compileRequest.KeepInCache) { + } else if (keepInCache) { if (QueryCache.Insert(compileResult)) { Counters->CompileQueryCacheEvicted->Inc(); } diff --git a/ydb/core/kqp/session_actor/kqp_worker_common.cpp b/ydb/core/kqp/session_actor/kqp_worker_common.cpp index b57d707f13d..ed0dcaaa078 100644 --- a/ydb/core/kqp/session_actor/kqp_worker_common.cpp +++ b/ydb/core/kqp/session_actor/kqp_worker_common.cpp @@ -173,4 +173,14 @@ bool IsSameProtoType(const NKikimrMiniKQL::TType& actual, const NKikimrMiniKQL:: } } +bool CanCacheQuery(const NKqpProto::TKqpPhyQuery& query) { + for (const auto& tx : query.GetTransactions()) { + if (tx.GetType() == NKqpProto::TKqpPhyTx::TYPE_SCHEME) { + return false; + } + } + + return true; +} + } // namespace NKikimr::NKqp diff --git a/ydb/core/kqp/session_actor/kqp_worker_common.h b/ydb/core/kqp/session_actor/kqp_worker_common.h index 5edb413c5ae..ad32008494f 100644 --- a/ydb/core/kqp/session_actor/kqp_worker_common.h +++ b/ydb/core/kqp/session_actor/kqp_worker_common.h @@ -127,6 +127,8 @@ inline ETableReadType ExtractMostHeavyReadType(const TString& queryPlan) { return maxReadType; } +bool CanCacheQuery(const NKqpProto::TKqpPhyQuery& query); + void SlowLogQuery(const TActorContext &ctx, const NYql::TKikimrConfiguration* config, const TKqpRequestInfo& requestInfo, const TDuration& duration, Ydb::StatusIds::StatusCode status, const TIntrusiveConstPtr<NACLib::TUserToken>& userToken, ui64 parametersSize, NKikimrKqp::TEvQueryResponse *record, const std::function<TString()> extractQueryText); diff --git a/ydb/core/kqp/ut/service/kqp_query_service_ut.cpp b/ydb/core/kqp/ut/service/kqp_query_service_ut.cpp index 72f7828cbc8..21d4b697352 100644 --- a/ydb/core/kqp/ut/service/kqp_query_service_ut.cpp +++ b/ydb/core/kqp/ut/service/kqp_query_service_ut.cpp @@ -392,6 +392,54 @@ Y_UNIT_TEST_SUITE(KqpQueryService) { CompareYson(R"([[[1u];["One"]]])", FormatResultSetYson(result.GetResultSet(0))); } + Y_UNIT_TEST(QueryDdlCache) { + NKikimrConfig::TAppConfig appConfig; + appConfig.MutableTableServiceConfig()->SetEnablePreparedDdl(true); + auto setting = NKikimrKqp::TKqpSetting(); + auto serverSettings = TKikimrSettings() + .SetAppConfig(appConfig) + .SetKqpSettings({setting}); + + TKikimrRunner kikimr(serverSettings); + auto db = kikimr.GetQueryClient(); + + auto settings = TExecuteQuerySettings() + .StatsMode(EStatsMode::Basic); + + auto result = db.ExecuteQuery(R"( + CREATE TABLE TestDdl ( + Key Uint64, + Value String, + PRIMARY KEY (Key) + ); + )", TTxControl::NoTx(), settings).ExtractValueSync(); + UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString()); + + auto stats = NYdb::TProtoAccessor::GetProto(*result.GetStats()); + UNIT_ASSERT_VALUES_EQUAL(stats.compilation().from_cache(), false); + + { + // TODO: Switch to query service. + auto session = kikimr.GetTableClient().CreateSession().GetValueSync().GetSession(); + + UNIT_ASSERT(session.ExecuteSchemeQuery(R"( + DROP TABLE TestDdl; + )").GetValueSync().IsSuccess()); + } + + result = db.ExecuteQuery(R"( + CREATE TABLE TestDdl ( + Key Uint64, + Value String, + PRIMARY KEY (Key) + ); + )", TTxControl::NoTx(), settings).ExtractValueSync(); + UNIT_ASSERT_VALUES_EQUAL_C(result.GetStatus(), EStatus::SUCCESS, result.GetIssues().ToString()); + + stats = NYdb::TProtoAccessor::GetProto(*result.GetStats()); + UNIT_ASSERT_VALUES_EQUAL(stats.compilation().from_cache(), false); + } + Y_UNIT_TEST(MaterializeTxResults) { auto kikimr = DefaultKikimrRunner(); auto db = kikimr.GetQueryClient(); |