operation_helpers.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "operation_helpers.h"
  2. #include <yt/cpp/mapreduce/common/retry_lib.h>
  3. #include <yt/cpp/mapreduce/interface/config.h>
  4. #include <yt/cpp/mapreduce/interface/logging/yt_log.h>
  5. #include <yt/cpp/mapreduce/raw_client/raw_requests.h>
  6. #include <yt/cpp/mapreduce/http/context.h>
  7. #include <yt/cpp/mapreduce/http/requests.h>
  8. #include <util/string/builder.h>
  9. #include <util/system/mutex.h>
  10. #include <util/system/rwlock.h>
  11. namespace NYT::NDetail {
  12. ////////////////////////////////////////////////////////////////////////////////
  13. ui64 RoundUpFileSize(ui64 size)
  14. {
  15. constexpr ui64 roundUpTo = 4ull << 10;
  16. return (size + roundUpTo - 1) & ~(roundUpTo - 1);
  17. }
  18. bool UseLocalModeOptimization(const TClientContext& context, const IClientRetryPolicyPtr& clientRetryPolicy)
  19. {
  20. if (!context.Config->EnableLocalModeOptimization) {
  21. return false;
  22. }
  23. static THashMap<TString, bool> localModeMap;
  24. static TRWMutex mutex;
  25. {
  26. TReadGuard guard(mutex);
  27. auto it = localModeMap.find(context.ServerName);
  28. if (it != localModeMap.end()) {
  29. return it->second;
  30. }
  31. }
  32. bool isLocalMode = false;
  33. TString localModeAttr("//sys/@local_mode_fqdn");
  34. // We don't want to pollute logs with errors about failed request,
  35. // so we check if path exists before getting it.
  36. if (NRawClient::Exists(clientRetryPolicy->CreatePolicyForGenericRequest(),
  37. context,
  38. TTransactionId(),
  39. localModeAttr,
  40. TExistsOptions().ReadFrom(EMasterReadKind::Cache)))
  41. {
  42. auto fqdnNode = NRawClient::TryGet(
  43. clientRetryPolicy->CreatePolicyForGenericRequest(),
  44. context,
  45. TTransactionId(),
  46. localModeAttr,
  47. TGetOptions().ReadFrom(EMasterReadKind::Cache));
  48. if (!fqdnNode.IsUndefined()) {
  49. auto fqdn = fqdnNode.AsString();
  50. isLocalMode = (fqdn == TProcessState::Get()->FqdnHostName);
  51. YT_LOG_DEBUG("Checking local mode; LocalModeFqdn: %v FqdnHostName: %v IsLocalMode: %v",
  52. fqdn,
  53. TProcessState::Get()->FqdnHostName,
  54. isLocalMode ? "true" : "false");
  55. }
  56. }
  57. {
  58. TWriteGuard guard(mutex);
  59. localModeMap[context.ServerName] = isLocalMode;
  60. }
  61. return isLocalMode;
  62. }
  63. TString GetOperationWebInterfaceUrl(TStringBuf serverName, TOperationId operationId)
  64. {
  65. serverName.ChopSuffix(":80");
  66. serverName.ChopSuffix(".yt.yandex-team.ru");
  67. serverName.ChopSuffix(".yt.yandex.net");
  68. return ::TStringBuilder() << "https://yt.yandex-team.ru/" << serverName <<
  69. "/operations/" << GetGuidAsString(operationId);
  70. }
  71. ////////////////////////////////////////////////////////////////////////////////
  72. } // namespace NYT::NDetail