client.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. #include "client.h"
  2. #include "batch_request_impl.h"
  3. #include "client_reader.h"
  4. #include "client_writer.h"
  5. #include "file_reader.h"
  6. #include "file_writer.h"
  7. #include "format_hints.h"
  8. #include "lock.h"
  9. #include "operation.h"
  10. #include "retry_transaction.h"
  11. #include "retryful_writer.h"
  12. #include "transaction.h"
  13. #include "transaction_pinger.h"
  14. #include "yt_poller.h"
  15. #include <yt/cpp/mapreduce/common/helpers.h>
  16. #include <yt/cpp/mapreduce/common/retry_lib.h>
  17. #include <yt/cpp/mapreduce/http/helpers.h>
  18. #include <yt/cpp/mapreduce/http/http.h>
  19. #include <yt/cpp/mapreduce/http/http_client.h>
  20. #include <yt/cpp/mapreduce/http/requests.h>
  21. #include <yt/cpp/mapreduce/http/retry_request.h>
  22. #include <yt/cpp/mapreduce/interface/config.h>
  23. #include <yt/cpp/mapreduce/interface/client.h>
  24. #include <yt/cpp/mapreduce/interface/error_codes.h>
  25. #include <yt/cpp/mapreduce/interface/fluent.h>
  26. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  27. #include <yt/cpp/mapreduce/interface/skiff_row.h>
  28. #include <yt/cpp/mapreduce/io/yamr_table_reader.h>
  29. #include <yt/cpp/mapreduce/io/yamr_table_writer.h>
  30. #include <yt/cpp/mapreduce/io/node_table_reader.h>
  31. #include <yt/cpp/mapreduce/io/node_table_writer.h>
  32. #include <yt/cpp/mapreduce/io/proto_table_reader.h>
  33. #include <yt/cpp/mapreduce/io/proto_table_writer.h>
  34. #include <yt/cpp/mapreduce/io/skiff_row_table_reader.h>
  35. #include <yt/cpp/mapreduce/io/proto_helpers.h>
  36. #include <yt/cpp/mapreduce/library/table_schema/protobuf.h>
  37. #include <yt/cpp/mapreduce/raw_client/raw_requests.h>
  38. #include <yt/cpp/mapreduce/raw_client/rpc_parameters_serialization.h>
  39. #include <library/cpp/json/json_reader.h>
  40. #include <util/generic/algorithm.h>
  41. #include <util/string/type.h>
  42. #include <util/system/env.h>
  43. #include <exception>
  44. using namespace NYT::NDetail::NRawClient;
  45. namespace NYT {
  46. ////////////////////////////////////////////////////////////////////////////////
  47. namespace NDetail {
  48. ////////////////////////////////////////////////////////////////////////////////
  49. TClientBase::TClientBase(
  50. const TClientContext& context,
  51. const TTransactionId& transactionId,
  52. IClientRetryPolicyPtr retryPolicy)
  53. : Context_(context)
  54. , TransactionId_(transactionId)
  55. , ClientRetryPolicy_(std::move(retryPolicy))
  56. { }
  57. ITransactionPtr TClientBase::StartTransaction(
  58. const TStartTransactionOptions& options)
  59. {
  60. return MakeIntrusive<TTransaction>(GetParentClientImpl(), Context_, TransactionId_, options);
  61. }
  62. TNodeId TClientBase::Create(
  63. const TYPath& path,
  64. ENodeType type,
  65. const TCreateOptions& options)
  66. {
  67. return NRawClient::Create(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, type, options);
  68. }
  69. void TClientBase::Remove(
  70. const TYPath& path,
  71. const TRemoveOptions& options)
  72. {
  73. return NRawClient::Remove(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, options);
  74. }
  75. bool TClientBase::Exists(
  76. const TYPath& path,
  77. const TExistsOptions& options)
  78. {
  79. return NRawClient::Exists(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, options);
  80. }
  81. TNode TClientBase::Get(
  82. const TYPath& path,
  83. const TGetOptions& options)
  84. {
  85. return NRawClient::Get(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, options);
  86. }
  87. void TClientBase::Set(
  88. const TYPath& path,
  89. const TNode& value,
  90. const TSetOptions& options)
  91. {
  92. NRawClient::Set(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, value, options);
  93. }
  94. void TClientBase::MultisetAttributes(
  95. const TYPath& path, const TNode::TMapType& value, const TMultisetAttributesOptions& options)
  96. {
  97. NRawClient::MultisetAttributes(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, value, options);
  98. }
  99. TNode::TListType TClientBase::List(
  100. const TYPath& path,
  101. const TListOptions& options)
  102. {
  103. return NRawClient::List(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, options);
  104. }
  105. TNodeId TClientBase::Copy(
  106. const TYPath& sourcePath,
  107. const TYPath& destinationPath,
  108. const TCopyOptions& options)
  109. {
  110. try {
  111. return NRawClient::CopyInsideMasterCell(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, sourcePath, destinationPath, options);
  112. } catch (const TErrorResponse& e) {
  113. if (e.GetError().ContainsErrorCode(NClusterErrorCodes::NObjectClient::CrossCellAdditionalPath)) {
  114. // Do transaction for cross cell copying.
  115. std::function<TNodeId(ITransactionPtr)> lambda = [this, &sourcePath, &destinationPath, &options](ITransactionPtr transaction) {
  116. return NRawClient::CopyWithoutRetries(Context_, transaction->GetId(), sourcePath, destinationPath, options);
  117. };
  118. return RetryTransactionWithPolicy<TNodeId>(
  119. this,
  120. lambda,
  121. ClientRetryPolicy_->CreatePolicyForGenericRequest()
  122. );
  123. } else {
  124. throw;
  125. }
  126. }
  127. }
  128. TNodeId TClientBase::Move(
  129. const TYPath& sourcePath,
  130. const TYPath& destinationPath,
  131. const TMoveOptions& options)
  132. {
  133. try {
  134. return NRawClient::MoveInsideMasterCell(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, sourcePath, destinationPath, options);
  135. } catch (const TErrorResponse& e) {
  136. if (e.GetError().ContainsErrorCode(NClusterErrorCodes::NObjectClient::CrossCellAdditionalPath)) {
  137. // Do transaction for cross cell moving.
  138. std::function<TNodeId(ITransactionPtr)> lambda = [this, &sourcePath, &destinationPath, &options](ITransactionPtr transaction) {
  139. return NRawClient::MoveWithoutRetries(Context_, transaction->GetId(), sourcePath, destinationPath, options);
  140. };
  141. return RetryTransactionWithPolicy<TNodeId>(
  142. this,
  143. lambda,
  144. ClientRetryPolicy_->CreatePolicyForGenericRequest()
  145. );
  146. } else {
  147. throw;
  148. }
  149. }
  150. }
  151. TNodeId TClientBase::Link(
  152. const TYPath& targetPath,
  153. const TYPath& linkPath,
  154. const TLinkOptions& options)
  155. {
  156. return NRawClient::Link(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, targetPath, linkPath, options);
  157. }
  158. void TClientBase::Concatenate(
  159. const TVector<TRichYPath>& sourcePaths,
  160. const TRichYPath& destinationPath,
  161. const TConcatenateOptions& options)
  162. {
  163. std::function<void(ITransactionPtr)> lambda = [&sourcePaths, &destinationPath, &options, this](ITransactionPtr transaction) {
  164. if (!options.Append_ && !sourcePaths.empty() && !transaction->Exists(destinationPath.Path_)) {
  165. auto typeNode = transaction->Get(CanonizeYPath(sourcePaths.front()).Path_ + "/@type");
  166. auto type = FromString<ENodeType>(typeNode.AsString());
  167. transaction->Create(destinationPath.Path_, type, TCreateOptions().IgnoreExisting(true));
  168. }
  169. NRawClient::Concatenate(this->Context_, transaction->GetId(), sourcePaths, destinationPath, options);
  170. };
  171. RetryTransactionWithPolicy(this, lambda, ClientRetryPolicy_->CreatePolicyForGenericRequest());
  172. }
  173. TRichYPath TClientBase::CanonizeYPath(const TRichYPath& path)
  174. {
  175. return NRawClient::CanonizeYPath(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, path);
  176. }
  177. TVector<TTableColumnarStatistics> TClientBase::GetTableColumnarStatistics(
  178. const TVector<TRichYPath>& paths,
  179. const TGetTableColumnarStatisticsOptions& options)
  180. {
  181. return NRawClient::GetTableColumnarStatistics(
  182. ClientRetryPolicy_->CreatePolicyForGenericRequest(),
  183. Context_,
  184. TransactionId_,
  185. paths,
  186. options);
  187. }
  188. TMultiTablePartitions TClientBase::GetTablePartitions(
  189. const TVector<TRichYPath>& paths,
  190. const TGetTablePartitionsOptions& options)
  191. {
  192. return NRawClient::GetTablePartitions(
  193. ClientRetryPolicy_->CreatePolicyForGenericRequest(),
  194. Context_,
  195. TransactionId_,
  196. paths,
  197. options);
  198. }
  199. TMaybe<TYPath> TClientBase::GetFileFromCache(
  200. const TString& md5Signature,
  201. const TYPath& cachePath,
  202. const TGetFileFromCacheOptions& options)
  203. {
  204. return NRawClient::GetFileFromCache(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, md5Signature, cachePath, options);
  205. }
  206. TYPath TClientBase::PutFileToCache(
  207. const TYPath& filePath,
  208. const TString& md5Signature,
  209. const TYPath& cachePath,
  210. const TPutFileToCacheOptions& options)
  211. {
  212. return NRawClient::PutFileToCache(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, filePath, md5Signature, cachePath, options);
  213. }
  214. IFileReaderPtr TClientBase::CreateBlobTableReader(
  215. const TYPath& path,
  216. const TKey& key,
  217. const TBlobTableReaderOptions& options)
  218. {
  219. return new TBlobTableReader(
  220. path,
  221. key,
  222. ClientRetryPolicy_,
  223. GetTransactionPinger(),
  224. Context_,
  225. TransactionId_,
  226. options);
  227. }
  228. IFileReaderPtr TClientBase::CreateFileReader(
  229. const TRichYPath& path,
  230. const TFileReaderOptions& options)
  231. {
  232. return new TFileReader(
  233. CanonizeYPath(path),
  234. ClientRetryPolicy_,
  235. GetTransactionPinger(),
  236. Context_,
  237. TransactionId_,
  238. options);
  239. }
  240. IFileWriterPtr TClientBase::CreateFileWriter(
  241. const TRichYPath& path,
  242. const TFileWriterOptions& options)
  243. {
  244. auto realPath = CanonizeYPath(path);
  245. if (!NRawClient::Exists(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, realPath.Path_)) {
  246. NRawClient::Create(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, realPath.Path_, NT_FILE,
  247. TCreateOptions().IgnoreExisting(true));
  248. }
  249. return new TFileWriter(realPath, ClientRetryPolicy_, GetTransactionPinger(), Context_, TransactionId_, options);
  250. }
  251. TTableWriterPtr<::google::protobuf::Message> TClientBase::CreateTableWriter(
  252. const TRichYPath& path, const ::google::protobuf::Descriptor& descriptor, const TTableWriterOptions& options)
  253. {
  254. const Message* prototype = google::protobuf::MessageFactory::generated_factory()->GetPrototype(&descriptor);
  255. return new TTableWriter<::google::protobuf::Message>(CreateProtoWriter(path, options, prototype));
  256. }
  257. TRawTableReaderPtr TClientBase::CreateRawReader(
  258. const TRichYPath& path,
  259. const TFormat& format,
  260. const TTableReaderOptions& options)
  261. {
  262. return CreateClientReader(path, format, options).Get();
  263. }
  264. TRawTableWriterPtr TClientBase::CreateRawWriter(
  265. const TRichYPath& path,
  266. const TFormat& format,
  267. const TTableWriterOptions& options)
  268. {
  269. return ::MakeIntrusive<TRetryfulWriter>(
  270. ClientRetryPolicy_,
  271. GetTransactionPinger(),
  272. Context_,
  273. TransactionId_,
  274. GetWriteTableCommand(Context_.Config->ApiVersion),
  275. format,
  276. CanonizeYPath(path),
  277. options).Get();
  278. }
  279. IOperationPtr TClientBase::DoMap(
  280. const TMapOperationSpec& spec,
  281. ::TIntrusivePtr<IStructuredJob> mapper,
  282. const TOperationOptions& options)
  283. {
  284. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  285. auto prepareOperation = [
  286. this_ = ::TIntrusivePtr(this),
  287. operation,
  288. spec,
  289. mapper,
  290. options
  291. ] () {
  292. ExecuteMap(
  293. operation,
  294. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  295. spec,
  296. mapper,
  297. options);
  298. };
  299. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  300. }
  301. IOperationPtr TClientBase::RawMap(
  302. const TRawMapOperationSpec& spec,
  303. ::TIntrusivePtr<IRawJob> mapper,
  304. const TOperationOptions& options)
  305. {
  306. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  307. auto prepareOperation = [
  308. this_=::TIntrusivePtr(this),
  309. operation,
  310. spec,
  311. mapper,
  312. options
  313. ] () {
  314. ExecuteRawMap(
  315. operation,
  316. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  317. spec,
  318. mapper,
  319. options);
  320. };
  321. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  322. }
  323. IOperationPtr TClientBase::DoReduce(
  324. const TReduceOperationSpec& spec,
  325. ::TIntrusivePtr<IStructuredJob> reducer,
  326. const TOperationOptions& options)
  327. {
  328. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  329. auto prepareOperation = [
  330. this_=::TIntrusivePtr(this),
  331. operation,
  332. spec,
  333. reducer,
  334. options
  335. ] () {
  336. ExecuteReduce(
  337. operation,
  338. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  339. spec,
  340. reducer,
  341. options);
  342. };
  343. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  344. }
  345. IOperationPtr TClientBase::RawReduce(
  346. const TRawReduceOperationSpec& spec,
  347. ::TIntrusivePtr<IRawJob> reducer,
  348. const TOperationOptions& options)
  349. {
  350. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  351. auto prepareOperation = [
  352. this_=::TIntrusivePtr(this),
  353. operation,
  354. spec,
  355. reducer,
  356. options
  357. ] () {
  358. ExecuteRawReduce(
  359. operation,
  360. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  361. spec,
  362. reducer,
  363. options);
  364. };
  365. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  366. }
  367. IOperationPtr TClientBase::DoJoinReduce(
  368. const TJoinReduceOperationSpec& spec,
  369. ::TIntrusivePtr<IStructuredJob> reducer,
  370. const TOperationOptions& options)
  371. {
  372. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  373. auto prepareOperation = [
  374. this_=::TIntrusivePtr(this),
  375. operation,
  376. spec,
  377. reducer,
  378. options
  379. ] () {
  380. ExecuteJoinReduce(
  381. operation,
  382. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  383. spec,
  384. reducer,
  385. options);
  386. };
  387. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  388. }
  389. IOperationPtr TClientBase::RawJoinReduce(
  390. const TRawJoinReduceOperationSpec& spec,
  391. ::TIntrusivePtr<IRawJob> reducer,
  392. const TOperationOptions& options)
  393. {
  394. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  395. auto prepareOperation = [
  396. this_=::TIntrusivePtr(this),
  397. operation,
  398. spec,
  399. reducer,
  400. options
  401. ] () {
  402. ExecuteRawJoinReduce(
  403. operation,
  404. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  405. spec,
  406. reducer,
  407. options);
  408. };
  409. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  410. }
  411. IOperationPtr TClientBase::DoMapReduce(
  412. const TMapReduceOperationSpec& spec,
  413. ::TIntrusivePtr<IStructuredJob> mapper,
  414. ::TIntrusivePtr<IStructuredJob> reduceCombiner,
  415. ::TIntrusivePtr<IStructuredJob> reducer,
  416. const TOperationOptions& options)
  417. {
  418. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  419. auto prepareOperation = [
  420. this_=::TIntrusivePtr(this),
  421. operation,
  422. spec,
  423. mapper,
  424. reduceCombiner,
  425. reducer,
  426. options
  427. ] () {
  428. ExecuteMapReduce(
  429. operation,
  430. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  431. spec,
  432. mapper,
  433. reduceCombiner,
  434. reducer,
  435. options);
  436. };
  437. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  438. }
  439. IOperationPtr TClientBase::RawMapReduce(
  440. const TRawMapReduceOperationSpec& spec,
  441. ::TIntrusivePtr<IRawJob> mapper,
  442. ::TIntrusivePtr<IRawJob> reduceCombiner,
  443. ::TIntrusivePtr<IRawJob> reducer,
  444. const TOperationOptions& options)
  445. {
  446. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  447. auto prepareOperation = [
  448. this_=::TIntrusivePtr(this),
  449. operation,
  450. spec,
  451. mapper,
  452. reduceCombiner,
  453. reducer,
  454. options
  455. ] () {
  456. ExecuteRawMapReduce(
  457. operation,
  458. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  459. spec,
  460. mapper,
  461. reduceCombiner,
  462. reducer,
  463. options);
  464. };
  465. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  466. }
  467. IOperationPtr TClientBase::Sort(
  468. const TSortOperationSpec& spec,
  469. const TOperationOptions& options)
  470. {
  471. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  472. auto prepareOperation = [
  473. this_ = ::TIntrusivePtr(this),
  474. operation,
  475. spec,
  476. options
  477. ] () {
  478. ExecuteSort(
  479. operation,
  480. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  481. spec,
  482. options);
  483. };
  484. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  485. }
  486. IOperationPtr TClientBase::Merge(
  487. const TMergeOperationSpec& spec,
  488. const TOperationOptions& options)
  489. {
  490. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  491. auto prepareOperation = [
  492. this_ = ::TIntrusivePtr(this),
  493. operation,
  494. spec,
  495. options
  496. ] () {
  497. ExecuteMerge(
  498. operation,
  499. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  500. spec,
  501. options);
  502. };
  503. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  504. }
  505. IOperationPtr TClientBase::Erase(
  506. const TEraseOperationSpec& spec,
  507. const TOperationOptions& options)
  508. {
  509. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  510. auto prepareOperation = [
  511. this_ = ::TIntrusivePtr(this),
  512. operation,
  513. spec,
  514. options
  515. ] () {
  516. ExecuteErase(
  517. operation,
  518. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  519. spec,
  520. options);
  521. };
  522. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  523. }
  524. IOperationPtr TClientBase::RemoteCopy(
  525. const TRemoteCopyOperationSpec& spec,
  526. const TOperationOptions& options)
  527. {
  528. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  529. auto prepareOperation = [
  530. this_ = ::TIntrusivePtr(this),
  531. operation,
  532. spec,
  533. options
  534. ] () {
  535. ExecuteRemoteCopy(
  536. operation,
  537. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  538. spec,
  539. options);
  540. };
  541. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  542. }
  543. IOperationPtr TClientBase::RunVanilla(
  544. const TVanillaOperationSpec& spec,
  545. const TOperationOptions& options)
  546. {
  547. auto operation = ::MakeIntrusive<TOperation>(GetParentClientImpl());
  548. auto prepareOperation = [
  549. this_ = ::TIntrusivePtr(this),
  550. operation,
  551. spec,
  552. options
  553. ] () {
  554. ExecuteVanilla(
  555. operation,
  556. ::MakeIntrusive<TOperationPreparer>(this_->GetParentClientImpl(), this_->TransactionId_),
  557. spec,
  558. options);
  559. };
  560. return ProcessOperation(GetParentClientImpl(), std::move(prepareOperation), std::move(operation), options);
  561. }
  562. IOperationPtr TClientBase::AttachOperation(const TOperationId& operationId)
  563. {
  564. auto operation = ::MakeIntrusive<TOperation>(operationId, GetParentClientImpl());
  565. operation->GetBriefState(); // check that operation exists
  566. return operation;
  567. }
  568. EOperationBriefState TClientBase::CheckOperation(const TOperationId& operationId)
  569. {
  570. return NYT::NDetail::CheckOperation(ClientRetryPolicy_, Context_, operationId);
  571. }
  572. void TClientBase::AbortOperation(const TOperationId& operationId)
  573. {
  574. NRawClient::AbortOperation(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId);
  575. }
  576. void TClientBase::CompleteOperation(const TOperationId& operationId)
  577. {
  578. NRawClient::CompleteOperation(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId);
  579. }
  580. void TClientBase::WaitForOperation(const TOperationId& operationId)
  581. {
  582. NYT::NDetail::WaitForOperation(ClientRetryPolicy_, Context_, operationId);
  583. }
  584. void TClientBase::AlterTable(
  585. const TYPath& path,
  586. const TAlterTableOptions& options)
  587. {
  588. NRawClient::AlterTable(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, options);
  589. }
  590. ::TIntrusivePtr<TClientReader> TClientBase::CreateClientReader(
  591. const TRichYPath& path,
  592. const TFormat& format,
  593. const TTableReaderOptions& options,
  594. bool useFormatFromTableAttributes)
  595. {
  596. return ::MakeIntrusive<TClientReader>(
  597. CanonizeYPath(path),
  598. ClientRetryPolicy_,
  599. GetTransactionPinger(),
  600. Context_,
  601. TransactionId_,
  602. format,
  603. options,
  604. useFormatFromTableAttributes);
  605. }
  606. THolder<TClientWriter> TClientBase::CreateClientWriter(
  607. const TRichYPath& path,
  608. const TFormat& format,
  609. const TTableWriterOptions& options)
  610. {
  611. auto realPath = CanonizeYPath(path);
  612. if (!NRawClient::Exists(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, realPath.Path_)) {
  613. NRawClient::Create(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, realPath.Path_, NT_TABLE,
  614. TCreateOptions().IgnoreExisting(true));
  615. }
  616. return MakeHolder<TClientWriter>(
  617. realPath,
  618. ClientRetryPolicy_,
  619. GetTransactionPinger(),
  620. Context_,
  621. TransactionId_,
  622. format,
  623. options
  624. );
  625. }
  626. ::TIntrusivePtr<INodeReaderImpl> TClientBase::CreateNodeReader(
  627. const TRichYPath& path, const TTableReaderOptions& options)
  628. {
  629. auto format = TFormat::YsonBinary();
  630. ApplyFormatHints<TNode>(&format, options.FormatHints_);
  631. // Skiff is disabled here because of large header problem (see https://st.yandex-team.ru/YT-6926).
  632. // Revert this code to r3614168 when it is fixed.
  633. return new TNodeTableReader(
  634. CreateClientReader(path, format, options));
  635. }
  636. ::TIntrusivePtr<IYaMRReaderImpl> TClientBase::CreateYaMRReader(
  637. const TRichYPath& path, const TTableReaderOptions& options)
  638. {
  639. return new TYaMRTableReader(
  640. CreateClientReader(path, TFormat::YaMRLenval(), options, /* useFormatFromTableAttributes = */ true));
  641. }
  642. ::TIntrusivePtr<IProtoReaderImpl> TClientBase::CreateProtoReader(
  643. const TRichYPath& path,
  644. const TTableReaderOptions& options,
  645. const Message* prototype)
  646. {
  647. TVector<const ::google::protobuf::Descriptor*> descriptors;
  648. descriptors.push_back(prototype->GetDescriptor());
  649. if (Context_.Config->UseClientProtobuf) {
  650. return new TProtoTableReader(
  651. CreateClientReader(path, TFormat::YsonBinary(), options),
  652. std::move(descriptors));
  653. } else {
  654. auto format = TFormat::Protobuf({prototype->GetDescriptor()}, Context_.Config->ProtobufFormatWithDescriptors);
  655. return new TLenvalProtoTableReader(
  656. CreateClientReader(path, format, options),
  657. std::move(descriptors));
  658. }
  659. }
  660. ::TIntrusivePtr<ISkiffRowReaderImpl> TClientBase::CreateSkiffRowReader(
  661. const TRichYPath& path,
  662. const TTableReaderOptions& options,
  663. const ISkiffRowSkipperPtr& skipper,
  664. const NSkiff::TSkiffSchemaPtr& schema)
  665. {
  666. auto skiffOptions = TCreateSkiffSchemaOptions().HasRangeIndex(true);
  667. auto resultSchema = NYT::NDetail::CreateSkiffSchema(TVector{schema}, skiffOptions);
  668. return new TSkiffRowTableReader(
  669. CreateClientReader(path, NYT::NDetail::CreateSkiffFormat(resultSchema), options),
  670. resultSchema,
  671. {skipper},
  672. std::move(skiffOptions));
  673. }
  674. ::TIntrusivePtr<INodeWriterImpl> TClientBase::CreateNodeWriter(
  675. const TRichYPath& path, const TTableWriterOptions& options)
  676. {
  677. auto format = TFormat::YsonBinary();
  678. ApplyFormatHints<TNode>(&format, options.FormatHints_);
  679. return new TNodeTableWriter(
  680. CreateClientWriter(path, format, options));
  681. }
  682. ::TIntrusivePtr<IYaMRWriterImpl> TClientBase::CreateYaMRWriter(
  683. const TRichYPath& path, const TTableWriterOptions& options)
  684. {
  685. auto format = TFormat::YaMRLenval();
  686. ApplyFormatHints<TYaMRRow>(&format, options.FormatHints_);
  687. return new TYaMRTableWriter(
  688. CreateClientWriter(path, format, options));
  689. }
  690. ::TIntrusivePtr<IProtoWriterImpl> TClientBase::CreateProtoWriter(
  691. const TRichYPath& path,
  692. const TTableWriterOptions& options,
  693. const Message* prototype)
  694. {
  695. TVector<const ::google::protobuf::Descriptor*> descriptors;
  696. descriptors.push_back(prototype->GetDescriptor());
  697. auto pathWithSchema = path;
  698. if (options.InferSchema_.GetOrElse(Context_.Config->InferTableSchema) && !path.Schema_) {
  699. pathWithSchema.Schema(CreateTableSchema(*prototype->GetDescriptor()));
  700. }
  701. if (Context_.Config->UseClientProtobuf) {
  702. auto format = TFormat::YsonBinary();
  703. ApplyFormatHints<TNode>(&format, options.FormatHints_);
  704. return new TProtoTableWriter(
  705. CreateClientWriter(pathWithSchema, format, options),
  706. std::move(descriptors));
  707. } else {
  708. auto format = TFormat::Protobuf({prototype->GetDescriptor()}, Context_.Config->ProtobufFormatWithDescriptors);
  709. ApplyFormatHints<::google::protobuf::Message>(&format, options.FormatHints_);
  710. return new TLenvalProtoTableWriter(
  711. CreateClientWriter(pathWithSchema, format, options),
  712. std::move(descriptors));
  713. }
  714. }
  715. TBatchRequestPtr TClientBase::CreateBatchRequest()
  716. {
  717. return MakeIntrusive<TBatchRequest>(TransactionId_, GetParentClientImpl());
  718. }
  719. IClientPtr TClientBase::GetParentClient()
  720. {
  721. return GetParentClientImpl();
  722. }
  723. const TClientContext& TClientBase::GetContext() const
  724. {
  725. return Context_;
  726. }
  727. const IClientRetryPolicyPtr& TClientBase::GetRetryPolicy() const
  728. {
  729. return ClientRetryPolicy_;
  730. }
  731. ////////////////////////////////////////////////////////////////////////////////
  732. TTransaction::TTransaction(
  733. TClientPtr parentClient,
  734. const TClientContext& context,
  735. const TTransactionId& parentTransactionId,
  736. const TStartTransactionOptions& options)
  737. : TClientBase(context, parentTransactionId, parentClient->GetRetryPolicy())
  738. , TransactionPinger_(parentClient->GetTransactionPinger())
  739. , PingableTx_(
  740. MakeHolder<TPingableTransaction>(
  741. parentClient->GetRetryPolicy(),
  742. context,
  743. parentTransactionId,
  744. TransactionPinger_->GetChildTxPinger(),
  745. options))
  746. , ParentClient_(parentClient)
  747. {
  748. TransactionId_ = PingableTx_->GetId();
  749. }
  750. TTransaction::TTransaction(
  751. TClientPtr parentClient,
  752. const TClientContext& context,
  753. const TTransactionId& transactionId,
  754. const TAttachTransactionOptions& options)
  755. : TClientBase(context, transactionId, parentClient->GetRetryPolicy())
  756. , TransactionPinger_(parentClient->GetTransactionPinger())
  757. , PingableTx_(
  758. new TPingableTransaction(
  759. parentClient->GetRetryPolicy(),
  760. context,
  761. transactionId,
  762. parentClient->GetTransactionPinger()->GetChildTxPinger(),
  763. options))
  764. , ParentClient_(parentClient)
  765. { }
  766. const TTransactionId& TTransaction::GetId() const
  767. {
  768. return TransactionId_;
  769. }
  770. ILockPtr TTransaction::Lock(
  771. const TYPath& path,
  772. ELockMode mode,
  773. const TLockOptions& options)
  774. {
  775. auto lockId = NRawClient::Lock(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, mode, options);
  776. return ::MakeIntrusive<TLock>(lockId, GetParentClientImpl(), options.Waitable_);
  777. }
  778. void TTransaction::Unlock(
  779. const TYPath& path,
  780. const TUnlockOptions& options)
  781. {
  782. NRawClient::Unlock(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_, path, options);
  783. }
  784. void TTransaction::Commit()
  785. {
  786. PingableTx_->Commit();
  787. }
  788. void TTransaction::Abort()
  789. {
  790. PingableTx_->Abort();
  791. }
  792. void TTransaction::Ping()
  793. {
  794. PingTx(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, TransactionId_);
  795. }
  796. void TTransaction::Detach()
  797. {
  798. PingableTx_->Detach();
  799. }
  800. ITransactionPingerPtr TTransaction::GetTransactionPinger()
  801. {
  802. return TransactionPinger_;
  803. }
  804. TClientPtr TTransaction::GetParentClientImpl()
  805. {
  806. return ParentClient_;
  807. }
  808. ////////////////////////////////////////////////////////////////////////////////
  809. TClient::TClient(
  810. const TClientContext& context,
  811. const TTransactionId& globalId,
  812. IClientRetryPolicyPtr retryPolicy)
  813. : TClientBase(context, globalId, retryPolicy)
  814. , TransactionPinger_(nullptr)
  815. { }
  816. TClient::~TClient() = default;
  817. ITransactionPtr TClient::AttachTransaction(
  818. const TTransactionId& transactionId,
  819. const TAttachTransactionOptions& options)
  820. {
  821. CheckShutdown();
  822. return MakeIntrusive<TTransaction>(this, Context_, transactionId, options);
  823. }
  824. void TClient::MountTable(
  825. const TYPath& path,
  826. const TMountTableOptions& options)
  827. {
  828. CheckShutdown();
  829. THttpHeader header("POST", "mount_table");
  830. SetTabletParams(header, path, options);
  831. if (options.CellId_) {
  832. header.AddParameter("cell_id", GetGuidAsString(*options.CellId_));
  833. }
  834. header.AddParameter("freeze", options.Freeze_);
  835. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header);
  836. }
  837. void TClient::UnmountTable(
  838. const TYPath& path,
  839. const TUnmountTableOptions& options)
  840. {
  841. CheckShutdown();
  842. THttpHeader header("POST", "unmount_table");
  843. SetTabletParams(header, path, options);
  844. header.AddParameter("force", options.Force_);
  845. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header);
  846. }
  847. void TClient::RemountTable(
  848. const TYPath& path,
  849. const TRemountTableOptions& options)
  850. {
  851. CheckShutdown();
  852. THttpHeader header("POST", "remount_table");
  853. SetTabletParams(header, path, options);
  854. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header);
  855. }
  856. void TClient::FreezeTable(
  857. const TYPath& path,
  858. const TFreezeTableOptions& options)
  859. {
  860. CheckShutdown();
  861. NRawClient::FreezeTable(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, path, options);
  862. }
  863. void TClient::UnfreezeTable(
  864. const TYPath& path,
  865. const TUnfreezeTableOptions& options)
  866. {
  867. CheckShutdown();
  868. NRawClient::UnfreezeTable(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, path, options);
  869. }
  870. void TClient::ReshardTable(
  871. const TYPath& path,
  872. const TVector<TKey>& keys,
  873. const TReshardTableOptions& options)
  874. {
  875. CheckShutdown();
  876. THttpHeader header("POST", "reshard_table");
  877. SetTabletParams(header, path, options);
  878. header.AddParameter("pivot_keys", BuildYsonNodeFluently().List(keys));
  879. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header);
  880. }
  881. void TClient::ReshardTable(
  882. const TYPath& path,
  883. i64 tabletCount,
  884. const TReshardTableOptions& options)
  885. {
  886. CheckShutdown();
  887. THttpHeader header("POST", "reshard_table");
  888. SetTabletParams(header, path, options);
  889. header.AddParameter("tablet_count", tabletCount);
  890. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header);
  891. }
  892. void TClient::InsertRows(
  893. const TYPath& path,
  894. const TNode::TListType& rows,
  895. const TInsertRowsOptions& options)
  896. {
  897. CheckShutdown();
  898. THttpHeader header("PUT", "insert_rows");
  899. header.SetInputFormat(TFormat::YsonBinary());
  900. // TODO: use corresponding raw request
  901. header.MergeParameters(SerializeParametersForInsertRows(Context_.Config->Prefix, path, options));
  902. auto body = NodeListToYsonString(rows);
  903. TRequestConfig config;
  904. config.IsHeavy = true;
  905. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header, body, config);
  906. }
  907. void TClient::DeleteRows(
  908. const TYPath& path,
  909. const TNode::TListType& keys,
  910. const TDeleteRowsOptions& options)
  911. {
  912. CheckShutdown();
  913. return NRawClient::DeleteRows(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, path, keys, options);
  914. }
  915. void TClient::TrimRows(
  916. const TYPath& path,
  917. i64 tabletIndex,
  918. i64 rowCount,
  919. const TTrimRowsOptions& options)
  920. {
  921. CheckShutdown();
  922. THttpHeader header("POST", "trim_rows");
  923. header.AddParameter("trimmed_row_count", rowCount);
  924. header.AddParameter("tablet_index", tabletIndex);
  925. // TODO: use corresponding raw request
  926. header.MergeParameters(NRawClient::SerializeParametersForTrimRows(Context_.Config->Prefix, path, options));
  927. TRequestConfig config;
  928. config.IsHeavy = true;
  929. RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header, {}, config);
  930. }
  931. TNode::TListType TClient::LookupRows(
  932. const TYPath& path,
  933. const TNode::TListType& keys,
  934. const TLookupRowsOptions& options)
  935. {
  936. CheckShutdown();
  937. Y_UNUSED(options);
  938. THttpHeader header("PUT", "lookup_rows");
  939. header.AddPath(AddPathPrefix(path, Context_.Config->ApiVersion));
  940. header.SetInputFormat(TFormat::YsonBinary());
  941. header.SetOutputFormat(TFormat::YsonBinary());
  942. header.MergeParameters(BuildYsonNodeFluently().BeginMap()
  943. .DoIf(options.Timeout_.Defined(), [&] (TFluentMap fluent) {
  944. fluent.Item("timeout").Value(static_cast<i64>(options.Timeout_->MilliSeconds()));
  945. })
  946. .Item("keep_missing_rows").Value(options.KeepMissingRows_)
  947. .DoIf(options.Versioned_.Defined(), [&] (TFluentMap fluent) {
  948. fluent.Item("versioned").Value(*options.Versioned_);
  949. })
  950. .DoIf(options.Columns_.Defined(), [&] (TFluentMap fluent) {
  951. fluent.Item("column_names").Value(*options.Columns_);
  952. })
  953. .EndMap());
  954. auto body = NodeListToYsonString(keys);
  955. TRequestConfig config;
  956. config.IsHeavy = true;
  957. auto result = RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header, body, config);
  958. return NodeFromYsonString(result.Response, ::NYson::EYsonType::ListFragment).AsList();
  959. }
  960. TNode::TListType TClient::SelectRows(
  961. const TString& query,
  962. const TSelectRowsOptions& options)
  963. {
  964. CheckShutdown();
  965. THttpHeader header("GET", "select_rows");
  966. header.SetInputFormat(TFormat::YsonBinary());
  967. header.SetOutputFormat(TFormat::YsonBinary());
  968. header.MergeParameters(BuildYsonNodeFluently().BeginMap()
  969. .Item("query").Value(query)
  970. .DoIf(options.Timeout_.Defined(), [&] (TFluentMap fluent) {
  971. fluent.Item("timeout").Value(static_cast<i64>(options.Timeout_->MilliSeconds()));
  972. })
  973. .DoIf(options.InputRowLimit_.Defined(), [&] (TFluentMap fluent) {
  974. fluent.Item("input_row_limit").Value(*options.InputRowLimit_);
  975. })
  976. .DoIf(options.OutputRowLimit_.Defined(), [&] (TFluentMap fluent) {
  977. fluent.Item("output_row_limit").Value(*options.OutputRowLimit_);
  978. })
  979. .Item("range_expansion_limit").Value(options.RangeExpansionLimit_)
  980. .Item("fail_on_incomplete_result").Value(options.FailOnIncompleteResult_)
  981. .Item("verbose_logging").Value(options.VerboseLogging_)
  982. .Item("enable_code_cache").Value(options.EnableCodeCache_)
  983. .EndMap());
  984. TRequestConfig config;
  985. config.IsHeavy = true;
  986. auto result = RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header, {}, config);
  987. return NodeFromYsonString(result.Response, ::NYson::EYsonType::ListFragment).AsList();
  988. }
  989. void TClient::AlterTableReplica(const TReplicaId& replicaId, const TAlterTableReplicaOptions& options)
  990. {
  991. CheckShutdown();
  992. NRawClient::AlterTableReplica(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, replicaId, options);
  993. }
  994. ui64 TClient::GenerateTimestamp()
  995. {
  996. CheckShutdown();
  997. THttpHeader header("GET", "generate_timestamp");
  998. TRequestConfig config;
  999. config.IsHeavy = true;
  1000. auto requestResult = RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header, {}, config);
  1001. return NodeFromYsonString(requestResult.Response).AsUint64();
  1002. }
  1003. TAuthorizationInfo TClient::WhoAmI()
  1004. {
  1005. CheckShutdown();
  1006. THttpHeader header("GET", "auth/whoami", /* isApi = */ false);
  1007. auto requestResult = RetryRequestWithPolicy(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, header);
  1008. TAuthorizationInfo result;
  1009. NJson::TJsonValue jsonValue;
  1010. bool ok = NJson::ReadJsonTree(requestResult.Response, &jsonValue, /* throwOnError = */ true);
  1011. Y_ABORT_UNLESS(ok);
  1012. result.Login = jsonValue["login"].GetString();
  1013. result.Realm = jsonValue["realm"].GetString();
  1014. return result;
  1015. }
  1016. TOperationAttributes TClient::GetOperation(
  1017. const TOperationId& operationId,
  1018. const TGetOperationOptions& options)
  1019. {
  1020. CheckShutdown();
  1021. return NRawClient::GetOperation(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId, options);
  1022. }
  1023. TOperationAttributes TClient::GetOperation(
  1024. const TString& alias,
  1025. const TGetOperationOptions& options)
  1026. {
  1027. CheckShutdown();
  1028. return NRawClient::GetOperation(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, alias, options);
  1029. }
  1030. TListOperationsResult TClient::ListOperations(
  1031. const TListOperationsOptions& options)
  1032. {
  1033. CheckShutdown();
  1034. return NRawClient::ListOperations(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, options);
  1035. }
  1036. void TClient::UpdateOperationParameters(
  1037. const TOperationId& operationId,
  1038. const TUpdateOperationParametersOptions& options)
  1039. {
  1040. CheckShutdown();
  1041. return NRawClient::UpdateOperationParameters(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId, options);
  1042. }
  1043. TJobAttributes TClient::GetJob(
  1044. const TOperationId& operationId,
  1045. const TJobId& jobId,
  1046. const TGetJobOptions& options)
  1047. {
  1048. CheckShutdown();
  1049. return NRawClient::GetJob(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId, jobId, options);
  1050. }
  1051. TListJobsResult TClient::ListJobs(
  1052. const TOperationId& operationId,
  1053. const TListJobsOptions& options)
  1054. {
  1055. CheckShutdown();
  1056. return NRawClient::ListJobs(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId, options);
  1057. }
  1058. IFileReaderPtr TClient::GetJobInput(
  1059. const TJobId& jobId,
  1060. const TGetJobInputOptions& options)
  1061. {
  1062. CheckShutdown();
  1063. return NRawClient::GetJobInput(Context_, jobId, options);
  1064. }
  1065. IFileReaderPtr TClient::GetJobFailContext(
  1066. const TOperationId& operationId,
  1067. const TJobId& jobId,
  1068. const TGetJobFailContextOptions& options)
  1069. {
  1070. CheckShutdown();
  1071. return NRawClient::GetJobFailContext(Context_, operationId, jobId, options);
  1072. }
  1073. IFileReaderPtr TClient::GetJobStderr(
  1074. const TOperationId& operationId,
  1075. const TJobId& jobId,
  1076. const TGetJobStderrOptions& options)
  1077. {
  1078. CheckShutdown();
  1079. return NRawClient::GetJobStderr(Context_, operationId, jobId, options);
  1080. }
  1081. TNode::TListType TClient::SkyShareTable(
  1082. const std::vector<TYPath>& tablePaths,
  1083. const TSkyShareTableOptions& options)
  1084. {
  1085. CheckShutdown();
  1086. return NRawClient::SkyShareTable(
  1087. ClientRetryPolicy_->CreatePolicyForGenericRequest(),
  1088. Context_,
  1089. tablePaths,
  1090. options);
  1091. }
  1092. TCheckPermissionResponse TClient::CheckPermission(
  1093. const TString& user,
  1094. EPermission permission,
  1095. const TYPath& path,
  1096. const TCheckPermissionOptions& options)
  1097. {
  1098. CheckShutdown();
  1099. return NRawClient::CheckPermission(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, user, permission, path, options);
  1100. }
  1101. TVector<TTabletInfo> TClient::GetTabletInfos(
  1102. const TYPath& path,
  1103. const TVector<int>& tabletIndexes,
  1104. const TGetTabletInfosOptions& options)
  1105. {
  1106. CheckShutdown();
  1107. return NRawClient::GetTabletInfos(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, path, tabletIndexes, options);
  1108. }
  1109. void TClient::SuspendOperation(
  1110. const TOperationId& operationId,
  1111. const TSuspendOperationOptions& options)
  1112. {
  1113. CheckShutdown();
  1114. NRawClient::SuspendOperation(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId, options);
  1115. }
  1116. void TClient::ResumeOperation(
  1117. const TOperationId& operationId,
  1118. const TResumeOperationOptions& options)
  1119. {
  1120. CheckShutdown();
  1121. NRawClient::ResumeOperation(ClientRetryPolicy_->CreatePolicyForGenericRequest(), Context_, operationId, options);
  1122. }
  1123. TYtPoller& TClient::GetYtPoller()
  1124. {
  1125. auto g = Guard(Lock_);
  1126. if (!YtPoller_) {
  1127. CheckShutdown();
  1128. // We don't use current client and create new client because YtPoller_ might use
  1129. // this client during current client shutdown.
  1130. // That might lead to incrementing of current client refcount and double delete of current client object.
  1131. YtPoller_ = MakeHolder<TYtPoller>(Context_, ClientRetryPolicy_);
  1132. }
  1133. return *YtPoller_;
  1134. }
  1135. void TClient::Shutdown()
  1136. {
  1137. auto g = Guard(Lock_);
  1138. if (!Shutdown_.exchange(true) && YtPoller_) {
  1139. YtPoller_->Stop();
  1140. }
  1141. }
  1142. ITransactionPingerPtr TClient::GetTransactionPinger()
  1143. {
  1144. auto g = Guard(Lock_);
  1145. if (!TransactionPinger_) {
  1146. TransactionPinger_ = CreateTransactionPinger(Context_.Config);
  1147. }
  1148. return TransactionPinger_;
  1149. }
  1150. TClientPtr TClient::GetParentClientImpl()
  1151. {
  1152. return this;
  1153. }
  1154. template <class TOptions>
  1155. void TClient::SetTabletParams(
  1156. THttpHeader& header,
  1157. const TYPath& path,
  1158. const TOptions& options)
  1159. {
  1160. header.AddPath(AddPathPrefix(path, Context_.Config->Prefix));
  1161. if (options.FirstTabletIndex_) {
  1162. header.AddParameter("first_tablet_index", *options.FirstTabletIndex_);
  1163. }
  1164. if (options.LastTabletIndex_) {
  1165. header.AddParameter("last_tablet_index", *options.LastTabletIndex_);
  1166. }
  1167. }
  1168. void TClient::CheckShutdown() const
  1169. {
  1170. if (Shutdown_) {
  1171. ythrow TApiUsageError() << "Call client's methods after shutdown";
  1172. }
  1173. }
  1174. TClientPtr CreateClientImpl(
  1175. const TString& serverName,
  1176. const TCreateClientOptions& options)
  1177. {
  1178. TClientContext context;
  1179. context.Config = options.Config_ ? options.Config_ : TConfig::Get();
  1180. context.TvmOnly = options.TvmOnly_;
  1181. context.ProxyAddress = options.ProxyAddress_;
  1182. context.ServerName = serverName;
  1183. if (context.ServerName.find('.') == TString::npos &&
  1184. context.ServerName.find(':') == TString::npos)
  1185. {
  1186. context.ServerName += ".yt.yandex.net";
  1187. }
  1188. static constexpr char httpUrlSchema[] = "http://";
  1189. static constexpr char httpsUrlSchema[] = "https://";
  1190. if (options.UseTLS_) {
  1191. context.UseTLS = *options.UseTLS_;
  1192. } else {
  1193. context.UseTLS = context.ServerName.StartsWith(httpsUrlSchema);
  1194. }
  1195. if (context.ServerName.StartsWith(httpUrlSchema)) {
  1196. if (context.UseTLS) {
  1197. ythrow TApiUsageError() << "URL schema doesn't match UseTLS option";
  1198. }
  1199. context.ServerName.erase(0, sizeof(httpUrlSchema) - 1);
  1200. }
  1201. if (context.ServerName.StartsWith(httpsUrlSchema)) {
  1202. if (!context.UseTLS) {
  1203. ythrow TApiUsageError() << "URL schema doesn't match UseTLS option";
  1204. }
  1205. context.ServerName.erase(0, sizeof(httpsUrlSchema) - 1);
  1206. }
  1207. if (context.ServerName.find(':') == TString::npos) {
  1208. context.ServerName = CreateHostNameWithPort(context.ServerName, context);
  1209. }
  1210. if (options.TvmOnly_) {
  1211. context.ServerName = Format("tvm.%v", context.ServerName);
  1212. }
  1213. if (context.UseTLS || options.UseCoreHttpClient_) {
  1214. context.HttpClient = NHttpClient::CreateCoreHttpClient(context.UseTLS, context.Config);
  1215. } else {
  1216. context.HttpClient = NHttpClient::CreateDefaultHttpClient();
  1217. }
  1218. context.Token = context.Config->Token;
  1219. if (options.Token_) {
  1220. context.Token = options.Token_;
  1221. } else if (options.TokenPath_) {
  1222. context.Token = TConfig::LoadTokenFromFile(options.TokenPath_);
  1223. } else if (options.ServiceTicketAuth_) {
  1224. context.ServiceTicketAuth = options.ServiceTicketAuth_;
  1225. }
  1226. context.ImpersonationUser = options.ImpersonationUser_;
  1227. if (context.Token) {
  1228. TConfig::ValidateToken(context.Token);
  1229. }
  1230. auto globalTxId = GetGuid(context.Config->GlobalTxId);
  1231. auto retryConfigProvider = options.RetryConfigProvider_;
  1232. if (!retryConfigProvider) {
  1233. retryConfigProvider = CreateDefaultRetryConfigProvider();
  1234. }
  1235. return new NDetail::TClient(context, globalTxId, CreateDefaultClientRetryPolicy(retryConfigProvider, context.Config));
  1236. }
  1237. ////////////////////////////////////////////////////////////////////////////////
  1238. } // namespace NDetail
  1239. ////////////////////////////////////////////////////////////////////////////////
  1240. IClientPtr CreateClient(
  1241. const TString& serverName,
  1242. const TCreateClientOptions& options)
  1243. {
  1244. return NDetail::CreateClientImpl(serverName, options);
  1245. }
  1246. IClientPtr CreateClientFromEnv(const TCreateClientOptions& options)
  1247. {
  1248. auto serverName = GetEnv("YT_PROXY");
  1249. if (!serverName) {
  1250. ythrow yexception() << "YT_PROXY is not set";
  1251. }
  1252. return NDetail::CreateClientImpl(serverName, options);
  1253. }
  1254. ////////////////////////////////////////////////////////////////////////////////
  1255. } // namespace NYT