aboutsummaryrefslogtreecommitdiff
path: root/tensorflow/tsl/platform/cloud/gcs_dns_cache.cc
blob: 92ad032f0ddc5eceb34360cc77339202851e149f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/tsl/platform/cloud/gcs_dns_cache.h"

#include <cstring>

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "tensorflow/tsl/platform/errors.h"
#include "tensorflow/tsl/platform/retrying_utils.h"
#include "tensorflow/tsl/platform/status.h"
#ifndef _WIN32
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#else
#include <Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <sys/types.h>

namespace tsl {

namespace {

const std::vector<string>& kCachedDomainNames =
    *new std::vector<string>{"www.googleapis.com", "storage.googleapis.com"};

inline void print_getaddrinfo_error(const string& name, Status return_status) {
  // Status doesn't map well to EAI type errors.
  LOG(ERROR) << "Error resolving " << name << ": " << return_status;
}

// Selects one item at random from a vector of items, using a uniform
// distribution.
template <typename T>
const T& SelectRandomItemUniform(std::default_random_engine* random,
                                 const std::vector<T>& items) {
  CHECK_GT(items.size(), 0);
  std::uniform_int_distribution<size_t> distribution(0u, items.size() - 1u);
  size_t choice_index = distribution(*random);
  return items[choice_index];
}
}  // namespace

GcsDnsCache::GcsDnsCache(Env* env, int64_t refresh_rate_secs)
    : env_(env), refresh_rate_secs_(refresh_rate_secs) {}

void GcsDnsCache::AnnotateRequest(HttpRequest* request) {
  // TODO(saeta): Denylist failing IP addresses.
  mutex_lock l(mu_);
  if (!started_) {
    VLOG(1) << "Starting GCS DNS cache.";
    DCHECK(!worker_) << "Worker thread already exists!";
    // Perform DNS resolutions to warm the cache.
    addresses_ = ResolveNames(kCachedDomainNames);

    // Note: we opt to use a thread instead of a delayed closure.
    worker_.reset(env_->StartThread({}, "gcs_dns_worker",
                                    [this]() { return WorkerThread(); }));
    started_ = true;
  }

  CHECK_EQ(kCachedDomainNames.size(), addresses_.size());
  for (size_t i = 0; i < kCachedDomainNames.size(); ++i) {
    const string& name = kCachedDomainNames[i];
    const std::vector<string>& addresses = addresses_[i];
    if (!addresses.empty()) {
      const string& chosen_address =
          SelectRandomItemUniform(&random_, addresses);
      request->AddResolveOverride(name, 443, chosen_address);
      VLOG(1) << "Annotated DNS mapping: " << name << " --> " << chosen_address;
    } else {
      LOG(WARNING) << "No IP addresses available for " << name;
    }
  }
}

/* static */ std::vector<string> GcsDnsCache::ResolveName(const string& name) {
  VLOG(1) << "Resolving DNS name: " << name;

  addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;  // Only use IPv4 for now.
  hints.ai_socktype = SOCK_STREAM;
  addrinfo* result = nullptr;
  RetryConfig retryConfig(
      /* init_delay_time_us = */ 5000,
      /* max_delay_time_us = */ 50 * 1000 * 5000,
      /* max_retries = */ 5);

  const Status getaddrinfo_status = RetryingUtils::CallWithRetries(
      [&name, &hints, &result]() {
        int return_code = getaddrinfo(name.c_str(), nullptr, &hints, &result);
        absl::Status return_status;
        switch (return_code) {
          case 0:
            return_status = OkStatus();
            break;
#ifndef _WIN32
          case EAI_ADDRFAMILY:
          case EAI_SERVICE:
          case EAI_SOCKTYPE:
          case EAI_NONAME:
            return_status = absl::FailedPreconditionError(
                absl::StrCat("System in invalid state for getaddrinfo call: ",
                             gai_strerror(return_code)));
            break;
          case EAI_AGAIN:
          case EAI_NODATA:  // lump nodata in here - the domains being resolved
                            // should always have data
            return_status = absl::UnavailableError(absl::StrCat(
                "Resolving ", name, " is temporarily unavailable"));
            break;
          case EAI_BADFLAGS:
          case EAI_FAMILY:
            return_status = absl::InvalidArgumentError(absl::StrCat(
                "Bad arguments for getaddrinfo: ", gai_strerror(return_code)));
            break;
          case EAI_FAIL:
            return_status = absl::NotFoundError(
                absl::StrCat("Permanent failure resolving ", name, ": ",
                             gai_strerror(return_code)));
            break;
          case EAI_MEMORY:
            return_status = absl::ResourceExhaustedError("Out of memory");
            break;
          case EAI_SYSTEM:
          default:
            return_status = absl::UnknownError(strerror(return_code));
#else
          // mapping from
          // https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo#return-value
          case WSATYPE_NOT_FOUND:
          case WSAESOCKTNOSUPPORT:
          case WSAHOST_NOT_FOUND:
            return_status = absl::FailedPreconditionError(
                absl::StrCat("System in invalid state for getaddrinfo call: ",
                             gai_strerror(return_code)));
            break;
          case WSATRY_AGAIN:
            return_status = absl::UnavailableError(absl::StrCat(
                "Resolving ", name, " is temporarily unavailable"));
            break;
          case WSAEINVAL:
          case WSAEAFNOSUPPORT:
            return_status = absl::InvalidArgumentError(absl::StrCat(
                "Bad arguments for getaddrinfo: ", gai_strerror(return_code)));
            break;
          case WSANO_RECOVERY:
            return_status = absl::NotFoundError(
                absl::StrCat("Permanent failure resolving ", name, ": ",
                             gai_strerror(return_code)));
            break;
          case WSA_NOT_ENOUGH_MEMORY:
            return_status = absl::ResourceExhaustedError("Out of memory");
            break;
          default:
            return_status = absl::UnknownError(strerror(return_code));
#endif
        }

        return Status(return_status);
      },
      retryConfig);

  std::vector<string> output;
  if (getaddrinfo_status.ok()) {
    for (const addrinfo* i = result; i != nullptr; i = i->ai_next) {
      if (i->ai_family != AF_INET || i->ai_addr->sa_family != AF_INET) {
        LOG(WARNING) << "Non-IPv4 address returned. ai_family: " << i->ai_family
                     << ". sa_family: " << i->ai_addr->sa_family << ".";
        continue;
      }
      char buf[INET_ADDRSTRLEN];
      void* address_ptr =
          &(reinterpret_cast<sockaddr_in*>(i->ai_addr)->sin_addr);
      const char* formatted = nullptr;
      if ((formatted = inet_ntop(i->ai_addr->sa_family, address_ptr, buf,
                                 INET_ADDRSTRLEN)) == nullptr) {
        LOG(ERROR) << "Error converting response to IP address for " << name
                   << ": " << strerror(errno);
      } else {
        output.emplace_back(buf);
        VLOG(1) << "... address: " << buf;
      }
    }
  } else {
    print_getaddrinfo_error(name, getaddrinfo_status);
  }
  if (result != nullptr) {
    freeaddrinfo(result);
  }
  return output;
}

// Performs DNS resolution for a set of DNS names. The return vector contains
// one element for each element in 'names', and each element is itself a
// vector of IP addresses (in textual form).
//
// If DNS resolution fails for any name, then that slot in the return vector
// will still be present, but will be an empty vector.
//
// Ensures: names.size() == return_value.size()

std::vector<std::vector<string>> GcsDnsCache::ResolveNames(
    const std::vector<string>& names) {
  std::vector<std::vector<string>> all_addresses;
  all_addresses.reserve(names.size());
  for (const string& name : names) {
    all_addresses.push_back(ResolveName(name));
  }
  return all_addresses;
}

void GcsDnsCache::WorkerThread() {
  while (true) {
    {
      // Don't immediately re-resolve the addresses.
      mutex_lock l(mu_);
      if (cancelled_) return;
      cond_var_.wait_for(l, std::chrono::seconds(refresh_rate_secs_));
      if (cancelled_) return;
    }

    // Resolve DNS values
    auto new_addresses = ResolveNames(kCachedDomainNames);

    {
      mutex_lock l(mu_);
      // Update instance variables.
      addresses_.swap(new_addresses);
    }
  }
}

}  // namespace tsl