|
2 | 2 | #include "cli_cmds.h"
|
3 | 3 |
|
4 | 4 | #include <ydb/core/protos/base.pb.h>
|
| 5 | +#include <ydb/library/grpc/client/grpc_common.h> |
| 6 | +#include <ydb/public/api/grpc/draft/ydb_tablet_v1.grpc.pb.h> |
5 | 7 |
|
6 | 8 | namespace NKikimr {
|
7 | 9 | namespace NDriverClient {
|
@@ -83,123 +85,163 @@ class TClientCommandKeyValue : public TClientCommandTree {
|
83 | 85 | }
|
84 | 86 | };
|
85 | 87 |
|
86 |
| -class TClientCommandTabletExec : public TClientCommandBase { |
| 88 | +template<class TRequest, class TResponse> |
| 89 | +class TClientCommandTabletCommon { |
| 90 | +protected: |
| 91 | + int RunTabletCommon(TClientCommand::TConfig& config) { |
| 92 | + grpc::ClientContext context; |
| 93 | + if (auto token = AcquireSecurityToken(config)) { |
| 94 | + context.AddMetadata("x-ydb-auth-ticket", *token); |
| 95 | + } |
| 96 | + auto channel = NYdbGrpc::CreateChannelInterface(CommandConfig.ClientConfig); |
| 97 | + auto stub = Ydb::Tablet::V1::TabletService::NewStub(channel); |
| 98 | + if (NClient::TKikimr::DUMP_REQUESTS) { |
| 99 | + Cerr << "<-- " << TypeName<TRequest>() << "\n" << Request.DebugString(); |
| 100 | + } |
| 101 | + auto status = Send(context, stub); |
| 102 | + if (!status.ok()) { |
| 103 | + Cerr << "ERROR: " << int(status.error_code()) << " " << status.error_message() << Endl; |
| 104 | + return 1; |
| 105 | + } |
| 106 | + if (NClient::TKikimr::DUMP_REQUESTS) { |
| 107 | + Cerr << "--> " << TypeName<TResponse>() << "\n" << Response.DebugString(); |
| 108 | + } |
| 109 | + if (Response.status() != Ydb::StatusIds::SUCCESS) { |
| 110 | + Cerr << "ERROR: " << Response.status() << Endl; |
| 111 | + for (const auto& issue : Response.issues()) { |
| 112 | + Cerr << issue.message() << Endl; |
| 113 | + } |
| 114 | + return 1; |
| 115 | + } |
| 116 | + return 0; |
| 117 | + } |
| 118 | + |
| 119 | + virtual grpc::Status Send( |
| 120 | + grpc::ClientContext& context, |
| 121 | + const std::unique_ptr<Ydb::Tablet::V1::TabletService::Stub>& stub) = 0; |
| 122 | + |
| 123 | +protected: |
| 124 | + TRequest Request; |
| 125 | + TResponse Response; |
| 126 | +}; |
| 127 | + |
| 128 | +class TClientCommandTabletExec |
| 129 | + : public TClientCommandBase |
| 130 | + , public TClientCommandTabletCommon< |
| 131 | + Ydb::Tablet::ExecuteTabletMiniKQLRequest, |
| 132 | + Ydb::Tablet::ExecuteTabletMiniKQLResponse> |
| 133 | +{ |
87 | 134 | public:
|
88 | 135 | TClientCommandTabletExec()
|
89 | 136 | : TClientCommandBase("execute", { "exec" })
|
90 | 137 | {
|
91 | 138 | }
|
92 | 139 |
|
93 |
| - TAutoPtr<NMsgBusProxy::TBusTabletLocalMKQL> Request; |
94 | 140 | TString Program;
|
95 |
| - TString Params; |
96 | 141 |
|
97 | 142 | virtual void Config(TConfig& config) override {
|
98 | 143 | TClientCommand::Config(config);
|
99 |
| - config.Opts->AddLongOption("follower", "connect to follower").NoArgument(); |
100 |
| - config.Opts->AddLongOption("json-ui64-as-string", "json output ui64 as string").NoArgument(); |
101 |
| - config.Opts->AddLongOption("json-binary-as-base64", "json output binary data in base64").NoArgument(); |
102 |
| - config.SetFreeArgsNum(1, 2); |
| 144 | + config.Opts->AddLongOption("dry-run", "test changes without applying").NoArgument(); |
| 145 | + config.SetFreeArgsNum(1, 1); |
103 | 146 | SetFreeArgTitle(0, "<PROGRAM>", "Program to execute");
|
104 |
| - SetFreeArgTitle(1, "<PARAMS>", "Parameters of the program"); |
105 | 147 | }
|
106 | 148 |
|
107 | 149 | virtual void Parse(TConfig& config) override {
|
108 | 150 | TClientCommand::Parse(config);
|
109 | 151 |
|
110 | 152 | Program = GetMiniKQL(config.ParseResult->GetFreeArgs().at(0));
|
111 |
| - if (config.ParseResult->GetFreeArgCount() > 1) |
112 |
| - Params = GetMiniKQL(config.ParseResult->GetFreeArgs().at(1)); |
113 |
| - |
114 |
| - Request = new NMsgBusProxy::TBusTabletLocalMKQL; |
115 |
| - Request->Record.SetTabletID(config.TabletId); |
116 |
| - auto* pgm = Request->Record.MutableProgram(); |
117 |
| - if (IsMiniKQL(Program)) { |
118 |
| - pgm->MutableProgram()->SetText(Program); |
119 |
| - } else { |
120 |
| - pgm->MutableProgram()->SetBin(Program); |
121 |
| - } |
122 | 153 |
|
123 |
| - if (!Params.empty()) { |
124 |
| - if (IsMiniKQL(Params)) { |
125 |
| - pgm->MutableParams()->SetText(Params); |
126 |
| - } else { |
127 |
| - pgm->MutableParams()->SetBin(Params); |
128 |
| - } |
129 |
| - } |
130 |
| - |
131 |
| - Request->Record.SetConnectToFollower(config.ParseResult->Has("follower")); |
132 |
| - config.JsonUi64AsText = config.ParseResult->Has("json-ui64-as-string"); |
133 |
| - config.JsonBinaryAsBase64 = config.ParseResult->Has("json-binary-as-base64"); |
| 154 | + Request.set_tablet_id(config.TabletId); |
| 155 | + Request.set_program(Program); |
| 156 | + Request.set_dry_run(config.ParseResult->Has("dry-run")); |
134 | 157 | }
|
135 | 158 |
|
136 | 159 | virtual int Run(TConfig& config) override {
|
137 |
| - return MessageBusCall(config, Request); |
| 160 | + return RunTabletCommon(config); |
| 161 | + } |
| 162 | + |
| 163 | + virtual grpc::Status Send( |
| 164 | + grpc::ClientContext& context, |
| 165 | + const std::unique_ptr<Ydb::Tablet::V1::TabletService::Stub>& stub) override |
| 166 | + { |
| 167 | + return stub->ExecuteTabletMiniKQL(&context, Request, &Response); |
138 | 168 | }
|
139 | 169 | };
|
140 | 170 |
|
141 |
| -class TClientCommandTabletKill : public TClientCommand { |
| 171 | +class TClientCommandTabletKill |
| 172 | + : public TClientCommand |
| 173 | + , public TClientCommandTabletCommon< |
| 174 | + Ydb::Tablet::RestartTabletRequest, |
| 175 | + Ydb::Tablet::RestartTabletResponse> |
| 176 | +{ |
142 | 177 | public:
|
143 | 178 | TClientCommandTabletKill()
|
144 | 179 | : TClientCommand("kill")
|
145 | 180 | {
|
146 | 181 | }
|
147 | 182 |
|
148 |
| - TAutoPtr<NMsgBusProxy::TBusTabletKillRequest> Request; |
149 |
| - |
150 | 183 | virtual void Config(TConfig& config) override {
|
151 | 184 | TClientCommand::Config(config);
|
152 | 185 | config.SetFreeArgsNum(0);
|
153 | 186 | }
|
154 | 187 |
|
155 | 188 | virtual void Parse(TConfig& config) override {
|
156 | 189 | TClientCommand::Parse(config);
|
157 |
| - Request = new NMsgBusProxy::TBusTabletKillRequest; |
158 |
| - Request->Record.SetTabletID(config.TabletId); |
| 190 | + Request.set_tablet_id(config.TabletId); |
159 | 191 | }
|
160 | 192 |
|
161 | 193 | virtual int Run(TConfig& config) override {
|
162 |
| - return MessageBusCall(config, Request); |
| 194 | + return RunTabletCommon(config); |
| 195 | + } |
| 196 | + |
| 197 | + virtual grpc::Status Send( |
| 198 | + grpc::ClientContext& context, |
| 199 | + const std::unique_ptr<Ydb::Tablet::V1::TabletService::Stub>& stub) override |
| 200 | + { |
| 201 | + return stub->RestartTablet(&context, Request, &Response); |
163 | 202 | }
|
164 | 203 | };
|
165 | 204 |
|
166 |
| -class TClientCommandTabletSchemeTx : public TClientCommand { |
| 205 | +class TClientCommandTabletSchemeTx |
| 206 | + : public TClientCommand |
| 207 | + , public TClientCommandTabletCommon< |
| 208 | + Ydb::Tablet::ChangeTabletSchemaRequest, |
| 209 | + Ydb::Tablet::ChangeTabletSchemaResponse> |
| 210 | +{ |
167 | 211 | public:
|
168 | 212 | TClientCommandTabletSchemeTx()
|
169 | 213 | : TClientCommand("scheme-tx", { "scheme" })
|
170 | 214 | {
|
171 | 215 | }
|
172 | 216 |
|
173 |
| - TAutoPtr<NMsgBusProxy::TBusTabletLocalSchemeTx> Request; |
174 | 217 | TString SchemeChanges;
|
175 | 218 |
|
176 | 219 | virtual void Config(TConfig& config) override {
|
177 | 220 | TClientCommand::Config(config);
|
178 |
| - config.Opts->AddLongOption("follower", "connect to follower"); |
179 |
| - config.Opts->AddLongOption("dry-run", "test changes without applying"); |
| 221 | + config.Opts->AddLongOption("dry-run", "test changes without applying").NoArgument(); |
180 | 222 | config.SetFreeArgsNum(1, 1);
|
181 |
| - SetFreeArgTitle(0, "<SCHEME CHANGES>", "Scheme changes to apply"); |
| 223 | + SetFreeArgTitle(0, "<SCHEME CHANGES>", "Scheme changes json to apply"); |
182 | 224 | }
|
183 | 225 |
|
184 | 226 | virtual void Parse(TConfig& config) override {
|
185 | 227 | TClientCommand::Parse(config);
|
186 | 228 |
|
187 | 229 | SchemeChanges = config.ParseResult->GetFreeArgs().at(0);
|
188 | 230 |
|
189 |
| - Request = new NMsgBusProxy::TBusTabletLocalSchemeTx; |
190 |
| - Request->Record.SetTabletID(config.TabletId); |
191 |
| - auto* schemeChanges = Request->Record.MutableSchemeChanges(); |
192 |
| - if (!google::protobuf::TextFormat::ParseFromString(SchemeChanges, schemeChanges)) { |
193 |
| - ythrow TWithBackTrace<yexception>() << "Invalid scheme changes protobuf passed"; |
194 |
| - } |
195 |
| - |
196 |
| - if (config.ParseResult->Has("follower")) |
197 |
| - Request->Record.SetConnectToFollower(true); |
198 |
| - Request->Record.SetDryRun(config.ParseResult->Has("dry-run")); |
| 231 | + Request.set_tablet_id(config.TabletId); |
| 232 | + Request.set_schema_changes(SchemeChanges); |
| 233 | + Request.set_dry_run(config.ParseResult->Has("dry-run")); |
199 | 234 | }
|
200 | 235 |
|
201 | 236 | virtual int Run(TConfig& config) override {
|
202 |
| - return MessageBusCall(config, Request); |
| 237 | + return RunTabletCommon(config); |
| 238 | + } |
| 239 | + |
| 240 | + virtual grpc::Status Send( |
| 241 | + grpc::ClientContext& context, |
| 242 | + const std::unique_ptr<Ydb::Tablet::V1::TabletService::Stub>& stub) override |
| 243 | + { |
| 244 | + return stub->ChangeTabletSchema(&context, Request, &Response); |
203 | 245 | }
|
204 | 246 | };
|
205 | 247 |
|
|
0 commit comments