1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#pragma once
#include "defs.h"
#include "rpc_deferrable.h"
#include <ydb/core/base/kikimr_issue.h>
#include <ydb/core/cms/console/configs_dispatcher.h>
#include <ydb/core/kqp/common/kqp.h>
#include <ydb/core/ydb_convert/ydb_convert.h>
#include <ydb/public/lib/operation_id/operation_id.h>
#include <ydb/public/sdk/cpp/client/resources/ydb_resources.h>
namespace NKikimr {
namespace NGRpcService {
inline void SetAuthToken(NKikimrKqp::TEvQueryRequest& req, const IRequestCtxMtSafe& ctx) {
if (ctx.GetInternalToken()) {
req.SetUserToken(ctx.GetInternalToken());
}
}
inline void SetDatabase(NKikimrKqp::TEvQueryRequest& req, const IRequestCtxMtSafe& ctx) {
// Empty database in case of absent header
req.MutableRequest()->SetDatabase(CanonizePath(ctx.GetDatabaseName().GetOrElse("")));
}
inline TString DecodePreparedQueryId(const TString& in) {
if (in.empty()) {
throw NYql::TErrorException(NKikimrIssues::TIssuesIds::DEFAULT_ERROR)
<< "got empty preparedQueryId message";
}
NOperationId::TOperationId opId(in);
const auto& ids = opId.GetValue("id");
if (ids.size() != 1) {
throw NYql::TErrorException(NKikimrIssues::TIssuesIds::DEFAULT_ERROR)
<< "expected exactly one preparedQueryId identifier";
}
return *ids[0];
}
inline TString GetTransactionModeName(const Ydb::Table::TransactionSettings& settings) {
switch (settings.tx_mode_case()) {
case Ydb::Table::TransactionSettings::kSerializableReadWrite:
return "SerializableReadWrite";
case Ydb::Table::TransactionSettings::kOnlineReadOnly:
return "OnlineReadOnly";
case Ydb::Table::TransactionSettings::kStaleReadOnly:
return "StaleReadOnly";
case Ydb::Table::TransactionSettings::kSnapshotReadOnly:
return "SnapshotReadOnly";
default:
return "Unknown";
}
}
inline NYql::NDqProto::EDqStatsMode GetKqpStatsMode(Ydb::Table::QueryStatsCollection::Mode mode) {
switch (mode) {
case Ydb::Table::QueryStatsCollection::STATS_COLLECTION_BASIC:
return NYql::NDqProto::DQ_STATS_MODE_BASIC;
case Ydb::Table::QueryStatsCollection::STATS_COLLECTION_FULL:
return NYql::NDqProto::DQ_STATS_MODE_PROFILE;
default:
return NYql::NDqProto::DQ_STATS_MODE_NONE;
}
}
inline bool CheckSession(const TString& sessionId, NYql::TIssues& issues) {
if (sessionId.empty()) {
issues.AddIssue(MakeIssue(NKikimrIssues::TIssuesIds::DEFAULT_ERROR, "Empty session id"));
return false;
}
return true;
}
inline bool CheckQuery(const TString& query, NYql::TIssues& issues) {
if (query.empty()) {
issues.AddIssue(MakeIssue(NKikimrIssues::TIssuesIds::DEFAULT_ERROR, "Empty query text"));
return false;
}
return true;
}
void FillQueryStats(Ydb::TableStats::QueryStats& queryStats, const NKikimrKqp::TQueryResponse& kqpResponse);
template <typename TDerived, typename TRequest>
class TRpcKqpRequestActor : public TRpcOperationRequestActor<TDerived, TRequest> {
using TBase = TRpcOperationRequestActor<TDerived, TRequest>;
public:
TRpcKqpRequestActor(IRequestOpCtx* request)
: TBase(request) {}
void OnOperationTimeout(const TActorContext& ctx) {
Y_UNUSED(ctx);
}
protected:
void StateWork(TAutoPtr<IEventHandle>& ev, const TActorContext& ctx) {
switch (ev->GetTypeRewrite()) {
HFunc(NKqp::TEvKqp::TEvProcessResponse, Handle);
default: TBase::StateFuncBase(ev, ctx);
}
}
template<typename TKqpResponse>
void AddServerHintsIfAny(const TKqpResponse& kqpResponse) {
if (kqpResponse.GetWorkerIsClosing()) {
this->Request_->AddServerHint(TString(NYdb::YDB_SESSION_CLOSE));
}
}
template<typename TKqpResponse>
void OnGenericQueryResponseError(const TKqpResponse& kqpResponse, const TActorContext& ctx) {
RaiseIssuesFromKqp(kqpResponse);
this->Request_->ReplyWithYdbStatus(kqpResponse.GetYdbStatus());
this->Die(ctx);
}
template<typename TKqpResponse>
void OnQueryResponseErrorWithTxMeta(const TKqpResponse& kqpResponse, const TActorContext& ctx) {
RaiseIssuesFromKqp(kqpResponse);
auto queryResult = TRequest::template AllocateResult<typename TDerived::TResult>(this->Request_);
if (kqpResponse.GetResponse().HasTxMeta()) {
queryResult->mutable_tx_meta()->CopyFrom(kqpResponse.GetResponse().GetTxMeta());
}
this->Request_->SendResult(*queryResult, kqpResponse.GetYdbStatus());
this->Die(ctx);
}
void OnQueryResponseError(const NKikimrKqp::TEvCreateSessionResponse& kqpResponse, const TActorContext& ctx) {
if (kqpResponse.HasError()) {
NYql::TIssues issues;
issues.AddIssue(MakeIssue(NKikimrIssues::TIssuesIds::DEFAULT_ERROR, kqpResponse.GetError()));
return this->Reply(kqpResponse.GetYdbStatus(), issues, ctx);
} else {
return this->Reply(kqpResponse.GetYdbStatus(), ctx);
}
}
template<typename TKqpResponse>
void OnKqpError(const TKqpResponse& response, const TActorContext& ctx) {
NYql::TIssues issues;
NYql::IssuesFromMessage(response.GetIssues(), issues);
this->Request_->RaiseIssues(issues);
this->Request_->ReplyWithYdbStatus(response.GetStatus());
this->Die(ctx);
}
void OnProcessError(const NKikimrKqp::TEvProcessResponse& kqpResponse, const TActorContext& ctx) {
if (kqpResponse.HasError()) {
NYql::TIssues issues;
issues.AddIssue(MakeIssue(NKikimrIssues::TIssuesIds::DEFAULT_ERROR, kqpResponse.GetError()));
return this->Reply(kqpResponse.GetYdbStatus(), issues, ctx);
} else {
return this->Reply(kqpResponse.GetYdbStatus(), ctx);
}
}
private:
void Handle(NKqp::TEvKqp::TEvProcessResponse::TPtr& ev, const TActorContext& ctx) {
auto& record = ev->Get()->Record;
NYql::TIssues issues;
if (record.HasError()) {
issues.AddIssue(MakeIssue(NKikimrIssues::TIssuesIds::DEFAULT_ERROR, record.GetError()));
}
return this->Reply(record.GetYdbStatus(), issues, ctx);
}
private:
template<typename TKqpResponse>
void RaiseIssuesFromKqp(const TKqpResponse& kqpResponse) {
NYql::TIssues issues;
const auto& issueMessage = kqpResponse.GetResponse().GetQueryIssues();
NYql::IssuesFromMessage(issueMessage, issues);
this->Request_->RaiseIssues(issues);
}
};
} // namespace NGRpcService
} // namespace NKikimr
|