operation.h 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549
  1. #pragma once
  2. ///
  3. /// @file yt/cpp/mapreduce/interface/operation.h
  4. ///
  5. /// Header containing interface to run operations in YT
  6. /// and retrieve information about them.
  7. /// @see [the doc](https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map_reduce_overview.html).
  8. #include "client_method_options.h"
  9. #include "errors.h"
  10. #include "io.h"
  11. #include "job_statistics.h"
  12. #include "job_counters.h"
  13. #include <library/cpp/threading/future/future.h>
  14. #include <library/cpp/type_info/type_info.h>
  15. #include <util/datetime/base.h>
  16. #include <util/generic/variant.h>
  17. #include <util/generic/vector.h>
  18. #include <util/generic/maybe.h>
  19. #include <util/system/file.h>
  20. #include <util/system/types.h>
  21. namespace NYT {
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /// Tag class marking that the row type for table is not specified.
  24. struct TUnspecifiedTableStructure
  25. { };
  26. /// Tag class marking that table rows have protobuf type.
  27. struct TProtobufTableStructure
  28. {
  29. /// @brief Descriptor of the protobuf type of table rows.
  30. ///
  31. /// @note If table is tagged with @ref ::google::protobuf::Message instead of real proto class
  32. /// this descriptor might be null.
  33. const ::google::protobuf::Descriptor* Descriptor = nullptr;
  34. };
  35. /// Tag class to specify table row type.
  36. using TTableStructure = std::variant<
  37. TUnspecifiedTableStructure,
  38. TProtobufTableStructure
  39. >;
  40. bool operator==(const TUnspecifiedTableStructure&, const TUnspecifiedTableStructure&);
  41. bool operator==(const TProtobufTableStructure& lhs, const TProtobufTableStructure& rhs);
  42. /// Table path marked with @ref NYT::TTableStructure tag.
  43. struct TStructuredTablePath
  44. {
  45. TStructuredTablePath(TRichYPath richYPath = TRichYPath(), TTableStructure description = TUnspecifiedTableStructure())
  46. : RichYPath(std::move(richYPath))
  47. , Description(std::move(description))
  48. { }
  49. TStructuredTablePath(TRichYPath richYPath, const ::google::protobuf::Descriptor* descriptor)
  50. : RichYPath(std::move(richYPath))
  51. , Description(TProtobufTableStructure({descriptor}))
  52. { }
  53. TStructuredTablePath(TYPath path)
  54. : RichYPath(std::move(path))
  55. , Description(TUnspecifiedTableStructure())
  56. { }
  57. TStructuredTablePath(const char* path)
  58. : RichYPath(path)
  59. , Description(TUnspecifiedTableStructure())
  60. { }
  61. TRichYPath RichYPath;
  62. TTableStructure Description;
  63. };
  64. /// Create marked table path from row type.
  65. template <typename TRow>
  66. TStructuredTablePath Structured(TRichYPath richYPath);
  67. /// Create tag class from row type.
  68. template <typename TRow>
  69. TTableStructure StructuredTableDescription();
  70. ///////////////////////////////////////////////////////////////////////////////
  71. /// Tag class marking that row stream is empty.
  72. struct TVoidStructuredRowStream
  73. { };
  74. /// Tag class marking that row stream consists of `NYT::TNode`.
  75. struct TTNodeStructuredRowStream
  76. { };
  77. /// Tag class marking that row stream consists of @ref NYT::TYaMRRow.
  78. struct TTYaMRRowStructuredRowStream
  79. { };
  80. /// Tag class marking that row stream consists of protobuf rows of given type.
  81. struct TProtobufStructuredRowStream
  82. {
  83. /// @brief Descriptor of the protobuf type of table rows.
  84. ///
  85. /// @note If `Descriptor` is nullptr, then row stream consists of multiple message types.
  86. const ::google::protobuf::Descriptor* Descriptor = nullptr;
  87. };
  88. /// Tag class to specify type of rows in an operation row stream
  89. using TStructuredRowStreamDescription = std::variant<
  90. TVoidStructuredRowStream,
  91. TTNodeStructuredRowStream,
  92. TTYaMRRowStructuredRowStream,
  93. TProtobufStructuredRowStream
  94. >;
  95. ///////////////////////////////////////////////////////////////////////////////
  96. /// Tag class marking that current binary should be used in operation.
  97. struct TJobBinaryDefault
  98. { };
  99. /// Tag class marking that binary from specified local path should be used in operation.
  100. struct TJobBinaryLocalPath
  101. {
  102. TString Path;
  103. TMaybe<TString> MD5CheckSum;
  104. };
  105. /// Tag class marking that binary from specified Cypress path should be used in operation.
  106. struct TJobBinaryCypressPath
  107. {
  108. TYPath Path;
  109. TMaybe<TTransactionId> TransactionId;
  110. };
  111. ////////////////////////////////////////////////////////////////////////////////
  112. /// @cond Doxygen_Suppress
  113. namespace NDetail {
  114. extern i64 OutputTableCount;
  115. } // namespace NDetail
  116. /// @endcond
  117. ////////////////////////////////////////////////////////////////////////////////
  118. ///
  119. /// @brief Auto merge mode.
  120. ///
  121. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/automerge
  122. enum class EAutoMergeMode
  123. {
  124. /// Auto merge is disabled.
  125. Disabled /* "disabled" */,
  126. /// Mode that tries to achieve good chunk sizes and doesn't limit usage of chunk quota for intermediate chunks.
  127. Relaxed /* "relaxed" */,
  128. /// Mode that tries to optimize usage of chunk quota for intermediate chunks, operation might run slower.
  129. Economy /* "economy" */,
  130. ///
  131. /// @brief Manual configuration of automerge parameters.
  132. ///
  133. /// @ref TAutoMergeSpec
  134. Manual /* "manual" */,
  135. };
  136. ///
  137. /// @brief Options for auto merge operation stage.
  138. ///
  139. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/automerge
  140. class TAutoMergeSpec
  141. {
  142. public:
  143. /// @cond Doxygen_Suppress
  144. using TSelf = TAutoMergeSpec;
  145. /// @endcond
  146. /// Mode of the auto merge.
  147. FLUENT_FIELD_OPTION(EAutoMergeMode, Mode);
  148. /// @brief Upper limit for number of intermediate chunks.
  149. ///
  150. /// Works only for Manual mode.
  151. FLUENT_FIELD_OPTION(i64, MaxIntermediateChunkCount);
  152. /// @brief Number of chunks limit to merge in one job.
  153. ///
  154. /// Works only for Manual mode.
  155. FLUENT_FIELD_OPTION(i64, ChunkCountPerMergeJob);
  156. /// @brief Automerge will not merge chunks that are larger than `DesiredChunkSize * (ChunkSizeThreshold / 100.)`
  157. ///
  158. /// Works only for Manual mode.
  159. FLUENT_FIELD_OPTION(i64, ChunkSizeThreshold);
  160. };
  161. /// Base for operations with auto merge options.
  162. template <class TDerived>
  163. class TWithAutoMergeSpec
  164. {
  165. public:
  166. /// @cond Doxygen_Suppress
  167. using TSelf = TDerived;
  168. /// @endcond
  169. /// @brief Options for auto merge operation stage.
  170. ///
  171. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/automerge
  172. FLUENT_FIELD_OPTION(TAutoMergeSpec, AutoMerge);
  173. };
  174. ///
  175. /// @brief Resources controlled by scheduler and used by running operations.
  176. ///
  177. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/scheduler/scheduler-and-pools#resources
  178. class TSchedulerResources
  179. {
  180. public:
  181. /// @cond Doxygen_Suppress
  182. using TSelf = TSchedulerResources;
  183. /// @endcond
  184. /// Each job consumes exactly one user slot.
  185. FLUENT_FIELD_OPTION_ENCAPSULATED(i64, UserSlots);
  186. /// Number of (virtual) cpu cores consumed by all jobs.
  187. FLUENT_FIELD_OPTION_ENCAPSULATED(i64, Cpu);
  188. /// Amount of memory in bytes.
  189. FLUENT_FIELD_OPTION_ENCAPSULATED(i64, Memory);
  190. };
  191. /// Base for input format hints of a user job.
  192. template <class TDerived>
  193. class TUserJobInputFormatHintsBase
  194. {
  195. public:
  196. /// @cond Doxygen_Suppress
  197. using TSelf = TDerived;
  198. /// @endcond
  199. /// @brief Fine tune input format of the job.
  200. FLUENT_FIELD_OPTION(TFormatHints, InputFormatHints);
  201. };
  202. /// Base for output format hints of a user job.
  203. template <class TDerived>
  204. class TUserJobOutputFormatHintsBase
  205. {
  206. public:
  207. /// @cond Doxygen_Suppress
  208. using TSelf = TDerived;
  209. /// @endcond
  210. /// @brief Fine tune output format of the job.
  211. FLUENT_FIELD_OPTION(TFormatHints, OutputFormatHints);
  212. };
  213. /// Base for format hints of a user job.
  214. template <class TDerived>
  215. class TUserJobFormatHintsBase
  216. : public TUserJobInputFormatHintsBase<TDerived>
  217. , public TUserJobOutputFormatHintsBase<TDerived>
  218. {
  219. public:
  220. /// @cond Doxygen_Suppress
  221. using TSelf = TDerived;
  222. /// @endcond
  223. };
  224. /// User job format hints.
  225. class TUserJobFormatHints
  226. : public TUserJobFormatHintsBase<TUserJobFormatHints>
  227. { };
  228. /// Spec of input and output tables of a raw operation.
  229. template <class TDerived>
  230. class TRawOperationIoTableSpec
  231. {
  232. public:
  233. /// @cond Doxygen_Suppress
  234. using TSelf = TDerived;
  235. /// @endcond
  236. /// Add input table path to input path list.
  237. TDerived& AddInput(const TRichYPath& path);
  238. /// Set input table path no. `tableIndex`.
  239. TDerived& SetInput(size_t tableIndex, const TRichYPath& path);
  240. /// Add output table path to output path list.
  241. TDerived& AddOutput(const TRichYPath& path);
  242. /// Set output table path no. `tableIndex`.
  243. TDerived& SetOutput(size_t tableIndex, const TRichYPath& path);
  244. /// Get all input table paths.
  245. const TVector<TRichYPath>& GetInputs() const;
  246. /// Get all output table paths.
  247. const TVector<TRichYPath>& GetOutputs() const;
  248. private:
  249. TVector<TRichYPath> Inputs_;
  250. TVector<TRichYPath> Outputs_;
  251. };
  252. /// Base spec for IO in "simple" raw operations (Map, Reduce etc.).
  253. template <class TDerived>
  254. struct TSimpleRawOperationIoSpec
  255. : public TRawOperationIoTableSpec<TDerived>
  256. {
  257. /// @cond Doxygen_Suppress
  258. using TSelf = TDerived;
  259. /// @endcond
  260. /// @brief Describes format for both input and output.
  261. ///
  262. /// @note `Format' is overridden by `InputFormat' and `OutputFormat'.
  263. FLUENT_FIELD_OPTION(TFormat, Format);
  264. /// Describes input format.
  265. FLUENT_FIELD_OPTION(TFormat, InputFormat);
  266. /// Describes output format.
  267. FLUENT_FIELD_OPTION(TFormat, OutputFormat);
  268. };
  269. /// Spec for IO in MapReduce operation.
  270. template <class TDerived>
  271. class TRawMapReduceOperationIoSpec
  272. : public TRawOperationIoTableSpec<TDerived>
  273. {
  274. public:
  275. /// @cond Doxygen_Suppress
  276. using TSelf = TDerived;
  277. /// @endcond
  278. /// @brief Describes format for both input and output of mapper.
  279. ///
  280. /// @note `MapperFormat' is overridden by `MapperInputFormat' and `MapperOutputFormat'.
  281. FLUENT_FIELD_OPTION(TFormat, MapperFormat);
  282. /// Describes mapper input format.
  283. FLUENT_FIELD_OPTION(TFormat, MapperInputFormat);
  284. /// Describes mapper output format.
  285. FLUENT_FIELD_OPTION(TFormat, MapperOutputFormat);
  286. /// @brief Describes format for both input and output of reduce combiner.
  287. ///
  288. /// @note `ReduceCombinerFormat' is overridden by `ReduceCombinerInputFormat' and `ReduceCombinerOutputFormat'.
  289. FLUENT_FIELD_OPTION(TFormat, ReduceCombinerFormat);
  290. /// Describes reduce combiner input format.
  291. FLUENT_FIELD_OPTION(TFormat, ReduceCombinerInputFormat);
  292. /// Describes reduce combiner output format.
  293. FLUENT_FIELD_OPTION(TFormat, ReduceCombinerOutputFormat);
  294. /// @brief Describes format for both input and output of reducer.
  295. ///
  296. /// @note `ReducerFormat' is overridden by `ReducerInputFormat' and `ReducerOutputFormat'.
  297. FLUENT_FIELD_OPTION(TFormat, ReducerFormat);
  298. /// Describes reducer input format.
  299. FLUENT_FIELD_OPTION(TFormat, ReducerInputFormat);
  300. /// Describes reducer output format.
  301. FLUENT_FIELD_OPTION(TFormat, ReducerOutputFormat);
  302. /// Add direct map output table path.
  303. TDerived& AddMapOutput(const TRichYPath& path);
  304. /// Set direct map output table path no. `tableIndex`.
  305. TDerived& SetMapOutput(size_t tableIndex, const TRichYPath& path);
  306. /// Get all direct map output table paths
  307. const TVector<TRichYPath>& GetMapOutputs() const;
  308. private:
  309. TVector<TRichYPath> MapOutputs_;
  310. };
  311. ///
  312. /// @brief Base spec of operations with input tables.
  313. class TOperationInputSpecBase
  314. {
  315. public:
  316. template <class T, class = void>
  317. struct TFormatAdder;
  318. ///
  319. /// @brief Add input table path to input path list and specify type of rows.
  320. template <class T>
  321. void AddInput(const TRichYPath& path);
  322. ///
  323. /// @brief Add input table path as structured paths.
  324. void AddStructuredInput(TStructuredTablePath path);
  325. ///
  326. /// @brief Set input table path and type.
  327. template <class T>
  328. void SetInput(size_t tableIndex, const TRichYPath& path);
  329. ///
  330. /// @brief All input paths.
  331. TVector<TRichYPath> Inputs_;
  332. ///
  333. /// @brief Get all input structured paths.
  334. const TVector<TStructuredTablePath>& GetStructuredInputs() const;
  335. private:
  336. TVector<TStructuredTablePath> StructuredInputs_;
  337. friend struct TOperationIOSpecBase;
  338. template <class T>
  339. friend struct TOperationIOSpec;
  340. };
  341. ///
  342. /// @brief Base spec of operations with output tables.
  343. class TOperationOutputSpecBase
  344. {
  345. public:
  346. template <class T, class = void>
  347. struct TFormatAdder;
  348. ///
  349. /// @brief Add output table path to output path list and specify type of rows.
  350. template <class T>
  351. void AddOutput(const TRichYPath& path);
  352. ///
  353. /// @brief Add output table path as structured paths.
  354. void AddStructuredOutput(TStructuredTablePath path);
  355. ///
  356. /// @brief Set output table path and type.
  357. template <class T>
  358. void SetOutput(size_t tableIndex, const TRichYPath& path);
  359. ///
  360. /// @brief All output paths.
  361. TVector<TRichYPath> Outputs_;
  362. ///
  363. /// @brief Get all output structured paths.
  364. const TVector<TStructuredTablePath>& GetStructuredOutputs() const;
  365. private:
  366. TVector<TStructuredTablePath> StructuredOutputs_;
  367. friend struct TOperationIOSpecBase;
  368. template <class T>
  369. friend struct TOperationIOSpec;
  370. };
  371. ///
  372. /// @brief Base spec for operations with inputs and outputs.
  373. struct TOperationIOSpecBase
  374. : public TOperationInputSpecBase
  375. , public TOperationOutputSpecBase
  376. { };
  377. ///
  378. /// @brief Base spec for operations with inputs and outputs.
  379. template <class TDerived>
  380. struct TOperationIOSpec
  381. : public TOperationIOSpecBase
  382. {
  383. /// @cond Doxygen_Suppress
  384. using TSelf = TDerived;
  385. /// @endcond
  386. template <class T>
  387. TDerived& AddInput(const TRichYPath& path);
  388. TDerived& AddStructuredInput(TStructuredTablePath path);
  389. template <class T>
  390. TDerived& SetInput(size_t tableIndex, const TRichYPath& path);
  391. template <class T>
  392. TDerived& AddOutput(const TRichYPath& path);
  393. TDerived& AddStructuredOutput(TStructuredTablePath path);
  394. template <class T>
  395. TDerived& SetOutput(size_t tableIndex, const TRichYPath& path);
  396. // DON'T USE THESE METHODS! They are left solely for backward compatibility.
  397. // These methods are the only way to do equivalent of (Add/Set)(Input/Output)<Message>
  398. // but please consider using (Add/Set)(Input/Output)<TConcreteMessage>
  399. // (where TConcreteMessage is some descendant of Message)
  400. // because they are faster and better (see https://st.yandex-team.ru/YT-6967)
  401. TDerived& AddProtobufInput_VerySlow_Deprecated(const TRichYPath& path);
  402. TDerived& AddProtobufOutput_VerySlow_Deprecated(const TRichYPath& path);
  403. };
  404. ///
  405. /// @brief Base spec for all operations.
  406. ///
  407. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/operations_options
  408. template <class TDerived>
  409. struct TOperationSpecBase
  410. {
  411. /// @cond Doxygen_Suppress
  412. using TSelf = TDerived;
  413. /// @endcond
  414. ///
  415. /// @brief Limit on operation execution time.
  416. ///
  417. /// If operation doesn't finish in time it will be aborted.
  418. FLUENT_FIELD_OPTION(TDuration, TimeLimit);
  419. /// @brief Title to be shown in web interface.
  420. FLUENT_FIELD_OPTION(TString, Title);
  421. /// @brief Pool to be used for this operation.
  422. FLUENT_FIELD_OPTION(TString, Pool);
  423. /// @brief Weight of operation.
  424. ///
  425. /// Coefficient defining how much resources operation gets relative to its siblings in the same pool.
  426. FLUENT_FIELD_OPTION(double, Weight);
  427. /// @brief Pool tree list that operation will use.
  428. FLUENT_OPTIONAL_VECTOR_FIELD_ENCAPSULATED(TString, PoolTree);
  429. /// How much resources can be consumed by operation.
  430. FLUENT_FIELD_OPTION_ENCAPSULATED(TSchedulerResources, ResourceLimits);
  431. /// How many jobs can fail before operation is failed.
  432. FLUENT_FIELD_OPTION(ui64, MaxFailedJobCount);
  433. };
  434. ///
  435. /// @brief Base spec for all operations with user jobs.
  436. template <class TDerived>
  437. struct TUserOperationSpecBase
  438. : TOperationSpecBase<TDerived>
  439. {
  440. /// @cond Doxygen_Suppress
  441. using TSelf = TDerived;
  442. /// @endcond
  443. /// On any unsuccessful job completion (i.e. abortion or failure) force the whole operation to fail.
  444. FLUENT_FIELD_OPTION(bool, FailOnJobRestart);
  445. ///
  446. /// @brief Table to save whole stderr of operation.
  447. ///
  448. /// @see https://clubs.at.yandex-team.ru/yt/1045
  449. FLUENT_FIELD_OPTION(TYPath, StderrTablePath);
  450. ///
  451. /// @brief Table to save coredumps of operation.
  452. ///
  453. /// @see https://clubs.at.yandex-team.ru/yt/1045
  454. FLUENT_FIELD_OPTION(TYPath, CoreTablePath);
  455. ///
  456. /// @brief How long should the scheduler wait for the job to be started on a node.
  457. ///
  458. /// When you run huge jobs that require preemption of all the other jobs on
  459. /// a node, the default timeout might be insufficient and your job may be
  460. /// aborted with 'waiting_timeout' reason. This is especially problematic
  461. /// when you are setting 'FailOnJobRestart' option.
  462. ///
  463. /// @note The value must be between 10 seconds and 10 minutes.
  464. FLUENT_FIELD_OPTION(TDuration, WaitingJobTimeout);
  465. };
  466. ///
  467. /// @brief Class to provide information on intermediate mapreduce stream protobuf types.
  468. ///
  469. /// When using protobuf format it is important to know exact types of proto messages
  470. /// that are used in input/output.
  471. ///
  472. /// Sometimes such messages cannot be derived from job class
  473. /// i.e. when job class uses `NYT::TTableReader<::google::protobuf::Message>`
  474. /// or `NYT::TTableWriter<::google::protobuf::Message>`.
  475. ///
  476. /// When using such jobs user can provide exact message type using this class.
  477. ///
  478. /// @note Only input/output that relate to intermediate tables can be hinted.
  479. /// Input to map and output of reduce is derived from `AddInput`/`AddOutput`.
  480. template <class TDerived>
  481. struct TIntermediateTablesHintSpec
  482. {
  483. /// Specify intermediate map output type.
  484. template <class T>
  485. TDerived& HintMapOutput();
  486. /// Specify reduce combiner input.
  487. template <class T>
  488. TDerived& HintReduceCombinerInput();
  489. /// Specify reduce combiner output.
  490. template <class T>
  491. TDerived& HintReduceCombinerOutput();
  492. /// Specify reducer input.
  493. template <class T>
  494. TDerived& HintReduceInput();
  495. ///
  496. /// @brief Add output of map stage.
  497. ///
  498. /// Mapper output table #0 is always intermediate table that is going to be reduced later.
  499. /// Rows that mapper write to tables #1, #2, ... are saved in MapOutput tables.
  500. template <class T>
  501. TDerived& AddMapOutput(const TRichYPath& path);
  502. TVector<TRichYPath> MapOutputs_;
  503. const TVector<TStructuredTablePath>& GetStructuredMapOutputs() const;
  504. const TMaybe<TTableStructure>& GetIntermediateMapOutputDescription() const;
  505. const TMaybe<TTableStructure>& GetIntermediateReduceCombinerInputDescription() const;
  506. const TMaybe<TTableStructure>& GetIntermediateReduceCombinerOutputDescription() const;
  507. const TMaybe<TTableStructure>& GetIntermediateReducerInputDescription() const;
  508. private:
  509. TVector<TStructuredTablePath> StructuredMapOutputs_;
  510. TMaybe<TTableStructure> IntermediateMapOutputDescription_;
  511. TMaybe<TTableStructure> IntermediateReduceCombinerInputDescription_;
  512. TMaybe<TTableStructure> IntermediateReduceCombinerOutputDescription_;
  513. TMaybe<TTableStructure> IntermediateReducerInputDescription_;
  514. };
  515. ////////////////////////////////////////////////////////////////////////////////
  516. struct TAddLocalFileOptions
  517. {
  518. /// @cond Doxygen_Suppress
  519. using TSelf = TAddLocalFileOptions;
  520. /// @endcond
  521. ///
  522. /// @brief Path by which job will see the uploaded file.
  523. ///
  524. /// Defaults to basename of the local path.
  525. FLUENT_FIELD_OPTION(TString, PathInJob);
  526. ///
  527. /// @brief MD5 checksum of uploaded file.
  528. ///
  529. /// If not specified it is computed by this library.
  530. /// If this argument is provided, the user can some cpu and disk IO.
  531. FLUENT_FIELD_OPTION(TString, MD5CheckSum);
  532. ///
  533. /// @brief Do not put file into node cache
  534. ///
  535. /// @see NYT::TRichYPath::BypassArtifactCache
  536. FLUENT_FIELD_OPTION(bool, BypassArtifactCache);
  537. };
  538. ////////////////////////////////////////////////////////////////////////////////
  539. /// @brief Binary to run job profiler on.
  540. enum class EProfilingBinary
  541. {
  542. /// Profile job proxy.
  543. JobProxy /* "job_proxy" */,
  544. /// Profile user job.
  545. UserJob /* "user_job" */,
  546. };
  547. /// @brief Type of job profiler.
  548. enum class EProfilerType
  549. {
  550. /// Profile CPU usage.
  551. Cpu /* "cpu" */,
  552. /// Profile memory usage.
  553. Memory /* "memory" */,
  554. /// Profiler peak memory usage.
  555. PeakMemory /* "peak_memory" */,
  556. };
  557. /// @brief Specifies a job profiler.
  558. struct TJobProfilerSpec
  559. {
  560. /// @cond Doxygen_Suppress
  561. using TSelf = TJobProfilerSpec;
  562. /// @endcond
  563. /// @brief Binary to profile.
  564. FLUENT_FIELD_OPTION(EProfilingBinary, ProfilingBinary);
  565. /// @brief Type of the profiler.
  566. FLUENT_FIELD_OPTION(EProfilerType, ProfilerType);
  567. /// @brief Probability of the job being selected for profiling.
  568. FLUENT_FIELD_OPTION(double, ProfilingProbability);
  569. /// @brief For sampling profilers, sets the number of samples per second.
  570. FLUENT_FIELD_OPTION(int, SamplingFrequency);
  571. };
  572. /////////////////////////////////////////////////////////////////////////////
  573. /// @brief Specification of a disk that will be available in job.
  574. ///
  575. /// Disk request should be used in case job requires specific requirements for disk (i.e. it requires NVME or SSD).
  576. ///
  577. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/operations-options#disk_request
  578. struct TDiskRequest
  579. {
  580. /// @cond Doxygen_Suppress
  581. using TSelf = TDiskRequest;
  582. /// @endcond
  583. /// Required disk space in bytes.
  584. FLUENT_FIELD_OPTION(i64, DiskSpace);
  585. /// Limit for inodes.
  586. FLUENT_FIELD_OPTION(i64, InodeCount);
  587. /// Account which quota is going to be used.
  588. /// Account must have available quota for the specified medium.
  589. FLUENT_FIELD_OPTION(TString, Account);
  590. /// Name of the medium corresponding to required disk type.
  591. FLUENT_FIELD_OPTION(TString, MediumName);
  592. };
  593. ////////////////////////////////////////////////////////////////////////////////
  594. ///
  595. /// @brief Spec of user job.
  596. ///
  597. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/operations-options#user_script_options
  598. struct TUserJobSpec
  599. {
  600. /// @cond Doxygen_Suppress
  601. using TSelf = TUserJobSpec;
  602. /// @endcond
  603. ///
  604. /// @brief Specify a local file to upload to Cypress and prepare for use in job.
  605. TSelf& AddLocalFile(const TLocalFilePath& path, const TAddLocalFileOptions& options = TAddLocalFileOptions());
  606. ///
  607. /// @brief Get the list of all added local files.
  608. TVector<std::tuple<TLocalFilePath, TAddLocalFileOptions>> GetLocalFiles() const;
  609. /// @brief Paths to files in Cypress to use in job.
  610. FLUENT_VECTOR_FIELD(TRichYPath, File);
  611. /// @brief Porto layers to use in the job. Layers are listed from top to bottom.
  612. FLUENT_VECTOR_FIELD(TYPath, Layer);
  613. ///
  614. /// @brief MemoryLimit specifies how much memory job process can use.
  615. ///
  616. /// @note
  617. /// If job uses tmpfs (check @ref NYT::TOperationOptions::MountSandboxInTmpfs)
  618. /// YT computes its memory usage as total of:
  619. /// - memory usage of job process itself (including mapped files);
  620. /// - total size of tmpfs used by this job.
  621. ///
  622. /// @note
  623. /// When @ref NYT::TOperationOptions::MountSandboxInTmpfs is enabled library will compute
  624. /// total size of all files used by this job and add this total size to MemoryLimit.
  625. /// Thus you shouldn't include size of your files (e.g. binary file) into MemoryLimit.
  626. ///
  627. /// @note
  628. /// Final memory memory_limit passed to YT is calculated as follows:
  629. ///
  630. /// @note
  631. /// ```
  632. /// memory_limit = MemoryLimit + <total-size-of-used-files> + ExtraTmpfsSize
  633. /// ```
  634. ///
  635. /// @see NYT::TUserJobSpec::ExtraTmpfsSize
  636. FLUENT_FIELD_OPTION(i64, MemoryLimit);
  637. ///
  638. /// @brief Size of data that is going to be written to tmpfs.
  639. ///
  640. /// This option should be used if job writes data to tmpfs.
  641. ///
  642. /// ExtraTmpfsSize should not include size of files specified with
  643. /// @ref NYT::TUserJobSpec::AddLocalFile or @ref NYT::TUserJobSpec::AddFile
  644. /// These files are copied to tmpfs automatically and their total size
  645. /// is computed automatically.
  646. ///
  647. /// @see NYT::TOperationOptions::MountSandboxInTmpfs
  648. /// @see NYT::TUserJobSpec::MemoryLimit
  649. FLUENT_FIELD_OPTION(i64, ExtraTmpfsSize);
  650. ///
  651. /// @brief Maximum number of CPU cores for a single job to use.
  652. FLUENT_FIELD_OPTION(double, CpuLimit);
  653. ///
  654. /// @brief Fraction of @ref NYT::TUserJobSpec::MemoryLimit that job gets at start.
  655. ///
  656. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/operations-options#memory_reserve_factor
  657. FLUENT_FIELD_OPTION(double, MemoryReserveFactor);
  658. ///
  659. /// @brief Local path to executable to be used inside jobs.
  660. ////
  661. /// Provided executable must use C++ YT API library (this library)
  662. /// and implement job class that is going to be used.
  663. ///
  664. /// This option might be useful if we want to start operation from nonlinux machines
  665. /// (in that case we use `JobBinary` to provide path to the same program compiled for linux).
  666. /// Other example of using this option is uploading executable to cypress in advance
  667. /// and save the time required to upload current executable to cache.
  668. /// `md5` argument can be used to save cpu time and disk IO when binary MD5 checksum is known.
  669. /// When argument is not provided library will compute it itself.
  670. TUserJobSpec& JobBinaryLocalPath(TString path, TMaybe<TString> md5 = Nothing());
  671. ///
  672. /// @brief Cypress path to executable to be used inside jobs.
  673. TUserJobSpec& JobBinaryCypressPath(TString path, TMaybe<TTransactionId> transactionId = Nothing());
  674. ///
  675. /// @brief String that will be prepended to the command.
  676. ///
  677. /// This option overrides @ref NYT::TOperationOptions::JobCommandPrefix.
  678. FLUENT_FIELD(TString, JobCommandPrefix);
  679. ///
  680. /// @brief String that will be appended to the command.
  681. ///
  682. /// This option overrides @ref NYT::TOperationOptions::JobCommandSuffix.
  683. FLUENT_FIELD(TString, JobCommandSuffix);
  684. ///
  685. /// @brief Map of environment variables that will be set for jobs.
  686. FLUENT_MAP_FIELD(TString, TString, Environment);
  687. ///
  688. /// @brief Limit for all files inside job sandbox (in bytes).
  689. FLUENT_FIELD_OPTION(ui64, DiskSpaceLimit);
  690. ///
  691. /// @brief Number of ports reserved for the job (passed through environment in YT_PORT_0, YT_PORT_1, ...).
  692. FLUENT_FIELD_OPTION(ui16, PortCount);
  693. ///
  694. /// @brief Network project used to isolate job network.
  695. FLUENT_FIELD_OPTION(TString, NetworkProject);
  696. ///
  697. /// @brief Limit on job execution time.
  698. ///
  699. /// Jobs that exceed this limit will be considered failed.
  700. FLUENT_FIELD_OPTION(TDuration, JobTimeLimit);
  701. ///
  702. /// @brief Get job binary config.
  703. const TJobBinaryConfig& GetJobBinary() const;
  704. ///
  705. /// @brief List of profilers to run.
  706. FLUENT_VECTOR_FIELD(TJobProfilerSpec, JobProfiler);
  707. ///
  708. /// @brief Specification of a disk required for job.
  709. ///
  710. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/operations-options#disk_request
  711. FLUENT_FIELD_OPTION(TDiskRequest, DiskRequest);
  712. private:
  713. TVector<std::tuple<TLocalFilePath, TAddLocalFileOptions>> LocalFiles_;
  714. TJobBinaryConfig JobBinary_;
  715. };
  716. ////////////////////////////////////////////////////////////////////////////////
  717. ///
  718. /// @brief Spec of Map operation.
  719. ///
  720. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  721. template <typename TDerived>
  722. struct TMapOperationSpecBase
  723. : public TUserOperationSpecBase<TDerived>
  724. , public TWithAutoMergeSpec<TDerived>
  725. {
  726. /// @cond Doxygen_Suppress
  727. using TSelf = TDerived;
  728. /// @endcond
  729. ///
  730. /// @brief Spec of mapper job.
  731. FLUENT_FIELD(TUserJobSpec, MapperSpec);
  732. ///
  733. /// @brief Whether to guarantee the order of rows passed to mapper matches the order in the table.
  734. ///
  735. /// When `Ordered' is false (by default), there is no guaranties about order of reading rows.
  736. /// In this case mapper might work slightly faster because row delivered from fast node can be processed YT waits
  737. /// response from slow nodes.
  738. /// When `Ordered' is true, rows will come in order in which they are stored in input tables.
  739. FLUENT_FIELD_OPTION(bool, Ordered);
  740. ///
  741. /// @brief Recommended number of jobs to run.
  742. ///
  743. /// `JobCount' has higher priority than @ref NYT::TMapOperationSpecBase::DataSizePerJob.
  744. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  745. FLUENT_FIELD_OPTION(ui32, JobCount);
  746. ///
  747. /// @brief Recommended of data size for each job.
  748. ///
  749. /// `DataSizePerJob` has lower priority that @ref NYT::TMapOperationSpecBase::JobCount.
  750. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  751. FLUENT_FIELD_OPTION(ui64, DataSizePerJob);
  752. };
  753. ///
  754. /// @brief Spec of Map operation.
  755. ///
  756. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  757. struct TMapOperationSpec
  758. : public TMapOperationSpecBase<TMapOperationSpec>
  759. , public TOperationIOSpec<TMapOperationSpec>
  760. , public TUserJobFormatHintsBase<TMapOperationSpec>
  761. { };
  762. ///
  763. /// @brief Spec of raw Map operation.
  764. ///
  765. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  766. struct TRawMapOperationSpec
  767. : public TMapOperationSpecBase<TRawMapOperationSpec>
  768. , public TSimpleRawOperationIoSpec<TRawMapOperationSpec>
  769. { };
  770. ////////////////////////////////////////////////////////////////////////////////
  771. ///
  772. /// @brief Spec of Reduce operation.
  773. ///
  774. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  775. template <typename TDerived>
  776. struct TReduceOperationSpecBase
  777. : public TUserOperationSpecBase<TDerived>
  778. , public TWithAutoMergeSpec<TDerived>
  779. {
  780. /// @cond Doxygen_Suppress
  781. using TSelf = TDerived;
  782. /// @endcond
  783. ///
  784. /// @brief Spec of reduce job.
  785. FLUENT_FIELD(TUserJobSpec, ReducerSpec);
  786. ///
  787. /// @brief Columns to sort rows by (must include `ReduceBy` as prefix).
  788. FLUENT_FIELD(TSortColumns, SortBy);
  789. ///
  790. /// @brief Columns to group rows by.
  791. FLUENT_FIELD(TSortColumns, ReduceBy);
  792. ///
  793. /// @brief Columns to join foreign tables by (must be prefix of `ReduceBy`).
  794. ///
  795. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce#foreign_tables
  796. FLUENT_FIELD_OPTION(TSortColumns, JoinBy);
  797. ///
  798. /// @brief Guarantee to feed all rows with same `ReduceBy` columns to a single job (`true` by default).
  799. FLUENT_FIELD_OPTION(bool, EnableKeyGuarantee);
  800. ///
  801. /// @brief Recommended number of jobs to run.
  802. ///
  803. /// `JobCount' has higher priority than @ref NYT::TReduceOperationSpecBase::DataSizePerJob.
  804. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  805. FLUENT_FIELD_OPTION(ui32, JobCount);
  806. ///
  807. /// @brief Recommended of data size for each job.
  808. ///
  809. /// `DataSizePerJob` has lower priority that @ref NYT::TReduceOperationSpecBase::JobCount.
  810. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  811. FLUENT_FIELD_OPTION(ui64, DataSizePerJob);
  812. };
  813. ///
  814. /// @brief Spec of Reduce operation.
  815. ///
  816. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  817. struct TReduceOperationSpec
  818. : public TReduceOperationSpecBase<TReduceOperationSpec>
  819. , public TOperationIOSpec<TReduceOperationSpec>
  820. , public TUserJobFormatHintsBase<TReduceOperationSpec>
  821. { };
  822. ///
  823. /// @brief Spec of raw Reduce operation.
  824. ///
  825. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  826. struct TRawReduceOperationSpec
  827. : public TReduceOperationSpecBase<TRawReduceOperationSpec>
  828. , public TSimpleRawOperationIoSpec<TRawReduceOperationSpec>
  829. { };
  830. ////////////////////////////////////////////////////////////////////////////////
  831. ///
  832. /// @brief Spec of JoinReduce operation.
  833. ///
  834. /// @deprecated Instead the user should run a reduce operation
  835. /// with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false`.
  836. ///
  837. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce#foreign_tables
  838. template <typename TDerived>
  839. struct TJoinReduceOperationSpecBase
  840. : public TUserOperationSpecBase<TDerived>
  841. {
  842. /// @cond Doxygen_Suppress
  843. using TSelf = TDerived;
  844. /// @endcond
  845. ///
  846. /// @brief Spec of reduce job.
  847. FLUENT_FIELD(TUserJobSpec, ReducerSpec);
  848. ///
  849. /// @brief Columns to join foreign tables by (must be prefix of `ReduceBy`).
  850. ///
  851. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce#foreign_tables
  852. FLUENT_FIELD(TSortColumns, JoinBy);
  853. ///
  854. /// @brief Recommended number of jobs to run.
  855. ///
  856. /// `JobCount' has higher priority than @ref NYT::TJoinReduceOperationSpecBase::DataSizePerJob.
  857. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  858. FLUENT_FIELD_OPTION(ui32, JobCount);
  859. ///
  860. /// @brief Recommended of data size for each job.
  861. ///
  862. /// `DataSizePerJob` has lower priority that @ref NYT::TJoinReduceOperationSpecBase::JobCount.
  863. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  864. FLUENT_FIELD_OPTION(ui64, DataSizePerJob);
  865. };
  866. ///
  867. /// @brief Spec of JoinReduce operation.
  868. ///
  869. /// @deprecated Instead the user should run a reduce operation
  870. /// with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false`.
  871. ///
  872. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce#foreign_tables
  873. struct TJoinReduceOperationSpec
  874. : public TJoinReduceOperationSpecBase<TJoinReduceOperationSpec>
  875. , public TOperationIOSpec<TJoinReduceOperationSpec>
  876. , public TUserJobFormatHintsBase<TJoinReduceOperationSpec>
  877. { };
  878. ///
  879. /// @brief Spec of raw JoinReduce operation.
  880. ///
  881. /// @deprecated Instead the user should run a reduce operation
  882. /// with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false`.
  883. ///
  884. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce#foreign_tables
  885. struct TRawJoinReduceOperationSpec
  886. : public TJoinReduceOperationSpecBase<TRawJoinReduceOperationSpec>
  887. , public TSimpleRawOperationIoSpec<TRawJoinReduceOperationSpec>
  888. { };
  889. ////////////////////////////////////////////////////////////////////////////////
  890. ///
  891. /// @brief Spec of MapReduce operation.
  892. ///
  893. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  894. template <typename TDerived>
  895. struct TMapReduceOperationSpecBase
  896. : public TUserOperationSpecBase<TDerived>
  897. {
  898. /// @cond Doxygen_Suppress
  899. using TSelf = TDerived;
  900. /// @endcond
  901. ///
  902. /// @brief Spec of map job.
  903. FLUENT_FIELD(TUserJobSpec, MapperSpec);
  904. ///
  905. /// @brief Spec of reduce job.
  906. FLUENT_FIELD(TUserJobSpec, ReducerSpec);
  907. ///
  908. /// @brief Spec of reduce combiner.
  909. FLUENT_FIELD(TUserJobSpec, ReduceCombinerSpec);
  910. ///
  911. /// @brief Columns to sort rows by (must include `ReduceBy` as prefix).
  912. FLUENT_FIELD(TSortColumns, SortBy);
  913. ///
  914. /// @brief Columns to group rows by.
  915. FLUENT_FIELD(TSortColumns, ReduceBy);
  916. ///
  917. /// @brief Recommended number of map jobs to run.
  918. ///
  919. /// `JobCount' has higher priority than @ref NYT::TMapReduceOperationSpecBase::DataSizePerMapJob.
  920. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  921. FLUENT_FIELD_OPTION(ui32, MapJobCount);
  922. ///
  923. /// @brief Recommended of data size for each map job.
  924. ///
  925. /// `DataSizePerJob` has lower priority that @ref NYT::TMapReduceOperationSpecBase::MapJobCount.
  926. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  927. FLUENT_FIELD_OPTION(ui64, DataSizePerMapJob);
  928. ///
  929. /// @brief Recommended number of intermediate data partitions.
  930. FLUENT_FIELD_OPTION(ui64, PartitionCount);
  931. ///
  932. /// @brief Recommended size of intermediate data partitions.
  933. FLUENT_FIELD_OPTION(ui64, PartitionDataSize);
  934. ///
  935. /// @brief Account to use for intermediate data.
  936. FLUENT_FIELD_OPTION(TString, IntermediateDataAccount);
  937. ///
  938. /// @brief Replication factor for intermediate data (1 by default).
  939. FLUENT_FIELD_OPTION(ui64, IntermediateDataReplicationFactor);
  940. ///
  941. /// @brief Recommended size of data to be passed to a single reduce combiner.
  942. FLUENT_FIELD_OPTION(ui64, DataSizePerSortJob);
  943. ///
  944. /// @brief Whether to guarantee the order of rows passed to mapper matches the order in the table.
  945. ///
  946. /// @see @ref NYT::TMapOperationSpec::Ordered for more info.
  947. FLUENT_FIELD_OPTION(bool, Ordered);
  948. ///
  949. /// @brief Guarantee to run reduce combiner before reducer.
  950. FLUENT_FIELD_OPTION(bool, ForceReduceCombiners);
  951. };
  952. ///
  953. /// @brief Spec of MapReduce operation.
  954. ///
  955. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  956. struct TMapReduceOperationSpec
  957. : public TMapReduceOperationSpecBase<TMapReduceOperationSpec>
  958. , public TOperationIOSpec<TMapReduceOperationSpec>
  959. , public TIntermediateTablesHintSpec<TMapReduceOperationSpec>
  960. {
  961. /// @cond Doxygen_Suppress
  962. using TSelf = TMapReduceOperationSpec;
  963. /// @endcond
  964. ///
  965. /// @brief Format hints for mapper.
  966. FLUENT_FIELD_DEFAULT(TUserJobFormatHints, MapperFormatHints, TUserJobFormatHints());
  967. ///
  968. /// @brief Format hints for reducer.
  969. FLUENT_FIELD_DEFAULT(TUserJobFormatHints, ReducerFormatHints, TUserJobFormatHints());
  970. ///
  971. /// @brief Format hints for reduce combiner.
  972. FLUENT_FIELD_DEFAULT(TUserJobFormatHints, ReduceCombinerFormatHints, TUserJobFormatHints());
  973. };
  974. ///
  975. /// @brief Spec of raw MapReduce operation.
  976. ///
  977. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  978. struct TRawMapReduceOperationSpec
  979. : public TMapReduceOperationSpecBase<TRawMapReduceOperationSpec>
  980. , public TRawMapReduceOperationIoSpec<TRawMapReduceOperationSpec>
  981. { };
  982. ////////////////////////////////////////////////////////////////////////////////
  983. ///
  984. /// @brief Schema inference mode.
  985. ///
  986. /// @see https://ytsaurus.tech/docs/en/user-guide/storage/static-schema.html#schema_inference
  987. enum class ESchemaInferenceMode : int
  988. {
  989. FromInput /* "from_input" */,
  990. FromOutput /* "from_output" */,
  991. Auto /* "auto" */,
  992. };
  993. ///
  994. /// @brief Spec of Sort operation.
  995. ///
  996. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/sort
  997. struct TSortOperationSpec
  998. : TOperationSpecBase<TSortOperationSpec>
  999. {
  1000. /// @cond Doxygen_Suppress
  1001. using TSelf = TSortOperationSpec;
  1002. /// @endcond
  1003. ///
  1004. /// @brief Paths to input tables.
  1005. FLUENT_VECTOR_FIELD(TRichYPath, Input);
  1006. ///
  1007. /// @brief Path to output table.
  1008. FLUENT_FIELD(TRichYPath, Output);
  1009. ///
  1010. /// @brief Columns to sort table by.
  1011. FLUENT_FIELD(TSortColumns, SortBy);
  1012. ///
  1013. /// @brief Recommended number of intermediate data partitions.
  1014. FLUENT_FIELD_OPTION(ui64, PartitionCount);
  1015. ///
  1016. /// @brief Recommended size of intermediate data partitions.
  1017. FLUENT_FIELD_OPTION(ui64, PartitionDataSize);
  1018. ///
  1019. /// @brief Recommended number of partition jobs to run.
  1020. ///
  1021. /// `JobCount' has higher priority than @ref NYT::TSortOperationSpec::DataSizePerPartitionJob.
  1022. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  1023. FLUENT_FIELD_OPTION(ui64, PartitionJobCount);
  1024. ///
  1025. /// @brief Recommended of data size for each partition job.
  1026. ///
  1027. /// `DataSizePerJob` has lower priority that @ref NYT::TSortOperationSpec::PartitionJobCount.
  1028. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  1029. FLUENT_FIELD_OPTION(ui64, DataSizePerPartitionJob);
  1030. ///
  1031. /// @brief Inference mode for output table schema.
  1032. ///
  1033. /// @see https://ytsaurus.tech/docs/en/user-guide/storage/static-schema.html#schema_inference
  1034. FLUENT_FIELD_OPTION(ESchemaInferenceMode, SchemaInferenceMode);
  1035. ///
  1036. /// @brief Account to use for intermediate data.
  1037. FLUENT_FIELD_OPTION(TString, IntermediateDataAccount);
  1038. ///
  1039. /// @brief Replication factor for intermediate data (1 by default).
  1040. FLUENT_FIELD_OPTION(ui64, IntermediateDataReplicationFactor);
  1041. };
  1042. ///
  1043. /// @brief Merge mode.
  1044. enum EMergeMode : int
  1045. {
  1046. MM_UNORDERED /* "unordered" */,
  1047. MM_ORDERED /* "ordered" */,
  1048. MM_SORTED /* "sorted" */,
  1049. };
  1050. ///
  1051. /// @brief Spec of Merge operation.
  1052. ///
  1053. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/merge
  1054. struct TMergeOperationSpec
  1055. : TOperationSpecBase<TMergeOperationSpec>
  1056. {
  1057. /// @cond Doxygen_Suppress
  1058. using TSelf = TMergeOperationSpec;
  1059. /// @endcond
  1060. ///
  1061. /// @brief Paths to input tables.
  1062. FLUENT_VECTOR_FIELD(TRichYPath, Input);
  1063. ///
  1064. /// @brief Path to output table.
  1065. FLUENT_FIELD(TRichYPath, Output);
  1066. ///
  1067. /// @brief Columns by which to merge (for @ref NYT::EMergeMode::MM_SORTED).
  1068. FLUENT_FIELD(TSortColumns, MergeBy);
  1069. ///
  1070. /// @brief Merge mode.
  1071. FLUENT_FIELD_DEFAULT(EMergeMode, Mode, MM_UNORDERED);
  1072. ///
  1073. /// @brief Combine output chunks to larger ones.
  1074. FLUENT_FIELD_DEFAULT(bool, CombineChunks, false);
  1075. ///
  1076. /// @brief Guarantee that all input chunks will be read.
  1077. FLUENT_FIELD_DEFAULT(bool, ForceTransform, false);
  1078. ///
  1079. /// @brief Recommended number of jobs to run.
  1080. ///
  1081. /// `JobCount' has higher priority than @ref NYT::TMergeOperationSpec::DataSizePerJob.
  1082. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  1083. FLUENT_FIELD_OPTION(ui32, JobCount);
  1084. ///
  1085. /// @brief Recommended of data size for each job.
  1086. ///
  1087. /// `DataSizePerJob` has lower priority that @ref NYT::TMergeOperationSpec::JobCount.
  1088. /// This option only provide a recommendation and may be ignored if conflicting with YT internal limits.
  1089. FLUENT_FIELD_OPTION(ui64, DataSizePerJob);
  1090. ///
  1091. /// @brief Inference mode for output table schema.
  1092. ///
  1093. /// @see https://ytsaurus.tech/docs/en/user-guide/storage/static-schema.html#schema_inference
  1094. FLUENT_FIELD_OPTION(ESchemaInferenceMode, SchemaInferenceMode);
  1095. };
  1096. ///
  1097. /// @brief Spec of Erase operation.
  1098. ///
  1099. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/erase
  1100. struct TEraseOperationSpec
  1101. : TOperationSpecBase<TEraseOperationSpec>
  1102. {
  1103. /// @cond Doxygen_Suppress
  1104. using TSelf = TEraseOperationSpec;
  1105. /// @endcond
  1106. ///
  1107. /// @brief Which table (or row range) to erase.
  1108. FLUENT_FIELD(TRichYPath, TablePath);
  1109. ///
  1110. /// Combine output chunks to larger ones.
  1111. FLUENT_FIELD_DEFAULT(bool, CombineChunks, false);
  1112. ///
  1113. /// @brief Inference mode for output table schema.
  1114. ///
  1115. /// @see https://ytsaurus.tech/docs/en/user-guide/storage/static-schema.html#schema_inference
  1116. FLUENT_FIELD_OPTION(ESchemaInferenceMode, SchemaInferenceMode);
  1117. };
  1118. ///
  1119. /// @brief Spec of RemoteCopy operation.
  1120. ///
  1121. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/remote_copy
  1122. struct TRemoteCopyOperationSpec
  1123. : TOperationSpecBase<TRemoteCopyOperationSpec>
  1124. {
  1125. /// @cond Doxygen_Suppress
  1126. using TSelf = TRemoteCopyOperationSpec;
  1127. /// @endcond
  1128. ///
  1129. /// @brief Source cluster name.
  1130. FLUENT_FIELD(TString, ClusterName);
  1131. ///
  1132. /// @brief Network to use for copy (all remote cluster nodes must have it configured).
  1133. FLUENT_FIELD_OPTION(TString, NetworkName);
  1134. ///
  1135. /// @brief Paths to input tables.
  1136. FLUENT_VECTOR_FIELD(TRichYPath, Input);
  1137. ///
  1138. /// @brief Path to output table.
  1139. FLUENT_FIELD(TRichYPath, Output);
  1140. ///
  1141. /// @brief Inference mode for output table schema.
  1142. ///
  1143. /// @see https://ytsaurus.tech/docs/en/user-guide/storage/static-schema.html#schema_inference
  1144. FLUENT_FIELD_OPTION(ESchemaInferenceMode, SchemaInferenceMode);
  1145. ///
  1146. /// @brief Copy user attributes from input to output table (allowed only for single input table).
  1147. FLUENT_FIELD_DEFAULT(bool, CopyAttributes, false);
  1148. ///
  1149. /// @brief Names of user attributes to copy from input to output table.
  1150. ///
  1151. /// @note To make this option make sense set @ref NYT::TRemoteCopyOperationSpec::CopyAttributes to `true`.
  1152. FLUENT_VECTOR_FIELD(TString, AttributeKey);
  1153. private:
  1154. ///
  1155. /// @brief Config for remote cluster connection.
  1156. FLUENT_FIELD_OPTION(TNode, ClusterConnection);
  1157. };
  1158. class IVanillaJobBase;
  1159. ///
  1160. /// @brief Task of Vanilla operation.
  1161. ///
  1162. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/vanilla
  1163. struct TVanillaTask
  1164. : public TOperationOutputSpecBase
  1165. , public TUserJobOutputFormatHintsBase<TVanillaTask>
  1166. {
  1167. /// @cond Doxygen_Suppress
  1168. using TSelf = TVanillaTask;
  1169. /// @endcond
  1170. ///
  1171. /// @brief Add output table path and specify the task output type (i.e. TMyProtoMessage).
  1172. template <class T>
  1173. TSelf& AddOutput(const TRichYPath& path);
  1174. ///
  1175. /// @brief Add output table path as structured path.
  1176. TSelf& AddStructuredOutput(TStructuredTablePath path);
  1177. ///
  1178. /// @brief Set output table path and specify the task output type (i.e. TMyProtoMessage).
  1179. template <class T>
  1180. TSelf& SetOutput(size_t tableIndex, const TRichYPath& path);
  1181. ///
  1182. /// @brief Task name.
  1183. FLUENT_FIELD(TString, Name);
  1184. ///
  1185. /// @brief Job to be executed in this task.
  1186. FLUENT_FIELD(::TIntrusivePtr<IVanillaJobBase>, Job);
  1187. ///
  1188. /// @brief User job spec.
  1189. FLUENT_FIELD(TUserJobSpec, Spec);
  1190. ///
  1191. /// @brief Number of jobs to run and wait for successful completion.
  1192. ///
  1193. /// @note If @ref NYT::TUserOperationSpecBase::FailOnJobRestart is `false`, a failed job will be restarted
  1194. /// and will not count in this amount.
  1195. FLUENT_FIELD(ui64, JobCount);
  1196. ///
  1197. /// @brief Network project name.
  1198. FLUENT_FIELD(TMaybe<TString>, NetworkProject);
  1199. };
  1200. ///
  1201. /// @brief Spec of Vanilla operation.
  1202. ///
  1203. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/vanilla
  1204. struct TVanillaOperationSpec
  1205. : TUserOperationSpecBase<TVanillaOperationSpec>
  1206. {
  1207. /// @cond Doxygen_Suppress
  1208. using TSelf = TVanillaOperationSpec;
  1209. /// @endcond
  1210. ///
  1211. /// @brief Description of tasks to run in this operation.
  1212. FLUENT_VECTOR_FIELD(TVanillaTask, Task);
  1213. };
  1214. ////////////////////////////////////////////////////////////////////////////////
  1215. ///
  1216. /// @brief Options for @ref NYT::IOperationClient::Map and other operation start commands.
  1217. struct TOperationOptions
  1218. {
  1219. /// @cond Doxygen_Suppress
  1220. using TSelf = TOperationOptions;
  1221. /// @endcond
  1222. ///
  1223. /// @brief Additional field to put to operation spec.
  1224. FLUENT_FIELD_OPTION(TNode, Spec);
  1225. ///
  1226. /// @brief Start operation mode.
  1227. enum class EStartOperationMode : int
  1228. {
  1229. ///
  1230. /// @brief Prepare operation asynchronously. Call IOperation::Start() to start operation.
  1231. AsyncPrepare,
  1232. ///
  1233. /// @brief Prepare and start operation asynchronously. Don't wait for operation completion.
  1234. AsyncStart,
  1235. ///
  1236. /// @brief Prepare and start operation synchronously. Don't wait for operation completion.
  1237. SyncStart,
  1238. ///
  1239. /// @brief Prepare, start and wait for operation completion synchronously.
  1240. SyncWait,
  1241. };
  1242. ///
  1243. /// @brief Start operation mode.
  1244. FLUENT_FIELD_DEFAULT(EStartOperationMode, StartOperationMode, EStartOperationMode::SyncWait);
  1245. ///
  1246. /// @brief Wait for operation finish synchronously.
  1247. ///
  1248. /// @deprecated Use StartOperationMode() instead.
  1249. TSelf& Wait(bool value) {
  1250. StartOperationMode_ = value ? EStartOperationMode::SyncWait : EStartOperationMode::SyncStart;
  1251. return static_cast<TSelf&>(*this);
  1252. }
  1253. ///
  1254. ///
  1255. /// @brief Use format from table attribute (for YAMR-like format).
  1256. ///
  1257. /// @deprecated
  1258. FLUENT_FIELD_DEFAULT(bool, UseTableFormats, false);
  1259. ///
  1260. /// @brief Prefix for bash command running the jobs.
  1261. ///
  1262. /// Can be overridden for the specific job type in the @ref NYT::TUserJobSpec.
  1263. FLUENT_FIELD(TString, JobCommandPrefix);
  1264. ///
  1265. /// @brief Suffix for bash command running the jobs.
  1266. ///
  1267. /// Can be overridden for the specific job type in the @ref NYT::TUserJobSpec.
  1268. FLUENT_FIELD(TString, JobCommandSuffix);
  1269. ///
  1270. /// @brief Put all files required by the job into tmpfs.
  1271. ///
  1272. /// This option can be set globally using @ref NYT::TConfig::MountSandboxInTmpfs.
  1273. /// @see https://ytsaurus.tech/docs/en/problems/woodpeckers
  1274. FLUENT_FIELD_DEFAULT(bool, MountSandboxInTmpfs, false);
  1275. ///
  1276. /// @brief Path to directory to store temporary files.
  1277. FLUENT_FIELD_OPTION(TString, FileStorage);
  1278. ///
  1279. /// @brief Expiration timeout for uploaded files.
  1280. FLUENT_FIELD_OPTION(TDuration, FileExpirationTimeout);
  1281. ///
  1282. /// @brief Info to be passed securely to the job.
  1283. FLUENT_FIELD_OPTION(TNode, SecureVault);
  1284. ///
  1285. /// @brief File cache mode.
  1286. enum class EFileCacheMode : int
  1287. {
  1288. ///
  1289. /// @brief Use YT API commands "get_file_from_cache" and "put_file_to_cache".
  1290. ApiCommandBased,
  1291. ///
  1292. /// @brief Upload files to random paths inside @ref NYT::TOperationOptions::FileStorage without caching.
  1293. CachelessRandomPathUpload,
  1294. };
  1295. ///
  1296. /// @brief File cache mode.
  1297. FLUENT_FIELD_DEFAULT(EFileCacheMode, FileCacheMode, EFileCacheMode::ApiCommandBased);
  1298. ///
  1299. /// @brief Id of transaction within which all Cypress file storage entries will be checked/created.
  1300. ///
  1301. /// By default, the root transaction is used.
  1302. ///
  1303. /// @note Set a specific transaction only if you
  1304. /// 1. specify non-default file storage path in @ref NYT::TOperationOptions::FileStorage or in @ref NYT::TConfig::RemoteTempFilesDirectory.
  1305. /// 2. use `CachelessRandomPathUpload` caching mode (@ref NYT::TOperationOptions::FileCacheMode).
  1306. FLUENT_FIELD(TTransactionId, FileStorageTransactionId);
  1307. ///
  1308. /// @brief Ensure stderr and core tables exist before starting operation.
  1309. ///
  1310. /// If set to `false`, it is user's responsibility to ensure these tables exist.
  1311. FLUENT_FIELD_DEFAULT(bool, CreateDebugOutputTables, true);
  1312. ///
  1313. /// @brief Ensure output tables exist before starting operation.
  1314. ///
  1315. /// If set to `false`, it is user's responsibility to ensure output tables exist.
  1316. FLUENT_FIELD_DEFAULT(bool, CreateOutputTables, true);
  1317. ///
  1318. /// @brief Try to infer schema of inexistent table from the type of written rows.
  1319. ///
  1320. /// @note Default values for this option may differ depending on the row type.
  1321. /// For protobuf it's currently `false` by default.
  1322. FLUENT_FIELD_OPTION(bool, InferOutputSchema);
  1323. };
  1324. ////////////////////////////////////////////////////////////////////////////////
  1325. ///
  1326. /// @brief Get operation secure vault (specified in @ref NYT::TOperationOptions::SecureVault) inside a job.
  1327. const TNode& GetJobSecureVault();
  1328. ////////////////////////////////////////////////////////////////////////////////
  1329. ///
  1330. /// @brief Context passed to @ref NYT::IRawJob::Do.
  1331. class TRawJobContext
  1332. {
  1333. public:
  1334. explicit TRawJobContext(size_t outputTableCount);
  1335. ///
  1336. /// @brief Get file corresponding to input stream.
  1337. const TFile& GetInputFile() const;
  1338. ///
  1339. /// @brief Get files corresponding to output streams.
  1340. const TVector<TFile>& GetOutputFileList() const;
  1341. private:
  1342. TFile InputFile_;
  1343. TVector<TFile> OutputFileList_;
  1344. };
  1345. ////////////////////////////////////////////////////////////////////////////////
  1346. ///
  1347. /// @brief Interface for classes that can be Saved/Loaded (to be used with @ref Y_SAVELOAD_JOB).
  1348. class ISerializableForJob
  1349. {
  1350. public:
  1351. virtual ~ISerializableForJob() = default;
  1352. ///
  1353. /// @brief Dump state to output stream to be restored in job.
  1354. virtual void Save(IOutputStream& stream) const = 0;
  1355. ///
  1356. /// @brief Load state from a stream.
  1357. virtual void Load(IInputStream& stream) = 0;
  1358. };
  1359. ////////////////////////////////////////////////////////////////////////////////
  1360. ///
  1361. /// @brief Provider of information about operation inputs/outputs during @ref NYT::IJob::PrepareOperation.
  1362. class IOperationPreparationContext
  1363. {
  1364. public:
  1365. virtual ~IOperationPreparationContext() = default;
  1366. /// @brief Get the number of input tables.
  1367. virtual int GetInputCount() const = 0;
  1368. /// @brief Get the number of output tables.
  1369. virtual int GetOutputCount() const = 0;
  1370. /// @brief Get the schema of input table no. `index`.
  1371. virtual const TTableSchema& GetInputSchema(int index) const = 0;
  1372. /// @brief Get all the input table schemas.
  1373. virtual const TVector<TTableSchema>& GetInputSchemas() const = 0;
  1374. /// @brief Path to the input table if available (`Nothing()` for intermediate tables).
  1375. virtual TMaybe<TYPath> GetInputPath(int index) const = 0;
  1376. /// @brief Path to the output table if available (`Nothing()` for intermediate tables).
  1377. virtual TMaybe<TYPath> GetOutputPath(int index) const = 0;
  1378. };
  1379. ///
  1380. /// @brief Fluent builder class for @ref NYT::IJob::PrepareOperation.
  1381. ///
  1382. /// @note Method calls are supposed to be chained.
  1383. class TJobOperationPreparer
  1384. {
  1385. public:
  1386. ///
  1387. /// @brief Group of input tables that allows to specify properties on all of them at once.
  1388. ///
  1389. /// The instances are created with @ref NYT::TJobOperationPreparer::BeginInputGroup, not directly.
  1390. class TInputGroup
  1391. {
  1392. public:
  1393. TInputGroup(TJobOperationPreparer& preparer, TVector<int> indices);
  1394. /// @brief Specify the type of input rows.
  1395. template <typename TRow>
  1396. TInputGroup& Description();
  1397. /// @brief Specify renaming of input columns.
  1398. TInputGroup& ColumnRenaming(const THashMap<TString, TString>& renaming);
  1399. /// @brief Specify what input columns to send to job
  1400. ///
  1401. /// @note Filter is applied before renaming, so it must specify original column names.
  1402. TInputGroup& ColumnFilter(const TVector<TString>& columns);
  1403. /// @brief Finish describing the input group.
  1404. TJobOperationPreparer& EndInputGroup();
  1405. private:
  1406. TJobOperationPreparer& Preparer_;
  1407. TVector<int> Indices_;
  1408. };
  1409. ///
  1410. /// @brief Group of output tables that allows to specify properties on all of them at once.
  1411. ///
  1412. /// The instances are created with @ref NYT::TJobOperationPreparer::BeginOutputGroup, not directly.
  1413. class TOutputGroup
  1414. {
  1415. public:
  1416. TOutputGroup(TJobOperationPreparer& preparer, TVector<int> indices);
  1417. /// @brief Specify the type of output rows.
  1418. ///
  1419. /// @tparam TRow type of output rows from tables of this group.
  1420. /// @param inferSchema Infer schema from `TRow` and specify it for these output tables.
  1421. template <typename TRow>
  1422. TOutputGroup& Description(bool inferSchema = true);
  1423. /// @brief Specify schema for these tables.
  1424. TOutputGroup& Schema(const TTableSchema& schema);
  1425. /// @brief Specify that all the the tables in this group are unschematized.
  1426. ///
  1427. /// It is equivalent of `.Schema(TTableSchema().Strict(false)`.
  1428. TOutputGroup& NoSchema();
  1429. /// @brief Finish describing the output group.
  1430. TJobOperationPreparer& EndOutputGroup();
  1431. private:
  1432. TJobOperationPreparer& Preparer_;
  1433. TVector<int> Indices_;
  1434. };
  1435. public:
  1436. explicit TJobOperationPreparer(const IOperationPreparationContext& context);
  1437. /// @brief Begin input group consisting of tables with indices `[begin, end)`.
  1438. ///
  1439. /// @param begin First index.
  1440. /// @param end Index after the last one.
  1441. TInputGroup BeginInputGroup(int begin, int end);
  1442. /// @brief Begin input group consisting of tables with indices from `indices`.
  1443. ///
  1444. /// @tparam TCont Container with integers. Must support `std::begin` and `std::end` functions.
  1445. /// @param indices Indices of tables to include in the group.
  1446. template <typename TCont>
  1447. TInputGroup BeginInputGroup(const TCont& indices);
  1448. /// @brief Begin output group consisting of tables with indices `[begin, end)`.
  1449. ///
  1450. /// @param begin First index.
  1451. /// @param end Index after the last one.
  1452. TOutputGroup BeginOutputGroup(int begin, int end);
  1453. /// @brief Begin input group consisting of tables with indices from `indices`.
  1454. ///
  1455. /// @tparam TCont Container with integers. Must support `std::begin` and `std::end` functions.
  1456. /// @param indices Indices of tables to include in the group.
  1457. template <typename TCont>
  1458. TOutputGroup BeginOutputGroup(const TCont& indices);
  1459. /// @brief Specify the schema for output table no `tableIndex`.
  1460. ///
  1461. /// @note All the output schemas must be specified either with this method, `NoOutputSchema` or `OutputDescription` with `inferSchema == true`
  1462. TJobOperationPreparer& OutputSchema(int tableIndex, TTableSchema schema);
  1463. /// @brief Mark the output table no. `tableIndex` as unschematized.
  1464. TJobOperationPreparer& NoOutputSchema(int tableIndex);
  1465. /// @brief Specify renaming of input columns for table no. `tableIndex`.
  1466. TJobOperationPreparer& InputColumnRenaming(int tableIndex, const THashMap<TString, TString>& renaming);
  1467. /// @brief Specify what input columns of table no. `tableIndex` to send to job
  1468. ///
  1469. /// @note Filter is applied before renaming, so it must specify original column names.
  1470. TJobOperationPreparer& InputColumnFilter(int tableIndex, const TVector<TString>& columns);
  1471. /// @brief Specify the type of input rows for table no. `tableIndex`.
  1472. ///
  1473. /// @tparam TRow type of input rows.
  1474. template <typename TRow>
  1475. TJobOperationPreparer& InputDescription(int tableIndex);
  1476. /// @brief Specify the type of output rows for table no. `tableIndex`.
  1477. ///
  1478. /// @tparam TRow type of output rows.
  1479. /// @param inferSchema Infer schema from `TRow` and specify it for the output tables.
  1480. template <typename TRow>
  1481. TJobOperationPreparer& OutputDescription(int tableIndex, bool inferSchema = true);
  1482. /// @brief Set type of output rows for table no. `tableIndex` to TNode
  1483. ///
  1484. /// @note Set schema via `OutputSchema` if needed
  1485. TJobOperationPreparer& NodeOutput(int tableIndex);
  1486. /// @brief Specify input format hints.
  1487. ///
  1488. /// These hints have lower priority than ones specified in spec.
  1489. TJobOperationPreparer& InputFormatHints(TFormatHints hints);
  1490. /// @brief Specify output format hints.
  1491. ///
  1492. /// These hints have lower priority than ones specified in spec.
  1493. TJobOperationPreparer& OutputFormatHints(TFormatHints hints);
  1494. /// @brief Specify format hints.
  1495. ///
  1496. /// These hints have lower priority than ones specified in spec.
  1497. TJobOperationPreparer& FormatHints(TUserJobFormatHints newFormatHints);
  1498. /// @name "Private" members
  1499. /// The following methods should not be used by clients in @ref NYT::IJob::PrepareOperation
  1500. ///@{
  1501. /// @brief Finish the building process.
  1502. void Finish();
  1503. /// @brief Get output table schemas as specified by the user.
  1504. TVector<TTableSchema> GetOutputSchemas();
  1505. /// @brief Get input column renamings as specified by the user.
  1506. const TVector<THashMap<TString, TString>>& GetInputColumnRenamings() const;
  1507. /// @brief Get input column filters as specified by the user.
  1508. const TVector<TMaybe<TVector<TString>>>& GetInputColumnFilters() const;
  1509. /// @brief Get input column descriptions as specified by the user.
  1510. const TVector<TMaybe<TTableStructure>>& GetInputDescriptions() const;
  1511. /// @brief Get output column descriptions as specified by the user.
  1512. const TVector<TMaybe<TTableStructure>>& GetOutputDescriptions() const;
  1513. /// @brief Get format hints as specified by the user.
  1514. const TUserJobFormatHints& GetFormatHints() const;
  1515. ///@}
  1516. private:
  1517. /// @brief Validate that schema for output table no. `tableIndex` has not been set yet.
  1518. void ValidateMissingOutputSchema(int tableIndex) const;
  1519. /// @brief Validate that description for input table no. `tableIndex` has not been set yet.
  1520. void ValidateMissingInputDescription(int tableIndex) const;
  1521. /// @brief Validate that description for output table no. `tableIndex` has not been set yet.
  1522. void ValidateMissingOutputDescription(int tableIndex) const;
  1523. /// @brief Validate that `tableIndex` is in correct range for input table indices.
  1524. ///
  1525. /// @param message Message to add to the exception in case of violation.
  1526. void ValidateInputTableIndex(int tableIndex, TStringBuf message) const;
  1527. /// @brief Validate that `tableIndex` is in correct range for output table indices.
  1528. ///
  1529. /// @param message Message to add to the exception in case of violation.
  1530. void ValidateOutputTableIndex(int tableIndex, TStringBuf message) const;
  1531. /// @brief Validate that all the output schemas has been set.
  1532. void FinallyValidate() const;
  1533. static TTableSchema EmptyNonstrictSchema();
  1534. private:
  1535. const IOperationPreparationContext& Context_;
  1536. TVector<TMaybe<TTableSchema>> OutputSchemas_;
  1537. TVector<THashMap<TString, TString>> InputColumnRenamings_;
  1538. TVector<TMaybe<TVector<TString>>> InputColumnFilters_;
  1539. TVector<TMaybe<TTableStructure>> InputTableDescriptions_;
  1540. TVector<TMaybe<TTableStructure>> OutputTableDescriptions_;
  1541. TUserJobFormatHints FormatHints_ = {};
  1542. };
  1543. ////////////////////////////////////////////////////////////////////////////////
  1544. ///
  1545. /// @brief Interface for all user jobs.
  1546. class IJob
  1547. : public TThrRefBase
  1548. {
  1549. public:
  1550. ///
  1551. /// @brief Type of job.
  1552. enum EType
  1553. {
  1554. Mapper,
  1555. Reducer,
  1556. ReducerAggregator,
  1557. RawJob,
  1558. VanillaJob,
  1559. };
  1560. ///
  1561. /// @brief Save job state to stream to be restored on cluster nodes.
  1562. virtual void Save(IOutputStream& stream) const
  1563. {
  1564. Y_UNUSED(stream);
  1565. }
  1566. ///
  1567. /// @brief Restore job state from a stream.
  1568. virtual void Load(IInputStream& stream)
  1569. {
  1570. Y_UNUSED(stream);
  1571. }
  1572. ///
  1573. /// @brief Get operation secure vault (specified in @ref NYT::TOperationOptions::SecureVault) inside a job.
  1574. const TNode& SecureVault() const
  1575. {
  1576. return GetJobSecureVault();
  1577. }
  1578. ///
  1579. /// @brief Get number of output tables.
  1580. i64 GetOutputTableCount() const
  1581. {
  1582. Y_ABORT_UNLESS(NDetail::OutputTableCount > 0);
  1583. return NDetail::OutputTableCount;
  1584. }
  1585. ///
  1586. /// @brief Method allowing user to control some properties of input and output tables and formats.
  1587. ///
  1588. /// User can override this method in their job class to:
  1589. /// - specify output table schemas.
  1590. /// The most natural way is usually through @ref NYT::TJobOperationPreparer::OutputDescription (especially for protobuf),
  1591. /// but you can use @ref NYT::TJobOperationPreparer::OutputSchema directly
  1592. /// - specify output row type (@ref NYT::TJobOperationPreparer::OutputDescription)
  1593. /// - specify input row type (@ref NYT::TJobOperationPreparer::InputDescription)
  1594. /// - specify input column filter and renaming (@ref NYT::TJobOperationPreparer::InputColumnFilter and @ref NYT::TJobOperationPreparer::InputColumnRenaming)
  1595. /// - specify format hints (@ref NYT::TJobOperationPreparer::InputFormatHints,
  1596. /// NYT::TJobOperationPreparer::OutputFormatHints and @ref NYT::TJobOperationPreparer::FormatHints)
  1597. /// - maybe something more, cf. the methods of @ref NYT::TJobOperationPreparer.
  1598. ///
  1599. /// If one has several similar tables, groups can be used.
  1600. /// Groups are delimited by @ref NYT::TJobOperationPreparer::BeginInputGroup /
  1601. /// @ref NYT::TJobOperationPreparer::TInputGroup::EndInputGroup and
  1602. /// @ref NYT::TJobOperationPreparer::BeginOutputGroup /
  1603. /// @ref NYT::TJobOperationPreparer::TOutputGroup::EndOutputGroup.
  1604. /// Example:
  1605. /// @code{.cpp}
  1606. /// preparer
  1607. /// .BeginInputGroup({1,2,4,8})
  1608. /// .ColumnRenaming({{"a", "b"}, {"c", "d"}})
  1609. /// .ColumnFilter({"a", "c"})
  1610. /// .EndInputGroup();
  1611. /// @endcode
  1612. ///
  1613. /// @note All the output table schemas must be set
  1614. /// (possibly as empty nonstrict using @ref NYT::TJobOperationPreparer::NoOutputSchema or
  1615. /// @ref NYT::TJobOperationPreparer::TOutputGroup::NoSchema).
  1616. /// By default all the output table schemas are marked as empty nonstrict.
  1617. virtual void PrepareOperation(const IOperationPreparationContext& context, TJobOperationPreparer& preparer) const;
  1618. };
  1619. ///
  1620. /// @brief Declare what fields of currently declared job class to save and restore on cluster node.
  1621. #define Y_SAVELOAD_JOB(...) \
  1622. virtual void Save(IOutputStream& stream) const override { Save(&stream); } \
  1623. virtual void Load(IInputStream& stream) override { Load(&stream); } \
  1624. Y_PASS_VA_ARGS(Y_SAVELOAD_DEFINE(__VA_ARGS__))
  1625. ///
  1626. /// @brief Same as the macro above, but also calls Base class's SaveLoad methods.
  1627. #define Y_SAVELOAD_JOB_DERIVED(Base, ...) \
  1628. virtual void Save(IOutputStream& stream) const override { \
  1629. Base::Save(stream); \
  1630. Save(&stream); \
  1631. } \
  1632. virtual void Load(IInputStream& stream) override { \
  1633. Base::Load(stream); \
  1634. Load(&stream); \
  1635. } \
  1636. Y_PASS_VA_ARGS(Y_SAVELOAD_DEFINE(__VA_ARGS__))
  1637. ////////////////////////////////////////////////////////////////////////////////
  1638. ///
  1639. /// @brief Interface for jobs with typed inputs and outputs.
  1640. class IStructuredJob
  1641. : public IJob
  1642. {
  1643. public:
  1644. ///
  1645. /// @brief This methods are called when creating table reader and writer for the job.
  1646. ///
  1647. /// Override them if you want to implement custom input logic. (e.g. additional bufferization)
  1648. virtual TRawTableReaderPtr CreateCustomRawJobReader(int fd) const;
  1649. virtual THolder<IProxyOutput> CreateCustomRawJobWriter(size_t outputTableCount) const;
  1650. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const = 0;
  1651. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const = 0;
  1652. };
  1653. ////////////////////////////////////////////////////////////////////////////////
  1654. ///
  1655. /// @brief Create default raw job reader.
  1656. TRawTableReaderPtr CreateRawJobReader(int fd = 0);
  1657. ///
  1658. /// @brief Create default raw job writer.
  1659. THolder<IProxyOutput> CreateRawJobWriter(size_t outputTableCount);
  1660. ////////////////////////////////////////////////////////////////////////////////
  1661. ///
  1662. /// @brief Base interface for structured (typed) map jobs.
  1663. class IMapperBase
  1664. : public IStructuredJob
  1665. { };
  1666. ///
  1667. /// @brief Base interface for structured (typed) map jobs with given reader and writer.
  1668. template <class TR, class TW>
  1669. class IMapper
  1670. : public IMapperBase
  1671. {
  1672. public:
  1673. using TReader = TR;
  1674. using TWriter = TW;
  1675. public:
  1676. /// Type of job implemented by this class.
  1677. static constexpr EType JobType = EType::Mapper;
  1678. ///
  1679. /// @brief This method is called before feeding input rows to mapper (before `Do` method).
  1680. virtual void Start(TWriter* writer)
  1681. {
  1682. Y_UNUSED(writer);
  1683. }
  1684. ///
  1685. /// @brief This method is called exactly once for the whole job input.
  1686. ///
  1687. /// Read input rows from `reader` and write output ones to `writer`.
  1688. virtual void Do(TReader* reader, TWriter* writer) = 0;
  1689. ///
  1690. /// @brief This method is called after feeding input rows to mapper (after `Do` method).
  1691. virtual void Finish(TWriter* writer)
  1692. {
  1693. Y_UNUSED(writer);
  1694. }
  1695. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1696. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1697. };
  1698. ////////////////////////////////////////////////////////////////////////////////
  1699. ///
  1700. /// @brief Base interface for structured (typed) reduce jobs.
  1701. ///
  1702. /// It is common base for @ref NYT::IReducer and @ref NYT::IAggregatorReducer.
  1703. class IReducerBase
  1704. : public IStructuredJob
  1705. { };
  1706. ///
  1707. /// @brief Base interface for structured (typed) reduce jobs with given reader and writer.
  1708. template <class TR, class TW>
  1709. class IReducer
  1710. : public IReducerBase
  1711. {
  1712. public:
  1713. using TReader = TR;
  1714. using TWriter = TW;
  1715. public:
  1716. /// Type of job implemented by this class.
  1717. static constexpr EType JobType = EType::Reducer;
  1718. public:
  1719. ///
  1720. /// @brief This method is called before feeding input rows to reducer (before `Do` method).
  1721. virtual void Start(TWriter* writer)
  1722. {
  1723. Y_UNUSED(writer);
  1724. }
  1725. ///
  1726. /// @brief This method is called exactly once for each range with same value of `ReduceBy` (or `JoinBy`) keys.
  1727. virtual void Do(TReader* reader, TWriter* writer) = 0;
  1728. ///
  1729. /// @brief This method is called after feeding input rows to reducer (after `Do` method).
  1730. virtual void Finish(TWriter* writer)
  1731. {
  1732. Y_UNUSED(writer);
  1733. }
  1734. ///
  1735. /// @brief Refuse to process the remaining row ranges and finish the job (successfully).
  1736. void Break();
  1737. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1738. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1739. };
  1740. ////////////////////////////////////////////////////////////////////////////////
  1741. ///
  1742. /// @brief Base interface of jobs used inside reduce operations.
  1743. ///
  1744. /// Unlike @ref NYT::IReducer jobs their `Do' method is called only once
  1745. /// and takes whole range of records split by key boundaries.
  1746. ///
  1747. /// Template argument `TR` must be @ref NYT::TTableRangesReader.
  1748. template <class TR, class TW>
  1749. class IAggregatorReducer
  1750. : public IReducerBase
  1751. {
  1752. public:
  1753. using TReader = TR;
  1754. using TWriter = TW;
  1755. public:
  1756. /// Type of job implemented by this class.
  1757. static constexpr EType JobType = EType::ReducerAggregator;
  1758. public:
  1759. ///
  1760. /// @brief This method is called before feeding input rows to reducer (before `Do` method).
  1761. virtual void Start(TWriter* writer)
  1762. {
  1763. Y_UNUSED(writer);
  1764. }
  1765. ///
  1766. /// @brief This method is called exactly once for the whole job input.
  1767. virtual void Do(TReader* reader, TWriter* writer) = 0;
  1768. ///
  1769. /// @brief This method is called after feeding input rows to reducer (after `Do` method).
  1770. virtual void Finish(TWriter* writer)
  1771. {
  1772. Y_UNUSED(writer);
  1773. }
  1774. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1775. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1776. };
  1777. ////////////////////////////////////////////////////////////////////////////////
  1778. ///
  1779. /// @brief Interface for raw jobs (i.e. reading and writing byte streams).
  1780. class IRawJob
  1781. : public IJob
  1782. {
  1783. public:
  1784. /// Type of job implemented by this class.
  1785. static constexpr EType JobType = EType::RawJob;
  1786. ///
  1787. /// @brief This method is called exactly once for the whole job input.
  1788. virtual void Do(const TRawJobContext& jobContext) = 0;
  1789. };
  1790. ///
  1791. /// @brief Interface of jobs that run the given bash command.
  1792. class ICommandJob
  1793. : public IJob
  1794. {
  1795. public:
  1796. ///
  1797. /// @brief Get bash command to run.
  1798. ///
  1799. /// @note This method is called on the client side.
  1800. virtual const TString& GetCommand() const = 0;
  1801. };
  1802. ///
  1803. /// @brief Raw job executing given bash command.
  1804. ///
  1805. /// @note The binary will not be uploaded.
  1806. class TCommandRawJob
  1807. : public IRawJob
  1808. , public ICommandJob
  1809. {
  1810. public:
  1811. ///
  1812. /// @brief Create job with specified command.
  1813. ///
  1814. /// @param command Bash command to run.
  1815. explicit TCommandRawJob(TStringBuf command = {});
  1816. const TString& GetCommand() const override;
  1817. void Do(const TRawJobContext& jobContext) override;
  1818. private:
  1819. TString Command_;
  1820. };
  1821. ////////////////////////////////////////////////////////////////////////////////
  1822. ///
  1823. /// @brief Base interface for vanilla jobs.
  1824. ///
  1825. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/vanilla
  1826. class IVanillaJobBase
  1827. : public virtual IStructuredJob
  1828. {
  1829. public:
  1830. /// Type of job implemented by this class.
  1831. static constexpr EType JobType = EType::VanillaJob;
  1832. };
  1833. template <class TW = void>
  1834. class IVanillaJob;
  1835. ///
  1836. /// @brief Interface of vanilla job without outputs.
  1837. template <>
  1838. class IVanillaJob<void>
  1839. : public IVanillaJobBase
  1840. {
  1841. public:
  1842. ///
  1843. /// @brief This method is called exactly once for each vanilla job.
  1844. virtual void Do() = 0;
  1845. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1846. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1847. };
  1848. ///
  1849. /// @brief Vanilla job executing given bash command.
  1850. ///
  1851. /// @note The binary will not be uploaded.
  1852. class TCommandVanillaJob
  1853. : public IVanillaJob<>
  1854. , public ICommandJob
  1855. {
  1856. public:
  1857. ///
  1858. /// @brief Create job with specified command.
  1859. ///
  1860. /// @param command Bash command to run.
  1861. explicit TCommandVanillaJob(TStringBuf command = {});
  1862. const TString& GetCommand() const override;
  1863. void Do() override;
  1864. private:
  1865. TString Command_;
  1866. };
  1867. ///
  1868. /// @brief Interface for vanilla jobs with output tables.
  1869. template <class TW>
  1870. class IVanillaJob
  1871. : public IVanillaJobBase
  1872. {
  1873. public:
  1874. using TWriter = TW;
  1875. public:
  1876. ///
  1877. /// @brief This method is called before `Do` method.
  1878. virtual void Start(TWriter* /* writer */)
  1879. { }
  1880. ///
  1881. /// @brief This method is called exactly once for each vanilla job.
  1882. ///
  1883. /// Write output rows to `writer`.
  1884. virtual void Do(TWriter* writer) = 0;
  1885. ///
  1886. /// @brief This method is called after `Do` method.
  1887. virtual void Finish(TWriter* /* writer */)
  1888. { }
  1889. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1890. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1891. };
  1892. ////////////////////////////////////////////////////////////////////////////////
  1893. ///
  1894. /// @brief Attributes to request for an operation.
  1895. enum class EOperationAttribute : int
  1896. {
  1897. Id /* "id" */,
  1898. Type /* "type" */,
  1899. State /* "state" */,
  1900. AuthenticatedUser /* "authenticated_user" */,
  1901. StartTime /* "start_time" */,
  1902. FinishTime /* "finish_time" */,
  1903. BriefProgress /* "brief_progress" */,
  1904. BriefSpec /* "brief_spec" */,
  1905. Suspended /* "suspended" */,
  1906. Result /* "result" */,
  1907. Progress /* "progress" */,
  1908. Events /* "events" */,
  1909. Spec /* "spec" */,
  1910. FullSpec /* "full_spec" */,
  1911. UnrecognizedSpec /* "unrecognized_spec" */,
  1912. };
  1913. ///
  1914. /// @brief Class describing which attributes to request in @ref NYT::IClient::GetOperation or @ref NYT::IClient::ListOperations.
  1915. struct TOperationAttributeFilter
  1916. {
  1917. /// @cond Doxygen_Suppress
  1918. using TSelf = TOperationAttributeFilter;
  1919. /// @endcond
  1920. TVector<EOperationAttribute> Attributes_;
  1921. ///
  1922. /// @brief Add attribute to the filter. Calls are supposed to be chained.
  1923. TSelf& Add(EOperationAttribute attribute)
  1924. {
  1925. Attributes_.push_back(attribute);
  1926. return *this;
  1927. }
  1928. };
  1929. ///
  1930. /// @brief Options for @ref NYT::IClient::GetOperation call.
  1931. struct TGetOperationOptions
  1932. {
  1933. /// @cond Doxygen_Suppress
  1934. using TSelf = TGetOperationOptions;
  1935. /// @endcond
  1936. ///
  1937. /// @brief What attributes to request (if omitted, the default set of attributes will be requested).
  1938. FLUENT_FIELD_OPTION(TOperationAttributeFilter, AttributeFilter);
  1939. FLUENT_FIELD_OPTION(bool, IncludeRuntime);
  1940. };
  1941. ///
  1942. /// @brief "Coarse-grained" state of an operation.
  1943. enum class EOperationBriefState : int
  1944. {
  1945. InProgress /* "in_progress" */,
  1946. Completed /* "completed" */,
  1947. Aborted /* "aborted" */,
  1948. /// Failed
  1949. Failed /* "failed" */,
  1950. };
  1951. ///
  1952. /// @brief Operation type.
  1953. enum class EOperationType : int
  1954. {
  1955. Map /* "map" */,
  1956. Merge /* "merge" */,
  1957. Erase /* "erase" */,
  1958. Sort /* "sort" */,
  1959. Reduce /* "reduce" */,
  1960. MapReduce /* "map_reduce" */,
  1961. RemoteCopy /* "remote_copy" */,
  1962. JoinReduce /* "join_reduce" */,
  1963. Vanilla /* "vanilla" */,
  1964. };
  1965. ///
  1966. /// @brief Operation progress.
  1967. struct TOperationProgress
  1968. {
  1969. ///
  1970. /// @brief Total job statistics.
  1971. TJobStatistics JobStatistics;
  1972. ///
  1973. /// @brief Job counter for various job states with hierarchy.
  1974. TJobCounters JobCounters;
  1975. ///
  1976. /// @brief Time when this progress was built on scheduler or CA.
  1977. TMaybe<TInstant> BuildTime;
  1978. };
  1979. ///
  1980. /// @brief Brief operation progress (numbers of jobs in these states).
  1981. struct TOperationBriefProgress
  1982. {
  1983. ui64 Aborted = 0;
  1984. ui64 Completed = 0;
  1985. ui64 Failed = 0;
  1986. ui64 Lost = 0;
  1987. ui64 Pending = 0;
  1988. ui64 Running = 0;
  1989. ui64 Total = 0;
  1990. };
  1991. ///
  1992. /// @brief Operation result.
  1993. struct TOperationResult
  1994. {
  1995. ///
  1996. /// @brief For a unsuccessfully finished operation: description of error.
  1997. TMaybe<TYtError> Error;
  1998. };
  1999. ///
  2000. /// @brief Operation event (change of state).
  2001. struct TOperationEvent
  2002. {
  2003. ///
  2004. /// @brief New state of operation.
  2005. TString State;
  2006. ///
  2007. /// @brief Time of state change.
  2008. TInstant Time;
  2009. };
  2010. ///
  2011. /// @brief Operation info.
  2012. ///
  2013. /// A field may be `Nothing()` either if it was not requested (see @ref NYT::TGetOperationOptions::AttributeFilter)
  2014. /// or it is not available (i.e. `FinishTime` for a running operation).
  2015. /// @see https://ytsaurus.tech/docs/en/api/commands#get_operation
  2016. struct TOperationAttributes
  2017. {
  2018. ///
  2019. /// @brief Operation id.
  2020. TMaybe<TOperationId> Id;
  2021. ///
  2022. /// @brief Operation type.
  2023. TMaybe<EOperationType> Type;
  2024. ///
  2025. /// @brief Operation state.
  2026. TMaybe<TString> State;
  2027. ///
  2028. /// @brief "Coarse-grained" operation state.
  2029. TMaybe<EOperationBriefState> BriefState;
  2030. ///
  2031. /// @brief Name of user that started the operation.
  2032. TMaybe<TString> AuthenticatedUser;
  2033. ///
  2034. /// @brief Operation start time.
  2035. TMaybe<TInstant> StartTime;
  2036. ///
  2037. /// @brief Operation finish time (if the operation has finished).
  2038. TMaybe<TInstant> FinishTime;
  2039. ///
  2040. /// @brief Brief progress of the operation.
  2041. TMaybe<TOperationBriefProgress> BriefProgress;
  2042. ///
  2043. /// @brief Brief spec of operation (light-weight fields only).
  2044. TMaybe<TNode> BriefSpec;
  2045. ///
  2046. /// @brief Spec of the operation as provided by the user.
  2047. TMaybe<TNode> Spec;
  2048. ///
  2049. /// @brief Full spec of operation (all fields not specified by user are filled with default values).
  2050. TMaybe<TNode> FullSpec;
  2051. ///
  2052. /// @brief Fields not recognized by scheduler.
  2053. TMaybe<TNode> UnrecognizedSpec;
  2054. ///
  2055. /// @brief Is operation suspended.
  2056. TMaybe<bool> Suspended;
  2057. ///
  2058. /// @brief Operation result.
  2059. TMaybe<TOperationResult> Result;
  2060. ///
  2061. /// @brief Operation progress.
  2062. TMaybe<TOperationProgress> Progress;
  2063. ///
  2064. /// @brief List of operation events (changes of state).
  2065. TMaybe<TVector<TOperationEvent>> Events;
  2066. ///
  2067. /// @brief Map from alert name to its description.
  2068. TMaybe<THashMap<TString, TYtError>> Alerts;
  2069. };
  2070. ///
  2071. /// @brief Direction of cursor for paging, see @ref NYT::TListOperationsOptions::CursorDirection.
  2072. enum class ECursorDirection
  2073. {
  2074. Past /* "past" */,
  2075. Future /* "future" */,
  2076. };
  2077. ///
  2078. /// @brief Options of @ref NYT::IClient::ListOperations command.
  2079. ///
  2080. /// @see https://ytsaurus.tech/docs/en/api/commands.html#list_operations
  2081. struct TListOperationsOptions
  2082. {
  2083. /// @cond Doxygen_Suppress
  2084. using TSelf = TListOperationsOptions;
  2085. /// @endcond
  2086. ///
  2087. /// @name Time range specification
  2088. ///
  2089. /// List operations with start time in half-closed interval
  2090. /// `[CursorTime, ToTime)` if `CursorDirection == Future` or
  2091. /// `[FromTime, CursorTime)` if `CursorDirection == Past`.
  2092. ///@{
  2093. ///
  2094. /// @brief Search for operations with start time >= `FromTime`.
  2095. FLUENT_FIELD_OPTION(TInstant, FromTime);
  2096. ///
  2097. /// @brief Search for operations with start time < `ToTime`.
  2098. FLUENT_FIELD_OPTION(TInstant, ToTime);
  2099. ///
  2100. /// @brief Additional restriction on operation start time (useful for pagination).
  2101. ///
  2102. /// Search for operations with start time >= `CursorTime` if `CursorDirection == Future`
  2103. /// and with start time < `CursorTime` if `CursorDirection == Past`
  2104. FLUENT_FIELD_OPTION(TInstant, CursorTime);
  2105. ///
  2106. /// @brief Direction of pagination (see @ref NYT::TListOperationsOptions::CursorTime).
  2107. FLUENT_FIELD_OPTION(ECursorDirection, CursorDirection);
  2108. ///@}
  2109. ///
  2110. /// @name Filters
  2111. /// Choose operations satisfying given filters.
  2112. ///@{
  2113. ///
  2114. /// @brief Search for `Filter` as a substring in operation text factors
  2115. /// (e.g. title or input/output table paths).
  2116. FLUENT_FIELD_OPTION(TString, Filter);
  2117. ///
  2118. /// @brief Choose operations whose pools include `Pool`.
  2119. FLUENT_FIELD_OPTION(TString, Pool);
  2120. ///
  2121. /// @brief Choose operations with given @ref NYT::TOperationAttributes::AuthenticatedUser.
  2122. FLUENT_FIELD_OPTION(TString, User);
  2123. ///
  2124. /// @brief Choose operations with given @ref NYT::TOperationAttributes::State.
  2125. FLUENT_FIELD_OPTION(TString, State);
  2126. ///
  2127. /// @brief Choose operations with given @ref NYT::TOperationAttributes::Type.
  2128. FLUENT_FIELD_OPTION(EOperationType, Type);
  2129. ///
  2130. /// @brief Choose operations having (or not having) any failed jobs.
  2131. FLUENT_FIELD_OPTION(bool, WithFailedJobs);
  2132. ///@}
  2133. ///
  2134. /// @brief Search for operations in the archive in addition to Cypress.
  2135. FLUENT_FIELD_OPTION(bool, IncludeArchive);
  2136. ///
  2137. /// @brief Include the counters for different filter parameters in the response.
  2138. ///
  2139. /// Include number of operations for each pool, user, state, type
  2140. /// and the number of operations having failed jobs.
  2141. FLUENT_FIELD_OPTION(bool, IncludeCounters);
  2142. ///
  2143. /// @brief Return no more than `Limit` operations (current default and maximum value is 1000).
  2144. FLUENT_FIELD_OPTION(i64, Limit);
  2145. };
  2146. ///
  2147. /// @brief Response for @ref NYT::IClient::ListOperations command.
  2148. struct TListOperationsResult
  2149. {
  2150. ///
  2151. /// @brief Found operations' attributes.
  2152. TVector<TOperationAttributes> Operations;
  2153. ///
  2154. /// @name Counters for different filter.
  2155. ///
  2156. /// If counters were requested (@ref NYT::TListOperationsOptions::IncludeCounters is `true`)
  2157. /// the maps contain the number of operations found for each pool, user, state and type.
  2158. /// NOTE:
  2159. /// 1) Counters ignore CursorTime and CursorDirection,
  2160. /// they always are collected in the whole [FromTime, ToTime) interval.
  2161. /// 2) Each next counter in the sequence [pool, user, state, type, with_failed_jobs]
  2162. /// takes into account all the previous filters (i.e. if you set User filter to "some-user"
  2163. /// type counts describe only operations with user "some-user").
  2164. /// @{
  2165. ///
  2166. /// @brief Number of operations for each pool.
  2167. TMaybe<THashMap<TString, i64>> PoolCounts;
  2168. ///
  2169. /// @brief Number of operations for each user (subject to previous filters).
  2170. TMaybe<THashMap<TString, i64>> UserCounts;
  2171. ///
  2172. /// @brief Number of operations for each state (subject to previous filters).
  2173. TMaybe<THashMap<TString, i64>> StateCounts;
  2174. ///
  2175. /// @brief Number of operations for each type (subject to previous filters).
  2176. TMaybe<THashMap<EOperationType, i64>> TypeCounts;
  2177. ///
  2178. /// @brief Number of operations having failed jobs (subject to all previous filters).
  2179. TMaybe<i64> WithFailedJobsCount;
  2180. /// @}
  2181. ///
  2182. /// @brief Whether some operations were not returned due to @ref NYT::TListOperationsOptions::Limit.
  2183. ///
  2184. /// `Incomplete == true` means that not all operations satisfying filters
  2185. /// were returned (limit exceeded) and you need to repeat the request with new @ref NYT::TListOperationsOptions::CursorTime
  2186. /// (e.g. `CursorTime == *Operations.back().StartTime`, but don't forget to
  2187. /// remove the duplicates).
  2188. bool Incomplete;
  2189. };
  2190. ////////////////////////////////////////////////////////////////////////////////
  2191. ///
  2192. /// @brief Data source for @ref NYT::IClient::ListJobs command.
  2193. enum class EListJobsDataSource : int
  2194. {
  2195. Runtime /* "runtime" */,
  2196. Archive /* "archive" */,
  2197. Auto /* "auto" */,
  2198. Manual /* "manual" */,
  2199. };
  2200. ///
  2201. /// @brief Job type.
  2202. enum class EJobType : int
  2203. {
  2204. SchedulerFirst /* "scheduler_first" */,
  2205. Map /* "map" */,
  2206. PartitionMap /* "partition_map" */,
  2207. SortedMerge /* "sorted_merge" */,
  2208. OrderedMerge /* "ordered_merge" */,
  2209. UnorderedMerge /* "unordered_merge" */,
  2210. Partition /* "partition" */,
  2211. SimpleSort /* "simple_sort" */,
  2212. FinalSort /* "final_sort" */,
  2213. SortedReduce /* "sorted_reduce" */,
  2214. PartitionReduce /* "partition_reduce" */,
  2215. ReduceCombiner /* "reduce_combiner" */,
  2216. RemoteCopy /* "remote_copy" */,
  2217. IntermediateSort /* "intermediate_sort" */,
  2218. OrderedMap /* "ordered_map" */,
  2219. JoinReduce /* "join_reduce" */,
  2220. Vanilla /* "vanilla" */,
  2221. SchedulerUnknown /* "scheduler_unknown" */,
  2222. SchedulerLast /* "scheduler_last" */,
  2223. ReplicatorFirst /* "replicator_first" */,
  2224. ReplicateChunk /* "replicate_chunk" */,
  2225. RemoveChunk /* "remove_chunk" */,
  2226. RepairChunk /* "repair_chunk" */,
  2227. SealChunk /* "seal_chunk" */,
  2228. ReplicatorLast /* "replicator_last" */,
  2229. };
  2230. ///
  2231. /// @brief Well-known task names.
  2232. enum class ETaskName : int
  2233. {
  2234. Map /* "map" */,
  2235. PartitionMap0 /* "partition_map(0)" */,
  2236. SortedMerge /* "sorted_merge" */,
  2237. OrderedMerge /* "ordered_merge" */,
  2238. UnorderedMerge /* "unordered_merge" */,
  2239. Partition0 /* "partition(0)" */,
  2240. Partition1 /* "partition(1)" */,
  2241. Partition2 /* "partition(2)" */,
  2242. SimpleSort /* "simple_sort" */,
  2243. FinalSort /* "final_sort" */,
  2244. SortedReduce /* "sorted_reduce" */,
  2245. PartitionReduce /* "partition_reduce" */,
  2246. ReduceCombiner /* "reduce_combiner" */,
  2247. RemoteCopy /* "remote_copy" */,
  2248. IntermediateSort /* "intermediate_sort" */,
  2249. OrderedMap /* "ordered_map" */,
  2250. JoinReduce /* "join_reduce" */,
  2251. };
  2252. ///
  2253. /// @brief Task name (can either well-known or just a string).
  2254. class TTaskName
  2255. {
  2256. public:
  2257. // Constructors are implicit by design.
  2258. ///
  2259. /// @brief Construct a custom task name.
  2260. TTaskName(TString taskName);
  2261. ///
  2262. /// @brief Construct a custom task name.
  2263. TTaskName(const char* taskName);
  2264. ///
  2265. /// @brief Construct a well-known task name.
  2266. TTaskName(ETaskName taskName);
  2267. const TString& Get() const;
  2268. private:
  2269. TString TaskName_;
  2270. };
  2271. ///
  2272. /// @brief Job state.
  2273. enum class EJobState : int
  2274. {
  2275. None /* "none" */,
  2276. Waiting /* "waiting" */,
  2277. Running /* "running" */,
  2278. Aborting /* "aborting" */,
  2279. Completed /* "completed" */,
  2280. Failed /* "failed" */,
  2281. Aborted /* "aborted" */,
  2282. Lost /* "lost" */,
  2283. };
  2284. ///
  2285. /// @brief Job sort field.
  2286. ///
  2287. /// @see @ref NYT::TListJobsOptions.
  2288. enum class EJobSortField : int
  2289. {
  2290. Type /* "type" */,
  2291. State /* "state" */,
  2292. StartTime /* "start_time" */,
  2293. FinishTime /* "finish_time" */,
  2294. Address /* "address" */,
  2295. Duration /* "duration" */,
  2296. Progress /* "progress" */,
  2297. Id /* "id" */,
  2298. };
  2299. ///
  2300. /// @brief Job sort direction.
  2301. ///
  2302. /// @see @ref NYT::TListJobsOptions.
  2303. enum class EJobSortDirection : int
  2304. {
  2305. Ascending /* "ascending" */,
  2306. Descending /* "descending" */,
  2307. };
  2308. ///
  2309. /// @brief Options for @ref NYT::IClient::ListJobs.
  2310. ///
  2311. /// @see https://ytsaurus.tech/docs/en/api/commands.html#list_jobs
  2312. struct TListJobsOptions
  2313. {
  2314. /// @cond Doxygen_Suppress
  2315. using TSelf = TListJobsOptions;
  2316. /// @endcond
  2317. ///
  2318. /// @name Filters
  2319. /// Return only jobs with given value of parameter (type, state, address and existence of stderr).
  2320. /// If a field is `Nothing()`, return jobs with all possible values of the corresponding parameter.
  2321. /// @{
  2322. ///
  2323. /// @brief Job type.
  2324. FLUENT_FIELD_OPTION(EJobType, Type);
  2325. ///
  2326. /// @brief Job state.
  2327. FLUENT_FIELD_OPTION(EJobState, State);
  2328. ///
  2329. /// @brief Address of the cluster node where job was running.
  2330. FLUENT_FIELD_OPTION(TString, Address);
  2331. ///
  2332. /// @brief Return only jobs whose stderr has been saved.
  2333. FLUENT_FIELD_OPTION(bool, WithStderr);
  2334. ///
  2335. /// @brief Return only jobs whose spec has been saved.
  2336. FLUENT_FIELD_OPTION(bool, WithSpec);
  2337. ///
  2338. /// @brief Return only jobs whose fail context has been saved.
  2339. FLUENT_FIELD_OPTION(bool, WithFailContext);
  2340. ///
  2341. /// @brief Return only jobs with monitoring descriptor.
  2342. FLUENT_FIELD_OPTION(bool, WithMonitoringDescriptor);
  2343. /// @}
  2344. ///
  2345. /// @name Sort options
  2346. /// @{
  2347. ///
  2348. /// @brief Sort by this field.
  2349. FLUENT_FIELD_OPTION(EJobSortField, SortField);
  2350. ///
  2351. /// @brief Sort order.
  2352. FLUENT_FIELD_OPTION(ESortOrder, SortOrder);
  2353. /// @}
  2354. ///
  2355. /// @brief Data source.
  2356. ///
  2357. /// Where to search for jobs: in scheduler and Cypress ('Runtime'), in archive ('Archive'),
  2358. /// automatically basing on operation presence in Cypress ('Auto') or choose manually (`Manual').
  2359. FLUENT_FIELD_OPTION(EListJobsDataSource, DataSource);
  2360. /// @deprecated
  2361. FLUENT_FIELD_OPTION(bool, IncludeCypress);
  2362. /// @deprecated
  2363. FLUENT_FIELD_OPTION(bool, IncludeControllerAgent);
  2364. /// @deprecated
  2365. FLUENT_FIELD_OPTION(bool, IncludeArchive);
  2366. ///
  2367. /// @brief Maximum number of jobs to return.
  2368. FLUENT_FIELD_OPTION(i64, Limit);
  2369. ///
  2370. /// @brief Number of jobs (in specified sort order) to skip.
  2371. ///
  2372. /// Together with @ref NYT::TListJobsOptions::Limit may be used for pagination.
  2373. FLUENT_FIELD_OPTION(i64, Offset);
  2374. };
  2375. ///
  2376. /// @brief Description of a core dump that happened in the job.
  2377. struct TCoreInfo
  2378. {
  2379. i64 ProcessId;
  2380. TString ExecutableName;
  2381. TMaybe<ui64> Size;
  2382. TMaybe<TYtError> Error;
  2383. };
  2384. ///
  2385. /// @brief Job attributes.
  2386. ///
  2387. /// A field may be `Nothing()` if it is not available (i.e. `FinishTime` for a running job).
  2388. ///
  2389. /// @see https://ytsaurus.tech/docs/en/api/commands#get_job
  2390. struct TJobAttributes
  2391. {
  2392. ///
  2393. /// @brief Job id.
  2394. TMaybe<TJobId> Id;
  2395. ///
  2396. /// @brief Job type
  2397. TMaybe<EJobType> Type;
  2398. ///
  2399. /// @brief Job state.
  2400. TMaybe<EJobState> State;
  2401. ///
  2402. /// @brief Address of a cluster node where job was running.
  2403. TMaybe<TString> Address;
  2404. ///
  2405. /// @brief The name of the task that job corresponds to.
  2406. TMaybe<TString> TaskName;
  2407. ///
  2408. /// @brief Job start time.
  2409. TMaybe<TInstant> StartTime;
  2410. ///
  2411. /// @brief Job finish time (for a finished job).
  2412. TMaybe<TInstant> FinishTime;
  2413. ///
  2414. /// @brief Estimated ratio of job's completed work.
  2415. TMaybe<double> Progress;
  2416. ///
  2417. /// @brief Size of saved job stderr.
  2418. TMaybe<i64> StderrSize;
  2419. ///
  2420. /// @brief Error for a unsuccessfully finished job.
  2421. TMaybe<TYtError> Error;
  2422. ///
  2423. /// @brief Job brief statistics.
  2424. TMaybe<TNode> BriefStatistics;
  2425. ///
  2426. /// @brief Job input paths (with ranges).
  2427. TMaybe<TVector<TRichYPath>> InputPaths;
  2428. ///
  2429. /// @brief Infos for core dumps produced by job.
  2430. TMaybe<TVector<TCoreInfo>> CoreInfos;
  2431. };
  2432. ///
  2433. /// @brief Response for @ref NYT::IOperation::ListJobs.
  2434. struct TListJobsResult
  2435. {
  2436. ///
  2437. /// @brief Jobs.
  2438. TVector<TJobAttributes> Jobs;
  2439. ///
  2440. /// @deprecated
  2441. TMaybe<i64> CypressJobCount;
  2442. ///
  2443. /// @brief Number of jobs retrieved from controller agent.
  2444. TMaybe<i64> ControllerAgentJobCount;
  2445. ///
  2446. /// @brief Number of jobs retrieved from archive.
  2447. TMaybe<i64> ArchiveJobCount;
  2448. };
  2449. ////////////////////////////////////////////////////////////////////
  2450. ///
  2451. /// @brief Options for @ref NYT::IClient::GetJob.
  2452. struct TGetJobOptions
  2453. {
  2454. /// @cond Doxygen_Suppress
  2455. using TSelf = TGetJobOptions;
  2456. /// @endcond
  2457. };
  2458. ///
  2459. /// @brief Options for @ref NYT::IClient::GetJobInput.
  2460. struct TGetJobInputOptions
  2461. {
  2462. /// @cond Doxygen_Suppress
  2463. using TSelf = TGetJobInputOptions;
  2464. /// @endcond
  2465. };
  2466. ///
  2467. /// @brief Options for @ref NYT::IClient::GetJobFailContext.
  2468. struct TGetJobFailContextOptions
  2469. {
  2470. /// @cond Doxygen_Suppress
  2471. using TSelf = TGetJobFailContextOptions;
  2472. /// @endcond
  2473. };
  2474. ///
  2475. /// @brief Options for @ref NYT::IClient::GetJobStderr.
  2476. struct TGetJobStderrOptions
  2477. {
  2478. /// @cond Doxygen_Suppress
  2479. using TSelf = TGetJobStderrOptions;
  2480. /// @endcond
  2481. };
  2482. ////////////////////////////////////////////////////////////////////
  2483. ///
  2484. /// @brief Options for @ref NYT::IOperation::GetFailedJobInfo.
  2485. struct TGetFailedJobInfoOptions
  2486. {
  2487. /// @cond Doxygen_Suppress
  2488. using TSelf = TGetFailedJobInfoOptions;
  2489. /// @endcond
  2490. ///
  2491. /// @brief How many jobs to download. Which jobs will be chosen is undefined.
  2492. FLUENT_FIELD_DEFAULT(ui64, MaxJobCount, 10);
  2493. ///
  2494. /// @brief How much of stderr tail should be downloaded.
  2495. FLUENT_FIELD_DEFAULT(ui64, StderrTailSize, 64 * 1024);
  2496. };
  2497. ////////////////////////////////////////////////////////////////////////////////
  2498. ///
  2499. /// @brief Interface representing an operation.
  2500. struct IOperation
  2501. : public TThrRefBase
  2502. {
  2503. virtual ~IOperation() = default;
  2504. ///
  2505. /// @brief Get operation id.
  2506. virtual const TOperationId& GetId() const = 0;
  2507. ///
  2508. /// @brief Get URL of the operation in YT Web UI.
  2509. virtual TString GetWebInterfaceUrl() const = 0;
  2510. ///
  2511. /// @brief Get last error for not started operations. Get state on YT cluster for started operations.
  2512. ///
  2513. /// For not started operations last error is an error that's being retried during operation
  2514. /// preparation/start (e.g. lock files, start operation request).
  2515. virtual TString GetStatus() const = 0;
  2516. ///
  2517. /// @brief Get preparation future.
  2518. ///
  2519. /// @return future that is set when operation is prepared.
  2520. virtual ::NThreading::TFuture<void> GetPreparedFuture() = 0;
  2521. ///
  2522. /// @brief Start operation synchronously.
  2523. ///
  2524. /// @note: Do NOT call this method twice.
  2525. ///
  2526. /// If operation is not prepared yet, Start() will block waiting for preparation finish.
  2527. /// Be ready to catch exception if operation preparation or start failed.
  2528. virtual void Start() = 0;
  2529. ///
  2530. /// @brief Is the operation started
  2531. ///
  2532. /// Returns true if the operation is started on the cluster
  2533. virtual bool IsStarted() const = 0;
  2534. ///
  2535. /// @brief Get start future.
  2536. ///
  2537. /// @return future that is set when operation is started.
  2538. virtual ::NThreading::TFuture<void> GetStartedFuture() = 0;
  2539. ///
  2540. /// @brief Start watching operation.
  2541. ///
  2542. /// @return future that is set when operation is complete.
  2543. ///
  2544. /// @note: the user should check value of returned future to ensure that operation completed successfully e.g.
  2545. /// @code{.cpp}
  2546. /// auto operationComplete = operation->Watch();
  2547. /// operationComplete.Wait();
  2548. /// operationComplete.GetValue(); /// will throw if operation completed with errors
  2549. /// @endcode
  2550. ///
  2551. /// If operation is completed successfully the returned future contains void value.
  2552. /// If operation is completed with error future contains @ref NYT::TOperationFailedError.
  2553. /// In rare cases when error occurred while waiting (e.g. YT become unavailable) future might contain other exception.
  2554. virtual ::NThreading::TFuture<void> Watch() = 0;
  2555. ///
  2556. /// @brief Get information about failed jobs.
  2557. ///
  2558. /// Can be called for operation in any stage.
  2559. /// Though user should keep in mind that this method always fetches info from cypress
  2560. /// and doesn't work when operation is archived. Successfully completed operations can be archived
  2561. /// quite quickly (in about ~30 seconds).
  2562. virtual TVector<TFailedJobInfo> GetFailedJobInfo(const TGetFailedJobInfoOptions& options = TGetFailedJobInfoOptions()) = 0;
  2563. ///
  2564. /// Get operation brief state.
  2565. virtual EOperationBriefState GetBriefState() = 0;
  2566. ///
  2567. /// @brief Get error (if operation has failed).
  2568. ///
  2569. /// @return `Nothing()` if operation is in 'Completed' or 'InProgress' state (or reason for failed / aborted operation).
  2570. virtual TMaybe<TYtError> GetError() = 0;
  2571. ///
  2572. /// Get job statistics.
  2573. virtual TJobStatistics GetJobStatistics() = 0;
  2574. ///
  2575. /// Get operation progress.
  2576. ///
  2577. /// @return `Nothing()` if operation has no running jobs yet, e.g. when it is in "materializing" or "pending" state.
  2578. virtual TMaybe<TOperationBriefProgress> GetBriefProgress() = 0;
  2579. ///
  2580. /// @brief Abort operation.
  2581. ///
  2582. /// Operation will be finished immediately.
  2583. /// All results of completed/running jobs will be lost.
  2584. ///
  2585. /// @see https://ytsaurus.tech/docs/en/api/commands#abort_op
  2586. virtual void AbortOperation() = 0;
  2587. ///
  2588. /// @brief Complete operation.
  2589. ///
  2590. /// Operation will be finished immediately.
  2591. /// All results of completed jobs will appear in output tables.
  2592. /// All results of running (not completed) jobs will be lost.
  2593. ///
  2594. /// @see https://ytsaurus.tech/docs/en/api/commands#complete_op
  2595. virtual void CompleteOperation() = 0;
  2596. ///
  2597. /// @brief Suspend operation.
  2598. ///
  2599. /// Jobs will not be aborted by default, c.f. @ref NYT::TSuspendOperationOptions.
  2600. ///
  2601. /// @see https://ytsaurus.tech/docs/en/api/commands#suspend_op
  2602. virtual void SuspendOperation(
  2603. const TSuspendOperationOptions& options = TSuspendOperationOptions()) = 0;
  2604. ///
  2605. /// @brief Resume previously suspended operation.
  2606. ///
  2607. /// @see https://ytsaurus.tech/docs/en/api/commands#resume_op
  2608. virtual void ResumeOperation(
  2609. const TResumeOperationOptions& options = TResumeOperationOptions()) = 0;
  2610. ///
  2611. /// @brief Get operation attributes.
  2612. ///
  2613. /// @see https://ytsaurus.tech/docs/en/api/commands#get_operation
  2614. virtual TOperationAttributes GetAttributes(
  2615. const TGetOperationOptions& options = TGetOperationOptions()) = 0;
  2616. ///
  2617. /// @brief Update operation runtime parameters.
  2618. ///
  2619. /// @see https://ytsaurus.tech/docs/en/api/commands#update_op_parameters
  2620. virtual void UpdateParameters(
  2621. const TUpdateOperationParametersOptions& options = TUpdateOperationParametersOptions()) = 0;
  2622. ///
  2623. /// @brief Get job attributes.
  2624. ///
  2625. /// @see https://ytsaurus.tech/docs/en/api/commands#get_job
  2626. virtual TJobAttributes GetJob(
  2627. const TJobId& jobId,
  2628. const TGetJobOptions& options = TGetJobOptions()) = 0;
  2629. ///
  2630. /// List jobs satisfying given filters (see @ref NYT::TListJobsOptions).
  2631. ///
  2632. /// @see https://ytsaurus.tech/docs/en/api/commands#list_jobs
  2633. virtual TListJobsResult ListJobs(
  2634. const TListJobsOptions& options = TListJobsOptions()) = 0;
  2635. };
  2636. ///
  2637. /// @brief Interface of client capable of managing operations.
  2638. struct IOperationClient
  2639. {
  2640. ///
  2641. /// @brief Run Map operation.
  2642. ///
  2643. /// @param spec Operation spec.
  2644. /// @param mapper Instance of a job to run.
  2645. /// @param options Optional parameters.
  2646. ///
  2647. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  2648. IOperationPtr Map(
  2649. const TMapOperationSpec& spec,
  2650. ::TIntrusivePtr<IMapperBase> mapper,
  2651. const TOperationOptions& options = TOperationOptions());
  2652. ///
  2653. /// @brief Run Map operation.
  2654. ///
  2655. /// @param mapper Instance of a job to run.
  2656. /// @param input Input table(s)
  2657. /// @param output Output table(s)
  2658. /// @param spec Operation spec.
  2659. /// @param options Optional parameters.
  2660. ///
  2661. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  2662. IOperationPtr Map(
  2663. ::TIntrusivePtr<IMapperBase> mapper,
  2664. const TOneOrMany<TStructuredTablePath>& input,
  2665. const TOneOrMany<TStructuredTablePath>& output,
  2666. const TMapOperationSpec& spec = TMapOperationSpec(),
  2667. const TOperationOptions& options = TOperationOptions());
  2668. ///
  2669. /// @brief Run raw Map operation.
  2670. ///
  2671. /// @param spec Operation spec.
  2672. /// @param rawJob Instance of a raw mapper to run.
  2673. /// @param options Optional parameters.
  2674. ///
  2675. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  2676. virtual IOperationPtr RawMap(
  2677. const TRawMapOperationSpec& spec,
  2678. ::TIntrusivePtr<IRawJob> rawJob,
  2679. const TOperationOptions& options = TOperationOptions()) = 0;
  2680. ///
  2681. /// @brief Run Reduce operation.
  2682. ///
  2683. /// @param spec Operation spec.
  2684. /// @param reducer Instance of a job to run.
  2685. /// @param options Optional parameters.
  2686. ///
  2687. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  2688. IOperationPtr Reduce(
  2689. const TReduceOperationSpec& spec,
  2690. ::TIntrusivePtr<IReducerBase> reducer,
  2691. const TOperationOptions& options = TOperationOptions());
  2692. ///
  2693. /// @brief Run Reduce operation.
  2694. ///
  2695. /// @param reducer Instance of a job to run.
  2696. /// @param input Input table(s)
  2697. /// @param output Output table(s)
  2698. /// @param reduceBy Columns to group rows by.
  2699. /// @param spec Operation spec.
  2700. /// @param options Optional parameters.
  2701. ///
  2702. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  2703. IOperationPtr Reduce(
  2704. ::TIntrusivePtr<IReducerBase> reducer,
  2705. const TOneOrMany<TStructuredTablePath>& input,
  2706. const TOneOrMany<TStructuredTablePath>& output,
  2707. const TSortColumns& reduceBy,
  2708. const TReduceOperationSpec& spec = TReduceOperationSpec(),
  2709. const TOperationOptions& options = TOperationOptions());
  2710. ///
  2711. /// @brief Run raw Reduce operation.
  2712. ///
  2713. /// @param spec Operation spec.
  2714. /// @param rawJob Instance of a raw reducer to run.
  2715. /// @param options Optional parameters.
  2716. ///
  2717. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  2718. virtual IOperationPtr RawReduce(
  2719. const TRawReduceOperationSpec& spec,
  2720. ::TIntrusivePtr<IRawJob> rawJob,
  2721. const TOperationOptions& options = TOperationOptions()) = 0;
  2722. ///
  2723. /// @brief Run JoinReduce operation.
  2724. ///
  2725. /// @param spec Operation spec.
  2726. /// @param reducer Instance of a job to run.
  2727. /// @param options Optional parameters.
  2728. ///
  2729. /// @deprecated Use @ref NYT::IOperationClient::Reduce with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false.
  2730. IOperationPtr JoinReduce(
  2731. const TJoinReduceOperationSpec& spec,
  2732. ::TIntrusivePtr<IReducerBase> reducer,
  2733. const TOperationOptions& options = TOperationOptions());
  2734. ///
  2735. /// @brief Run raw JoinReduce operation.
  2736. ///
  2737. /// @param spec Operation spec.
  2738. /// @param rawJob Instance of a raw reducer to run.
  2739. /// @param options Optional parameters.
  2740. ///
  2741. /// @deprecated Use @ref NYT::IOperationClient::RawReduce with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false.
  2742. virtual IOperationPtr RawJoinReduce(
  2743. const TRawJoinReduceOperationSpec& spec,
  2744. ::TIntrusivePtr<IRawJob> rawJob,
  2745. const TOperationOptions& options = TOperationOptions()) = 0;
  2746. ///
  2747. /// @brief Run MapReduce operation.
  2748. ///
  2749. /// @param spec Operation spec.
  2750. /// @param mapper Instance of a map job to run (identity mapper if `nullptr`).
  2751. /// @param reducer Instance of a reduce job to run.
  2752. /// @param options Optional parameters.
  2753. ///
  2754. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2755. IOperationPtr MapReduce(
  2756. const TMapReduceOperationSpec& spec,
  2757. ::TIntrusivePtr<IMapperBase> mapper,
  2758. ::TIntrusivePtr<IReducerBase> reducer,
  2759. const TOperationOptions& options = TOperationOptions());
  2760. ///
  2761. /// @brief Run MapReduce operation.
  2762. ///
  2763. /// @param spec Operation spec.
  2764. /// @param mapper Instance of a map job to run (identity mapper if `nullptr`).
  2765. /// @param reducerCombiner Instance of a reduce combiner to run (identity reduce combiner if `nullptr`).
  2766. /// @param reducer Instance of a reduce job to run.
  2767. /// @param options Optional parameters.
  2768. ///
  2769. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2770. IOperationPtr MapReduce(
  2771. const TMapReduceOperationSpec& spec,
  2772. ::TIntrusivePtr<IMapperBase> mapper,
  2773. ::TIntrusivePtr<IReducerBase> reduceCombiner,
  2774. ::TIntrusivePtr<IReducerBase> reducer,
  2775. const TOperationOptions& options = TOperationOptions());
  2776. ///
  2777. /// @brief Run MapReduce operation.
  2778. ///
  2779. /// @param mapper Instance of mapper to run (identity mapper if `nullptr`).
  2780. /// @param reducer Instance of reducer to run.
  2781. /// @param input Input table(s)
  2782. /// @param output Output table(s)
  2783. /// @param reduceBy Columns to group rows by.
  2784. /// @param spec Operation spec.
  2785. /// @param options Optional parameters.
  2786. ///
  2787. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2788. IOperationPtr MapReduce(
  2789. ::TIntrusivePtr<IMapperBase> mapper,
  2790. ::TIntrusivePtr<IReducerBase> reducer,
  2791. const TOneOrMany<TStructuredTablePath>& input,
  2792. const TOneOrMany<TStructuredTablePath>& output,
  2793. const TSortColumns& reduceBy,
  2794. TMapReduceOperationSpec spec = TMapReduceOperationSpec(),
  2795. const TOperationOptions& options = TOperationOptions());
  2796. ///
  2797. /// @brief Run MapReduce operation.
  2798. ///
  2799. /// @param mapper Instance of mapper to run (identity mapper if `nullptr`).
  2800. /// @param reduceCombiner Instance of reduceCombiner to run (identity reduce combiner if `nullptr`).
  2801. /// @param reducer Instance of reducer to run.
  2802. /// @param input Input table(s)
  2803. /// @param output Output table(s)
  2804. /// @param reduceBy Columns to group rows by.
  2805. /// @param spec Operation spec.
  2806. /// @param options Optional parameters.
  2807. ///
  2808. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2809. IOperationPtr MapReduce(
  2810. ::TIntrusivePtr<IMapperBase> mapper,
  2811. ::TIntrusivePtr<IReducerBase> reduceCombiner,
  2812. ::TIntrusivePtr<IReducerBase> reducer,
  2813. const TOneOrMany<TStructuredTablePath>& input,
  2814. const TOneOrMany<TStructuredTablePath>& output,
  2815. const TSortColumns& reduceBy,
  2816. TMapReduceOperationSpec spec = TMapReduceOperationSpec(),
  2817. const TOperationOptions& options = TOperationOptions());
  2818. ///
  2819. /// @brief Run raw MapReduce operation.
  2820. ///
  2821. /// @param spec Operation spec.
  2822. /// @param mapper Instance of a raw mapper to run (identity mapper if `nullptr`).
  2823. /// @param mapper Instance of a raw reduce combiner to run (identity reduce combiner if `nullptr`).
  2824. /// @param mapper Instance of a raw reducer to run.
  2825. /// @param options Optional parameters.
  2826. ///
  2827. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2828. virtual IOperationPtr RawMapReduce(
  2829. const TRawMapReduceOperationSpec& spec,
  2830. ::TIntrusivePtr<IRawJob> mapper,
  2831. ::TIntrusivePtr<IRawJob> reduceCombiner,
  2832. ::TIntrusivePtr<IRawJob> reducer,
  2833. const TOperationOptions& options = TOperationOptions()) = 0;
  2834. ///
  2835. /// @brief Run Sort operation.
  2836. ///
  2837. /// @param spec Operation spec.
  2838. /// @param options Optional parameters.
  2839. ///
  2840. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/sort
  2841. virtual IOperationPtr Sort(
  2842. const TSortOperationSpec& spec,
  2843. const TOperationOptions& options = TOperationOptions()) = 0;
  2844. ///
  2845. /// @brief Run Sort operation.
  2846. ///
  2847. /// @param input Input table(s).
  2848. /// @param output Output table.
  2849. /// @param sortBy Columns to sort input rows by.
  2850. /// @param spec Operation spec.
  2851. /// @param options Optional parameters.
  2852. ///
  2853. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/sort
  2854. IOperationPtr Sort(
  2855. const TOneOrMany<TRichYPath>& input,
  2856. const TRichYPath& output,
  2857. const TSortColumns& sortBy,
  2858. const TSortOperationSpec& spec = TSortOperationSpec(),
  2859. const TOperationOptions& options = TOperationOptions());
  2860. ///
  2861. /// @brief Run Merge operation.
  2862. ///
  2863. /// @param spec Operation spec.
  2864. /// @param options Optional parameters.
  2865. ///
  2866. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/merge
  2867. virtual IOperationPtr Merge(
  2868. const TMergeOperationSpec& spec,
  2869. const TOperationOptions& options = TOperationOptions()) = 0;
  2870. ///
  2871. /// @brief Run Erase operation.
  2872. ///
  2873. /// @param spec Operation spec.
  2874. /// @param options Optional parameters.
  2875. ///
  2876. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/erase
  2877. virtual IOperationPtr Erase(
  2878. const TEraseOperationSpec& spec,
  2879. const TOperationOptions& options = TOperationOptions()) = 0;
  2880. ///
  2881. /// @brief Run RemoteCopy operation.
  2882. ///
  2883. /// @param spec Operation spec.
  2884. /// @param options Optional parameters.
  2885. ///
  2886. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/remote_copy
  2887. virtual IOperationPtr RemoteCopy(
  2888. const TRemoteCopyOperationSpec& spec,
  2889. const TOperationOptions& options = TOperationOptions()) = 0;
  2890. ///
  2891. /// @brief Run Vanilla operation.
  2892. ///
  2893. /// @param spec Operation spec.
  2894. /// @param options Optional parameters.
  2895. ///
  2896. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/vanilla
  2897. virtual IOperationPtr RunVanilla(
  2898. const TVanillaOperationSpec& spec,
  2899. const TOperationOptions& options = TOperationOptions()) = 0;
  2900. ///
  2901. /// @brief Abort operation.
  2902. ///
  2903. /// @see https://ytsaurus.tech/docs/en/api/commands#abort_operation
  2904. virtual void AbortOperation(
  2905. const TOperationId& operationId) = 0;
  2906. ///
  2907. /// @brief Complete operation.
  2908. ///
  2909. /// @see https://ytsaurus.tech/docs/en/api/commands#complete_operation
  2910. virtual void CompleteOperation(
  2911. const TOperationId& operationId) = 0;
  2912. ///
  2913. /// @brief Wait for operation to finish.
  2914. virtual void WaitForOperation(
  2915. const TOperationId& operationId) = 0;
  2916. ///
  2917. /// @brief Check and return operation status.
  2918. ///
  2919. /// @note this function will never return @ref NYT::EOperationBriefState::Failed or @ref NYT::EOperationBriefState::Aborted status,
  2920. /// it will throw @ref NYT::TOperationFailedError instead.
  2921. virtual EOperationBriefState CheckOperation(
  2922. const TOperationId& operationId) = 0;
  2923. ///
  2924. /// @brief Create an operation object given operation id.
  2925. ///
  2926. /// @throw @ref NYT::TErrorResponse if the operation doesn't exist.
  2927. virtual IOperationPtr AttachOperation(const TOperationId& operationId) = 0;
  2928. private:
  2929. virtual IOperationPtr DoMap(
  2930. const TMapOperationSpec& spec,
  2931. ::TIntrusivePtr<IStructuredJob> mapper,
  2932. const TOperationOptions& options) = 0;
  2933. virtual IOperationPtr DoReduce(
  2934. const TReduceOperationSpec& spec,
  2935. ::TIntrusivePtr<IStructuredJob> reducer,
  2936. const TOperationOptions& options) = 0;
  2937. virtual IOperationPtr DoJoinReduce(
  2938. const TJoinReduceOperationSpec& spec,
  2939. ::TIntrusivePtr<IStructuredJob> reducer,
  2940. const TOperationOptions& options) = 0;
  2941. virtual IOperationPtr DoMapReduce(
  2942. const TMapReduceOperationSpec& spec,
  2943. ::TIntrusivePtr<IStructuredJob> mapper,
  2944. ::TIntrusivePtr<IStructuredJob> reduceCombiner,
  2945. ::TIntrusivePtr<IStructuredJob> reducer,
  2946. const TOperationOptions& options) = 0;
  2947. };
  2948. ////////////////////////////////////////////////////////////////////////////////
  2949. } // namespace NYT
  2950. #define OPERATION_INL_H_
  2951. #include "operation-inl.h"
  2952. #undef OPERATION_INL_H_