From e231d063fb922c13552ee1e3990313f2fce18d16 Mon Sep 17 00:00:00 2001 From: Jianfei Hu Date: Tue, 31 Oct 2023 05:06:08 +0000 Subject: [PATCH] fix the exception handlig. Signed-off-by: Jianfei Hu --- src/IO/S3/Credentials.cpp | 32 ++++++++++++++++++-------------- src/IO/S3/Credentials.h | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/IO/S3/Credentials.cpp b/src/IO/S3/Credentials.cpp index a5ec210f284..7d1e118bcfc 100644 --- a/src/IO/S3/Credentials.cpp +++ b/src/IO/S3/Credentials.cpp @@ -27,7 +27,6 @@ # include # include -/// TODO: these changes do not need to be under AWS_S3. consider to remove or make it separate file libraries. #include #include @@ -236,7 +235,7 @@ std::shared_ptr InitEC2MetadataClient(const Aws::Client::C return std::make_shared(client_configuration, endpoint.c_str()); } -std::variant AWSEC2MetadataClient::getAvailabilityZoneOrException() +String AWSEC2MetadataClient::getAvailabilityZoneOrException() { Poco::URI uri(awsMetadataEndpoint() + EC2_AVAILABILITY_ZONE_RESOURCE); Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); @@ -255,7 +254,7 @@ std::variant AWSEC2MetadataClient::getAvailabilityZo return response_data; } -std::variant getGCPAvailabilityZoneOrException() +String getGCPAvailabilityZoneOrException() { Poco::URI uri("http://169.254.169.254/computeMetadata/v1/instance/zone"); Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort()); @@ -283,17 +282,22 @@ String getRunningAvailabilityZone() { LOG_INFO(&Poco::Logger::get("Application"), "Trying to detect the availability zone."); - auto aws_az_or_exception = AWSEC2MetadataClient::getAvailabilityZoneOrException(); - if (const auto * aws_az = std::get_if(&aws_az_or_exception)) - return *aws_az; - - auto gcp_zone = getGCPAvailabilityZoneOrException(); - if (const auto * gcp_az = std::get_if(&gcp_zone)) - return *gcp_az; - - /// TODO(incfly): Add Azure support. - - throw DB::Exception(ErrorCodes::UNSUPPORTED_METHOD, "Failed to find the availability zone, tried both AWS and GCP"); + try{ + auto aws_az = AWSEC2MetadataClient::getAvailabilityZoneOrException(); + return aws_az; + } + catch (const DB::Exception & aws_ex) + { + try{ + auto gcp_zone = getGCPAvailabilityZoneOrException(); + return gcp_zone; + } + catch(const DB::Exception & gcp_ex) + { + throw DB::Exception(ErrorCodes::UNSUPPORTED_METHOD, + "Failed to find the availability zone, tried AWS, GCP. AWS Error {}\n GCP Error {}", aws_ex.displayText(), gcp_ex.displayText()); + } + } } AWSEC2InstanceProfileConfigLoader::AWSEC2InstanceProfileConfigLoader(const std::shared_ptr & client_, bool use_secure_pull_) diff --git a/src/IO/S3/Credentials.h b/src/IO/S3/Credentials.h index 6de34c836f5..9576718ea72 100644 --- a/src/IO/S3/Credentials.h +++ b/src/IO/S3/Credentials.h @@ -58,7 +58,7 @@ public: private: std::pair getEC2MetadataToken(const std::string & user_agent_string) const; - static std::variant getAvailabilityZoneOrException(); + static String getAvailabilityZoneOrException(); const Aws::String endpoint; mutable std::recursive_mutex token_mutex;