You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by gr...@apache.org on 2021/02/10 03:50:22 UTC

[kudu] 03/03: [test] Fix InstanceDetectorTest.Timeout when run on AWS

This is an automated email from the ASF dual-hosted git repository.

granthenke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 37bc427a60f9cb3ab687d3610b50199f12875a08
Author: Grant Henke <gr...@apache.org>
AuthorDate: Tue Feb 9 17:00:03 2021 -0600

    [test] Fix InstanceDetectorTest.Timeout when run on AWS
    
    When running InstanceDetectorTest.Timeout on AWS the test
    frequently fails because even though a timeout of 1 ms is set,
    the curl request returns in less time than that.
    
    In order to reliably force a timeout regardless of where the test
    is run this patch configures the test to use bad DNS servers
    preventing a timely response.
    
    Example Failure:
    [ RUN      ] InstanceDetectorTest.Timeout
    W0208 03:46:56.690357  4190 instance_detector.cc:116] could not retrieve GCE instance metadata: Network error: curl error: Couldn't resolve host name: Could not resolve host: metadata.google.internal
    W0208 03:46:56.690555  4191 instance_detector.cc:116] could not retrieve OpenStack instance metadata: Network error: curl error: HTTP response code said error: The requested URL returned error: 404 Not Found
    W0208 03:46:56.690815  4189 instance_detector.cc:116] could not retrieve Azure instance metadata: Network error: curl error: HTTP response code said error: The requested URL returned error: 404 Not Found
    /data0/somelongdirectorytoavoidrpathissues/src/kudu/src/kudu/util/cloud/instance_detector-test.cc:110: Failure
    Value of: s.IsNotFound()
      Actual: false
    Expected: true
    OK
    
    Change-Id: I0880c2d8f94ae6208adfdd0e21eea91727469979
    Reviewed-on: http://gerrit.cloudera.org:8080/17052
    Tested-by: Kudu Jenkins
    Reviewed-by: Alexey Serbin <as...@cloudera.com>
---
 src/kudu/util/cloud/instance_detector-test.cc |  5 +++++
 src/kudu/util/cloud/instance_metadata.cc      | 10 ++++++++++
 src/kudu/util/curl_util.cc                    |  5 +++++
 src/kudu/util/curl_util.h                     |  9 +++++++++
 4 files changed, 29 insertions(+)

diff --git a/src/kudu/util/cloud/instance_detector-test.cc b/src/kudu/util/cloud/instance_detector-test.cc
index a846e62..bc20448 100644
--- a/src/kudu/util/cloud/instance_detector-test.cc
+++ b/src/kudu/util/cloud/instance_detector-test.cc
@@ -29,6 +29,7 @@
 #include "kudu/util/cloud/instance_metadata.h"
 #include "kudu/util/status.h"
 
+DECLARE_string(cloud_curl_dns_servers_for_testing);
 DECLARE_uint32(cloud_metadata_server_request_timeout_ms);
 
 using std::string;
@@ -104,6 +105,10 @@ TEST(InstanceDetectorTest, Basic) {
 TEST(InstanceDetectorTest, Timeout) {
   // Set very short interval for the timeout.
   FLAGS_cloud_metadata_server_request_timeout_ms = 1;
+  // Configure a bad DNS server to ensure a timeout,
+  // even when run on fast cloud instances.
+  FLAGS_cloud_curl_dns_servers_for_testing = "192.0.2.0";
+
   InstanceDetector detector;
   unique_ptr<InstanceMetadata> metadata;
   const auto s = detector.Detect(&metadata);
diff --git a/src/kudu/util/cloud/instance_metadata.cc b/src/kudu/util/cloud/instance_metadata.cc
index f8d640b..e71533d 100644
--- a/src/kudu/util/cloud/instance_metadata.cc
+++ b/src/kudu/util/cloud/instance_metadata.cc
@@ -103,6 +103,11 @@ DEFINE_string(cloud_openstack_metadata_url,
 TAG_FLAG(cloud_openstack_metadata_url, advanced);
 TAG_FLAG(cloud_openstack_metadata_url, runtime);
 
+DEFINE_string(cloud_curl_dns_servers_for_testing, "",
+          "Set the list of DNS servers to be used instead of the system default.");
+TAG_FLAG(cloud_curl_dns_servers_for_testing, hidden);
+TAG_FLAG(cloud_curl_dns_servers_for_testing, runtime);
+
 DEFINE_validator(cloud_metadata_server_request_timeout_ms,
                  [](const char* name, const uint32_t val) {
   if (val == 0) {
@@ -169,6 +174,11 @@ Status InstanceMetadata::Fetch(const string& url,
   EasyCurl curl;
   curl.set_timeout(timeout);
   curl.set_fail_on_http_error(true);
+
+  if (PREDICT_FALSE(!FLAGS_cloud_curl_dns_servers_for_testing.empty())) {
+    curl.set_dns_servers(FLAGS_cloud_curl_dns_servers_for_testing);
+  }
+
   faststring resp;
   RETURN_NOT_OK(curl.FetchURL(url, &resp, headers));
   if (out) {
diff --git a/src/kudu/util/curl_util.cc b/src/kudu/util/curl_util.cc
index 8e8425e..cd73107 100644
--- a/src/kudu/util/curl_util.cc
+++ b/src/kudu/util/curl_util.cc
@@ -195,6 +195,11 @@ Status EasyCurl::DoRequest(const string& url,
     CURL_RETURN_NOT_OK(curl_easy_setopt(
         curl_, CURLOPT_TIMEOUT_MS, timeout_.ToMilliseconds()));
   }
+
+  if (!dns_servers_.empty()) {
+    CURL_RETURN_NOT_OK(curl_easy_setopt(curl_, CURLOPT_DNS_SERVERS, dns_servers_.c_str()));
+  }
+
   CURL_RETURN_NOT_OK(curl_easy_perform(curl_));
   long val; // NOLINT(*) curl wants a long
   CURL_RETURN_NOT_OK(curl_easy_getinfo(curl_, CURLINFO_NUM_CONNECTS, &val));
diff --git a/src/kudu/util/curl_util.h b/src/kudu/util/curl_util.h
index ed01e17..3781688 100644
--- a/src/kudu/util/curl_util.h
+++ b/src/kudu/util/curl_util.h
@@ -79,6 +79,13 @@ class EasyCurl {
     timeout_ = t;
   }
 
+  // Set the list of DNS servers to be used instead of the system default.
+  // The format of the dns servers option is:
+  //   host[:port][,host[:port]]...
+  void set_dns_servers(std::string dns_servers) {
+    dns_servers_ = std::move(dns_servers);
+  }
+
   Status set_auth(CurlAuthType auth_type, std::string username = "", std::string password = "") {
     auth_type_ = std::move(auth_type);
     username_ = std::move(username);
@@ -153,6 +160,8 @@ class EasyCurl {
 
   MonoDelta timeout_;
 
+  std::string dns_servers_;
+
   int num_connects_ = 0;
 
   char errbuf_[kErrBufSize];