operation.h 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556
  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. /// Useful if you want to control how lifetime of uploaded files.
  1278. FLUENT_FIELD_OPTION(TString, FileStorage);
  1279. ///
  1280. /// @brief Expiration timeout for uploaded files.
  1281. ///
  1282. /// Set attribute ExpirationTimeout for files being uploaded during operation preparation.
  1283. /// Useful when using custom FileStorage and don't want to create separate cleanup process.
  1284. ///
  1285. /// When using default FileStorage inside //tmp this parameter is almost useless.
  1286. /// //tmp directory is cleaned up by separate process and files can be deleted before FileExpiratoinTimeout is reached.
  1287. FLUENT_FIELD_OPTION(TDuration, FileExpirationTimeout);
  1288. ///
  1289. /// @brief Info to be passed securely to the job.
  1290. FLUENT_FIELD_OPTION(TNode, SecureVault);
  1291. ///
  1292. /// @brief File cache mode.
  1293. enum class EFileCacheMode : int
  1294. {
  1295. ///
  1296. /// @brief Use YT API commands "get_file_from_cache" and "put_file_to_cache".
  1297. ApiCommandBased,
  1298. ///
  1299. /// @brief Upload files to random paths inside @ref NYT::TOperationOptions::FileStorage without caching.
  1300. CachelessRandomPathUpload,
  1301. };
  1302. ///
  1303. /// @brief File cache mode.
  1304. FLUENT_FIELD_DEFAULT(EFileCacheMode, FileCacheMode, EFileCacheMode::ApiCommandBased);
  1305. ///
  1306. /// @brief Id of transaction within which all Cypress file storage entries will be checked/created.
  1307. ///
  1308. /// By default, the root transaction is used.
  1309. ///
  1310. /// @note Set a specific transaction only if you
  1311. /// 1. specify non-default file storage path in @ref NYT::TOperationOptions::FileStorage or in @ref NYT::TConfig::RemoteTempFilesDirectory.
  1312. /// 2. use `CachelessRandomPathUpload` caching mode (@ref NYT::TOperationOptions::FileCacheMode).
  1313. FLUENT_FIELD(TTransactionId, FileStorageTransactionId);
  1314. ///
  1315. /// @brief Ensure stderr and core tables exist before starting operation.
  1316. ///
  1317. /// If set to `false`, it is user's responsibility to ensure these tables exist.
  1318. FLUENT_FIELD_DEFAULT(bool, CreateDebugOutputTables, true);
  1319. ///
  1320. /// @brief Ensure output tables exist before starting operation.
  1321. ///
  1322. /// If set to `false`, it is user's responsibility to ensure output tables exist.
  1323. FLUENT_FIELD_DEFAULT(bool, CreateOutputTables, true);
  1324. ///
  1325. /// @brief Try to infer schema of inexistent table from the type of written rows.
  1326. ///
  1327. /// @note Default values for this option may differ depending on the row type.
  1328. /// For protobuf it's currently `false` by default.
  1329. FLUENT_FIELD_OPTION(bool, InferOutputSchema);
  1330. };
  1331. ////////////////////////////////////////////////////////////////////////////////
  1332. ///
  1333. /// @brief Get operation secure vault (specified in @ref NYT::TOperationOptions::SecureVault) inside a job.
  1334. const TNode& GetJobSecureVault();
  1335. ////////////////////////////////////////////////////////////////////////////////
  1336. ///
  1337. /// @brief Context passed to @ref NYT::IRawJob::Do.
  1338. class TRawJobContext
  1339. {
  1340. public:
  1341. explicit TRawJobContext(size_t outputTableCount);
  1342. ///
  1343. /// @brief Get file corresponding to input stream.
  1344. const TFile& GetInputFile() const;
  1345. ///
  1346. /// @brief Get files corresponding to output streams.
  1347. const TVector<TFile>& GetOutputFileList() const;
  1348. private:
  1349. TFile InputFile_;
  1350. TVector<TFile> OutputFileList_;
  1351. };
  1352. ////////////////////////////////////////////////////////////////////////////////
  1353. ///
  1354. /// @brief Interface for classes that can be Saved/Loaded (to be used with @ref Y_SAVELOAD_JOB).
  1355. class ISerializableForJob
  1356. {
  1357. public:
  1358. virtual ~ISerializableForJob() = default;
  1359. ///
  1360. /// @brief Dump state to output stream to be restored in job.
  1361. virtual void Save(IOutputStream& stream) const = 0;
  1362. ///
  1363. /// @brief Load state from a stream.
  1364. virtual void Load(IInputStream& stream) = 0;
  1365. };
  1366. ////////////////////////////////////////////////////////////////////////////////
  1367. ///
  1368. /// @brief Provider of information about operation inputs/outputs during @ref NYT::IJob::PrepareOperation.
  1369. class IOperationPreparationContext
  1370. {
  1371. public:
  1372. virtual ~IOperationPreparationContext() = default;
  1373. /// @brief Get the number of input tables.
  1374. virtual int GetInputCount() const = 0;
  1375. /// @brief Get the number of output tables.
  1376. virtual int GetOutputCount() const = 0;
  1377. /// @brief Get the schema of input table no. `index`.
  1378. virtual const TTableSchema& GetInputSchema(int index) const = 0;
  1379. /// @brief Get all the input table schemas.
  1380. virtual const TVector<TTableSchema>& GetInputSchemas() const = 0;
  1381. /// @brief Path to the input table if available (`Nothing()` for intermediate tables).
  1382. virtual TMaybe<TYPath> GetInputPath(int index) const = 0;
  1383. /// @brief Path to the output table if available (`Nothing()` for intermediate tables).
  1384. virtual TMaybe<TYPath> GetOutputPath(int index) const = 0;
  1385. };
  1386. ///
  1387. /// @brief Fluent builder class for @ref NYT::IJob::PrepareOperation.
  1388. ///
  1389. /// @note Method calls are supposed to be chained.
  1390. class TJobOperationPreparer
  1391. {
  1392. public:
  1393. ///
  1394. /// @brief Group of input tables that allows to specify properties on all of them at once.
  1395. ///
  1396. /// The instances are created with @ref NYT::TJobOperationPreparer::BeginInputGroup, not directly.
  1397. class TInputGroup
  1398. {
  1399. public:
  1400. TInputGroup(TJobOperationPreparer& preparer, TVector<int> indices);
  1401. /// @brief Specify the type of input rows.
  1402. template <typename TRow>
  1403. TInputGroup& Description();
  1404. /// @brief Specify renaming of input columns.
  1405. TInputGroup& ColumnRenaming(const THashMap<TString, TString>& renaming);
  1406. /// @brief Specify what input columns to send to job
  1407. ///
  1408. /// @note Filter is applied before renaming, so it must specify original column names.
  1409. TInputGroup& ColumnFilter(const TVector<TString>& columns);
  1410. /// @brief Finish describing the input group.
  1411. TJobOperationPreparer& EndInputGroup();
  1412. private:
  1413. TJobOperationPreparer& Preparer_;
  1414. TVector<int> Indices_;
  1415. };
  1416. ///
  1417. /// @brief Group of output tables that allows to specify properties on all of them at once.
  1418. ///
  1419. /// The instances are created with @ref NYT::TJobOperationPreparer::BeginOutputGroup, not directly.
  1420. class TOutputGroup
  1421. {
  1422. public:
  1423. TOutputGroup(TJobOperationPreparer& preparer, TVector<int> indices);
  1424. /// @brief Specify the type of output rows.
  1425. ///
  1426. /// @tparam TRow type of output rows from tables of this group.
  1427. /// @param inferSchema Infer schema from `TRow` and specify it for these output tables.
  1428. template <typename TRow>
  1429. TOutputGroup& Description(bool inferSchema = true);
  1430. /// @brief Specify schema for these tables.
  1431. TOutputGroup& Schema(const TTableSchema& schema);
  1432. /// @brief Specify that all the the tables in this group are unschematized.
  1433. ///
  1434. /// It is equivalent of `.Schema(TTableSchema().Strict(false)`.
  1435. TOutputGroup& NoSchema();
  1436. /// @brief Finish describing the output group.
  1437. TJobOperationPreparer& EndOutputGroup();
  1438. private:
  1439. TJobOperationPreparer& Preparer_;
  1440. TVector<int> Indices_;
  1441. };
  1442. public:
  1443. explicit TJobOperationPreparer(const IOperationPreparationContext& context);
  1444. /// @brief Begin input group consisting of tables with indices `[begin, end)`.
  1445. ///
  1446. /// @param begin First index.
  1447. /// @param end Index after the last one.
  1448. TInputGroup BeginInputGroup(int begin, int end);
  1449. /// @brief Begin input group consisting of tables with indices from `indices`.
  1450. ///
  1451. /// @tparam TCont Container with integers. Must support `std::begin` and `std::end` functions.
  1452. /// @param indices Indices of tables to include in the group.
  1453. template <typename TCont>
  1454. TInputGroup BeginInputGroup(const TCont& indices);
  1455. /// @brief Begin output group consisting of tables with indices `[begin, end)`.
  1456. ///
  1457. /// @param begin First index.
  1458. /// @param end Index after the last one.
  1459. TOutputGroup BeginOutputGroup(int begin, int end);
  1460. /// @brief Begin input group consisting of tables with indices from `indices`.
  1461. ///
  1462. /// @tparam TCont Container with integers. Must support `std::begin` and `std::end` functions.
  1463. /// @param indices Indices of tables to include in the group.
  1464. template <typename TCont>
  1465. TOutputGroup BeginOutputGroup(const TCont& indices);
  1466. /// @brief Specify the schema for output table no `tableIndex`.
  1467. ///
  1468. /// @note All the output schemas must be specified either with this method, `NoOutputSchema` or `OutputDescription` with `inferSchema == true`
  1469. TJobOperationPreparer& OutputSchema(int tableIndex, TTableSchema schema);
  1470. /// @brief Mark the output table no. `tableIndex` as unschematized.
  1471. TJobOperationPreparer& NoOutputSchema(int tableIndex);
  1472. /// @brief Specify renaming of input columns for table no. `tableIndex`.
  1473. TJobOperationPreparer& InputColumnRenaming(int tableIndex, const THashMap<TString, TString>& renaming);
  1474. /// @brief Specify what input columns of table no. `tableIndex` to send to job
  1475. ///
  1476. /// @note Filter is applied before renaming, so it must specify original column names.
  1477. TJobOperationPreparer& InputColumnFilter(int tableIndex, const TVector<TString>& columns);
  1478. /// @brief Specify the type of input rows for table no. `tableIndex`.
  1479. ///
  1480. /// @tparam TRow type of input rows.
  1481. template <typename TRow>
  1482. TJobOperationPreparer& InputDescription(int tableIndex);
  1483. /// @brief Specify the type of output rows for table no. `tableIndex`.
  1484. ///
  1485. /// @tparam TRow type of output rows.
  1486. /// @param inferSchema Infer schema from `TRow` and specify it for the output tables.
  1487. template <typename TRow>
  1488. TJobOperationPreparer& OutputDescription(int tableIndex, bool inferSchema = true);
  1489. /// @brief Set type of output rows for table no. `tableIndex` to TNode
  1490. ///
  1491. /// @note Set schema via `OutputSchema` if needed
  1492. TJobOperationPreparer& NodeOutput(int tableIndex);
  1493. /// @brief Specify input format hints.
  1494. ///
  1495. /// These hints have lower priority than ones specified in spec.
  1496. TJobOperationPreparer& InputFormatHints(TFormatHints hints);
  1497. /// @brief Specify output format hints.
  1498. ///
  1499. /// These hints have lower priority than ones specified in spec.
  1500. TJobOperationPreparer& OutputFormatHints(TFormatHints hints);
  1501. /// @brief Specify format hints.
  1502. ///
  1503. /// These hints have lower priority than ones specified in spec.
  1504. TJobOperationPreparer& FormatHints(TUserJobFormatHints newFormatHints);
  1505. /// @name "Private" members
  1506. /// The following methods should not be used by clients in @ref NYT::IJob::PrepareOperation
  1507. ///@{
  1508. /// @brief Finish the building process.
  1509. void Finish();
  1510. /// @brief Get output table schemas as specified by the user.
  1511. TVector<TTableSchema> GetOutputSchemas();
  1512. /// @brief Get input column renamings as specified by the user.
  1513. const TVector<THashMap<TString, TString>>& GetInputColumnRenamings() const;
  1514. /// @brief Get input column filters as specified by the user.
  1515. const TVector<TMaybe<TVector<TString>>>& GetInputColumnFilters() const;
  1516. /// @brief Get input column descriptions as specified by the user.
  1517. const TVector<TMaybe<TTableStructure>>& GetInputDescriptions() const;
  1518. /// @brief Get output column descriptions as specified by the user.
  1519. const TVector<TMaybe<TTableStructure>>& GetOutputDescriptions() const;
  1520. /// @brief Get format hints as specified by the user.
  1521. const TUserJobFormatHints& GetFormatHints() const;
  1522. ///@}
  1523. private:
  1524. /// @brief Validate that schema for output table no. `tableIndex` has not been set yet.
  1525. void ValidateMissingOutputSchema(int tableIndex) const;
  1526. /// @brief Validate that description for input table no. `tableIndex` has not been set yet.
  1527. void ValidateMissingInputDescription(int tableIndex) const;
  1528. /// @brief Validate that description for output table no. `tableIndex` has not been set yet.
  1529. void ValidateMissingOutputDescription(int tableIndex) const;
  1530. /// @brief Validate that `tableIndex` is in correct range for input table indices.
  1531. ///
  1532. /// @param message Message to add to the exception in case of violation.
  1533. void ValidateInputTableIndex(int tableIndex, TStringBuf message) const;
  1534. /// @brief Validate that `tableIndex` is in correct range for output table indices.
  1535. ///
  1536. /// @param message Message to add to the exception in case of violation.
  1537. void ValidateOutputTableIndex(int tableIndex, TStringBuf message) const;
  1538. /// @brief Validate that all the output schemas has been set.
  1539. void FinallyValidate() const;
  1540. static TTableSchema EmptyNonstrictSchema();
  1541. private:
  1542. const IOperationPreparationContext& Context_;
  1543. TVector<TMaybe<TTableSchema>> OutputSchemas_;
  1544. TVector<THashMap<TString, TString>> InputColumnRenamings_;
  1545. TVector<TMaybe<TVector<TString>>> InputColumnFilters_;
  1546. TVector<TMaybe<TTableStructure>> InputTableDescriptions_;
  1547. TVector<TMaybe<TTableStructure>> OutputTableDescriptions_;
  1548. TUserJobFormatHints FormatHints_ = {};
  1549. };
  1550. ////////////////////////////////////////////////////////////////////////////////
  1551. ///
  1552. /// @brief Interface for all user jobs.
  1553. class IJob
  1554. : public TThrRefBase
  1555. {
  1556. public:
  1557. ///
  1558. /// @brief Type of job.
  1559. enum EType
  1560. {
  1561. Mapper,
  1562. Reducer,
  1563. ReducerAggregator,
  1564. RawJob,
  1565. VanillaJob,
  1566. };
  1567. ///
  1568. /// @brief Save job state to stream to be restored on cluster nodes.
  1569. virtual void Save(IOutputStream& stream) const
  1570. {
  1571. Y_UNUSED(stream);
  1572. }
  1573. ///
  1574. /// @brief Restore job state from a stream.
  1575. virtual void Load(IInputStream& stream)
  1576. {
  1577. Y_UNUSED(stream);
  1578. }
  1579. ///
  1580. /// @brief Get operation secure vault (specified in @ref NYT::TOperationOptions::SecureVault) inside a job.
  1581. const TNode& SecureVault() const
  1582. {
  1583. return GetJobSecureVault();
  1584. }
  1585. ///
  1586. /// @brief Get number of output tables.
  1587. i64 GetOutputTableCount() const
  1588. {
  1589. Y_ABORT_UNLESS(NDetail::OutputTableCount > 0);
  1590. return NDetail::OutputTableCount;
  1591. }
  1592. ///
  1593. /// @brief Method allowing user to control some properties of input and output tables and formats.
  1594. ///
  1595. /// User can override this method in their job class to:
  1596. /// - specify output table schemas.
  1597. /// The most natural way is usually through @ref NYT::TJobOperationPreparer::OutputDescription (especially for protobuf),
  1598. /// but you can use @ref NYT::TJobOperationPreparer::OutputSchema directly
  1599. /// - specify output row type (@ref NYT::TJobOperationPreparer::OutputDescription)
  1600. /// - specify input row type (@ref NYT::TJobOperationPreparer::InputDescription)
  1601. /// - specify input column filter and renaming (@ref NYT::TJobOperationPreparer::InputColumnFilter and @ref NYT::TJobOperationPreparer::InputColumnRenaming)
  1602. /// - specify format hints (@ref NYT::TJobOperationPreparer::InputFormatHints,
  1603. /// NYT::TJobOperationPreparer::OutputFormatHints and @ref NYT::TJobOperationPreparer::FormatHints)
  1604. /// - maybe something more, cf. the methods of @ref NYT::TJobOperationPreparer.
  1605. ///
  1606. /// If one has several similar tables, groups can be used.
  1607. /// Groups are delimited by @ref NYT::TJobOperationPreparer::BeginInputGroup /
  1608. /// @ref NYT::TJobOperationPreparer::TInputGroup::EndInputGroup and
  1609. /// @ref NYT::TJobOperationPreparer::BeginOutputGroup /
  1610. /// @ref NYT::TJobOperationPreparer::TOutputGroup::EndOutputGroup.
  1611. /// Example:
  1612. /// @code{.cpp}
  1613. /// preparer
  1614. /// .BeginInputGroup({1,2,4,8})
  1615. /// .ColumnRenaming({{"a", "b"}, {"c", "d"}})
  1616. /// .ColumnFilter({"a", "c"})
  1617. /// .EndInputGroup();
  1618. /// @endcode
  1619. ///
  1620. /// @note All the output table schemas must be set
  1621. /// (possibly as empty nonstrict using @ref NYT::TJobOperationPreparer::NoOutputSchema or
  1622. /// @ref NYT::TJobOperationPreparer::TOutputGroup::NoSchema).
  1623. /// By default all the output table schemas are marked as empty nonstrict.
  1624. virtual void PrepareOperation(const IOperationPreparationContext& context, TJobOperationPreparer& preparer) const;
  1625. };
  1626. ///
  1627. /// @brief Declare what fields of currently declared job class to save and restore on cluster node.
  1628. #define Y_SAVELOAD_JOB(...) \
  1629. virtual void Save(IOutputStream& stream) const override { Save(&stream); } \
  1630. virtual void Load(IInputStream& stream) override { Load(&stream); } \
  1631. Y_PASS_VA_ARGS(Y_SAVELOAD_DEFINE(__VA_ARGS__))
  1632. ///
  1633. /// @brief Same as the macro above, but also calls Base class's SaveLoad methods.
  1634. #define Y_SAVELOAD_JOB_DERIVED(Base, ...) \
  1635. virtual void Save(IOutputStream& stream) const override { \
  1636. Base::Save(stream); \
  1637. Save(&stream); \
  1638. } \
  1639. virtual void Load(IInputStream& stream) override { \
  1640. Base::Load(stream); \
  1641. Load(&stream); \
  1642. } \
  1643. Y_PASS_VA_ARGS(Y_SAVELOAD_DEFINE(__VA_ARGS__))
  1644. ////////////////////////////////////////////////////////////////////////////////
  1645. ///
  1646. /// @brief Interface for jobs with typed inputs and outputs.
  1647. class IStructuredJob
  1648. : public IJob
  1649. {
  1650. public:
  1651. ///
  1652. /// @brief This methods are called when creating table reader and writer for the job.
  1653. ///
  1654. /// Override them if you want to implement custom input logic. (e.g. additional bufferization)
  1655. virtual TRawTableReaderPtr CreateCustomRawJobReader(int fd) const;
  1656. virtual THolder<IProxyOutput> CreateCustomRawJobWriter(size_t outputTableCount) const;
  1657. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const = 0;
  1658. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const = 0;
  1659. };
  1660. ////////////////////////////////////////////////////////////////////////////////
  1661. ///
  1662. /// @brief Create default raw job reader.
  1663. TRawTableReaderPtr CreateRawJobReader(int fd = 0);
  1664. ///
  1665. /// @brief Create default raw job writer.
  1666. THolder<IProxyOutput> CreateRawJobWriter(size_t outputTableCount);
  1667. ////////////////////////////////////////////////////////////////////////////////
  1668. ///
  1669. /// @brief Base interface for structured (typed) map jobs.
  1670. class IMapperBase
  1671. : public IStructuredJob
  1672. { };
  1673. ///
  1674. /// @brief Base interface for structured (typed) map jobs with given reader and writer.
  1675. template <class TR, class TW>
  1676. class IMapper
  1677. : public IMapperBase
  1678. {
  1679. public:
  1680. using TReader = TR;
  1681. using TWriter = TW;
  1682. public:
  1683. /// Type of job implemented by this class.
  1684. static constexpr EType JobType = EType::Mapper;
  1685. ///
  1686. /// @brief This method is called before feeding input rows to mapper (before `Do` method).
  1687. virtual void Start(TWriter* writer)
  1688. {
  1689. Y_UNUSED(writer);
  1690. }
  1691. ///
  1692. /// @brief This method is called exactly once for the whole job input.
  1693. ///
  1694. /// Read input rows from `reader` and write output ones to `writer`.
  1695. virtual void Do(TReader* reader, TWriter* writer) = 0;
  1696. ///
  1697. /// @brief This method is called after feeding input rows to mapper (after `Do` method).
  1698. virtual void Finish(TWriter* writer)
  1699. {
  1700. Y_UNUSED(writer);
  1701. }
  1702. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1703. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1704. };
  1705. ////////////////////////////////////////////////////////////////////////////////
  1706. ///
  1707. /// @brief Base interface for structured (typed) reduce jobs.
  1708. ///
  1709. /// It is common base for @ref NYT::IReducer and @ref NYT::IAggregatorReducer.
  1710. class IReducerBase
  1711. : public IStructuredJob
  1712. { };
  1713. ///
  1714. /// @brief Base interface for structured (typed) reduce jobs with given reader and writer.
  1715. template <class TR, class TW>
  1716. class IReducer
  1717. : public IReducerBase
  1718. {
  1719. public:
  1720. using TReader = TR;
  1721. using TWriter = TW;
  1722. public:
  1723. /// Type of job implemented by this class.
  1724. static constexpr EType JobType = EType::Reducer;
  1725. public:
  1726. ///
  1727. /// @brief This method is called before feeding input rows to reducer (before `Do` method).
  1728. virtual void Start(TWriter* writer)
  1729. {
  1730. Y_UNUSED(writer);
  1731. }
  1732. ///
  1733. /// @brief This method is called exactly once for each range with same value of `ReduceBy` (or `JoinBy`) keys.
  1734. virtual void Do(TReader* reader, TWriter* writer) = 0;
  1735. ///
  1736. /// @brief This method is called after feeding input rows to reducer (after `Do` method).
  1737. virtual void Finish(TWriter* writer)
  1738. {
  1739. Y_UNUSED(writer);
  1740. }
  1741. ///
  1742. /// @brief Refuse to process the remaining row ranges and finish the job (successfully).
  1743. void Break();
  1744. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1745. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1746. };
  1747. ////////////////////////////////////////////////////////////////////////////////
  1748. ///
  1749. /// @brief Base interface of jobs used inside reduce operations.
  1750. ///
  1751. /// Unlike @ref NYT::IReducer jobs their `Do' method is called only once
  1752. /// and takes whole range of records split by key boundaries.
  1753. ///
  1754. /// Template argument `TR` must be @ref NYT::TTableRangesReader.
  1755. template <class TR, class TW>
  1756. class IAggregatorReducer
  1757. : public IReducerBase
  1758. {
  1759. public:
  1760. using TReader = TR;
  1761. using TWriter = TW;
  1762. public:
  1763. /// Type of job implemented by this class.
  1764. static constexpr EType JobType = EType::ReducerAggregator;
  1765. public:
  1766. ///
  1767. /// @brief This method is called before feeding input rows to reducer (before `Do` method).
  1768. virtual void Start(TWriter* writer)
  1769. {
  1770. Y_UNUSED(writer);
  1771. }
  1772. ///
  1773. /// @brief This method is called exactly once for the whole job input.
  1774. virtual void Do(TReader* reader, TWriter* writer) = 0;
  1775. ///
  1776. /// @brief This method is called after feeding input rows to reducer (after `Do` method).
  1777. virtual void Finish(TWriter* writer)
  1778. {
  1779. Y_UNUSED(writer);
  1780. }
  1781. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1782. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1783. };
  1784. ////////////////////////////////////////////////////////////////////////////////
  1785. ///
  1786. /// @brief Interface for raw jobs (i.e. reading and writing byte streams).
  1787. class IRawJob
  1788. : public IJob
  1789. {
  1790. public:
  1791. /// Type of job implemented by this class.
  1792. static constexpr EType JobType = EType::RawJob;
  1793. ///
  1794. /// @brief This method is called exactly once for the whole job input.
  1795. virtual void Do(const TRawJobContext& jobContext) = 0;
  1796. };
  1797. ///
  1798. /// @brief Interface of jobs that run the given bash command.
  1799. class ICommandJob
  1800. : public IJob
  1801. {
  1802. public:
  1803. ///
  1804. /// @brief Get bash command to run.
  1805. ///
  1806. /// @note This method is called on the client side.
  1807. virtual const TString& GetCommand() const = 0;
  1808. };
  1809. ///
  1810. /// @brief Raw job executing given bash command.
  1811. ///
  1812. /// @note The binary will not be uploaded.
  1813. class TCommandRawJob
  1814. : public IRawJob
  1815. , public ICommandJob
  1816. {
  1817. public:
  1818. ///
  1819. /// @brief Create job with specified command.
  1820. ///
  1821. /// @param command Bash command to run.
  1822. explicit TCommandRawJob(TStringBuf command = {});
  1823. const TString& GetCommand() const override;
  1824. void Do(const TRawJobContext& jobContext) override;
  1825. private:
  1826. TString Command_;
  1827. };
  1828. ////////////////////////////////////////////////////////////////////////////////
  1829. ///
  1830. /// @brief Base interface for vanilla jobs.
  1831. ///
  1832. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/vanilla
  1833. class IVanillaJobBase
  1834. : public virtual IStructuredJob
  1835. {
  1836. public:
  1837. /// Type of job implemented by this class.
  1838. static constexpr EType JobType = EType::VanillaJob;
  1839. };
  1840. template <class TW = void>
  1841. class IVanillaJob;
  1842. ///
  1843. /// @brief Interface of vanilla job without outputs.
  1844. template <>
  1845. class IVanillaJob<void>
  1846. : public IVanillaJobBase
  1847. {
  1848. public:
  1849. ///
  1850. /// @brief This method is called exactly once for each vanilla job.
  1851. virtual void Do() = 0;
  1852. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1853. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1854. };
  1855. ///
  1856. /// @brief Vanilla job executing given bash command.
  1857. ///
  1858. /// @note The binary will not be uploaded.
  1859. class TCommandVanillaJob
  1860. : public IVanillaJob<>
  1861. , public ICommandJob
  1862. {
  1863. public:
  1864. ///
  1865. /// @brief Create job with specified command.
  1866. ///
  1867. /// @param command Bash command to run.
  1868. explicit TCommandVanillaJob(TStringBuf command = {});
  1869. const TString& GetCommand() const override;
  1870. void Do() override;
  1871. private:
  1872. TString Command_;
  1873. };
  1874. ///
  1875. /// @brief Interface for vanilla jobs with output tables.
  1876. template <class TW>
  1877. class IVanillaJob
  1878. : public IVanillaJobBase
  1879. {
  1880. public:
  1881. using TWriter = TW;
  1882. public:
  1883. ///
  1884. /// @brief This method is called before `Do` method.
  1885. virtual void Start(TWriter* /* writer */)
  1886. { }
  1887. ///
  1888. /// @brief This method is called exactly once for each vanilla job.
  1889. ///
  1890. /// Write output rows to `writer`.
  1891. virtual void Do(TWriter* writer) = 0;
  1892. ///
  1893. /// @brief This method is called after `Do` method.
  1894. virtual void Finish(TWriter* /* writer */)
  1895. { }
  1896. virtual TStructuredRowStreamDescription GetInputRowStreamDescription() const override;
  1897. virtual TStructuredRowStreamDescription GetOutputRowStreamDescription() const override;
  1898. };
  1899. ////////////////////////////////////////////////////////////////////////////////
  1900. ///
  1901. /// @brief Attributes to request for an operation.
  1902. enum class EOperationAttribute : int
  1903. {
  1904. Id /* "id" */,
  1905. Type /* "type" */,
  1906. State /* "state" */,
  1907. AuthenticatedUser /* "authenticated_user" */,
  1908. StartTime /* "start_time" */,
  1909. FinishTime /* "finish_time" */,
  1910. BriefProgress /* "brief_progress" */,
  1911. BriefSpec /* "brief_spec" */,
  1912. Suspended /* "suspended" */,
  1913. Result /* "result" */,
  1914. Progress /* "progress" */,
  1915. Events /* "events" */,
  1916. Spec /* "spec" */,
  1917. FullSpec /* "full_spec" */,
  1918. UnrecognizedSpec /* "unrecognized_spec" */,
  1919. };
  1920. ///
  1921. /// @brief Class describing which attributes to request in @ref NYT::IClient::GetOperation or @ref NYT::IClient::ListOperations.
  1922. struct TOperationAttributeFilter
  1923. {
  1924. /// @cond Doxygen_Suppress
  1925. using TSelf = TOperationAttributeFilter;
  1926. /// @endcond
  1927. TVector<EOperationAttribute> Attributes_;
  1928. ///
  1929. /// @brief Add attribute to the filter. Calls are supposed to be chained.
  1930. TSelf& Add(EOperationAttribute attribute)
  1931. {
  1932. Attributes_.push_back(attribute);
  1933. return *this;
  1934. }
  1935. };
  1936. ///
  1937. /// @brief Options for @ref NYT::IClient::GetOperation call.
  1938. struct TGetOperationOptions
  1939. {
  1940. /// @cond Doxygen_Suppress
  1941. using TSelf = TGetOperationOptions;
  1942. /// @endcond
  1943. ///
  1944. /// @brief What attributes to request (if omitted, the default set of attributes will be requested).
  1945. FLUENT_FIELD_OPTION(TOperationAttributeFilter, AttributeFilter);
  1946. FLUENT_FIELD_OPTION(bool, IncludeRuntime);
  1947. };
  1948. ///
  1949. /// @brief "Coarse-grained" state of an operation.
  1950. enum class EOperationBriefState : int
  1951. {
  1952. InProgress /* "in_progress" */,
  1953. Completed /* "completed" */,
  1954. Aborted /* "aborted" */,
  1955. /// Failed
  1956. Failed /* "failed" */,
  1957. };
  1958. ///
  1959. /// @brief Operation type.
  1960. enum class EOperationType : int
  1961. {
  1962. Map /* "map" */,
  1963. Merge /* "merge" */,
  1964. Erase /* "erase" */,
  1965. Sort /* "sort" */,
  1966. Reduce /* "reduce" */,
  1967. MapReduce /* "map_reduce" */,
  1968. RemoteCopy /* "remote_copy" */,
  1969. JoinReduce /* "join_reduce" */,
  1970. Vanilla /* "vanilla" */,
  1971. };
  1972. ///
  1973. /// @brief Operation progress.
  1974. struct TOperationProgress
  1975. {
  1976. ///
  1977. /// @brief Total job statistics.
  1978. TJobStatistics JobStatistics;
  1979. ///
  1980. /// @brief Job counter for various job states with hierarchy.
  1981. TJobCounters JobCounters;
  1982. ///
  1983. /// @brief Time when this progress was built on scheduler or CA.
  1984. TMaybe<TInstant> BuildTime;
  1985. };
  1986. ///
  1987. /// @brief Brief operation progress (numbers of jobs in these states).
  1988. struct TOperationBriefProgress
  1989. {
  1990. ui64 Aborted = 0;
  1991. ui64 Completed = 0;
  1992. ui64 Failed = 0;
  1993. ui64 Lost = 0;
  1994. ui64 Pending = 0;
  1995. ui64 Running = 0;
  1996. ui64 Total = 0;
  1997. };
  1998. ///
  1999. /// @brief Operation result.
  2000. struct TOperationResult
  2001. {
  2002. ///
  2003. /// @brief For a unsuccessfully finished operation: description of error.
  2004. TMaybe<TYtError> Error;
  2005. };
  2006. ///
  2007. /// @brief Operation event (change of state).
  2008. struct TOperationEvent
  2009. {
  2010. ///
  2011. /// @brief New state of operation.
  2012. TString State;
  2013. ///
  2014. /// @brief Time of state change.
  2015. TInstant Time;
  2016. };
  2017. ///
  2018. /// @brief Operation info.
  2019. ///
  2020. /// A field may be `Nothing()` either if it was not requested (see @ref NYT::TGetOperationOptions::AttributeFilter)
  2021. /// or it is not available (i.e. `FinishTime` for a running operation).
  2022. /// @see https://ytsaurus.tech/docs/en/api/commands#get_operation
  2023. struct TOperationAttributes
  2024. {
  2025. ///
  2026. /// @brief Operation id.
  2027. TMaybe<TOperationId> Id;
  2028. ///
  2029. /// @brief Operation type.
  2030. TMaybe<EOperationType> Type;
  2031. ///
  2032. /// @brief Operation state.
  2033. TMaybe<TString> State;
  2034. ///
  2035. /// @brief "Coarse-grained" operation state.
  2036. TMaybe<EOperationBriefState> BriefState;
  2037. ///
  2038. /// @brief Name of user that started the operation.
  2039. TMaybe<TString> AuthenticatedUser;
  2040. ///
  2041. /// @brief Operation start time.
  2042. TMaybe<TInstant> StartTime;
  2043. ///
  2044. /// @brief Operation finish time (if the operation has finished).
  2045. TMaybe<TInstant> FinishTime;
  2046. ///
  2047. /// @brief Brief progress of the operation.
  2048. TMaybe<TOperationBriefProgress> BriefProgress;
  2049. ///
  2050. /// @brief Brief spec of operation (light-weight fields only).
  2051. TMaybe<TNode> BriefSpec;
  2052. ///
  2053. /// @brief Spec of the operation as provided by the user.
  2054. TMaybe<TNode> Spec;
  2055. ///
  2056. /// @brief Full spec of operation (all fields not specified by user are filled with default values).
  2057. TMaybe<TNode> FullSpec;
  2058. ///
  2059. /// @brief Fields not recognized by scheduler.
  2060. TMaybe<TNode> UnrecognizedSpec;
  2061. ///
  2062. /// @brief Is operation suspended.
  2063. TMaybe<bool> Suspended;
  2064. ///
  2065. /// @brief Operation result.
  2066. TMaybe<TOperationResult> Result;
  2067. ///
  2068. /// @brief Operation progress.
  2069. TMaybe<TOperationProgress> Progress;
  2070. ///
  2071. /// @brief List of operation events (changes of state).
  2072. TMaybe<TVector<TOperationEvent>> Events;
  2073. ///
  2074. /// @brief Map from alert name to its description.
  2075. TMaybe<THashMap<TString, TYtError>> Alerts;
  2076. };
  2077. ///
  2078. /// @brief Direction of cursor for paging, see @ref NYT::TListOperationsOptions::CursorDirection.
  2079. enum class ECursorDirection
  2080. {
  2081. Past /* "past" */,
  2082. Future /* "future" */,
  2083. };
  2084. ///
  2085. /// @brief Options of @ref NYT::IClient::ListOperations command.
  2086. ///
  2087. /// @see https://ytsaurus.tech/docs/en/api/commands.html#list_operations
  2088. struct TListOperationsOptions
  2089. {
  2090. /// @cond Doxygen_Suppress
  2091. using TSelf = TListOperationsOptions;
  2092. /// @endcond
  2093. ///
  2094. /// @name Time range specification
  2095. ///
  2096. /// List operations with start time in half-closed interval
  2097. /// `[CursorTime, ToTime)` if `CursorDirection == Future` or
  2098. /// `[FromTime, CursorTime)` if `CursorDirection == Past`.
  2099. ///@{
  2100. ///
  2101. /// @brief Search for operations with start time >= `FromTime`.
  2102. FLUENT_FIELD_OPTION(TInstant, FromTime);
  2103. ///
  2104. /// @brief Search for operations with start time < `ToTime`.
  2105. FLUENT_FIELD_OPTION(TInstant, ToTime);
  2106. ///
  2107. /// @brief Additional restriction on operation start time (useful for pagination).
  2108. ///
  2109. /// Search for operations with start time >= `CursorTime` if `CursorDirection == Future`
  2110. /// and with start time < `CursorTime` if `CursorDirection == Past`
  2111. FLUENT_FIELD_OPTION(TInstant, CursorTime);
  2112. ///
  2113. /// @brief Direction of pagination (see @ref NYT::TListOperationsOptions::CursorTime).
  2114. FLUENT_FIELD_OPTION(ECursorDirection, CursorDirection);
  2115. ///@}
  2116. ///
  2117. /// @name Filters
  2118. /// Choose operations satisfying given filters.
  2119. ///@{
  2120. ///
  2121. /// @brief Search for `Filter` as a substring in operation text factors
  2122. /// (e.g. title or input/output table paths).
  2123. FLUENT_FIELD_OPTION(TString, Filter);
  2124. ///
  2125. /// @brief Choose operations whose pools include `Pool`.
  2126. FLUENT_FIELD_OPTION(TString, Pool);
  2127. ///
  2128. /// @brief Choose operations with given @ref NYT::TOperationAttributes::AuthenticatedUser.
  2129. FLUENT_FIELD_OPTION(TString, User);
  2130. ///
  2131. /// @brief Choose operations with given @ref NYT::TOperationAttributes::State.
  2132. FLUENT_FIELD_OPTION(TString, State);
  2133. ///
  2134. /// @brief Choose operations with given @ref NYT::TOperationAttributes::Type.
  2135. FLUENT_FIELD_OPTION(EOperationType, Type);
  2136. ///
  2137. /// @brief Choose operations having (or not having) any failed jobs.
  2138. FLUENT_FIELD_OPTION(bool, WithFailedJobs);
  2139. ///@}
  2140. ///
  2141. /// @brief Search for operations in the archive in addition to Cypress.
  2142. FLUENT_FIELD_OPTION(bool, IncludeArchive);
  2143. ///
  2144. /// @brief Include the counters for different filter parameters in the response.
  2145. ///
  2146. /// Include number of operations for each pool, user, state, type
  2147. /// and the number of operations having failed jobs.
  2148. FLUENT_FIELD_OPTION(bool, IncludeCounters);
  2149. ///
  2150. /// @brief Return no more than `Limit` operations (current default and maximum value is 1000).
  2151. FLUENT_FIELD_OPTION(i64, Limit);
  2152. };
  2153. ///
  2154. /// @brief Response for @ref NYT::IClient::ListOperations command.
  2155. struct TListOperationsResult
  2156. {
  2157. ///
  2158. /// @brief Found operations' attributes.
  2159. TVector<TOperationAttributes> Operations;
  2160. ///
  2161. /// @name Counters for different filter.
  2162. ///
  2163. /// If counters were requested (@ref NYT::TListOperationsOptions::IncludeCounters is `true`)
  2164. /// the maps contain the number of operations found for each pool, user, state and type.
  2165. /// NOTE:
  2166. /// 1) Counters ignore CursorTime and CursorDirection,
  2167. /// they always are collected in the whole [FromTime, ToTime) interval.
  2168. /// 2) Each next counter in the sequence [pool, user, state, type, with_failed_jobs]
  2169. /// takes into account all the previous filters (i.e. if you set User filter to "some-user"
  2170. /// type counts describe only operations with user "some-user").
  2171. /// @{
  2172. ///
  2173. /// @brief Number of operations for each pool.
  2174. TMaybe<THashMap<TString, i64>> PoolCounts;
  2175. ///
  2176. /// @brief Number of operations for each user (subject to previous filters).
  2177. TMaybe<THashMap<TString, i64>> UserCounts;
  2178. ///
  2179. /// @brief Number of operations for each state (subject to previous filters).
  2180. TMaybe<THashMap<TString, i64>> StateCounts;
  2181. ///
  2182. /// @brief Number of operations for each type (subject to previous filters).
  2183. TMaybe<THashMap<EOperationType, i64>> TypeCounts;
  2184. ///
  2185. /// @brief Number of operations having failed jobs (subject to all previous filters).
  2186. TMaybe<i64> WithFailedJobsCount;
  2187. /// @}
  2188. ///
  2189. /// @brief Whether some operations were not returned due to @ref NYT::TListOperationsOptions::Limit.
  2190. ///
  2191. /// `Incomplete == true` means that not all operations satisfying filters
  2192. /// were returned (limit exceeded) and you need to repeat the request with new @ref NYT::TListOperationsOptions::CursorTime
  2193. /// (e.g. `CursorTime == *Operations.back().StartTime`, but don't forget to
  2194. /// remove the duplicates).
  2195. bool Incomplete;
  2196. };
  2197. ////////////////////////////////////////////////////////////////////////////////
  2198. ///
  2199. /// @brief Data source for @ref NYT::IClient::ListJobs command.
  2200. enum class EListJobsDataSource : int
  2201. {
  2202. Runtime /* "runtime" */,
  2203. Archive /* "archive" */,
  2204. Auto /* "auto" */,
  2205. Manual /* "manual" */,
  2206. };
  2207. ///
  2208. /// @brief Job type.
  2209. enum class EJobType : int
  2210. {
  2211. SchedulerFirst /* "scheduler_first" */,
  2212. Map /* "map" */,
  2213. PartitionMap /* "partition_map" */,
  2214. SortedMerge /* "sorted_merge" */,
  2215. OrderedMerge /* "ordered_merge" */,
  2216. UnorderedMerge /* "unordered_merge" */,
  2217. Partition /* "partition" */,
  2218. SimpleSort /* "simple_sort" */,
  2219. FinalSort /* "final_sort" */,
  2220. SortedReduce /* "sorted_reduce" */,
  2221. PartitionReduce /* "partition_reduce" */,
  2222. ReduceCombiner /* "reduce_combiner" */,
  2223. RemoteCopy /* "remote_copy" */,
  2224. IntermediateSort /* "intermediate_sort" */,
  2225. OrderedMap /* "ordered_map" */,
  2226. JoinReduce /* "join_reduce" */,
  2227. Vanilla /* "vanilla" */,
  2228. SchedulerUnknown /* "scheduler_unknown" */,
  2229. SchedulerLast /* "scheduler_last" */,
  2230. ReplicatorFirst /* "replicator_first" */,
  2231. ReplicateChunk /* "replicate_chunk" */,
  2232. RemoveChunk /* "remove_chunk" */,
  2233. RepairChunk /* "repair_chunk" */,
  2234. SealChunk /* "seal_chunk" */,
  2235. ReplicatorLast /* "replicator_last" */,
  2236. };
  2237. ///
  2238. /// @brief Well-known task names.
  2239. enum class ETaskName : int
  2240. {
  2241. Map /* "map" */,
  2242. PartitionMap0 /* "partition_map(0)" */,
  2243. SortedMerge /* "sorted_merge" */,
  2244. OrderedMerge /* "ordered_merge" */,
  2245. UnorderedMerge /* "unordered_merge" */,
  2246. Partition0 /* "partition(0)" */,
  2247. Partition1 /* "partition(1)" */,
  2248. Partition2 /* "partition(2)" */,
  2249. SimpleSort /* "simple_sort" */,
  2250. FinalSort /* "final_sort" */,
  2251. SortedReduce /* "sorted_reduce" */,
  2252. PartitionReduce /* "partition_reduce" */,
  2253. ReduceCombiner /* "reduce_combiner" */,
  2254. RemoteCopy /* "remote_copy" */,
  2255. IntermediateSort /* "intermediate_sort" */,
  2256. OrderedMap /* "ordered_map" */,
  2257. JoinReduce /* "join_reduce" */,
  2258. };
  2259. ///
  2260. /// @brief Task name (can either well-known or just a string).
  2261. class TTaskName
  2262. {
  2263. public:
  2264. // Constructors are implicit by design.
  2265. ///
  2266. /// @brief Construct a custom task name.
  2267. TTaskName(TString taskName);
  2268. ///
  2269. /// @brief Construct a custom task name.
  2270. TTaskName(const char* taskName);
  2271. ///
  2272. /// @brief Construct a well-known task name.
  2273. TTaskName(ETaskName taskName);
  2274. const TString& Get() const;
  2275. private:
  2276. TString TaskName_;
  2277. };
  2278. ///
  2279. /// @brief Job state.
  2280. enum class EJobState : int
  2281. {
  2282. None /* "none" */,
  2283. Waiting /* "waiting" */,
  2284. Running /* "running" */,
  2285. Aborting /* "aborting" */,
  2286. Completed /* "completed" */,
  2287. Failed /* "failed" */,
  2288. Aborted /* "aborted" */,
  2289. Lost /* "lost" */,
  2290. };
  2291. ///
  2292. /// @brief Job sort field.
  2293. ///
  2294. /// @see @ref NYT::TListJobsOptions.
  2295. enum class EJobSortField : int
  2296. {
  2297. Type /* "type" */,
  2298. State /* "state" */,
  2299. StartTime /* "start_time" */,
  2300. FinishTime /* "finish_time" */,
  2301. Address /* "address" */,
  2302. Duration /* "duration" */,
  2303. Progress /* "progress" */,
  2304. Id /* "id" */,
  2305. };
  2306. ///
  2307. /// @brief Job sort direction.
  2308. ///
  2309. /// @see @ref NYT::TListJobsOptions.
  2310. enum class EJobSortDirection : int
  2311. {
  2312. Ascending /* "ascending" */,
  2313. Descending /* "descending" */,
  2314. };
  2315. ///
  2316. /// @brief Options for @ref NYT::IClient::ListJobs.
  2317. ///
  2318. /// @see https://ytsaurus.tech/docs/en/api/commands.html#list_jobs
  2319. struct TListJobsOptions
  2320. {
  2321. /// @cond Doxygen_Suppress
  2322. using TSelf = TListJobsOptions;
  2323. /// @endcond
  2324. ///
  2325. /// @name Filters
  2326. /// Return only jobs with given value of parameter (type, state, address and existence of stderr).
  2327. /// If a field is `Nothing()`, return jobs with all possible values of the corresponding parameter.
  2328. /// @{
  2329. ///
  2330. /// @brief Job type.
  2331. FLUENT_FIELD_OPTION(EJobType, Type);
  2332. ///
  2333. /// @brief Job state.
  2334. FLUENT_FIELD_OPTION(EJobState, State);
  2335. ///
  2336. /// @brief Address of the cluster node where job was running.
  2337. FLUENT_FIELD_OPTION(TString, Address);
  2338. ///
  2339. /// @brief Return only jobs whose stderr has been saved.
  2340. FLUENT_FIELD_OPTION(bool, WithStderr);
  2341. ///
  2342. /// @brief Return only jobs whose spec has been saved.
  2343. FLUENT_FIELD_OPTION(bool, WithSpec);
  2344. ///
  2345. /// @brief Return only jobs whose fail context has been saved.
  2346. FLUENT_FIELD_OPTION(bool, WithFailContext);
  2347. ///
  2348. /// @brief Return only jobs with monitoring descriptor.
  2349. FLUENT_FIELD_OPTION(bool, WithMonitoringDescriptor);
  2350. /// @}
  2351. ///
  2352. /// @name Sort options
  2353. /// @{
  2354. ///
  2355. /// @brief Sort by this field.
  2356. FLUENT_FIELD_OPTION(EJobSortField, SortField);
  2357. ///
  2358. /// @brief Sort order.
  2359. FLUENT_FIELD_OPTION(ESortOrder, SortOrder);
  2360. /// @}
  2361. ///
  2362. /// @brief Data source.
  2363. ///
  2364. /// Where to search for jobs: in scheduler and Cypress ('Runtime'), in archive ('Archive'),
  2365. /// automatically basing on operation presence in Cypress ('Auto') or choose manually (`Manual').
  2366. FLUENT_FIELD_OPTION(EListJobsDataSource, DataSource);
  2367. /// @deprecated
  2368. FLUENT_FIELD_OPTION(bool, IncludeCypress);
  2369. /// @deprecated
  2370. FLUENT_FIELD_OPTION(bool, IncludeControllerAgent);
  2371. /// @deprecated
  2372. FLUENT_FIELD_OPTION(bool, IncludeArchive);
  2373. ///
  2374. /// @brief Maximum number of jobs to return.
  2375. FLUENT_FIELD_OPTION(i64, Limit);
  2376. ///
  2377. /// @brief Number of jobs (in specified sort order) to skip.
  2378. ///
  2379. /// Together with @ref NYT::TListJobsOptions::Limit may be used for pagination.
  2380. FLUENT_FIELD_OPTION(i64, Offset);
  2381. };
  2382. ///
  2383. /// @brief Description of a core dump that happened in the job.
  2384. struct TCoreInfo
  2385. {
  2386. i64 ProcessId;
  2387. TString ExecutableName;
  2388. TMaybe<ui64> Size;
  2389. TMaybe<TYtError> Error;
  2390. };
  2391. ///
  2392. /// @brief Job attributes.
  2393. ///
  2394. /// A field may be `Nothing()` if it is not available (i.e. `FinishTime` for a running job).
  2395. ///
  2396. /// @see https://ytsaurus.tech/docs/en/api/commands#get_job
  2397. struct TJobAttributes
  2398. {
  2399. ///
  2400. /// @brief Job id.
  2401. TMaybe<TJobId> Id;
  2402. ///
  2403. /// @brief Job type
  2404. TMaybe<EJobType> Type;
  2405. ///
  2406. /// @brief Job state.
  2407. TMaybe<EJobState> State;
  2408. ///
  2409. /// @brief Address of a cluster node where job was running.
  2410. TMaybe<TString> Address;
  2411. ///
  2412. /// @brief The name of the task that job corresponds to.
  2413. TMaybe<TString> TaskName;
  2414. ///
  2415. /// @brief Job start time.
  2416. TMaybe<TInstant> StartTime;
  2417. ///
  2418. /// @brief Job finish time (for a finished job).
  2419. TMaybe<TInstant> FinishTime;
  2420. ///
  2421. /// @brief Estimated ratio of job's completed work.
  2422. TMaybe<double> Progress;
  2423. ///
  2424. /// @brief Size of saved job stderr.
  2425. TMaybe<i64> StderrSize;
  2426. ///
  2427. /// @brief Error for a unsuccessfully finished job.
  2428. TMaybe<TYtError> Error;
  2429. ///
  2430. /// @brief Job brief statistics.
  2431. TMaybe<TNode> BriefStatistics;
  2432. ///
  2433. /// @brief Job input paths (with ranges).
  2434. TMaybe<TVector<TRichYPath>> InputPaths;
  2435. ///
  2436. /// @brief Infos for core dumps produced by job.
  2437. TMaybe<TVector<TCoreInfo>> CoreInfos;
  2438. };
  2439. ///
  2440. /// @brief Response for @ref NYT::IOperation::ListJobs.
  2441. struct TListJobsResult
  2442. {
  2443. ///
  2444. /// @brief Jobs.
  2445. TVector<TJobAttributes> Jobs;
  2446. ///
  2447. /// @deprecated
  2448. TMaybe<i64> CypressJobCount;
  2449. ///
  2450. /// @brief Number of jobs retrieved from controller agent.
  2451. TMaybe<i64> ControllerAgentJobCount;
  2452. ///
  2453. /// @brief Number of jobs retrieved from archive.
  2454. TMaybe<i64> ArchiveJobCount;
  2455. };
  2456. ////////////////////////////////////////////////////////////////////////////////
  2457. ///
  2458. /// @brief Options for @ref NYT::IClient::GetJob.
  2459. struct TGetJobOptions
  2460. {
  2461. /// @cond Doxygen_Suppress
  2462. using TSelf = TGetJobOptions;
  2463. /// @endcond
  2464. };
  2465. ///
  2466. /// @brief Options for @ref NYT::IClient::GetJobInput.
  2467. struct TGetJobInputOptions
  2468. {
  2469. /// @cond Doxygen_Suppress
  2470. using TSelf = TGetJobInputOptions;
  2471. /// @endcond
  2472. };
  2473. ///
  2474. /// @brief Options for @ref NYT::IClient::GetJobFailContext.
  2475. struct TGetJobFailContextOptions
  2476. {
  2477. /// @cond Doxygen_Suppress
  2478. using TSelf = TGetJobFailContextOptions;
  2479. /// @endcond
  2480. };
  2481. ///
  2482. /// @brief Options for @ref NYT::IClient::GetJobStderr.
  2483. struct TGetJobStderrOptions
  2484. {
  2485. /// @cond Doxygen_Suppress
  2486. using TSelf = TGetJobStderrOptions;
  2487. /// @endcond
  2488. };
  2489. ////////////////////////////////////////////////////////////////////////////////
  2490. ///
  2491. /// @brief Options for @ref NYT::IOperation::GetFailedJobInfo.
  2492. struct TGetFailedJobInfoOptions
  2493. {
  2494. /// @cond Doxygen_Suppress
  2495. using TSelf = TGetFailedJobInfoOptions;
  2496. /// @endcond
  2497. ///
  2498. /// @brief How many jobs to download. Which jobs will be chosen is undefined.
  2499. FLUENT_FIELD_DEFAULT(ui64, MaxJobCount, 10);
  2500. ///
  2501. /// @brief How much of stderr tail should be downloaded.
  2502. FLUENT_FIELD_DEFAULT(ui64, StderrTailSize, 64 * 1024);
  2503. };
  2504. ////////////////////////////////////////////////////////////////////////////////
  2505. ///
  2506. /// @brief Interface representing an operation.
  2507. struct IOperation
  2508. : public TThrRefBase
  2509. {
  2510. virtual ~IOperation() = default;
  2511. ///
  2512. /// @brief Get operation id.
  2513. virtual const TOperationId& GetId() const = 0;
  2514. ///
  2515. /// @brief Get URL of the operation in YT Web UI.
  2516. virtual TString GetWebInterfaceUrl() const = 0;
  2517. ///
  2518. /// @brief Get last error for not started operations. Get state on YT cluster for started operations.
  2519. ///
  2520. /// For not started operations last error is an error that's being retried during operation
  2521. /// preparation/start (e.g. lock files, start operation request).
  2522. virtual TString GetStatus() const = 0;
  2523. ///
  2524. /// @brief Get preparation future.
  2525. ///
  2526. /// @return future that is set when operation is prepared.
  2527. virtual ::NThreading::TFuture<void> GetPreparedFuture() = 0;
  2528. ///
  2529. /// @brief Start operation synchronously.
  2530. ///
  2531. /// @note: Do NOT call this method twice.
  2532. ///
  2533. /// If operation is not prepared yet, Start() will block waiting for preparation finish.
  2534. /// Be ready to catch exception if operation preparation or start failed.
  2535. virtual void Start() = 0;
  2536. ///
  2537. /// @brief Is the operation started
  2538. ///
  2539. /// Returns true if the operation is started on the cluster
  2540. virtual bool IsStarted() const = 0;
  2541. ///
  2542. /// @brief Get start future.
  2543. ///
  2544. /// @return future that is set when operation is started.
  2545. virtual ::NThreading::TFuture<void> GetStartedFuture() = 0;
  2546. ///
  2547. /// @brief Start watching operation.
  2548. ///
  2549. /// @return future that is set when operation is complete.
  2550. ///
  2551. /// @note: the user should check value of returned future to ensure that operation completed successfully e.g.
  2552. /// @code{.cpp}
  2553. /// auto operationComplete = operation->Watch();
  2554. /// operationComplete.Wait();
  2555. /// operationComplete.GetValue(); /// will throw if operation completed with errors
  2556. /// @endcode
  2557. ///
  2558. /// If operation is completed successfully the returned future contains void value.
  2559. /// If operation is completed with error future contains @ref NYT::TOperationFailedError.
  2560. /// In rare cases when error occurred while waiting (e.g. YT become unavailable) future might contain other exception.
  2561. virtual ::NThreading::TFuture<void> Watch() = 0;
  2562. ///
  2563. /// @brief Get information about failed jobs.
  2564. ///
  2565. /// Can be called for operation in any stage.
  2566. /// Though user should keep in mind that this method always fetches info from cypress
  2567. /// and doesn't work when operation is archived. Successfully completed operations can be archived
  2568. /// quite quickly (in about ~30 seconds).
  2569. virtual TVector<TFailedJobInfo> GetFailedJobInfo(const TGetFailedJobInfoOptions& options = TGetFailedJobInfoOptions()) = 0;
  2570. ///
  2571. /// Get operation brief state.
  2572. virtual EOperationBriefState GetBriefState() = 0;
  2573. ///
  2574. /// @brief Get error (if operation has failed).
  2575. ///
  2576. /// @return `Nothing()` if operation is in 'Completed' or 'InProgress' state (or reason for failed / aborted operation).
  2577. virtual TMaybe<TYtError> GetError() = 0;
  2578. ///
  2579. /// Get job statistics.
  2580. virtual TJobStatistics GetJobStatistics() = 0;
  2581. ///
  2582. /// Get operation progress.
  2583. ///
  2584. /// @return `Nothing()` if operation has no running jobs yet, e.g. when it is in "materializing" or "pending" state.
  2585. virtual TMaybe<TOperationBriefProgress> GetBriefProgress() = 0;
  2586. ///
  2587. /// @brief Abort operation.
  2588. ///
  2589. /// Operation will be finished immediately.
  2590. /// All results of completed/running jobs will be lost.
  2591. ///
  2592. /// @see https://ytsaurus.tech/docs/en/api/commands#abort_op
  2593. virtual void AbortOperation() = 0;
  2594. ///
  2595. /// @brief Complete operation.
  2596. ///
  2597. /// Operation will be finished immediately.
  2598. /// All results of completed jobs will appear in output tables.
  2599. /// All results of running (not completed) jobs will be lost.
  2600. ///
  2601. /// @see https://ytsaurus.tech/docs/en/api/commands#complete_op
  2602. virtual void CompleteOperation() = 0;
  2603. ///
  2604. /// @brief Suspend operation.
  2605. ///
  2606. /// Jobs will not be aborted by default, c.f. @ref NYT::TSuspendOperationOptions.
  2607. ///
  2608. /// @see https://ytsaurus.tech/docs/en/api/commands#suspend_op
  2609. virtual void SuspendOperation(
  2610. const TSuspendOperationOptions& options = TSuspendOperationOptions()) = 0;
  2611. ///
  2612. /// @brief Resume previously suspended operation.
  2613. ///
  2614. /// @see https://ytsaurus.tech/docs/en/api/commands#resume_op
  2615. virtual void ResumeOperation(
  2616. const TResumeOperationOptions& options = TResumeOperationOptions()) = 0;
  2617. ///
  2618. /// @brief Get operation attributes.
  2619. ///
  2620. /// @see https://ytsaurus.tech/docs/en/api/commands#get_operation
  2621. virtual TOperationAttributes GetAttributes(
  2622. const TGetOperationOptions& options = TGetOperationOptions()) = 0;
  2623. ///
  2624. /// @brief Update operation runtime parameters.
  2625. ///
  2626. /// @see https://ytsaurus.tech/docs/en/api/commands#update_op_parameters
  2627. virtual void UpdateParameters(
  2628. const TUpdateOperationParametersOptions& options = TUpdateOperationParametersOptions()) = 0;
  2629. ///
  2630. /// @brief Get job attributes.
  2631. ///
  2632. /// @see https://ytsaurus.tech/docs/en/api/commands#get_job
  2633. virtual TJobAttributes GetJob(
  2634. const TJobId& jobId,
  2635. const TGetJobOptions& options = TGetJobOptions()) = 0;
  2636. ///
  2637. /// List jobs satisfying given filters (see @ref NYT::TListJobsOptions).
  2638. ///
  2639. /// @see https://ytsaurus.tech/docs/en/api/commands#list_jobs
  2640. virtual TListJobsResult ListJobs(
  2641. const TListJobsOptions& options = TListJobsOptions()) = 0;
  2642. };
  2643. ///
  2644. /// @brief Interface of client capable of managing operations.
  2645. struct IOperationClient
  2646. {
  2647. ///
  2648. /// @brief Run Map operation.
  2649. ///
  2650. /// @param spec Operation spec.
  2651. /// @param mapper Instance of a job to run.
  2652. /// @param options Optional parameters.
  2653. ///
  2654. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  2655. IOperationPtr Map(
  2656. const TMapOperationSpec& spec,
  2657. ::TIntrusivePtr<IMapperBase> mapper,
  2658. const TOperationOptions& options = TOperationOptions());
  2659. ///
  2660. /// @brief Run Map operation.
  2661. ///
  2662. /// @param mapper Instance of a job to run.
  2663. /// @param input Input table(s)
  2664. /// @param output Output table(s)
  2665. /// @param spec Operation spec.
  2666. /// @param options Optional parameters.
  2667. ///
  2668. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  2669. IOperationPtr Map(
  2670. ::TIntrusivePtr<IMapperBase> mapper,
  2671. const TOneOrMany<TStructuredTablePath>& input,
  2672. const TOneOrMany<TStructuredTablePath>& output,
  2673. const TMapOperationSpec& spec = TMapOperationSpec(),
  2674. const TOperationOptions& options = TOperationOptions());
  2675. ///
  2676. /// @brief Run raw Map operation.
  2677. ///
  2678. /// @param spec Operation spec.
  2679. /// @param rawJob Instance of a raw mapper to run.
  2680. /// @param options Optional parameters.
  2681. ///
  2682. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/map
  2683. virtual IOperationPtr RawMap(
  2684. const TRawMapOperationSpec& spec,
  2685. ::TIntrusivePtr<IRawJob> rawJob,
  2686. const TOperationOptions& options = TOperationOptions()) = 0;
  2687. ///
  2688. /// @brief Run Reduce operation.
  2689. ///
  2690. /// @param spec Operation spec.
  2691. /// @param reducer Instance of a job to run.
  2692. /// @param options Optional parameters.
  2693. ///
  2694. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  2695. IOperationPtr Reduce(
  2696. const TReduceOperationSpec& spec,
  2697. ::TIntrusivePtr<IReducerBase> reducer,
  2698. const TOperationOptions& options = TOperationOptions());
  2699. ///
  2700. /// @brief Run Reduce operation.
  2701. ///
  2702. /// @param reducer Instance of a job to run.
  2703. /// @param input Input table(s)
  2704. /// @param output Output table(s)
  2705. /// @param reduceBy Columns to group rows by.
  2706. /// @param spec Operation spec.
  2707. /// @param options Optional parameters.
  2708. ///
  2709. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  2710. IOperationPtr Reduce(
  2711. ::TIntrusivePtr<IReducerBase> reducer,
  2712. const TOneOrMany<TStructuredTablePath>& input,
  2713. const TOneOrMany<TStructuredTablePath>& output,
  2714. const TSortColumns& reduceBy,
  2715. const TReduceOperationSpec& spec = TReduceOperationSpec(),
  2716. const TOperationOptions& options = TOperationOptions());
  2717. ///
  2718. /// @brief Run raw Reduce operation.
  2719. ///
  2720. /// @param spec Operation spec.
  2721. /// @param rawJob Instance of a raw reducer to run.
  2722. /// @param options Optional parameters.
  2723. ///
  2724. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/reduce
  2725. virtual IOperationPtr RawReduce(
  2726. const TRawReduceOperationSpec& spec,
  2727. ::TIntrusivePtr<IRawJob> rawJob,
  2728. const TOperationOptions& options = TOperationOptions()) = 0;
  2729. ///
  2730. /// @brief Run JoinReduce operation.
  2731. ///
  2732. /// @param spec Operation spec.
  2733. /// @param reducer Instance of a job to run.
  2734. /// @param options Optional parameters.
  2735. ///
  2736. /// @deprecated Use @ref NYT::IOperationClient::Reduce with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false.
  2737. IOperationPtr JoinReduce(
  2738. const TJoinReduceOperationSpec& spec,
  2739. ::TIntrusivePtr<IReducerBase> reducer,
  2740. const TOperationOptions& options = TOperationOptions());
  2741. ///
  2742. /// @brief Run raw JoinReduce operation.
  2743. ///
  2744. /// @param spec Operation spec.
  2745. /// @param rawJob Instance of a raw reducer to run.
  2746. /// @param options Optional parameters.
  2747. ///
  2748. /// @deprecated Use @ref NYT::IOperationClient::RawReduce with @ref NYT::TReduceOperationSpec::EnableKeyGuarantee set to `false.
  2749. virtual IOperationPtr RawJoinReduce(
  2750. const TRawJoinReduceOperationSpec& spec,
  2751. ::TIntrusivePtr<IRawJob> rawJob,
  2752. const TOperationOptions& options = TOperationOptions()) = 0;
  2753. ///
  2754. /// @brief Run MapReduce operation.
  2755. ///
  2756. /// @param spec Operation spec.
  2757. /// @param mapper Instance of a map job to run (identity mapper if `nullptr`).
  2758. /// @param reducer Instance of a reduce job to run.
  2759. /// @param options Optional parameters.
  2760. ///
  2761. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2762. IOperationPtr MapReduce(
  2763. const TMapReduceOperationSpec& spec,
  2764. ::TIntrusivePtr<IMapperBase> mapper,
  2765. ::TIntrusivePtr<IReducerBase> reducer,
  2766. const TOperationOptions& options = TOperationOptions());
  2767. ///
  2768. /// @brief Run MapReduce operation.
  2769. ///
  2770. /// @param spec Operation spec.
  2771. /// @param mapper Instance of a map job to run (identity mapper if `nullptr`).
  2772. /// @param reducerCombiner Instance of a reduce combiner to run (identity reduce combiner if `nullptr`).
  2773. /// @param reducer Instance of a reduce job to run.
  2774. /// @param options Optional parameters.
  2775. ///
  2776. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2777. IOperationPtr MapReduce(
  2778. const TMapReduceOperationSpec& spec,
  2779. ::TIntrusivePtr<IMapperBase> mapper,
  2780. ::TIntrusivePtr<IReducerBase> reduceCombiner,
  2781. ::TIntrusivePtr<IReducerBase> reducer,
  2782. const TOperationOptions& options = TOperationOptions());
  2783. ///
  2784. /// @brief Run MapReduce operation.
  2785. ///
  2786. /// @param mapper Instance of mapper to run (identity mapper if `nullptr`).
  2787. /// @param reducer Instance of reducer to run.
  2788. /// @param input Input table(s)
  2789. /// @param output Output table(s)
  2790. /// @param reduceBy Columns to group rows by.
  2791. /// @param spec Operation spec.
  2792. /// @param options Optional parameters.
  2793. ///
  2794. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2795. IOperationPtr MapReduce(
  2796. ::TIntrusivePtr<IMapperBase> mapper,
  2797. ::TIntrusivePtr<IReducerBase> reducer,
  2798. const TOneOrMany<TStructuredTablePath>& input,
  2799. const TOneOrMany<TStructuredTablePath>& output,
  2800. const TSortColumns& reduceBy,
  2801. TMapReduceOperationSpec spec = TMapReduceOperationSpec(),
  2802. const TOperationOptions& options = TOperationOptions());
  2803. ///
  2804. /// @brief Run MapReduce operation.
  2805. ///
  2806. /// @param mapper Instance of mapper to run (identity mapper if `nullptr`).
  2807. /// @param reduceCombiner Instance of reduceCombiner to run (identity reduce combiner if `nullptr`).
  2808. /// @param reducer Instance of reducer to run.
  2809. /// @param input Input table(s)
  2810. /// @param output Output table(s)
  2811. /// @param reduceBy Columns to group rows by.
  2812. /// @param spec Operation spec.
  2813. /// @param options Optional parameters.
  2814. ///
  2815. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2816. IOperationPtr MapReduce(
  2817. ::TIntrusivePtr<IMapperBase> mapper,
  2818. ::TIntrusivePtr<IReducerBase> reduceCombiner,
  2819. ::TIntrusivePtr<IReducerBase> reducer,
  2820. const TOneOrMany<TStructuredTablePath>& input,
  2821. const TOneOrMany<TStructuredTablePath>& output,
  2822. const TSortColumns& reduceBy,
  2823. TMapReduceOperationSpec spec = TMapReduceOperationSpec(),
  2824. const TOperationOptions& options = TOperationOptions());
  2825. ///
  2826. /// @brief Run raw MapReduce operation.
  2827. ///
  2828. /// @param spec Operation spec.
  2829. /// @param mapper Instance of a raw mapper to run (identity mapper if `nullptr`).
  2830. /// @param mapper Instance of a raw reduce combiner to run (identity reduce combiner if `nullptr`).
  2831. /// @param mapper Instance of a raw reducer to run.
  2832. /// @param options Optional parameters.
  2833. ///
  2834. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/mapreduce
  2835. virtual IOperationPtr RawMapReduce(
  2836. const TRawMapReduceOperationSpec& spec,
  2837. ::TIntrusivePtr<IRawJob> mapper,
  2838. ::TIntrusivePtr<IRawJob> reduceCombiner,
  2839. ::TIntrusivePtr<IRawJob> reducer,
  2840. const TOperationOptions& options = TOperationOptions()) = 0;
  2841. ///
  2842. /// @brief Run Sort operation.
  2843. ///
  2844. /// @param spec Operation spec.
  2845. /// @param options Optional parameters.
  2846. ///
  2847. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/sort
  2848. virtual IOperationPtr Sort(
  2849. const TSortOperationSpec& spec,
  2850. const TOperationOptions& options = TOperationOptions()) = 0;
  2851. ///
  2852. /// @brief Run Sort operation.
  2853. ///
  2854. /// @param input Input table(s).
  2855. /// @param output Output table.
  2856. /// @param sortBy Columns to sort input rows by.
  2857. /// @param spec Operation spec.
  2858. /// @param options Optional parameters.
  2859. ///
  2860. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/sort
  2861. IOperationPtr Sort(
  2862. const TOneOrMany<TRichYPath>& input,
  2863. const TRichYPath& output,
  2864. const TSortColumns& sortBy,
  2865. const TSortOperationSpec& spec = TSortOperationSpec(),
  2866. const TOperationOptions& options = TOperationOptions());
  2867. ///
  2868. /// @brief Run Merge operation.
  2869. ///
  2870. /// @param spec Operation spec.
  2871. /// @param options Optional parameters.
  2872. ///
  2873. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/merge
  2874. virtual IOperationPtr Merge(
  2875. const TMergeOperationSpec& spec,
  2876. const TOperationOptions& options = TOperationOptions()) = 0;
  2877. ///
  2878. /// @brief Run Erase operation.
  2879. ///
  2880. /// @param spec Operation spec.
  2881. /// @param options Optional parameters.
  2882. ///
  2883. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/erase
  2884. virtual IOperationPtr Erase(
  2885. const TEraseOperationSpec& spec,
  2886. const TOperationOptions& options = TOperationOptions()) = 0;
  2887. ///
  2888. /// @brief Run RemoteCopy operation.
  2889. ///
  2890. /// @param spec Operation spec.
  2891. /// @param options Optional parameters.
  2892. ///
  2893. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/remote_copy
  2894. virtual IOperationPtr RemoteCopy(
  2895. const TRemoteCopyOperationSpec& spec,
  2896. const TOperationOptions& options = TOperationOptions()) = 0;
  2897. ///
  2898. /// @brief Run Vanilla operation.
  2899. ///
  2900. /// @param spec Operation spec.
  2901. /// @param options Optional parameters.
  2902. ///
  2903. /// @see https://ytsaurus.tech/docs/en/user-guide/data-processing/operations/vanilla
  2904. virtual IOperationPtr RunVanilla(
  2905. const TVanillaOperationSpec& spec,
  2906. const TOperationOptions& options = TOperationOptions()) = 0;
  2907. ///
  2908. /// @brief Abort operation.
  2909. ///
  2910. /// @see https://ytsaurus.tech/docs/en/api/commands#abort_operation
  2911. virtual void AbortOperation(
  2912. const TOperationId& operationId) = 0;
  2913. ///
  2914. /// @brief Complete operation.
  2915. ///
  2916. /// @see https://ytsaurus.tech/docs/en/api/commands#complete_operation
  2917. virtual void CompleteOperation(
  2918. const TOperationId& operationId) = 0;
  2919. ///
  2920. /// @brief Wait for operation to finish.
  2921. virtual void WaitForOperation(
  2922. const TOperationId& operationId) = 0;
  2923. ///
  2924. /// @brief Check and return operation status.
  2925. ///
  2926. /// @note this function will never return @ref NYT::EOperationBriefState::Failed or @ref NYT::EOperationBriefState::Aborted status,
  2927. /// it will throw @ref NYT::TOperationFailedError instead.
  2928. virtual EOperationBriefState CheckOperation(
  2929. const TOperationId& operationId) = 0;
  2930. ///
  2931. /// @brief Create an operation object given operation id.
  2932. ///
  2933. /// @throw @ref NYT::TErrorResponse if the operation doesn't exist.
  2934. virtual IOperationPtr AttachOperation(const TOperationId& operationId) = 0;
  2935. private:
  2936. virtual IOperationPtr DoMap(
  2937. const TMapOperationSpec& spec,
  2938. ::TIntrusivePtr<IStructuredJob> mapper,
  2939. const TOperationOptions& options) = 0;
  2940. virtual IOperationPtr DoReduce(
  2941. const TReduceOperationSpec& spec,
  2942. ::TIntrusivePtr<IStructuredJob> reducer,
  2943. const TOperationOptions& options) = 0;
  2944. virtual IOperationPtr DoJoinReduce(
  2945. const TJoinReduceOperationSpec& spec,
  2946. ::TIntrusivePtr<IStructuredJob> reducer,
  2947. const TOperationOptions& options) = 0;
  2948. virtual IOperationPtr DoMapReduce(
  2949. const TMapReduceOperationSpec& spec,
  2950. ::TIntrusivePtr<IStructuredJob> mapper,
  2951. ::TIntrusivePtr<IStructuredJob> reduceCombiner,
  2952. ::TIntrusivePtr<IStructuredJob> reducer,
  2953. const TOperationOptions& options) = 0;
  2954. };
  2955. ////////////////////////////////////////////////////////////////////////////////
  2956. } // namespace NYT
  2957. #define OPERATION_INL_H_
  2958. #include "operation-inl.h"
  2959. #undef OPERATION_INL_H_