config.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "config.h"
  2. #include "serialize.h"
  3. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  4. #include <library/cpp/json/json_reader.h>
  5. #include <library/cpp/svnversion/svnversion.h>
  6. #include <library/cpp/yson/node/node_builder.h>
  7. #include <library/cpp/yson/node/node_io.h>
  8. #include <library/cpp/yson/json/yson2json_adapter.h>
  9. #include <util/folder/dirut.h>
  10. #include <util/folder/path.h>
  11. #include <util/generic/singleton.h>
  12. #include <util/stream/file.h>
  13. #include <util/string/builder.h>
  14. #include <util/string/cast.h>
  15. #include <util/string/strip.h>
  16. #include <util/string/type.h>
  17. #include <util/system/env.h>
  18. #include <util/system/execpath.h>
  19. #include <util/system/hostname.h>
  20. #include <util/system/user.h>
  21. namespace NYT {
  22. ////////////////////////////////////////////////////////////////////////////////
  23. bool TConfig::GetBool(const char* var, bool defaultValue)
  24. {
  25. TString val = GetEnv(var, "");
  26. if (val.empty()) {
  27. return defaultValue;
  28. }
  29. return IsTrue(val);
  30. }
  31. int TConfig::GetInt(const char* var, int defaultValue)
  32. {
  33. int result = 0;
  34. TString val = GetEnv(var, "");
  35. if (val.empty()) {
  36. return defaultValue;
  37. }
  38. try {
  39. result = FromString<int>(val);
  40. } catch (const yexception& e) {
  41. ythrow yexception() << "Cannot parse " << var << '=' << val << " as integer: " << e.what();
  42. }
  43. return result;
  44. }
  45. TDuration TConfig::GetDuration(const char* var, TDuration defaultValue)
  46. {
  47. return TDuration::Seconds(GetInt(var, defaultValue.Seconds()));
  48. }
  49. EEncoding TConfig::GetEncoding(const char* var)
  50. {
  51. const TString encodingName = GetEnv(var, "identity");
  52. EEncoding encoding;
  53. if (TryFromString(encodingName, encoding)) {
  54. return encoding;
  55. } else {
  56. ythrow yexception() << var << ": encoding '" << encodingName << "' is not supported";
  57. }
  58. }
  59. EUploadDeduplicationMode TConfig::GetUploadingDeduplicationMode(
  60. const char* var,
  61. EUploadDeduplicationMode defaultValue)
  62. {
  63. const TString deduplicationMode = GetEnv(var, TEnumTraits<EUploadDeduplicationMode>::ToString(defaultValue));
  64. return TEnumTraits<EUploadDeduplicationMode>::FromString(deduplicationMode);
  65. }
  66. void TConfig::ValidateToken(const TString& token)
  67. {
  68. for (size_t i = 0; i < token.size(); ++i) {
  69. ui8 ch = token[i];
  70. if (ch < 0x21 || ch > 0x7e) {
  71. ythrow yexception() << "Incorrect token character '" << ch << "' at position " << i;
  72. }
  73. }
  74. }
  75. TString TConfig::LoadTokenFromFile(const TString& tokenPath)
  76. {
  77. TFsPath path(tokenPath);
  78. return path.IsFile() ? Strip(TIFStream(path).ReadAll()) : TString();
  79. }
  80. TNode TConfig::LoadJsonSpec(const TString& strSpec)
  81. {
  82. TNode spec;
  83. TStringInput input(strSpec);
  84. TNodeBuilder builder(&spec);
  85. TYson2JsonCallbacksAdapter callbacks(&builder);
  86. Y_ENSURE(NJson::ReadJson(&input, &callbacks), "Cannot parse json spec: " << strSpec);
  87. Y_ENSURE(spec.IsMap(), "Json spec is not a map");
  88. return spec;
  89. }
  90. TRichYPath TConfig::LoadApiFilePathOptions(const TString& ysonMap)
  91. {
  92. TNode attributes;
  93. try {
  94. attributes = NodeFromYsonString(ysonMap);
  95. } catch (const yexception& exc) {
  96. ythrow yexception() << "Failed to parse YT_API_FILE_PATH_OPTIONS (it must be yson map): " << exc;
  97. }
  98. TNode pathNode = "";
  99. pathNode.Attributes() = attributes;
  100. TRichYPath path;
  101. Deserialize(path, pathNode);
  102. return path;
  103. }
  104. void TConfig::LoadToken()
  105. {
  106. if (auto envToken = GetEnv("YT_TOKEN")) {
  107. Token = envToken;
  108. } else if (auto envToken = GetEnv("YT_SECURE_VAULT_YT_TOKEN")) {
  109. // If this code runs inside an vanilla peration in YT
  110. // it should not use regular environment variable `YT_TOKEN`
  111. // because it would be visible in UI.
  112. // Token should be passed via `secure_vault` parameter in operation spec.
  113. Token = envToken;
  114. } else if (auto tokenPath = GetEnv("YT_TOKEN_PATH")) {
  115. Token = LoadTokenFromFile(tokenPath);
  116. } else {
  117. Token = LoadTokenFromFile(GetHomeDir() + "/.yt/token");
  118. }
  119. ValidateToken(Token);
  120. }
  121. void TConfig::LoadSpec()
  122. {
  123. TString strSpec = GetEnv("YT_SPEC", "{}");
  124. Spec = LoadJsonSpec(strSpec);
  125. strSpec = GetEnv("YT_TABLE_WRITER", "{}");
  126. TableWriter = LoadJsonSpec(strSpec);
  127. }
  128. void TConfig::LoadTimings()
  129. {
  130. ConnectTimeout = GetDuration("YT_CONNECT_TIMEOUT",
  131. TDuration::Seconds(10));
  132. SocketTimeout = GetDuration("YT_SOCKET_TIMEOUT",
  133. GetDuration("YT_SEND_RECEIVE_TIMEOUT", // common
  134. TDuration::Seconds(60)));
  135. AddressCacheExpirationTimeout = TDuration::Minutes(15);
  136. CacheLockTimeoutPerGb = TDuration::MilliSeconds(1000.0 * 1_GB * 8 / 20_MB); // 20 Mbps = 20 MBps / 8.
  137. TxTimeout = GetDuration("YT_TX_TIMEOUT",
  138. TDuration::Seconds(120));
  139. PingTimeout = GetDuration("YT_PING_TIMEOUT",
  140. TDuration::Seconds(5));
  141. PingInterval = GetDuration("YT_PING_INTERVAL",
  142. TDuration::Seconds(5));
  143. WaitLockPollInterval = TDuration::Seconds(5);
  144. RetryInterval = GetDuration("YT_RETRY_INTERVAL",
  145. TDuration::Seconds(3));
  146. ChunkErrorsRetryInterval = GetDuration("YT_CHUNK_ERRORS_RETRY_INTERVAL",
  147. TDuration::Seconds(60));
  148. RateLimitExceededRetryInterval = GetDuration("YT_RATE_LIMIT_EXCEEDED_RETRY_INTERVAL",
  149. TDuration::Seconds(60));
  150. StartOperationRetryInterval = GetDuration("YT_START_OPERATION_RETRY_INTERVAL",
  151. TDuration::Seconds(60));
  152. HostListUpdateInterval = TDuration::Seconds(60);
  153. }
  154. void TConfig::Reset()
  155. {
  156. Hosts = GetEnv("YT_HOSTS", "hosts");
  157. Pool = GetEnv("YT_POOL");
  158. Prefix = GetEnv("YT_PREFIX");
  159. ApiVersion = GetEnv("YT_VERSION", "v3");
  160. LogLevel = GetEnv("YT_LOG_LEVEL", "error");
  161. LogPath = GetEnv("YT_LOG_PATH");
  162. LogUseCore = GetBool("YT_LOG_USE_CORE", false);
  163. ContentEncoding = GetEncoding("YT_CONTENT_ENCODING");
  164. AcceptEncoding = GetEncoding("YT_ACCEPT_ENCODING");
  165. GlobalTxId = GetEnv("YT_TRANSACTION", "");
  166. AsyncHttpClientThreads = 1;
  167. AsyncTxPingerPoolThreads = 1;
  168. ForceIpV4 = GetBool("YT_FORCE_IPV4");
  169. ForceIpV6 = GetBool("YT_FORCE_IPV6");
  170. UseHosts = GetBool("YT_USE_HOSTS", true);
  171. LoadToken();
  172. LoadSpec();
  173. LoadTimings();
  174. CacheUploadDeduplicationMode = GetUploadingDeduplicationMode("YT_UPLOAD_DEDUPLICATION", EUploadDeduplicationMode::Host);
  175. CacheUploadDeduplicationThreshold = 10_MB;
  176. RetryCount = Max(GetInt("YT_RETRY_COUNT", 10), 1);
  177. ReadRetryCount = Max(GetInt("YT_READ_RETRY_COUNT", 30), 1);
  178. StartOperationRetryCount = Max(GetInt("YT_START_OPERATION_RETRY_COUNT", 30), 1);
  179. RemoteTempFilesDirectory = GetEnv("YT_FILE_STORAGE",
  180. "//tmp/yt_wrapper/file_storage");
  181. RemoteTempTablesDirectory = GetEnv("YT_TEMP_TABLES_STORAGE",
  182. "//tmp/yt_wrapper/table_storage");
  183. RemoteTempTablesDirectory = GetEnv("YT_TEMP_DIR",
  184. RemoteTempTablesDirectory);
  185. KeepTempTables = GetBool("YT_KEEP_TEMP_TABLES");
  186. InferTableSchema = false;
  187. UseClientProtobuf = GetBool("YT_USE_CLIENT_PROTOBUF", false);
  188. NodeReaderFormat = ENodeReaderFormat::Auto;
  189. ProtobufFormatWithDescriptors = true;
  190. MountSandboxInTmpfs = GetBool("YT_MOUNT_SANDBOX_IN_TMPFS");
  191. ApiFilePathOptions = LoadApiFilePathOptions(GetEnv("YT_API_FILE_PATH_OPTIONS", "{}"));
  192. ConnectionPoolSize = GetInt("YT_CONNECTION_POOL_SIZE", 16);
  193. TraceHttpRequestsMode = FromString<ETraceHttpRequestsMode>(to_lower(GetEnv("YT_TRACE_HTTP_REQUESTS", "never")));
  194. CommandsWithFraming = {
  195. "read_table",
  196. "get_table_columnar_statistics",
  197. "get_job_input",
  198. "concatenate",
  199. "partition_tables",
  200. };
  201. }
  202. TConfig::TConfig()
  203. {
  204. Reset();
  205. }
  206. TConfigPtr TConfig::Get()
  207. {
  208. struct TConfigHolder
  209. {
  210. TConfigHolder()
  211. : Config(::MakeIntrusive<TConfig>())
  212. { }
  213. TConfigPtr Config;
  214. };
  215. return Singleton<TConfigHolder>()->Config;
  216. }
  217. ////////////////////////////////////////////////////////////////////////////////
  218. TProcessState::TProcessState()
  219. {
  220. try {
  221. FqdnHostName = ::FQDNHostName();
  222. } catch (const yexception& e) {
  223. try {
  224. FqdnHostName = ::HostName();
  225. } catch (const yexception& e) {
  226. ythrow yexception() << "Cannot get fqdn and host name: " << e.what();
  227. }
  228. }
  229. try {
  230. UserName = ::GetUsername();
  231. } catch (const yexception& e) {
  232. #ifdef _win_
  233. ythrow yexception() << "Cannot get user name: " << e.what();
  234. #else
  235. UserName = "u" + ToString(geteuid());
  236. #endif
  237. }
  238. Pid = static_cast<int>(getpid());
  239. ClientVersion = ::TStringBuilder() << "YT C++ native " << GetProgramCommitId();
  240. BinaryPath = GetExecPath();
  241. BinaryName = GetBaseName(BinaryPath);
  242. }
  243. TProcessState* TProcessState::Get()
  244. {
  245. return Singleton<TProcessState>();
  246. }
  247. ////////////////////////////////////////////////////////////////////////////////
  248. } // namespace NYT