Skip to content

Commit 40be5e9

Browse files
authored
delete query from compile cache if it is existed during insert (#8537)
1 parent 42c8024 commit 40be5e9

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

ydb/core/kqp/common/simple/query_id.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <google/protobuf/util/message_differencer.h>
55

66
#include <util/generic/yexception.h>
7+
#include <util/string/escape.h>
78

89
#include <memory>
910

@@ -74,4 +75,25 @@ bool TKqpQueryId::operator==(const TKqpQueryId& other) const {
7475
return true;
7576
}
7677

78+
TString TKqpQueryId::SerializeToString() const {
79+
TStringBuilder result = TStringBuilder() << "{"
80+
<< "Cluster: " << Cluster << ", "
81+
<< "Database: " << Database << ", "
82+
<< "UserSid: " << UserSid << ", "
83+
<< "Text: " << EscapeC(Text) << ", "
84+
<< "Settings: " << Settings.SerializeToString() << ", ";
85+
if (QueryParameterTypes) {
86+
result << "QueryParameterTypes: [";
87+
for (const auto& param : *QueryParameterTypes) {
88+
result << "name: " << param.first << ", type: " << param.second.ShortDebugString();
89+
}
90+
result << "], ";
91+
} else {
92+
result << "QueryParameterTypes: <empty>, ";
93+
}
94+
95+
result << "GUCSettings: " << GUCSettings.SerializeToString() << "}";
96+
return result;
97+
}
98+
7799
} // namespace NKikimr::NKqp

ydb/core/kqp/common/simple/query_id.h

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ struct TKqpQueryId {
4444
GUCSettings.GetHash());
4545
return THash<decltype(tuple)>()(tuple);
4646
}
47+
48+
TString SerializeToString() const;
4749
};
4850
} // namespace NKikimr::NKqp
4951

ydb/core/kqp/common/simple/settings.h

+10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <ydb/core/protos/kqp.pb.h>
44
#include <ydb/public/api/protos/ydb_query.pb.h>
55

6+
#include <util/generic/string.h>
67
#include <util/str_stl.h>
8+
#include <util/string/builder.h>
79

810
#include <tuple>
911

@@ -39,6 +41,14 @@ struct TKqpQuerySettings {
3941
auto tuple = std::make_tuple(DocumentApiRestricted, IsInternalCall, QueryType, Syntax);
4042
return THash<decltype(tuple)>()(tuple);
4143
}
44+
45+
TString SerializeToString() const {
46+
TStringBuilder result = TStringBuilder() << "{"
47+
<< "DocumentApiRestricted: " << DocumentApiRestricted << ", "
48+
<< "IsInternalCall: " << IsInternalCall << ", "
49+
<< "QueryType: " << QueryType << "}";
50+
return result;
51+
}
4252
};
4353

4454
} // namespace NKikimr::NKqp

ydb/core/kqp/compile_service/kqp_compile_service.cpp

+18-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class TKqpQueryCache {
4141
YQL_ENSURE(compileResult->PreparedQuery);
4242

4343
auto queryIt = QueryIndex.emplace(query, compileResult->Uid);
44+
if (!queryIt.second) {
45+
EraseByUid(compileResult->Uid);
46+
}
4447
Y_ENSURE(queryIt.second);
4548
}
4649

@@ -672,6 +675,8 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
672675
Y_ENSURE(query.UserSid == userSid);
673676
}
674677

