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
187
188
189
190
191
192
193
|
#include "operation.h"
#define INCLUDE_YDB_INTERNAL_H
#include <ydb/public/sdk/cpp/client/impl/ydb_internal/make_request/make.h>
#undef INCLUDE_YDB_INTERNAL_H
/* Headers below used to instantiate concrete 'Get' & 'List' methods */
#include <ydb/public/sdk/cpp/client/ydb_export/export.h>
#include <ydb/public/sdk/cpp/client/ydb_import/import.h>
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
#include <ydb/public/api/grpc/ydb_operation_v1.grpc.pb.h>
#include <ydb/public/lib/operation_id/operation_id.h>
#include <ydb/public/sdk/cpp/client/ydb_common_client/impl/client.h>
namespace NYdb {
namespace NOperation {
constexpr TDuration OPERATION_CLIENT_TIMEOUT = TDuration::Seconds(5);
using namespace NThreading;
using namespace Ydb::Operation;
using namespace Ydb::Operations;
class TOperationClient::TImpl : public TClientImplCommon<TOperationClient::TImpl> {
template <typename TRequest, typename TResponse>
using TSimpleRpc = TGRpcConnectionsImpl::TSimpleRpc<V1::OperationService, TRequest, TResponse>;
template <typename TRequest, typename TResponse>
TAsyncStatus Run(TRequest&& request, TSimpleRpc<TRequest, TResponse> rpc) {
auto promise = NewPromise<TStatus>();
auto extractor = [promise]
(TResponse* response, TPlainStatus status) mutable {
if (response) {
NYql::TIssues opIssues;
NYql::IssuesFromMessage(response->issues(), opIssues);
TStatus st(static_cast<EStatus>(response->status()), std::move(opIssues));
promise.SetValue(std::move(st));
} else {
TStatus st(std::move(status));
promise.SetValue(std::move(st));
}
};
Connections_->Run<V1::OperationService, TRequest, TResponse>(
std::move(request),
extractor,
rpc,
DbDriverState_,
TRpcRequestSettings(),
OPERATION_CLIENT_TIMEOUT,
TString());
return promise.GetFuture();
}
public:
TImpl(std::shared_ptr<TGRpcConnectionsImpl>&& connections, const TCommonClientSettings& settings)
: TClientImplCommon(std::move(connections), settings)
{
}
template <typename TOp>
TFuture<TOp> Get(GetOperationRequest&& request) {
return RunOperation<V1::OperationService, GetOperationRequest, GetOperationResponse, TOp>(
std::move(request),
&V1::OperationService::Stub::AsyncGetOperation);
}
TAsyncStatus Cancel(CancelOperationRequest&& request) {
return Run<CancelOperationRequest, CancelOperationResponse>(std::move(request),
&V1::OperationService::Stub::AsyncCancelOperation);
}
TAsyncStatus Forget(ForgetOperationRequest&& request) {
return Run<ForgetOperationRequest, ForgetOperationResponse>(std::move(request),
&V1::OperationService::Stub::AsyncForgetOperation);
}
template <typename TOp>
TFuture<TOperationsList<TOp>> List(ListOperationsRequest&& request) {
auto promise = NewPromise<TOperationsList<TOp>>();
auto extractor = [promise]
(ListOperationsResponse* response, TPlainStatus status) mutable {
if (response) {
NYql::TIssues opIssues;
NYql::IssuesFromMessage(response->issues(), opIssues);
TStatus st(static_cast<EStatus>(response->status()), std::move(opIssues));
TVector<TOp> operations(Reserve(response->operations_size()));
for (auto& operation : *response->mutable_operations()) {
NYql::TIssues opIssues;
NYql::IssuesFromMessage(operation.issues(), opIssues);
operations.emplace_back(
TStatus(static_cast<EStatus>(operation.status()), std::move(opIssues)),
std::move(operation));
}
promise.SetValue(TOperationsList<TOp>(std::move(st), std::move(operations), response->next_page_token()));
} else {
TStatus st(std::move(status));
promise.SetValue(TOperationsList<TOp>(std::move(st)));
}
};
Connections_->Run<V1::OperationService, ListOperationsRequest, ListOperationsResponse>(
std::move(request),
extractor,
&V1::OperationService::Stub::AsyncListOperations,
DbDriverState_,
TRpcRequestSettings(),
OPERATION_CLIENT_TIMEOUT,
TString());
return promise.GetFuture();
}
};
////////////////////////////////////////////////////////////////////////////////
TOperationClient::TOperationClient(const TDriver& driver, const TCommonClientSettings& settings)
: Impl_(new TImpl(CreateInternalInterface(driver), settings))
{
}
template <typename TOp>
TFuture<TOp> TOperationClient::Get(const TOperation::TOperationId& id) {
auto request = MakeRequest<GetOperationRequest>();
request.set_id(NKikimr::NOperationId::ProtoToString(id));
return Impl_->Get<TOp>(std::move(request));
}
TAsyncStatus TOperationClient::Cancel(const TOperation::TOperationId& id) {
auto request = MakeRequest<CancelOperationRequest>();
request.set_id(NKikimr::NOperationId::ProtoToString(id));
return Impl_->Cancel(std::move(request));
}
TAsyncStatus TOperationClient::Forget(const TOperation::TOperationId& id) {
auto request = MakeRequest<ForgetOperationRequest>();
request.set_id(NKikimr::NOperationId::ProtoToString(id));
return Impl_->Forget(std::move(request));
}
template <typename TOp>
TFuture<TOperationsList<TOp>> TOperationClient::List(const TString& kind, ui64 pageSize, const TString& pageToken) {
auto request = MakeRequest<ListOperationsRequest>();
request.set_kind(kind);
if (pageSize) {
request.set_page_size(pageSize);
}
if (pageToken) {
request.set_page_token(pageToken);
}
return Impl_->List<TOp>(std::move(request));
}
// Instantiations
template TFuture<NExport::TExportToYtResponse> TOperationClient::Get(const TOperation::TOperationId& id);
template <>
TFuture<TOperationsList<NExport::TExportToYtResponse>> TOperationClient::List(ui64 pageSize, const TString& pageToken) {
// TODO: export -> export/yt
return List<NExport::TExportToYtResponse>("export", pageSize, pageToken);
}
template TFuture<NExport::TExportToS3Response> TOperationClient::Get(const TOperation::TOperationId& id);
template <>
TFuture<TOperationsList<NExport::TExportToS3Response>> TOperationClient::List(ui64 pageSize, const TString& pageToken) {
return List<NExport::TExportToS3Response>("export/s3", pageSize, pageToken);
}
template TFuture<NImport::TImportFromS3Response> TOperationClient::Get(const TOperation::TOperationId& id);
template <>
TFuture<TOperationsList<NImport::TImportFromS3Response>> TOperationClient::List(ui64 pageSize, const TString& pageToken) {
return List<NImport::TImportFromS3Response>("import/s3", pageSize, pageToken);
}
template TFuture<NTable::TBuildIndexOperation> TOperationClient::Get(const TOperation::TOperationId& id);
template <>
TFuture<TOperationsList<NTable::TBuildIndexOperation>> TOperationClient::List(ui64 pageSize, const TString& pageToken) {
return List<NTable::TBuildIndexOperation>("buildindex", pageSize, pageToken);
}
} // namespace NOperation
} // namespace NYdb
|