config.cpp 9.0 KB

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