init.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. auto logPath = TConfig::Get()->LogPath;
  123. auto logger = logPath.Empty() ? CreateStdErrLogger(logLevel) : CreateFileLogger(logLevel, logPath);
  124. SetLogger(logger);
  125. TProcessState::Get()->SetCommandLine(argc, argv);
  126. }
  127. void NonJobInitialize(const TInitializeOptions& options)
  128. {
  129. if (FromString<bool>(GetEnv("YT_CLEANUP_ON_TERMINATION", "0")) || options.CleanupOnTermination_) {
  130. TAbnormalTerminator::SetErrorTerminationHandler();
  131. }
  132. if (options.WaitProxy_) {
  133. NDetail::TWaitProxy::Get()->SetProxy(options.WaitProxy_);
  134. }
  135. WriteVersionToLog();
  136. }
  137. void ExecJob(int argc, const char** argv, const TInitializeOptions& options)
  138. {
  139. // Now we are definitely in job.
  140. // We take this setting from environment variable to be consistent with client code.
  141. TConfig::Get()->UseClientProtobuf = IsTrue(GetEnv("YT_USE_CLIENT_PROTOBUF", ""));
  142. auto execJobImpl = [&options](TString jobName, i64 outputTableCount, bool hasState) {
  143. auto jobProfiler = CreateJobProfiler();
  144. jobProfiler->Start();
  145. InitializeSecureVault();
  146. NDetail::OutputTableCount = static_cast<i64>(outputTableCount);
  147. THolder<IInputStream> jobStateStream;
  148. if (hasState) {
  149. jobStateStream = MakeHolder<TIFStream>("jobstate");
  150. } else {
  151. jobStateStream = MakeHolder<TBufferStream>(0);
  152. }
  153. int ret = 1;
  154. try {
  155. ret = TJobFactory::Get()->GetJobFunction(jobName.data())(outputTableCount, *jobStateStream);
  156. } catch (const TSystemError& ex) {
  157. if (ex.Status() == EPIPE) {
  158. // 32 == EPIPE, write number here so it's easier to grep this exit code in source files
  159. exit(32);
  160. }
  161. throw;
  162. }
  163. jobProfiler->Stop();
  164. if (options.JobOnExitFunction_) {
  165. (*options.JobOnExitFunction_)();
  166. }
  167. exit(ret);
  168. };
  169. auto jobArguments = NodeFromYsonString(GetEnv("YT_JOB_ARGUMENTS", "#"));
  170. if (jobArguments.HasValue()) {
  171. execJobImpl(
  172. jobArguments["job_name"].AsString(),
  173. jobArguments["output_table_count"].AsInt64(),
  174. jobArguments["has_state"].AsBool());
  175. Y_UNREACHABLE();
  176. }
  177. TString jobType = argc >= 2 ? argv[1] : TString();
  178. if (argc != 5 || jobType != "--yt-map" && jobType != "--yt-reduce") {
  179. // We are inside job but probably using old API
  180. // (i.e. both NYT::Initialize and NMR::Initialize are called).
  181. WriteVersionToLog();
  182. return;
  183. }
  184. TString jobName(argv[2]);
  185. i64 outputTableCount = FromString<i64>(argv[3]);
  186. int hasState = FromString<int>(argv[4]);
  187. execJobImpl(jobName, outputTableCount, hasState);
  188. Y_UNREACHABLE();
  189. }
  190. } // namespace NDetail
  191. ////////////////////////////////////////////////////////////////////////////////
  192. void JoblessInitialize(const TInitializeOptions& options)
  193. {
  194. static const char* fakeArgv[] = {"unknown..."};
  195. NDetail::CommonInitialize(1, fakeArgv);
  196. NDetail::NonJobInitialize(options);
  197. NDetail::ElevateInitStatus(NDetail::EInitStatus::JoblessInitialization);
  198. }
  199. void Initialize(int argc, const char* argv[], const TInitializeOptions& options)
  200. {
  201. NDetail::CommonInitialize(argc, argv);
  202. NDetail::ElevateInitStatus(NDetail::EInitStatus::FullInitialization);
  203. const bool isInsideJob = !GetEnv("YT_JOB_ID").empty();
  204. if (isInsideJob) {
  205. NDetail::ExecJob(argc, argv, options);
  206. } else {
  207. NDetail::NonJobInitialize(options);
  208. }
  209. }
  210. void Initialize(int argc, char* argv[], const TInitializeOptions& options)
  211. {
  212. return Initialize(argc, const_cast<const char**>(argv), options);
  213. }
  214. void Initialize(const TInitializeOptions& options)
  215. {
  216. static const char* fakeArgv[] = {"unknown..."};
  217. Initialize(1, fakeArgv, options);
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. } // namespace NYT