678+
LOG_DEBUG_S(ctx, NKikimrServices::KQP_COMPILE_SERVICE, "Try to find query by queryId, queryId: " << query.SerializeToString());
679+
675680
auto compileResult = QueryCache.FindByQuery(query, request.KeepInCache);
676681
if (HasTempTablesNameClashes(compileResult, request.TempTablesState)) {
677682
compileResult = nullptr;
@@ -853,7 +858,7 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
853858
try {
854859
if (compileResult->Status == Ydb::StatusIds::SUCCESS) {
855860
if (!hasTempTablesNameClashes) {
856-
UpdateQueryCache(compileResult, keepInCache, compileRequest.CompileSettings.IsQueryActionPrepare, isPerStatementExecution);
861+
UpdateQueryCache(ctx, compileResult, keepInCache, compileRequest.CompileSettings.IsQueryActionPrepare, isPerStatementExecution);
857862
}
858863

859864
if (ev->Get()->ReplayMessage && !QueryReplayBackend->IsNull()) {
@@ -935,15 +940,21 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
935940
return compileResult->PreparedQuery->HasTempTables(tempTablesState, withSessionId);
936941
}
937942

938-
void UpdateQueryCache(TKqpCompileResult::TConstPtr compileResult, bool keepInCache, bool isQueryActionPrepare, bool isPerStatementExecution) {
943+
void UpdateQueryCache(const TActorContext& ctx, TKqpCompileResult::TConstPtr compileResult, bool keepInCache, bool isQueryActionPrepare, bool isPerStatementExecution) {
939944
if (QueryCache.FindByUid(compileResult->Uid, false)) {
940945
QueryCache.Replace(compileResult);
941946
} else if (keepInCache) {
947+
if (compileResult->Query) {
948+
LOG_DEBUG_S(ctx, NKikimrServices::KQP_COMPILE_SERVICE, "Insert query into compile cache, queryId: " << compileResult->Query->SerializeToString());
949+
if (QueryCache.FindByQuery(*compileResult->Query, keepInCache)) {
950+
LOG_ERROR_S(ctx, NKikimrServices::KQP_COMPILE_SERVICE, "Trying to insert query into compile cache when it is already there");
951+
}
952+
}
942953
if (QueryCache.Insert(compileResult, TableServiceConfig.GetEnableAstCache(), isPerStatementExecution)) {
943954
Counters->CompileQueryCacheEvicted->Inc();
944955
}
945956
if (compileResult->Query && isQueryActionPrepare) {
946-
if (InsertPreparingQuery(compileResult, true, isPerStatementExecution)) {
957+
if (InsertPreparingQuery(ctx, compileResult, true, isPerStatementExecution)) {
947958
Counters->CompileQueryCacheEvicted->Inc();
948959
};
949960
}
@@ -954,6 +965,8 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
954965
YQL_ENSURE(queryAst.Ast);
955966
YQL_ENSURE(queryAst.Ast->IsOk());
956967
YQL_ENSURE(queryAst.Ast->Root);
968+
LOG_DEBUG_S(ctx, NKikimrServices::KQP_COMPILE_SERVICE, "Try to find query by ast, queryId: " << compileRequest.Query.SerializeToString()
969+
<< ", ast: " << queryAst.Ast->Root->ToString());
957970
auto compileResult = QueryCache.FindByAst(compileRequest.Query, *queryAst.Ast, compileRequest.CompileSettings.KeepInCache);
958971

959972
if (HasTempTablesNameClashes(compileResult, compileRequest.TempTablesState)) {
@@ -1022,7 +1035,7 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
10221035
}
10231036

10241037
private:
1025-
bool InsertPreparingQuery(const TKqpCompileResult::TConstPtr& compileResult, bool keepInCache, bool isPerStatementExecution) {
1038+
bool InsertPreparingQuery(const TActorContext& ctx, const TKqpCompileResult::TConstPtr& compileResult, bool keepInCache, bool isPerStatementExecution) {
10261039
YQL_ENSURE(compileResult->Query);
10271040
auto query = *compileResult->Query;
10281041

@@ -1047,6 +1060,7 @@ class TKqpCompileService : public TActorBootstrapped<TKqpCompileService> {
10471060
auto newCompileResult = TKqpCompileResult::Make(CreateGuidAsString(), compileResult->Status, compileResult->Issues, compileResult->MaxReadType, std::move(query), compileResult->Ast);
10481061
newCompileResult->AllowCache = compileResult->AllowCache;
10491062
newCompileResult->PreparedQuery = compileResult->PreparedQuery;
1063+
LOG_DEBUG_S(ctx, NKikimrServices::KQP_COMPILE_SERVICE, "Insert preparing query with params, queryId: " << query.SerializeToString());
10501064
return QueryCache.Insert(newCompileResult, TableServiceConfig.GetEnableAstCache(), isPerStatementExecution);
10511065
}
10521066

0 commit comments

Comments
 (0)