init.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "init.h"
  2. #include "abortable_registry.h"
  3. #include "job_profiler.h"
  4. #include <yt/cpp/mapreduce/http/requests.h>
  5. #include <yt/cpp/mapreduce/interface/config.h>
  6. #include <yt/cpp/mapreduce/interface/init.h>
  7. #include <yt/cpp/mapreduce/interface/operation.h>
  8. #include <yt/cpp/mapreduce/interface/logging/logger.h>
  9. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  10. #include <yt/cpp/mapreduce/io/job_reader.h>
  11. #include <yt/cpp/mapreduce/common/helpers.h>
  12. #include <yt/cpp/mapreduce/common/wait_proxy.h>
  13. #include <library/cpp/sighandler/async_signals_handler.h>
  14. #include <util/folder/dirut.h>
  15. #include <util/generic/singleton.h>
  16. #include <util/string/builder.h>
  17. #include <util/string/cast.h>
  18. #include <util/string/type.h>
  19. #include <util/system/env.h>
  20. #include <util/system/thread.h>
  21. namespace NYT {
  22. ////////////////////////////////////////////////////////////////////////////////
  23. namespace {
  24. void WriteVersionToLog()
  25. {
  26. YT_LOG_INFO("Wrapper version: %v",
  27. TProcessState::Get()->ClientVersion);
  28. }
  29. static TNode SecureVaultContents; // safe
  30. void InitializeSecureVault()
  31. {
  32. SecureVaultContents = NodeFromYsonString(
  33. GetEnv("YT_SECURE_VAULT", "{}"));
  34. }
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////
  37. const TNode& GetJobSecureVault()
  38. {
  39. return SecureVaultContents;
  40. }
  41. ////////////////////////////////////////////////////////////////////////////////
  42. class TAbnormalTerminator
  43. {
  44. public:
  45. TAbnormalTerminator() = default;
  46. static void SetErrorTerminationHandler()
  47. {
  48. if (Instance().OldHandler_ != nullptr) {
  49. return;
  50. }
  51. Instance().OldHandler_ = std::set_terminate(&TerminateHandler);
  52. SetAsyncSignalFunction(SIGINT, SignalHandler);
  53. SetAsyncSignalFunction(SIGTERM, SignalHandler);
  54. }
  55. private:
  56. static TAbnormalTerminator& Instance()
  57. {
  58. return *Singleton<TAbnormalTerminator>();
  59. }
  60. static void* Invoke(void* opaque)
  61. {
  62. (*reinterpret_cast<std::function<void()>*>(opaque))();
  63. return nullptr;
  64. }
  65. static void TerminateWithTimeout(
  66. const TDuration& timeout,
  67. const std::function<void(void)>& exitFunction,
  68. const TString& logMessage)
  69. {
  70. std::function<void()> threadFun = [=] {
  71. YT_LOG_INFO("%v",
  72. logMessage);
  73. NDetail::TAbortableRegistry::Get()->AbortAllAndBlockForever();
  74. };
  75. TThread thread(TThread::TParams(Invoke, &threadFun).SetName("aborter"));
  76. thread.Start();
  77. thread.Detach();
  78. Sleep(timeout);
  79. exitFunction();
  80. }
  81. static void SignalHandler(int signalNumber)
  82. {
  83. TerminateWithTimeout(
  84. TDuration::Seconds(5),
  85. std::bind(_exit, -signalNumber),
  86. ::TStringBuilder() << "Signal " << signalNumber << " received, aborting transactions. Waiting 5 seconds...");
  87. }
  88. static void TerminateHandler()
  89. {
  90. TerminateWithTimeout(
  91. TDuration::Seconds(5),
  92. [&] {
  93. if (Instance().OldHandler_) {
  94. Instance().OldHandler_();
  95. } else {
  96. abort();
  97. }
  98. },
  99. ::TStringBuilder() << "Terminate called, aborting transactions. Waiting 5 seconds...");
  100. }
  101. private:
  102. std::terminate_handler OldHandler_ = nullptr;
  103. };
  104. ////////////////////////////////////////////////////////////////////////////////
  105. namespace NDetail {
  106. EInitStatus& GetInitStatus()
  107. {
  108. static EInitStatus initStatus = EInitStatus::NotInitialized;
  109. return initStatus;
  110. }
  111. static void ElevateInitStatus(const EInitStatus newStatus) {
  112. NDetail::GetInitStatus() = Max(NDetail::GetInitStatus(), newStatus);
  113. }
  114. void CommonInitialize(int argc, const char** argv)
  115. {
  116. auto logLevelStr = to_lower(TConfig::Get()->LogLevel);
  117. ILogger::ELevel logLevel;
  118. if (!TryFromString(logLevelStr, logLevel)) {
  119. Cerr << "Invalid log level: " << TConfig::Get()->LogLevel << Endl;
  120. exit(1);
  121. }
  122. SetLogger(CreateStdErrLogger(logLevel));
  123. TProcessState::Get()->SetCommandLine(argc, argv);
  124. }
  125. void NonJobInitialize(const TInitializeOptions& options)
  126. {
  127. if (FromString<bool>(GetEnv("YT_CLEANUP_ON_TERMINATION", "0")) || options.CleanupOnTermination_) {
  128. TAbnormalTerminator::SetErrorTerminationHandler();
  129. }
  130. if (options.WaitProxy_) {
  131. NDetail::TWaitProxy::Get()->SetProxy(options.WaitProxy_);
  132. }
  133. WriteVersionToLog();
  134. }
  135. void ExecJob(int argc, const char** argv, const TInitializeOptions& options)
  136. {
  137. // Now we are definitely in job.
  138. // We take this setting from environment variable to be consistent with client code.
  139. TConfig::Get()->UseClientProtobuf = IsTrue(GetEnv("YT_USE_CLIENT_PROTOBUF", ""));
  140. auto execJobImpl = [&options](TString jobName, i64 outputTableCount, bool hasState) {
  141. auto jobProfiler = CreateJobProfiler();
  142. jobProfiler->Start();
  143. InitializeSecureVault();
  144. NDetail::OutputTableCount = static_cast<i64>(outputTableCount);
  145. THolder<IInputStream> jobStateStream;
  146. if (hasState) {
  147. jobStateStream = MakeHolder<TIFStream>("jobstate");
  148. } else {
  149. jobStateStream = MakeHolder<TBufferStream>(0);
  150. }
  151. int ret = 1;
  152. try {
  153. ret = TJobFactory::Get()->GetJobFunction(jobName.data())(outputTableCount, *jobStateStream);
  154. } catch (const TSystemError& ex) {
  155. if (ex.Status() == EPIPE) {
  156. // 32 == EPIPE, write number here so it's easier to grep this exit code in source files
  157. exit(32);
  158. }
  159. throw;
  160. }
  161. jobProfiler->Stop();
  162. if (options.JobOnExitFunction_) {
  163. (*options.JobOnExitFunction_)();
  164. }
  165. exit(ret);
  166. };
  167. auto jobArguments = NodeFromYsonString(GetEnv("YT_JOB_ARGUMENTS", "#"));
  168. if (jobArguments.HasValue()) {
  169. execJobImpl(
  170. jobArguments["job_name"].AsString(),
  171. jobArguments["output_table_count"].AsInt64(),
  172. jobArguments["has_state"].AsBool());
  173. Y_UNREACHABLE();
  174. }
  175. TString jobType = argc >= 2 ? argv[1] : TString();
  176. if (argc != 5 || jobType != "--yt-map" && jobType != "--yt-reduce") {
  177. // We are inside job but probably using old API
  178. // (i.e. both NYT::Initialize and NMR::Initialize are called).
  179. WriteVersionToLog();
  180. return;
  181. }
  182. TString jobName(argv[2]);
  183. i64 outputTableCount = FromString<i64>(argv[3]);
  184. int hasState = FromString<int>(argv[4]);
  185. execJobImpl(jobName, outputTableCount, hasState);
  186. Y_UNREACHABLE();
  187. }
  188. } // namespace NDetail
  189. ////////////////////////////////////////////////////////////////////////////////
  190. void JoblessInitialize(const TInitializeOptions& options)
  191. {
  192. static const char* fakeArgv[] = {"unknown..."};
  193. NDetail::CommonInitialize(1, fakeArgv);
  194. NDetail::NonJobInitialize(options);
  195. NDetail::ElevateInitStatus(NDetail::EInitStatus::JoblessInitialization);
  196. }
  197. void Initialize(int argc, const char* argv[], const TInitializeOptions& options)
  198. {
  199. NDetail::CommonInitialize(argc, argv);
  200. NDetail::ElevateInitStatus(NDetail::EInitStatus::FullInitialization);
  201. const bool isInsideJob = !GetEnv("YT_JOB_ID").empty();
  202. if (isInsideJob) {
  203. NDetail::ExecJob(argc, argv, options);
  204. } else {
  205. NDetail::NonJobInitialize(options);
  206. }
  207. }
  208. void Initialize(int argc, char* argv[], const TInitializeOptions& options)
  209. {
  210. return Initialize(argc, const_cast<const char**>(argv), options);
  211. }
  212. void Initialize(const TInitializeOptions& options)
  213. {
  214. static const char* fakeArgv[] = {"unknown..."};
  215. Initialize(1, fakeArgv, options);
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////
  218. } // namespace NYT