diff --git a/include/cos_config.h b/include/cos_config.h index a958ab5..8eaa93b 100644 --- a/include/cos_config.h +++ b/include/cos_config.h @@ -11,6 +11,11 @@ #include "util/log_util.h" namespace qcloud_cos { + +#define COS_DEFAULT_MAX_RETRY_TIMES 3 + +#define COS_DEFAULT_RETRY_INTERVAL_MS 100 + class CosConfig { public: /// \brief CosConfig构造函数 @@ -31,7 +36,9 @@ class CosConfig { m_dest_domain(""), m_is_domain_same_to_host(false), m_is_domain_same_to_host_enable(false), - m_config_parsed(false) {} + m_config_parsed(false), + m_max_retry_times(COS_DEFAULT_MAX_RETRY_TIMES), + m_retry_interval_ms(COS_DEFAULT_RETRY_INTERVAL_MS) {} /// \brief CosConfig构造函数 /// @@ -52,7 +59,9 @@ class CosConfig { m_dest_domain(""), m_is_domain_same_to_host(false), m_is_domain_same_to_host_enable(false), - m_config_parsed(false) {} + m_config_parsed(false), + m_max_retry_times(COS_DEFAULT_MAX_RETRY_TIMES), + m_retry_interval_ms(COS_DEFAULT_RETRY_INTERVAL_MS) {} /// \brief CosConfig构造函数 /// @@ -74,7 +83,9 @@ class CosConfig { m_dest_domain(""), m_is_domain_same_to_host(false), m_is_domain_same_to_host_enable(false), - m_config_parsed(false) {} + m_config_parsed(false), + m_max_retry_times(COS_DEFAULT_MAX_RETRY_TIMES), + m_retry_interval_ms(COS_DEFAULT_RETRY_INTERVAL_MS) {} /// \brief CosConfig复制构造函数 /// @@ -92,6 +103,8 @@ class CosConfig { m_is_domain_same_to_host = config.m_is_domain_same_to_host; m_is_domain_same_to_host_enable = config.m_is_domain_same_to_host; m_config_parsed = config.m_config_parsed; + m_max_retry_times = config.m_max_retry_times; + m_retry_interval_ms = config.m_retry_interval_ms; } /// \brief CosConfig赋值构造函数 @@ -110,6 +123,8 @@ class CosConfig { m_is_domain_same_to_host = config.m_is_domain_same_to_host; m_is_domain_same_to_host_enable = config.m_is_domain_same_to_host; m_config_parsed = config.m_config_parsed; + m_max_retry_times = config.m_max_retry_times; + m_retry_interval_ms = config.m_retry_interval_ms; return *this; } @@ -198,6 +213,14 @@ class CosConfig { /// \brief 设置日志回调 void SetLogCallback(const LogCallback log_callback); + uint64_t GetMaxRetryTimes() const; + + void SetMaxRetryTimes(uint64_t max_retry_times); + + uint64_t GetRetryIntervalMs() const; + + void SetRetryIntervalMs(uint64_t retry_interval_ms); + static bool JsonObjectGetStringValue( const Poco::JSON::Object::Ptr& json_object, const std::string& key, std::string* value); @@ -222,6 +245,8 @@ class CosConfig { bool m_is_domain_same_to_host; bool m_is_domain_same_to_host_enable; bool m_config_parsed; + uint64_t m_max_retry_times; + uint64_t m_retry_interval_ms; }; typedef std::shared_ptr SharedConfig; diff --git a/include/cos_params.h b/include/cos_params.h index 4656196..4375dd5 100644 --- a/include/cos_params.h +++ b/include/cos_params.h @@ -51,6 +51,7 @@ const char kReqHeaderDate[] = "Date"; const char kReqHeaderServer[] = "Server"; const char kReqHeaderXCosReqId[] = "x-cos-request-id"; const char kReqHeaderXCosTraceId[] = "x-cos-trace-id"; +const char kReqHeaderXCosSdkRetry[] = "x-cos-sdk-retry"; // Response Header const char kRespHeaderLastModified[] = "Last-Modified"; diff --git a/include/op/base_op.h b/include/op/base_op.h index b6797a9..0501305 100644 --- a/include/op/base_op.h +++ b/include/op/base_op.h @@ -2,7 +2,6 @@ #ifndef COS_CPP_SDK_V5_INCLUDE_OP_BASE_OP_H_ #define COS_CPP_SDK_V5_INCLUDE_OP_BASE_OP_H_ -#include #include #include @@ -11,6 +10,7 @@ #include "cos_config.h" #include "op/cos_result.h" #include "trsf/transfer_handler.h" +#include "util/base_op_util.h" namespace qcloud_cos { @@ -22,7 +22,7 @@ class BaseOp { /// \brief BaseOp构造函数 /// /// \param cos_conf Cos配置 - explicit BaseOp(const SharedConfig& cos_conf) : m_config(cos_conf) {} + explicit BaseOp(const SharedConfig& cos_conf) : m_config(cos_conf), m_op_util(m_config) {} BaseOp() {} @@ -51,12 +51,8 @@ class BaseOp { bool IsDomainSameToHost() const; - bool UseDefaultDomain() const; - bool IsDefaultHost(const std::string &host) const; - std::string ChangeHostSuffix(const std::string &host); - /// \brief 封装了cos Service/Bucket/Object 相关接口的通用操作, /// 包括签名计算、请求发送、返回内容解析等 /// @@ -128,6 +124,28 @@ class BaseOp { protected: bool CheckConfigValidation() const; SharedConfig m_config; + BaseOpUtil m_op_util; + +private: + CosResult NormalRequest( + const std::string& host, const std::string& path, const BaseReq& req, + const std::map& additional_headers, + const std::map& additional_params, + const std::string& req_body, bool check_body, BaseResp* resp, + const uint32_t &request_retry_num, bool is_ci_req = false); + + CosResult DownloadRequest(const std::string& host, const std::string& path, + const BaseReq& req, BaseResp* resp, std::ostream& os, + const uint32_t &request_retry_num, + const SharedTransferHandler& handler = nullptr); + + CosResult UploadRequest( + const std::string& host, const std::string& path, const BaseReq& req, + const std::map& additional_headers, + const std::map& additional_params, + std::istream& is, BaseResp* resp, const uint32_t &request_retry_num, const SharedTransferHandler& handler = nullptr); + + bool NoNeedRetry(const CosResult &result); }; } // namespace qcloud_cos diff --git a/include/op/file_copy_task.h b/include/op/file_copy_task.h index 2871d41..0d6221c 100644 --- a/include/op/file_copy_task.h +++ b/include/op/file_copy_task.h @@ -5,15 +5,19 @@ #include #include -#include "Poco/Foundation.h" #include "Poco/Runnable.h" #include "cos_defines.h" +#include "util/base_op_util.h" namespace qcloud_cos { class FileCopyTask : public Poco::Runnable { public: - FileCopyTask(const std::string& full_url, uint64_t conn_timeout_in_ms, + FileCopyTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, + uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms); ~FileCopyTask() {} @@ -45,7 +49,9 @@ class FileCopyTask : public Poco::Runnable { std::string GetLastModified() const { return m_last_modified; } private: - std::string m_full_url; + std::string m_host; + std::string m_path; + bool m_is_https; std::map m_headers; std::map m_params; uint64_t m_conn_timeout_in_ms; @@ -62,6 +68,10 @@ class FileCopyTask : public Poco::Runnable { std::string m_ca_location; SSLCtxCallback m_ssl_ctx_cb; void *m_user_data; + + BaseOpUtil m_op_util; + + void SendRequestOnce(std::string domain); }; } // namespace qcloud_cos \ No newline at end of file diff --git a/include/op/file_download_task.h b/include/op/file_download_task.h index fa2f669..aacd82f 100644 --- a/include/op/file_download_task.h +++ b/include/op/file_download_task.h @@ -11,16 +11,19 @@ #include #include -#include "Poco/Foundation.h" #include "Poco/Runnable.h" #include "cos_config.h" #include "trsf/transfer_handler.h" +#include "util/base_op_util.h" namespace qcloud_cos { class FileDownTask : public Poco::Runnable { public: - FileDownTask(const std::string& full_url, + FileDownTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, const std::map& headers, const std::map& params, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, @@ -57,7 +60,10 @@ class FileDownTask : public Poco::Runnable { std::string GetErrMsg() const { return m_err_msg; } private: - std::string m_full_url; + std::string m_host; + std::string m_path; + bool m_is_https; + BaseOpUtil m_op_util; std::map m_headers; std::map m_params; uint64_t m_conn_timeout_in_ms; @@ -79,6 +85,8 @@ class FileDownTask : public Poco::Runnable { void *m_user_data; SharedConfig m_config; + + void SendRequestOnce(std::string domain); }; } // namespace qcloud_cos \ No newline at end of file diff --git a/include/op/file_upload_task.h b/include/op/file_upload_task.h index 38d9fda..be92b3a 100644 --- a/include/op/file_upload_task.h +++ b/include/op/file_upload_task.h @@ -3,16 +3,20 @@ #include #include -#include "Poco/Foundation.h" #include "Poco/Runnable.h" #include "request/object_req.h" #include "trsf/transfer_handler.h" +#include "util/base_op_util.h" namespace qcloud_cos { class FileUploadTask : public Poco::Runnable { public: - FileUploadTask(const std::string& full_url, uint64_t conn_timeout_in_ms, + FileUploadTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, + uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, unsigned char* pbuf = NULL, const size_t data_len = 0, bool verify_cert = true, @@ -20,7 +24,10 @@ class FileUploadTask : public Poco::Runnable { SSLCtxCallback ssl_ctx_cb = nullptr, void *user_data = nullptr); - FileUploadTask(const std::string& full_url, + FileUploadTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, const std::map& headers, const std::map& params, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, @@ -30,7 +37,10 @@ class FileUploadTask : public Poco::Runnable { SSLCtxCallback ssl_ctx_cb = nullptr, void *user_data = nullptr); - FileUploadTask(const std::string& full_url, + FileUploadTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, const std::map& headers, const std::map& params, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, @@ -91,7 +101,9 @@ class FileUploadTask : public Poco::Runnable { } private: - std::string m_full_url; + std::string m_host; + std::string m_path; + bool m_is_https; std::map m_headers; std::map m_params; uint64_t m_conn_timeout_in_ms; @@ -115,6 +127,10 @@ class FileUploadTask : public Poco::Runnable { bool mb_check_crc64; uint64_t m_crc64_value; + + BaseOpUtil m_op_util; + + void SendRequestOnce(std::string domain, std::string md5_str); }; } // namespace qcloud_cos diff --git a/include/util/base_op_util.h b/include/util/base_op_util.h new file mode 100644 index 0000000..4f32a58 --- /dev/null +++ b/include/util/base_op_util.h @@ -0,0 +1,32 @@ +#ifndef COS_CPP_SDK_V5_INCLUDE_UTIL_BASE_OP_UTIL_H_ +#define COS_CPP_SDK_V5_INCLUDE_UTIL_BASE_OP_UTIL_H_ + +#include + +#include "cos_config.h" +#include "op/cos_result.h" + +namespace qcloud_cos { +class BaseOpUtil { +public: + explicit BaseOpUtil(SharedConfig cos_conf) : m_config(std::move(cos_conf)) {} + + BaseOpUtil() = default; + + bool ShouldChangeBackupDomain(const CosResult &result, const uint32_t &request_num, bool is_ci_req = false) const; + + void SleepBeforeRetry(const uint32_t &request_num) const; + + std::string GetRealUrl(const std::string& host, const std::string& path, bool is_https, bool is_generate_presigned_url = false) const; + + uint64_t GetMaxRetryTimes() const; + + static std::string ChangeHostSuffix(const std::string& host); + +private: + SharedConfig m_config; + + bool UseDefaultDomain() const; +}; +} // namespace qcloud_cos +#endif // COS_CPP_SDK_V5_INCLUDE_UTIL_BASE_OP_UTIL_H_ \ No newline at end of file diff --git a/include/util/http_sender.h b/include/util/http_sender.h index cf96a63..8ef56a4 100644 --- a/include/util/http_sender.h +++ b/include/util/http_sender.h @@ -36,22 +36,6 @@ class HttpSender { SSLCtxCallback ssl_ctx_cb = nullptr, void *user_data = nullptr); - static int SendRequest(const SharedTransferHandler& handler, - const std::string& http_method, - const std::string& url_str, - const std::map& req_params, - const std::map& req_headers, - const std::string& req_body, - uint64_t conn_timeout_in_ms, - uint64_t recv_timeout_in_ms, - std::map* resp_headers, - std::ostream& resp_stream, std::string* err_msg, - bool is_check_md5 = false, - bool is_verify_cert = true, - const std::string& ca_location = "", - SSLCtxCallback ssl_ctx_cb = nullptr, - void *user_data = nullptr); - static int SendRequest(const SharedTransferHandler& handler, const std::string& http_method, const std::string& url_str, @@ -72,16 +56,19 @@ class HttpSender { const std::string& url_str, const std::map& req_params, const std::map& req_headers, - std::istream& is, uint64_t conn_timeout_in_ms, + std::istream& is, + uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, std::map* resp_headers, - std::ostream& resp_stream, std::string* err_msg, + std::ostream& resp_stream, + std::string* err_msg, bool is_check_md5 = false, bool is_verify_cert = true, const std::string& ca_location = "", SSLCtxCallback ssl_ctx_cb = nullptr, void *user_data = nullptr, - const char *req_body_buf = nullptr, size_t req_body_len = 0); + const char *req_body_buf = nullptr, + size_t req_body_len = 0); static int SendRequest(const SharedTransferHandler& handler, const std::string& http_method, diff --git a/include/util/retry_util.h b/include/util/retry_util.h deleted file mode 100644 index a164d3d..0000000 --- a/include/util/retry_util.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef COS_CPP_SDK_V5_INCLUDE_UTIL_RETRY_UTIL_H_ -#define COS_CPP_SDK_V5_INCLUDE_UTIL_RETRY_UTIL_H_ -#include "op/cos_result.h" - -namespace qcloud_cos { - -class RetryUtil { - public: - static bool ShouldRetryWithChangeDomain(CosResult result); -}; -} // namespace qcloud_cos - -#endif // COS_CPP_SDK_V5_INCLUDE_UTIL_RETRY_UTIL_H_ \ No newline at end of file diff --git a/src/cos_api.cpp b/src/cos_api.cpp index fee5ab7..232b6d8 100644 --- a/src/cos_api.cpp +++ b/src/cos_api.cpp @@ -1,5 +1,4 @@ #include "cos_api.h" -#include #include "Poco/Net/HTTPSStreamFactory.h" #include "Poco/Net/HTTPStreamFactory.h" #include "Poco/Net/SSLManager.h" @@ -7,9 +6,6 @@ #include "cos_sys_config.h" #include "trsf/async_context.h" #include "trsf/async_task.h" -#include "util/file_util.h" -#include "util/string_util.h" -#include "util/retry_util.h" namespace qcloud_cos { @@ -106,382 +102,211 @@ CosResult CosAPI::GetService(const GetServiceReq& req, GetServiceResp* resp) { } CosResult CosAPI::HeadBucket(const HeadBucketReq& req, HeadBucketResp* resp) { - CosResult result = m_bucket_op.HeadBucket(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.HeadBucket(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.HeadBucket(req, resp); } CosResult CosAPI::PutBucket(const PutBucketReq& req, PutBucketResp* resp) { - CosResult result = m_bucket_op.PutBucket(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucket(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucket(req, resp); } CosResult CosAPI::GetBucket(const GetBucketReq& req, GetBucketResp* resp) { - CosResult result = m_bucket_op.GetBucket(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucket(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucket(req, resp); } CosResult CosAPI::ListMultipartUpload(const ListMultipartUploadReq& req, ListMultipartUploadResp* resp) { - CosResult result = m_bucket_op.ListMultipartUpload(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.ListMultipartUpload(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.ListMultipartUpload(req, resp); } CosResult CosAPI::DeleteBucket(const DeleteBucketReq& req, DeleteBucketResp* resp) { - CosResult result = m_bucket_op.DeleteBucket(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucket(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucket(req, resp); } CosResult CosAPI::GetBucketVersioning(const GetBucketVersioningReq& req, GetBucketVersioningResp* resp) { - CosResult result = m_bucket_op.GetBucketVersioning(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketVersioning(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketVersioning(req, resp); } CosResult CosAPI::PutBucketVersioning(const PutBucketVersioningReq& req, PutBucketVersioningResp* resp) { - CosResult result = m_bucket_op.PutBucketVersioning(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketVersioning(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketVersioning(req, resp); } CosResult CosAPI::GetBucketReplication(const GetBucketReplicationReq& req, GetBucketReplicationResp* resp) { - CosResult result = m_bucket_op.GetBucketReplication(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketReplication(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketReplication(req, resp); } CosResult CosAPI::PutBucketReplication(const PutBucketReplicationReq& req, PutBucketReplicationResp* resp) { - CosResult result = m_bucket_op.PutBucketReplication(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketReplication(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketReplication(req, resp); } CosResult CosAPI::DeleteBucketReplication(const DeleteBucketReplicationReq& req, DeleteBucketReplicationResp* resp) { - CosResult result = m_bucket_op.DeleteBucketReplication(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketReplication(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketReplication(req, resp); } CosResult CosAPI::GetBucketLifecycle(const GetBucketLifecycleReq& req, GetBucketLifecycleResp* resp) { - CosResult result = m_bucket_op.GetBucketLifecycle(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketLifecycle(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketLifecycle(req, resp); } CosResult CosAPI::PutBucketLifecycle(const PutBucketLifecycleReq& req, PutBucketLifecycleResp* resp) { - CosResult result = m_bucket_op.PutBucketLifecycle(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketLifecycle(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketLifecycle(req, resp); } CosResult CosAPI::DeleteBucketLifecycle(const DeleteBucketLifecycleReq& req, DeleteBucketLifecycleResp* resp) { - CosResult result = m_bucket_op.DeleteBucketLifecycle(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketLifecycle(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketLifecycle(req, resp); } CosResult CosAPI::GetBucketACL(const GetBucketACLReq& req, GetBucketACLResp* resp) { - CosResult result = m_bucket_op.GetBucketACL(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketACL(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketACL(req, resp); } CosResult CosAPI::PutBucketACL(const PutBucketACLReq& req, PutBucketACLResp* resp) { - CosResult result = m_bucket_op.PutBucketACL(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketACL(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketACL(req, resp); } CosResult CosAPI::PutBucketPolicy(const PutBucketPolicyReq& req, PutBucketPolicyResp* resp) { - CosResult result = m_bucket_op.PutBucketPolicy(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketPolicy(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketPolicy(req, resp); } CosResult CosAPI::GetBucketPolicy(const GetBucketPolicyReq& req, GetBucketPolicyResp* resp) { - CosResult result = m_bucket_op.GetBucketPolicy(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketPolicy(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketPolicy(req, resp); } CosResult CosAPI::DeleteBucketPolicy(const DeleteBucketPolicyReq& req, DeleteBucketPolicyResp* resp) { - CosResult result = m_bucket_op.DeleteBucketPolicy(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketPolicy(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketPolicy(req, resp); } CosResult CosAPI::GetBucketCORS(const GetBucketCORSReq& req, GetBucketCORSResp* resp) { - CosResult result = m_bucket_op.GetBucketCORS(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketCORS(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketCORS(req, resp); } CosResult CosAPI::PutBucketCORS(const PutBucketCORSReq& req, PutBucketCORSResp* resp) { - CosResult result = m_bucket_op.PutBucketCORS(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketCORS(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketCORS(req, resp); } CosResult CosAPI::DeleteBucketCORS(const DeleteBucketCORSReq& req, DeleteBucketCORSResp* resp) { - CosResult result = m_bucket_op.DeleteBucketCORS(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketCORS(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketCORS(req, resp); } CosResult CosAPI::PutBucketReferer(const PutBucketRefererReq& req, PutBucketRefererResp* resp) { - CosResult result = m_bucket_op.PutBucketReferer(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketReferer(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketReferer(req, resp); } CosResult CosAPI::GetBucketReferer(const GetBucketRefererReq& req, GetBucketRefererResp* resp) { - CosResult result = m_bucket_op.GetBucketReferer(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketReferer(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketReferer(req, resp); } CosResult CosAPI::PutBucketLogging(const PutBucketLoggingReq& req, PutBucketLoggingResp* resp) { - CosResult result = m_bucket_op.PutBucketLogging(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketLogging(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketLogging(req, resp); } CosResult CosAPI::GetBucketLogging(const GetBucketLoggingReq& req, GetBucketLoggingResp* resp) { - CosResult result = m_bucket_op.GetBucketLogging(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketLogging(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketLogging(req, resp); } CosResult CosAPI::PutBucketDomain(const PutBucketDomainReq& req, PutBucketDomainResp* resp) { - CosResult result = m_bucket_op.PutBucketDomain(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketDomain(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketDomain(req, resp); } CosResult CosAPI::GetBucketDomain(const GetBucketDomainReq& req, GetBucketDomainResp* resp) { - CosResult result = m_bucket_op.GetBucketDomain(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketDomain(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketDomain(req, resp); } CosResult CosAPI::PutBucketWebsite(const PutBucketWebsiteReq& req, PutBucketWebsiteResp* resp) { - CosResult result = m_bucket_op.PutBucketWebsite(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketWebsite(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketWebsite(req, resp); } CosResult CosAPI::GetBucketWebsite(const GetBucketWebsiteReq& req, GetBucketWebsiteResp* resp) { - CosResult result = m_bucket_op.GetBucketWebsite(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketWebsite(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketWebsite(req, resp); } CosResult CosAPI::DeleteBucketWebsite(const DeleteBucketWebsiteReq& req, DeleteBucketWebsiteResp* resp) { - CosResult result = m_bucket_op.DeleteBucketWebsite(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketWebsite(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketWebsite(req, resp); } CosResult CosAPI::PutBucketTagging(const PutBucketTaggingReq& req, PutBucketTaggingResp* resp) { - CosResult result = m_bucket_op.PutBucketTagging(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketTagging(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketTagging(req, resp); } CosResult CosAPI::GetBucketTagging(const GetBucketTaggingReq& req, GetBucketTaggingResp* resp) { - CosResult result = m_bucket_op.GetBucketTagging(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketTagging(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketTagging(req, resp); } CosResult CosAPI::DeleteBucketTagging(const DeleteBucketTaggingReq& req, DeleteBucketTaggingResp* resp) { - CosResult result = m_bucket_op.DeleteBucketTagging(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketTagging(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketTagging(req, resp); } CosResult CosAPI::PutBucketInventory(const PutBucketInventoryReq& req, PutBucketInventoryResp* resp) { - CosResult result = m_bucket_op.PutBucketInventory(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketInventory(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketInventory(req, resp); } CosResult CosAPI::GetBucketInventory(const GetBucketInventoryReq& req, GetBucketInventoryResp* resp) { - CosResult result = m_bucket_op.GetBucketInventory(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketInventory(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketInventory(req, resp); } CosResult CosAPI::ListBucketInventoryConfigurations( const ListBucketInventoryConfigurationsReq& req, ListBucketInventoryConfigurationsResp* resp) { - CosResult result = m_bucket_op.ListBucketInventoryConfigurations(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.ListBucketInventoryConfigurations(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.ListBucketInventoryConfigurations(req, resp); } CosResult CosAPI::DeleteBucketInventory(const DeleteBucketInventoryReq& req, DeleteBucketInventoryResp* resp) { - CosResult result = m_bucket_op.DeleteBucketInventory(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.DeleteBucketInventory(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.DeleteBucketInventory(req, resp); } CosResult CosAPI::GetBucketObjectVersions(const GetBucketObjectVersionsReq& req, GetBucketObjectVersionsResp* resp) { - CosResult result = m_bucket_op.GetBucketObjectVersions(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketObjectVersions(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketObjectVersions(req, resp); } CosResult CosAPI::PutObject(const PutObjectByFileReq& req, PutObjectByFileResp* resp) { - CosResult result = m_object_op.PutObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutObject(req, resp, nullptr, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutObject(req, resp); } CosResult CosAPI::PutObject(const PutObjectByStreamReq& req, PutObjectByStreamResp* resp) { - CosResult result = m_object_op.PutObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutObject(req, resp, nullptr, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutObject(req, resp); } CosResult CosAPI::GetObject(const GetObjectByStreamReq& req, GetObjectByStreamResp* resp) { - CosResult result = m_object_op.GetObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - std::streambuf* os_buf = req.GetStream().rdbuf(); - os_buf->pubseekpos(0, std::ios_base::out); - req.GetStream().clear(); - result = m_object_op.GetObject(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetObject(req, resp); } CosResult CosAPI::GetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp) { - CosResult result = m_object_op.GetObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.GetObject(req, resp, nullptr, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetObject(req, resp); } CosResult CosAPI::MultiGetObject(const MultiGetObjectReq& req, @@ -517,73 +342,41 @@ std::string CosAPI::GetObjectUrl(const std::string& bucket, CosResult CosAPI::DeleteObject(const DeleteObjectReq& req, DeleteObjectResp* resp) { - CosResult result = m_object_op.DeleteObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.DeleteObject(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.DeleteObject(req, resp); } CosResult CosAPI::DeleteObjects(const DeleteObjectsReq& req, DeleteObjectsResp* resp) { - CosResult result = m_object_op.DeleteObjects(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.DeleteObjects(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.DeleteObjects(req, resp); } CosResult CosAPI::HeadObject(const HeadObjectReq& req, HeadObjectResp* resp) { - CosResult result = m_object_op.HeadObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.HeadObject(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.HeadObject(req, resp); } CosResult CosAPI::InitMultiUpload(const InitMultiUploadReq& req, InitMultiUploadResp* resp) { - CosResult result = m_object_op.InitMultiUpload(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.InitMultiUpload(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.InitMultiUpload(req, resp); } CosResult CosAPI::UploadPartData(const UploadPartDataReq& req, UploadPartDataResp* resp) { - CosResult result = m_object_op.UploadPartData(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.UploadPartData(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.UploadPartData(req, resp); } CosResult CosAPI::UploadPartCopyData(const UploadPartCopyDataReq& req, UploadPartCopyDataResp* resp) { - CosResult result = m_object_op.UploadPartCopyData(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.UploadPartCopyData(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.UploadPartCopyData(req, resp); } CosResult CosAPI::CompleteMultiUpload(const CompleteMultiUploadReq& req, CompleteMultiUploadResp* resp) { - CosResult result = m_object_op.CompleteMultiUpload(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.CompleteMultiUpload(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.CompleteMultiUpload(req, resp); } CosResult CosAPI::MultiPutObject(const MultiPutObjectReq& req, MultiPutObjectResp* resp) { - CosResult result = m_object_op.MultiUploadObject(static_cast(req), resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.MultiUploadObject(static_cast(req), resp, nullptr, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.MultiUploadObject(static_cast(req), resp); } CosResult CosAPI::PutObjectResumableSingleThreadSync(const PutObjectResumableSingleSyncReq& req, @@ -593,37 +386,21 @@ CosResult CosAPI::PutObjectResumableSingleThreadSync(const PutObjectResumableSin CosResult CosAPI::AbortMultiUpload(const AbortMultiUploadReq& req, AbortMultiUploadResp* resp) { - CosResult result = m_object_op.AbortMultiUpload(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.AbortMultiUpload(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.AbortMultiUpload(req, resp); } CosResult CosAPI::ListParts(const ListPartsReq& req, ListPartsResp* resp) { - CosResult result = m_object_op.ListParts(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.ListParts(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.ListParts(req, resp); } CosResult CosAPI::GetObjectACL(const GetObjectACLReq& req, GetObjectACLResp* resp) { - CosResult result = m_object_op.GetObjectACL(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.GetObjectACL(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetObjectACL(req, resp); } CosResult CosAPI::PutObjectACL(const PutObjectACLReq& req, PutObjectACLResp* resp) { - CosResult result = m_object_op.PutObjectACL(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutObjectACL(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutObjectACL(req, resp); } CosResult CosAPI::PutObjectTagging(const PutObjectTaggingReq& req, @@ -644,46 +421,26 @@ CosResult CosAPI::DeleteObjectTagging(const DeleteObjectTaggingReq& req, CosResult CosAPI::PutObjectCopy(const PutObjectCopyReq& req, PutObjectCopyResp* resp) { - CosResult result = m_object_op.PutObjectCopy(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutObjectCopy(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutObjectCopy(req, resp); } CosResult CosAPI::Copy(const CopyReq& req, CopyResp* resp) { - CosResult result = m_object_op.Copy(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.Copy(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.Copy(req, resp); } CosResult CosAPI::PostObjectRestore(const PostObjectRestoreReq& req, PostObjectRestoreResp* resp) { - CosResult result = m_object_op.PostObjectRestore(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PostObjectRestore(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PostObjectRestore(req, resp); } CosResult CosAPI::OptionsObject(const OptionsObjectReq& req, OptionsObjectResp* resp) { - CosResult result = m_object_op.OptionsObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.OptionsObject(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.OptionsObject(req, resp); } CosResult CosAPI::SelectObjectContent(const SelectObjectContentReq& req, SelectObjectContentResp* resp) { - CosResult result = m_object_op.SelectObjectContent(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.SelectObjectContent(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.SelectObjectContent(req, resp); } CosResult CosAPI::AppendObject(const AppendObjectReq& req, @@ -693,11 +450,7 @@ CosResult CosAPI::AppendObject(const AppendObjectReq& req, CosResult CosAPI::PutLiveChannel(const PutLiveChannelReq& req, PutLiveChannelResp* resp) { - CosResult result = m_object_op.PutLiveChannel(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutLiveChannel(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutLiveChannel(req, resp); } std::string CosAPI::GetRtmpSignedPublishUrl( @@ -714,105 +467,59 @@ std::string CosAPI::GetRtmpSignedPublishUrl( CosResult CosAPI::PutLiveChannelSwitch(const PutLiveChannelSwitchReq& req, PutLiveChannelSwitchResp* resp) { - CosResult result = m_object_op.PutLiveChannelSwitch(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutLiveChannelSwitch(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutLiveChannelSwitch(req, resp); } CosResult CosAPI::GetLiveChannel(const GetLiveChannelReq& req, GetLiveChannelResp* resp) { - CosResult result = m_object_op.GetLiveChannel(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.GetLiveChannel(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetLiveChannel(req, resp); } CosResult CosAPI::GetLiveChannelHistory(const GetLiveChannelHistoryReq& req, GetLiveChannelHistoryResp* resp) { - CosResult result = m_object_op.GetLiveChannelHistory(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.GetLiveChannelHistory(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetLiveChannelHistory(req, resp); } CosResult CosAPI::GetLiveChannelStatus(const GetLiveChannelStatusReq& req, GetLiveChannelStatusResp* resp) { - CosResult result = m_object_op.GetLiveChannelStatus(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.GetLiveChannelStatus(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetLiveChannelStatus(req, resp); } CosResult CosAPI::DeleteLiveChannel(const DeleteLiveChannelReq& req, DeleteLiveChannelResp* resp) { - CosResult result = m_object_op.DeleteLiveChannel(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.DeleteLiveChannel(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.DeleteLiveChannel(req, resp); } CosResult CosAPI::GetLiveChannelVodPlaylist(const GetLiveChannelVodPlaylistReq& req, GetLiveChannelVodPlaylistResp* resp) { - CosResult result = m_object_op.GetLiveChannelVodPlaylist(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.GetLiveChannelVodPlaylist(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.GetLiveChannelVodPlaylist(req, resp); } CosResult CosAPI::PostLiveChannelVodPlaylist(const PostLiveChannelVodPlaylistReq& req, PostLiveChannelVodPlaylistResp* resp) { - CosResult result = m_object_op.PostLiveChannelVodPlaylist(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PostLiveChannelVodPlaylist(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PostLiveChannelVodPlaylist(req, resp); } CosResult CosAPI::ListLiveChannel(const ListLiveChannelReq& req, ListLiveChannelResp* resp) { - CosResult result = m_bucket_op.ListLiveChannel(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.ListLiveChannel(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.ListLiveChannel(req, resp); } -CosResult -CosAPI::PutBucketIntelligentTiering(const PutBucketIntelligentTieringReq& req, +CosResult CosAPI::PutBucketIntelligentTiering(const PutBucketIntelligentTieringReq& req, PutBucketIntelligentTieringResp* resp) { - CosResult result = m_bucket_op.PutBucketIntelligentTiering(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.PutBucketIntelligentTiering(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.PutBucketIntelligentTiering(req, resp); } -CosResult -CosAPI::GetBucketIntelligentTiering(const GetBucketIntelligentTieringReq& req, +CosResult CosAPI::GetBucketIntelligentTiering(const GetBucketIntelligentTieringReq& req, GetBucketIntelligentTieringResp* resp) { - CosResult result = m_bucket_op.GetBucketIntelligentTiering(req, resp); - if (m_bucket_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_bucket_op.GetBucketIntelligentTiering(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_bucket_op.GetBucketIntelligentTiering(req, resp); } CosResult CosAPI::ResumableGetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp) { - CosResult result = m_object_op.ResumableGetObject(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.ResumableGetObject(req, resp, nullptr, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.ResumableGetObject(req, resp); } SharedAsyncContext CosAPI::AsyncPutObject(const AsyncPutObjectReq& req) { @@ -822,9 +529,6 @@ SharedAsyncContext CosAPI::AsyncPutObject(const AsyncPutObjectReq& req) { TaskFunc fn = [=]() { PutObjectByFileResp resp; m_object_op.PutObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.PutObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; GetGlobalTaskManager().start(new AsyncTask(std::move(fn))); SharedAsyncContext context(new AsyncContext(handler)); @@ -838,9 +542,6 @@ SharedAsyncContext CosAPI::AsyncPutObject(const AsyncPutObjectReq& req, Poco::Ta TaskFunc fn = [=]() { PutObjectByFileResp resp; m_object_op.PutObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.PutObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; taskManager = &GetGlobalTaskManager(); (*taskManager).start(new AsyncTask(std::move(fn))); @@ -858,9 +559,6 @@ SharedAsyncContext CosAPI::AsyncPutObject(const AsyncPutObjectByStreamReq& req) TaskFunc fn = [=]() { PutObjectByStreamResp resp; m_object_op.PutObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.PutObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; GetGlobalTaskManager().start(new AsyncTask(std::move(fn))); SharedAsyncContext context(new AsyncContext(handler)); @@ -877,9 +575,6 @@ SharedAsyncContext CosAPI::AsyncPutObject(const AsyncPutObjectByStreamReq& req, TaskFunc fn = [=]() { PutObjectByStreamResp resp; m_object_op.PutObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.PutObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; taskManager = &GetGlobalTaskManager(); (*taskManager).start(new AsyncTask(std::move(fn))); @@ -895,9 +590,6 @@ SharedAsyncContext CosAPI::AsyncMultiPutObject(const AsyncMultiPutObjectReq& req TaskFunc fn = [=]() { MultiPutObjectResp resp; m_object_op.MultiUploadObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.MultiUploadObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; GetGlobalTaskManager().start(new AsyncTask(std::move(fn))); SharedAsyncContext context(new AsyncContext(handler)); @@ -911,9 +603,6 @@ SharedAsyncContext CosAPI::AsyncMultiPutObject(const AsyncMultiPutObjectReq& req TaskFunc fn = [=]() { MultiPutObjectResp resp; m_object_op.MultiUploadObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.MultiUploadObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; taskManager = &GetGlobalTaskManager(); (*taskManager).start(new AsyncTask(std::move(fn))); @@ -927,9 +616,6 @@ SharedAsyncContext CosAPI::AsyncGetObject(const AsyncGetObjectReq& req) { TaskFunc fn = [=]() { GetObjectByFileResp resp; m_object_op.GetObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.GetObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; GetGlobalTaskManager().start(new AsyncTask(std::move(fn))); SharedAsyncContext context(new AsyncContext(handler)); @@ -942,9 +628,6 @@ SharedAsyncContext CosAPI::AsyncGetObject(const AsyncGetObjectReq& req, Poco::Ta TaskFunc fn = [=]() { GetObjectByFileResp resp; m_object_op.GetObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.GetObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; taskManager = &GetGlobalTaskManager(); (*taskManager).start(new AsyncTask(std::move(fn))); @@ -958,9 +641,6 @@ SharedAsyncContext CosAPI::AsyncResumableGetObject(const AsyncGetObjectReq& req) TaskFunc fn = [=]() { GetObjectByFileResp resp; m_object_op.ResumableGetObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.ResumableGetObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; GetGlobalTaskManager().start(new AsyncTask(std::move(fn))); SharedAsyncContext context(new AsyncContext(handler)); @@ -973,9 +653,6 @@ SharedAsyncContext CosAPI::AsyncResumableGetObject(const AsyncGetObjectReq& req, TaskFunc fn = [=]() { GetObjectByFileResp resp; m_object_op.ResumableGetObject(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.ResumableGetObject(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; taskManager = &GetGlobalTaskManager(); (*taskManager).start(new AsyncTask(std::move(fn))); @@ -989,9 +666,6 @@ SharedAsyncContext CosAPI::AsyncMultiGetObject(const AsyncMultiGetObjectReq& req TaskFunc fn = [=]() { GetObjectByFileResp resp; m_object_op.MultiThreadDownload(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.MultiThreadDownload(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; GetGlobalTaskManager().start(new AsyncTask(std::move(fn))); SharedAsyncContext context(new AsyncContext(handler)); @@ -1004,9 +678,6 @@ SharedAsyncContext CosAPI::AsyncMultiGetObject(const AsyncMultiGetObjectReq& req TaskFunc fn = [=]() { GetObjectByFileResp resp; m_object_op.MultiThreadDownload(req, &resp, handler); - if(handler->GetStatus() == TransferStatus::RETRY){ - m_object_op.MultiThreadDownload(req, &resp, handler, COS_CHANGE_BACKUP_DOMAIN); - } }; taskManager = &GetGlobalTaskManager(); (*taskManager).start(new AsyncTask(std::move(fn))); @@ -1016,20 +687,12 @@ SharedAsyncContext CosAPI::AsyncMultiGetObject(const AsyncMultiGetObjectReq& req CosResult CosAPI::PutObjects(const PutObjectsByDirectoryReq& req, PutObjectsByDirectoryResp* resp) { - CosResult result = m_object_op.PutObjects(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutObjects(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutObjects(req, resp); } CosResult CosAPI::PutDirectory(const PutDirectoryReq& req, PutDirectoryResp* resp) { - CosResult result = m_object_op.PutDirectory(req, resp); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.PutDirectory(req, resp, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.PutDirectory(req, resp); } CosResult CosAPI::DeleteObjects(const DeleteObjectsByPrefixReq& req, @@ -1066,11 +729,7 @@ CosResult CosAPI::DeleteObjects(const DeleteObjectsByPrefixReq& req, }; CosResult CosAPI::MoveObject(const MoveObjectReq& req) { - CosResult result = m_object_op.MoveObject(req); - if (m_object_op.UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = m_object_op.MoveObject(req, COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return m_object_op.MoveObject(req); } CosResult CosAPI::PutBucketToCI(const PutBucketToCIReq& req, PutBucketToCIResp* resp) { diff --git a/src/cos_config.cpp b/src/cos_config.cpp index 2c431ad..d01af2c 100644 --- a/src/cos_config.cpp +++ b/src/cos_config.cpp @@ -21,7 +21,9 @@ CosConfig::CosConfig(const std::string& config_file) m_intranet_addr(""), m_dest_domain(""), m_is_domain_same_to_host(false), - m_config_parsed(false) { + m_config_parsed(false), + m_max_retry_times(COS_DEFAULT_MAX_RETRY_TIMES), + m_retry_interval_ms(COS_DEFAULT_RETRY_INTERVAL_MS) { if (InitConf(config_file)) { m_config_parsed = true; } @@ -114,6 +116,10 @@ bool CosConfig::InitConf(const std::string& config_file) { JsonObjectGetStringValue(object, "Region", &m_region); m_region = StringUtil::Trim(m_region); + JsonObjectGetIntegerValue(object, "RetryIntervalMs", &m_retry_interval_ms); + + JsonObjectGetIntegerValue(object, "MaxRetryTimes", &m_max_retry_times); + uint64_t integer_value; //设置签名超时时间,单位:秒 @@ -231,6 +237,22 @@ std::string CosConfig::GetTmpToken() const { return token; } +uint64_t CosConfig::GetMaxRetryTimes() const { + return m_max_retry_times; +} + +void CosConfig::SetMaxRetryTimes(uint64_t max_retry_count) { + m_max_retry_times = max_retry_count; +} + +uint64_t CosConfig::GetRetryIntervalMs() const { + return m_retry_interval_ms; +} + +void CosConfig::SetRetryIntervalMs(uint64_t retry_interval_ms) { + m_retry_interval_ms = retry_interval_ms; +} + void CosConfig::SetConfigCredentail(const std::string& access_key, const std::string& secret_key, const std::string& tmp_token) { diff --git a/src/op/base_op.cpp b/src/op/base_op.cpp index 5d45838..4bb7c0c 100644 --- a/src/op/base_op.cpp +++ b/src/op/base_op.cpp @@ -14,19 +14,11 @@ #include "request/base_req.h" #include "response/base_resp.h" #include "util/auth_tool.h" -#include "util/codec_util.h" #include "util/http_sender.h" #include "util/simple_dns_cache.h" #include "trsf/transfer_handler.h" namespace qcloud_cos { - -SimpleDnsCache& GetGlobalDnsCacheInstance() { - static SimpleDnsCache dns_cache(CosSysConfig::GetDnsCacheSize(), - CosSysConfig::GetDnsCacheExpireSeconds()); - return dns_cache; -} - CosConfig BaseOp::GetCosConfig() const { return *m_config; } uint64_t BaseOp::GetAppId() const { return m_config->GetAppId(); } @@ -49,17 +41,6 @@ bool BaseOp::IsDomainSameToHost() const { m_config->IsDomainSameToHost() : CosSysConfig::IsDomainSameToHost(); } -bool BaseOp::UseDefaultDomain() const { - if (m_config && m_config->GetSetIntranetOnce() && m_config->IsUseIntranet() && !m_config->GetIntranetAddr().empty() - || CosSysConfig::IsUseIntranet() && !CosSysConfig::GetIntranetAddr().empty() - || (!m_config->GetDestDomain().empty()) - || !CosSysConfig::GetDestDomain().empty() - || m_config->GetRegion().compare("accelerate") == 0) { - return false; - } - return true; -} - bool BaseOp::IsDefaultHost(const std::string &host) const { size_t dot_pos = host.find('.'); @@ -98,18 +79,16 @@ bool BaseOp::IsDefaultHost(const std::string &host) const { return i == len; } -std::string BaseOp::ChangeHostSuffix(const std::string& host) { - const std::string old_suffix = ".myqcloud.com"; - const std::string new_suffix = ".tencentcos.cn"; - - size_t suffix_pos = host.rfind(old_suffix); - if (suffix_pos != std::string::npos) { - std::string new_host = host.substr(0, suffix_pos) + new_suffix; - return new_host; - } else { - return host; - } + +bool BaseOp::NoNeedRetry(const CosResult &result) { + if (result.IsSucc()) { + // 请求成功, 不重试 + return true; + } + int statusCode = result.GetHttpStatus(); + return statusCode >= 400 && statusCode < 500; } + CosResult BaseOp::NormalAction(const std::string& host, const std::string& path, const BaseReq& req, const std::string& req_body, bool check_body, BaseResp* resp, bool is_ci_req) { @@ -136,6 +115,25 @@ CosResult BaseOp::NormalAction( return result; } + std::string domain = host; + for (uint32_t i = 0; ; i++) { + result = NormalRequest(domain, path, req, additional_headers, additional_params, req_body, check_body, resp, i, is_ci_req); + if (i >= m_op_util.GetMaxRetryTimes() || NoNeedRetry(result)) { + return result; + } + if (m_op_util.ShouldChangeBackupDomain(result, i, is_ci_req)) { + domain = BaseOpUtil::ChangeHostSuffix(domain); + } + m_op_util.SleepBeforeRetry(i); + } +} + +CosResult BaseOp::NormalRequest(const std::string& host, const std::string& path, const BaseReq& req, + const std::map& additional_headers, + const std::map& additional_params, + const std::string& req_body, bool check_body, BaseResp* resp, + const uint32_t &request_retry_num, bool is_ci_req) { + CosResult result; std::map req_headers = req.GetHeaders(); std::map req_params = req.GetParams(); req_headers.insert(additional_headers.begin(), additional_headers.end()); @@ -155,6 +153,11 @@ CosResult BaseOp::NormalAction( } else { req_headers["Host"] = GetDestDomain(); } + + if (request_retry_num > 0) { + req_headers[kReqHeaderXCosSdkRetry] = "true"; + } + std::unordered_set not_sign_headers; if (!req.SignHeaderHost()) { not_sign_headers.insert("Host"); @@ -162,12 +165,10 @@ CosResult BaseOp::NormalAction( req_headers[kHttpHeaderContentLength] = std::to_string(req_body.length()); // 2. 计算签名 - std::string auth_str = - AuthTool::Sign(GetAccessKey(), GetSecretKey(), req.GetMethod(), + std::string auth_str = AuthTool::Sign(GetAccessKey(), GetSecretKey(), req.GetMethod(), req.GetPath(), req_headers, req_params, not_sign_headers); if (auth_str.empty()) { - result.SetErrorMsg( - "Generate auth str fail, check your access_key/secret_key."); + result.SetErrorMsg("Generate auth str fail, check your access_key/secret_key."); return result; } req_headers["Authorization"] = auth_str; @@ -195,21 +196,20 @@ CosResult BaseOp::NormalAction( if (!result.ParseFromHttpResponse(resp_headers, resp_body)) { result.SetErrorMsg(resp_body); } - } else { - // 某些请求,如PutObjectCopy/Complete请求需要进一步检查Body - if (check_body && result.ParseFromHttpResponse(resp_headers, resp_body)) { - result.SetErrorMsg(resp_body); - return result; - } - - result.SetSucc(); - resp->ParseFromXmlString(resp_body); - resp->ParseFromHeaders(resp_headers); - resp->SetBody(resp_body); - // resp requestid to result - result.SetXCosRequestId(resp->GetXCosRequestId()); + return result; + } + // 某些请求,如PutObjectCopy/Complete请求需要进一步检查Body + if (check_body && result.ParseFromHttpResponse(resp_headers, resp_body)) { + result.SetErrorMsg(resp_body); + return result; } + result.SetSucc(); + resp->ParseFromXmlString(resp_body); + resp->ParseFromHeaders(resp_headers); + resp->SetBody(resp_body); + // resp requestid to result + result.SetXCosRequestId(resp->GetXCosRequestId()); return result; } @@ -228,6 +228,24 @@ CosResult BaseOp::DownloadAction(const std::string& host, return result; } + std::string domain = host; + for (uint32_t i = 0; ; i++) { + result = DownloadRequest(domain, path, req, resp, os, i, handler); + if (i >= m_op_util.GetMaxRetryTimes() || NoNeedRetry(result)) { + return result; + } + os.rdbuf()->pubseekpos(0, std::ios_base::out); + os.clear(); + if (m_op_util.ShouldChangeBackupDomain(result, i)) { + domain = BaseOpUtil::ChangeHostSuffix(domain); + } + m_op_util.SleepBeforeRetry(i); + } +} + +CosResult BaseOp::DownloadRequest(const std::string &host, const std::string &path, const BaseReq &req, + BaseResp *resp, std::ostream &os, const uint32_t &request_retry_num, const SharedTransferHandler &handler) { + CosResult result; std::map req_headers = req.GetHeaders(); std::map req_params = req.GetParams(); const std::string& tmp_token = m_config->GetTmpToken(); @@ -242,6 +260,10 @@ CosResult BaseOp::DownloadAction(const std::string& host, req_headers["Host"] = GetDestDomain(); } + if (request_retry_num > 0) { + req_headers[kReqHeaderXCosSdkRetry] = "true"; + } + std::unordered_set not_sign_headers; if (!req.SignHeaderHost()) { not_sign_headers.insert("Host"); @@ -320,11 +342,33 @@ CosResult BaseOp::UploadAction( return result; } + std::string domain = host; + for (uint32_t i = 0; ; i++) { + std::streampos initial_pos = is.tellg(); + result = UploadRequest(domain, path, req, additional_headers, additional_params, is, resp, i, handler); + if (i >= m_op_util.GetMaxRetryTimes() || NoNeedRetry(result) ) { + return result; + } + if (m_op_util.ShouldChangeBackupDomain(result, i)) { + domain = BaseOpUtil::ChangeHostSuffix(domain); + } + is.clear(); + is.seekg(initial_pos); + m_op_util.SleepBeforeRetry(i); + } +} + +CosResult BaseOp::UploadRequest( + const std::string &host, const std::string &path, const BaseReq &req, + const std::map &additional_headers, + const std::map &additional_params, + std::istream &is, BaseResp *resp, const uint32_t &request_retry_num, const SharedTransferHandler &handler) { + CosResult result; std::map req_headers = req.GetHeaders(); std::map req_params = req.GetParams(); req_headers.insert(additional_headers.begin(), additional_headers.end()); req_params.insert(additional_params.begin(), additional_params.end()); - const std::string& tmp_token = m_config->GetTmpToken(); + const std::string &tmp_token = m_config->GetTmpToken(); if (!tmp_token.empty()) { req_headers["x-cos-security-token"] = tmp_token; } @@ -336,6 +380,10 @@ CosResult BaseOp::UploadAction( req_headers["Host"] = GetDestDomain(); } + if (request_retry_num > 0) { + req_headers[kReqHeaderXCosSdkRetry] = "true"; + } + std::unordered_set not_sign_headers; if (!req.SignHeaderHost()) { not_sign_headers.insert("Host"); @@ -383,45 +431,13 @@ CosResult BaseOp::UploadAction( //resp->SetHeaders(resp_headers); result.SetXCosRequestId(resp->GetXCosRequestId()); } - return result; } // 1. host优先级,私有ip > 自定义域名 > DNS cache > 默认域名 std::string BaseOp::GetRealUrl(const std::string& host, const std::string& path, bool is_https, bool is_generate_presigned_url) { - std::string dest_uri; - std::string dest_host = host; - std::string dest_path = path; - std::string dest_protocal = "http://"; // NOCA:HttpHardcoded(ignore) - if (is_https) { - dest_protocal = "https://"; - } - - if (dest_path.empty() || '/' != dest_path[0]) { - dest_path = "/" + dest_path; - } - - if (m_config && - m_config->GetSetIntranetOnce() && - m_config->IsUseIntranet() && - !m_config->GetIntranetAddr().empty() && !is_generate_presigned_url) { - dest_host = m_config->GetIntranetAddr(); - } else if (CosSysConfig::IsUseIntranet() && - !CosSysConfig::GetIntranetAddr().empty() && !is_generate_presigned_url) { - dest_host = CosSysConfig::GetIntranetAddr(); - } else if (m_config && - (!m_config->GetDestDomain().empty())) { - dest_host = m_config->GetDestDomain(); - } else if (!CosSysConfig::GetDestDomain().empty()) { - dest_host = CosSysConfig::GetDestDomain(); - } else if (CosSysConfig::GetUseDnsCache() && !is_generate_presigned_url) { - dest_host = GetGlobalDnsCacheInstance().Resolve(host); - } - - dest_uri = dest_protocal + dest_host + CodecUtil::EncodeKey(dest_path); - SDK_LOG_DBG("dest_uri: %s", dest_uri.c_str()); - return dest_uri; + return m_op_util.GetRealUrl(host, path, is_https, is_generate_presigned_url); } bool BaseOp::CheckConfigValidation() const { diff --git a/src/op/bucket_op.cpp b/src/op/bucket_op.cpp index d568d02..54b4924 100644 --- a/src/op/bucket_op.cpp +++ b/src/op/bucket_op.cpp @@ -8,7 +8,6 @@ #include "op/bucket_op.h" #include "cos_defines.h" #include "util/codec_util.h" -#include "util/retry_util.h" namespace qcloud_cos { @@ -16,16 +15,7 @@ bool BucketOp::IsBucketExist(const std::string& bucket_name) { HeadBucketReq req(bucket_name); HeadBucketResp resp; CosResult result = HeadBucket(req, &resp); - - if (result.IsSucc()) { - return true; - } else if (UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = HeadBucket(req, &resp, COS_CHANGE_BACKUP_DOMAIN); - if (result.IsSucc()) { - return true; - } - } - return false; + return result.IsSucc(); } std::string BucketOp::GetBucketLocation(const std::string& bucket_name) { @@ -33,10 +23,6 @@ std::string BucketOp::GetBucketLocation(const std::string& bucket_name) { GetBucketLocationResp resp; CosResult result = GetBucketLocation(req, &resp); - if (result.IsSucc()) { - return resp.GetLocation(); - } - result = GetBucketLocation(req, &resp, COS_CHANGE_BACKUP_DOMAIN); if (result.IsSucc()) { return resp.GetLocation(); } @@ -593,33 +579,18 @@ CosResult BucketOp::ProcessReq(const BucketReq& req, BaseResp* resp, additional_headers.insert(std::make_pair("Content-MD5", raw_md5)); return NormalAction(host, path, req, additional_headers, additional_params, req_body, false, resp, is_ci_req); - } else { - return NormalAction(host, path, req, "", false, resp, is_ci_req); } + return NormalAction(host, path, req, "", false, resp, is_ci_req); + } + if (!req_body.empty()) { + std::string raw_md5 = CodecUtil::Base64Encode(CodecUtil::RawMd5(req_body)); + std::map additional_headers; + std::map additional_params; + additional_headers.insert(std::make_pair("Content-MD5", raw_md5)); + return NormalAction(host, path, req, additional_headers, additional_params, + req_body, false, resp, is_ci_req); } else { - if (!req_body.empty()) { - std::string raw_md5 = CodecUtil::Base64Encode(CodecUtil::RawMd5(req_body)); - std::map additional_headers; - std::map additional_params; - additional_headers.insert(std::make_pair("Content-MD5", raw_md5)); - CosResult result = NormalAction(host, path, req, additional_headers, additional_params, - req_body, false, resp, is_ci_req); - if(UseDefaultDomain() && (RetryUtil::ShouldRetryWithChangeDomain(result))) { - host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(), - req.GetBucketName(), COS_CHANGE_BACKUP_DOMAIN); - return NormalAction(host, path, req, additional_headers, additional_params, - req_body, false, resp, is_ci_req); - } - return result; - } else { - CosResult result = NormalAction(host, path, req, "", false, resp, is_ci_req); - if(UseDefaultDomain() && (RetryUtil::ShouldRetryWithChangeDomain(result))) { - host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(), - req.GetBucketName(), COS_CHANGE_BACKUP_DOMAIN); - return NormalAction(host, path, req, "", false, resp, is_ci_req); - } - return result; - } + return NormalAction(host, path, req, "", false, resp, is_ci_req); } } diff --git a/src/op/file_copy_task.cpp b/src/op/file_copy_task.cpp index 801b930..dff848e 100644 --- a/src/op/file_copy_task.cpp +++ b/src/op/file_copy_task.cpp @@ -4,13 +4,20 @@ #include "request/object_req.h" #include "response/object_resp.h" #include "util/http_sender.h" +#include "util/base_op_util.h" namespace qcloud_cos { -FileCopyTask::FileCopyTask(const std::string& full_url, +FileCopyTask::FileCopyTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms) - : m_full_url(full_url), + : m_host(host), + m_path(path), + m_is_https(is_https), + m_op_util(op_util), m_conn_timeout_in_ms(conn_timeout_in_ms), m_recv_timeout_in_ms(recv_timeout_in_ms), m_is_task_success(false), @@ -59,35 +66,53 @@ void FileCopyTask::run() { } void FileCopyTask::CopyTask() { - int loop = 0; - do { - loop++; + std::string domain = m_host; + for (int i = 0;; i++) { + SendRequestOnce(domain); + if (i >= m_op_util.GetMaxRetryTimes()) { + break; + } + if (m_is_task_success) { + break; + } + SDK_LOG_ERR("FileCopy: host(%s) path(%s) fail, retry num: %d, httpcode:%d, resp: %s", + domain.c_str(), m_path.c_str(), i, m_http_status, m_resp.c_str()); + if (m_http_status >= 400 && m_http_status < 500) { + break; + } + CosResult result; + result.SetHttpStatus(m_http_status); + result.ParseFromHttpResponse(m_resp_headers, m_resp); + if (m_op_util.ShouldChangeBackupDomain(result, i)) { + domain = m_op_util.ChangeHostSuffix(domain); + } + m_op_util.SleepBeforeRetry(i); + } +} + +void FileCopyTask::SendRequestOnce(std::string domain) { m_resp_headers.clear(); m_resp = ""; - m_http_status = HttpSender::SendRequest(nullptr, - "PUT", m_full_url, m_params, m_headers, "", m_conn_timeout_in_ms, - m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg, - false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, m_user_data); + std::string full_url = m_op_util.GetRealUrl(domain, m_path, m_is_https); + m_http_status = HttpSender::SendRequest(nullptr, "PUT", full_url, m_params, m_headers, "", m_conn_timeout_in_ms, + m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, + m_user_data); if (m_http_status != 200) { - SDK_LOG_ERR("FileUpload: url(%s) fail, httpcode:%d, resp: %s", - m_full_url.c_str(), m_http_status, m_resp.c_str()); - m_is_task_success = false; - continue; + m_is_task_success = false; + return; } UploadPartCopyDataResp resp; if (!resp.ParseFromXmlString(m_resp)) { - SDK_LOG_ERR("FileUpload response string is illegal. try again."); - m_is_task_success = false; - continue; + m_is_task_success = false; + return; } m_etag = resp.GetEtag(); m_last_modified = resp.GetLastModified(); m_is_task_success = true; - } while (!m_is_task_success && loop <= kMaxRetryTimes); } } // namespace qcloud_cos diff --git a/src/op/file_download_task.cpp b/src/op/file_download_task.cpp index d443303..ddc5c72 100644 --- a/src/op/file_download_task.cpp +++ b/src/op/file_download_task.cpp @@ -5,10 +5,14 @@ #include #include "cos_sys_config.h" #include "util/http_sender.h" +#include "util/base_op_util.h" namespace qcloud_cos { -FileDownTask::FileDownTask(const std::string& full_url, +FileDownTask::FileDownTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, const std::map& headers, const std::map& params, uint64_t conn_timeout_in_ms, @@ -20,7 +24,10 @@ FileDownTask::FileDownTask(const std::string& full_url, const std::string& ca_lication, SSLCtxCallback ssl_ctx_cb, void *user_data) - : m_full_url(full_url), + : m_host(host), + m_path(path), + m_is_https(is_https), + m_op_util(op_util), m_headers(headers), m_params(params), m_conn_timeout_in_ms(conn_timeout_in_ms), @@ -76,39 +83,49 @@ std::map FileDownTask::GetRespHeaders() { } void FileDownTask::DownTask() { - char range_head[128]; - memset(range_head, 0, sizeof(range_head)); - snprintf(range_head, sizeof(range_head), "bytes=%" PRIu64 "-%" PRIu64, m_offset, - (m_offset + m_data_len - 1)); - - // 增加Range头域,避免大文件时将整个文件下载 - m_headers["Range"] = range_head; - - int try_times = 0; - do { - try_times++; - //if (m_handler) { - // SDK_LOG_INFO("transfer send GET request"); - // std::istringstream iss(""); - // std::ostringstream oss; - // m_http_status = HttpSender::TransferSendRequest( - // m_handler, "GET", m_full_url, m_params, m_headers, iss, - // m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, oss, - // &m_err_msg, false); - // m_resp = oss.str(); - //} else { - m_http_status = HttpSender::SendRequest( - m_handler, "GET", m_full_url, m_params, m_headers, "", - m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, &m_resp, - &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, m_user_data); + char range_head[128]; + memset(range_head, 0, sizeof(range_head)); + snprintf(range_head, sizeof(range_head), "bytes=%" PRIu64 "-%" PRIu64, m_offset, (m_offset + m_data_len - 1)); + + // 增加Range头域,避免大文件时将整个文件下载 + m_headers["Range"] = range_head; + + std::string domain = m_host; + for (int i = 0;; i++) { + SendRequestOnce(domain); + if (i >= m_op_util.GetMaxRetryTimes()) { + break; + } + if (m_is_task_success) { + break; + } + SDK_LOG_ERR("FileDownload: host(%s) path(%s) fail, httpcode:%d, resp: %s, try_times: %d", domain.c_str(), + m_path.c_str(), m_http_status, m_resp.c_str(), i); + if (m_http_status >= 400 && m_http_status < 500) { + break; + } + CosResult result; + result.SetHttpStatus(m_http_status); + result.ParseFromHttpResponse(m_resp_headers, m_resp); + if (m_op_util.ShouldChangeBackupDomain(result, i)) { + domain = m_op_util.ChangeHostSuffix(domain); + } + m_op_util.SleepBeforeRetry(i); + } + return; +} + +void FileDownTask::SendRequestOnce(std::string domain) { + std::string full_url = m_op_util.GetRealUrl(domain, m_path, m_is_https); + m_http_status = HttpSender::SendRequest(m_handler, "GET", full_url, m_params, m_headers, "", m_conn_timeout_in_ms, + m_recv_timeout_in_ms, &m_resp_headers, &m_resp, &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, + m_user_data); //} - //当实际长度小于请求的数据长度时httpcode为206 + // 当实际长度小于请求的数据长度时httpcode为206 if (m_http_status != 200 && m_http_status != 206) { - SDK_LOG_ERR("FileDownload: url(%s) fail, httpcode:%d, resp: %s, try_times:%d", - m_full_url.c_str(), m_http_status, m_resp.c_str(), try_times); - m_is_task_success = false; - m_real_down_len = 0; - continue; + m_is_task_success = false; + m_real_down_len = 0; + return; } size_t buf_max_size = m_data_len; @@ -117,10 +134,6 @@ void FileDownTask::DownTask() { m_real_down_len = len; m_is_task_success = true; m_resp = ""; - return; - } while (!m_is_task_success && try_times <= kMaxRetryTimes); - - return; } } // namespace qcloud_cos diff --git a/src/op/file_upload_task.cpp b/src/op/file_upload_task.cpp index 642df46..b3efc9a 100644 --- a/src/op/file_upload_task.cpp +++ b/src/op/file_upload_task.cpp @@ -7,13 +7,17 @@ #include "util/string_util.h" #include "util/codec_util.h" #include "util/crc64.h" +#include "util/base_op_util.h" #ifdef USE_OPENSSL_MD5 #include #endif namespace qcloud_cos { -FileUploadTask::FileUploadTask(const std::string& full_url, +FileUploadTask::FileUploadTask(const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, unsigned char* pbuf, const size_t data_len, @@ -21,7 +25,10 @@ FileUploadTask::FileUploadTask(const std::string& full_url, const std::string& ca_location, SSLCtxCallback ssl_ctx_cb, void *user_data) - : m_full_url(full_url), + : m_host(host), + m_path(path), + m_is_https(is_https), + m_op_util(op_util), m_conn_timeout_in_ms(conn_timeout_in_ms), m_recv_timeout_in_ms(recv_timeout_in_ms), m_data_buf_ptr(pbuf), @@ -37,8 +44,12 @@ FileUploadTask::FileUploadTask(const std::string& full_url, mb_check_crc64(false), m_crc64_value(0) {} + FileUploadTask::FileUploadTask( - const std::string& full_url, + const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, const std::map& headers, const std::map& params, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, @@ -47,7 +58,10 @@ FileUploadTask::FileUploadTask( const std::string& ca_location, SSLCtxCallback ssl_ctx_cb, void *user_data) - : m_full_url(full_url), + : m_host(host), + m_path(path), + m_is_https(is_https), + m_op_util(op_util), m_headers(headers), m_params(params), m_conn_timeout_in_ms(conn_timeout_in_ms), @@ -65,8 +79,12 @@ FileUploadTask::FileUploadTask( mb_check_crc64(false), m_crc64_value(0) {} + FileUploadTask::FileUploadTask( - const std::string& full_url, + const std::string& host, + const std::string& path, + const bool is_https, + const BaseOpUtil& op_util, const std::map& headers, const std::map& params, uint64_t conn_timeout_in_ms, uint64_t recv_timeout_in_ms, @@ -75,7 +93,10 @@ FileUploadTask::FileUploadTask( const std::string& ca_location, SSLCtxCallback ssl_ctx_cb, void *user_data) - : m_full_url(full_url), + : m_host(host), + m_path(path), + m_is_https(is_https), + m_op_util(op_util), m_headers(headers), m_params(params), m_conn_timeout_in_ms(conn_timeout_in_ms), @@ -186,71 +207,81 @@ void FileUploadTask::UploadTask() { SDK_LOG_DBG("Part Md5: %s", md5_str.c_str()); } - int loop = 0; - do { - loop++; - m_resp_headers.clear(); - m_resp.clear(); + std::string domain = m_host; + for (int i = 0;; i++) { + SendRequestOnce(domain, md5_str); + if (i >= m_op_util.GetMaxRetryTimes()) { + break; + } + if (m_is_task_success) { + break; + } + SDK_LOG_ERR("FileUpload: host(%s) path(%s) fail, httpcode:%d, resp: %s", + domain.c_str(), m_path.c_str(), m_http_status, m_resp.c_str()); + if (m_http_status >= 400 && m_http_status < 500) { + break; + } + CosResult result; + result.SetHttpStatus(m_http_status); + result.ParseFromHttpResponse(m_resp_headers, m_resp); + if (m_op_util.ShouldChangeBackupDomain(result, i)) { + domain = m_op_util.ChangeHostSuffix(domain); + } + m_op_util.SleepBeforeRetry(i); + } - // if (m_handler) { - // SDK_LOG_INFO("transfer send request"); - // std::istringstream iss(body); - // std::ostringstream oss; - // m_http_status = HttpSender::TransferSendRequest( - // m_handler, "PUT", m_full_url, m_params, m_headers, iss, - // m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, oss, - // &m_err_msg, false); - // m_resp = oss.str(); - // } else { - std::istringstream is; - std::ostringstream oss; - m_http_status = HttpSender::SendRequest( - m_handler, "PUT", m_full_url, m_params, m_headers, is, - m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, oss, - &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, m_user_data, - (const char*)m_data_buf_ptr, m_data_len); - //} - m_resp = oss.str(); + return; +} - if (m_http_status != 200) { - SDK_LOG_ERR("FileUpload: url(%s) fail, httpcode:%d, resp: %s", - m_full_url.c_str(), m_http_status, m_resp.c_str()); - m_is_task_success = false; - continue; - } +void FileUploadTask::SendRequestOnce(std::string domain, std::string md5_str) { + m_resp_headers.clear(); + m_resp.clear(); - // crc64一致性校验 - if (mb_check_crc64) { - std::map::const_iterator c_itr = - m_resp_headers.find(kRespHeaderXCosHashCrc64Ecma); - if (c_itr == m_resp_headers.end() || - StringUtil::StringToUint64(c_itr->second) != m_crc64_value) { - SDK_LOG_ERR( - "Response x-cos-hash-crc64ecma is not correct, try again. Expect crc64 is %" PRIu64 ", but " - "return crc64 is %s", - m_crc64_value, c_itr->second.c_str()); - m_is_task_success = false; - continue; - } - SDK_LOG_DBG("Part Crc64 Check Success."); - } else { - std::map::const_iterator c_itr = - m_resp_headers.find("ETag"); - if (c_itr == m_resp_headers.end() || - StringUtil::Trim(c_itr->second, "\"") != md5_str) { - SDK_LOG_ERR( - "Response etag is not correct, try again. Expect md5 is %s, but " - "return etag is %s.", - md5_str.c_str(), StringUtil::Trim(c_itr->second, "\"").c_str()); - m_is_task_success = false; - continue; - } - SDK_LOG_DBG("Part Md5 Check Success."); - } + std::istringstream is; + std::ostringstream oss; + std::string url = m_op_util.GetRealUrl(domain, m_path, m_is_https); + m_http_status = HttpSender::SendRequest( + m_handler, "PUT", url, m_params, m_headers, is, + m_conn_timeout_in_ms, m_recv_timeout_in_ms, &m_resp_headers, oss, + &m_err_msg, false, m_verify_cert, m_ca_location, m_ssl_ctx_cb, m_user_data, + (const char*)m_data_buf_ptr, m_data_len); + m_resp = oss.str(); - m_is_task_success = true; - } while (!m_is_task_success && loop <= kMaxRetryTimes); + if (m_http_status != 200) { + SDK_LOG_ERR("FileUpload: url(%s) fail, httpcode:%d, resp: %s", + m_host.c_str(), m_http_status, m_resp.c_str()); + m_is_task_success = false; + return; + } - return; + // crc64一致性校验 + if (mb_check_crc64) { + std::map::const_iterator c_itr = + m_resp_headers.find(kRespHeaderXCosHashCrc64Ecma); + if (c_itr == m_resp_headers.end() || + StringUtil::StringToUint64(c_itr->second) != m_crc64_value) { + SDK_LOG_ERR( + "Response x-cos-hash-crc64ecma is not correct, try again. Expect crc64 is %" PRIu64 ", but " + "return crc64 is %s", + m_crc64_value, c_itr->second.c_str()); + m_is_task_success = false; + return; + } + SDK_LOG_DBG("Part Crc64 Check Success."); + } else { + std::map::const_iterator c_itr = + m_resp_headers.find("ETag"); + if (c_itr == m_resp_headers.end() || + StringUtil::Trim(c_itr->second, "\"") != md5_str) { + SDK_LOG_ERR( + "Response etag is not correct, try again. Expect md5 is %s, but " + "return etag is %s.", + md5_str.c_str(), StringUtil::Trim(c_itr->second, "\"").c_str()); + m_is_task_success = false; + return; + } + SDK_LOG_DBG("Part Md5 Check Success."); + } + m_is_task_success = true; } } // namespace qcloud_cos diff --git a/src/op/object_op.cpp b/src/op/object_op.cpp index 76e6b86..6fc63f2 100644 --- a/src/op/object_op.cpp +++ b/src/op/object_op.cpp @@ -21,8 +21,6 @@ #endif #include "Poco/DigestStream.h" -#include "Poco/DirectoryIterator.h" -#include "Poco/FileStream.h" #include "Poco/JSON/Parser.h" #include "Poco/MD5Engine.h" #include "Poco/RecursiveDirectoryIterator.h" @@ -40,9 +38,7 @@ #include "util/codec_util.h" #include "util/crc64.h" #include "util/file_util.h" -#include "util/http_sender.h" #include "util/string_util.h" -#include "util/retry_util.h" #include "util/illegal_intercept.h" namespace qcloud_cos { @@ -52,16 +48,7 @@ bool ObjectOp::IsObjectExist(const std::string& bucket_name, HeadObjectReq req(bucket_name, object_name); HeadObjectResp resp; CosResult result = HeadObject(req, &resp); - if (result.IsSucc()) { - return true; - } else if (UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = HeadObject(req, &resp, COS_CHANGE_BACKUP_DOMAIN); - if (result.IsSucc()) { - return true; - } - } - - return false; + return result.IsSucc(); } std::string ObjectOp::GetResumableUploadID(const PutObjectByFileReq& originReq, @@ -407,12 +394,10 @@ CosResult ObjectOp::GetObject(const GetObjectByFileReq& req, result = DownloadAction(host, path, req, resp, ofs, handler); ofs.close(); - if (result.IsSucc() && handler) { - handler->UpdateStatus(TransferStatus::COMPLETED, result, resp->GetHeaders(), - resp->GetBody()); - } else if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - handler->UpdateStatus(TransferStatus::RETRY, result); + if (handler) { + if (result.IsSucc()) { + handler->UpdateStatus(TransferStatus::COMPLETED, result, resp->GetHeaders(), + resp->GetBody()); } else { handler->UpdateStatus(TransferStatus::FAILED, result); } @@ -429,11 +414,7 @@ CosResult ObjectOp::MultiGetObject(const GetObjectByFileReq& req, result.SetFail(); return result; } - CosResult result = MultiThreadDownload(req, resp); - if(UseDefaultDomain() && (RetryUtil::ShouldRetryWithChangeDomain(result))) { - result = MultiThreadDownload(req, resp, nullptr , COS_CHANGE_BACKUP_DOMAIN); - } - return result; + return MultiThreadDownload(req, resp); } CosResult ObjectOp::PutObject(const PutObjectByStreamReq& req, @@ -490,11 +471,7 @@ CosResult ObjectOp::PutObject(const PutObjectByStreamReq& req, handler->UpdateStatus(TransferStatus::COMPLETED, result, resp->GetHeaders(), resp->GetBody()); } else if(handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - handler->UpdateStatus(TransferStatus::RETRY, result); - } else { - handler->UpdateStatus(TransferStatus::FAILED, result); - } + handler->UpdateStatus(TransferStatus::FAILED, result); } return result; } @@ -562,11 +539,7 @@ CosResult ObjectOp::PutObject(const PutObjectByFileReq& req, handler->UpdateStatus(TransferStatus::COMPLETED, result, resp->GetHeaders(), resp->GetBody()); } else if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - handler->UpdateStatus(TransferStatus::RETRY, result); - } else { - handler->UpdateStatus(TransferStatus::FAILED, result); - } + handler->UpdateStatus(TransferStatus::FAILED, result); } return result; } @@ -630,31 +603,6 @@ CosResult ObjectOp::MultiUploadObject(const PutObjectByFileReq& req, if (!resume_flag) { // 1. Init InitMultiUploadReq init_req(bucket_name, object_name); - /* - const std::string& server_side_encryption = - req.GetHeader("x-cos-server-side-encryption"); - if (!server_side_encryption.empty()) { - init_req.SetXCosServerSideEncryption(server_side_encryption); - } - - const std::string& storage_class = req.GetHeader("x-cos-storage-class"); - if (!storage_class.empty()) { - init_req.SetXCosStorageClass(storage_class); - } - const std::string& acl = req.GetHeader("x-cos-acl"); - if (!acl.empty()) { - init_req.SetXCosAcl(acl); - } - - if (req.IsSetXCosMeta()) { - const std::map xcos_meta = req.GetXCosMeta(); - std::map::const_iterator iter = - xcos_meta.begin(); - for (; iter != xcos_meta.end(); iter++) { - init_req.SetXCosMeta(iter->first, iter->second); - } - } - */ CosResult init_result; InitMultiUploadResp init_resp; @@ -672,11 +620,7 @@ CosResult ObjectOp::MultiUploadObject(const PutObjectByFileReq& req, std::string err_msg = "Init multipart upload failed"; SetResultAndLogError(init_result, err_msg); if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(init_result)) { - handler->UpdateStatus(TransferStatus::RETRY, init_result); - } else { - handler->UpdateStatus(TransferStatus::FAILED, init_result); - } + handler->UpdateStatus(TransferStatus::FAILED, init_result); } return init_result; } @@ -718,25 +662,8 @@ CosResult ObjectOp::MultiUploadObject(const PutObjectByFileReq& req, // Notice the cancel way not need to abort the uploadid if (!upload_result.IsSucc()) { - // 失败了不abort,再次上传走断点续传 - // When copy failed need abort. - // AbortMultiUploadReq abort_req(req.GetBucketName(), - // req.GetObjectName(), resume_uploadid); - // AbortMultiUploadResp abort_resp; - // CosResult abort_result = AbortMultiUpload(abort_req, &abort_resp); - // if (!abort_result.IsSucc()) { - // SDK_LOG_ERR("Upload failed, and abort muliti upload also failed" - // ", resume_uploadid=%s", resume_uploadid.c_str()); - // handler->m_result = abort_result; - // handler->UpdateStatus(TransferStatus::FAILED); - // return abort_result; - // } if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(upload_result)) { - handler->UpdateStatus(TransferStatus::RETRY, upload_result); - } else { - handler->UpdateStatus(TransferStatus::FAILED, upload_result); - } + handler->UpdateStatus(TransferStatus::FAILED, upload_result); } return upload_result; } @@ -782,11 +709,7 @@ CosResult ObjectOp::MultiUploadObject(const PutObjectByFileReq& req, } } else { if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(comp_result)) { - handler->UpdateStatus(TransferStatus::RETRY); - } else { - handler->UpdateStatus(TransferStatus::FAILED); - } + handler->UpdateStatus(TransferStatus::FAILED); } } @@ -1181,22 +1104,18 @@ CosResult ObjectOp::Copy(const CopyReq& req, CopyResp* resp, bool change_backup_ return result; } - // 以"/"分割的copy_source第一段就是host + // 查询源对象数据长度, 以"/"分割的copy_source第一段就是host HeadObjectReq head_req(src_bucket_appid, src_obj); head_req.SetConnTimeoutInms(req.GetConnTimeoutInms()); head_req.SetRecvTimeoutInms(req.GetRecvTimeoutInms()); HeadObjectResp head_resp; std::string host = v[0]; - if (change_backup_domain && IsDefaultHost(host)) { - host = ChangeHostSuffix(host); - } std::string path = head_req.GetPath(); result = NormalAction(host, path, head_req, "", false, &head_resp); if (!result.IsSucc()) { - SDK_LOG_ERR("Get object length before download object fail, req=[%s]", - req.DebugString().c_str()); - result.SetErrorMsg("Copy fail, can't get source object length."); - return result; + SDK_LOG_ERR("Get object length before download object fail, req=[%s]", head_req.DebugString().c_str()); + result.SetErrorMsg("Copy fail, can't get source object length."); + return result; } uint64_t file_size = head_resp.GetContentLength(); @@ -1265,10 +1184,9 @@ CosResult ObjectOp::Copy(const CopyReq& req, CopyResp* resp, bool change_backup_ std::string path = "/" + req.GetObjectName(); std::string host = CosSysConfig::GetHost(GetAppId(), m_config->GetRegion(), req.GetBucketName(), change_backup_domain); - std::string dest_url = GetRealUrl(host, path, req.IsHttps()); FileCopyTask** pptaskArr = new FileCopyTask*[pool_size]; for (unsigned i = 0; i < pool_size; ++i) { - pptaskArr[i] = new FileCopyTask(dest_url, req.GetConnTimeoutInms(), + pptaskArr[i] = new FileCopyTask(host, path, req.IsHttps(), m_op_util, req.GetConnTimeoutInms(), req.GetRecvTimeoutInms()); } @@ -1446,11 +1364,7 @@ ObjectOp::MultiThreadDownload(const GetObjectByFileReq& req, SetResultAndLogError( head_result, "failed to get object length before downloading object."); if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - handler->UpdateStatus(TransferStatus::RETRY, head_result); - } else { - handler->UpdateStatus(TransferStatus::FAILED, head_result); - } + handler->UpdateStatus(TransferStatus::FAILED, head_result); } return head_result; } @@ -1530,17 +1444,15 @@ ObjectOp::MultiThreadDownload(const GetObjectByFileReq& req, file_content_buf[i] = new unsigned char[slice_size]; } - std::string dest_url = GetRealUrl(host, path, req.IsHttps()); FileDownTask** pptaskArr = new FileDownTask*[pool_size]; for (unsigned i = 0; i < pool_size; ++i) { pptaskArr[i] = - new FileDownTask(dest_url, headers, params, req.GetConnTimeoutInms(), + new FileDownTask(host, path, req.IsHttps(), m_op_util, headers, params, req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), handler); } - SDK_LOG_INFO( - "download data,url=%s, poolsize=%u, slice_size=%u, file_size=%" PRIu64, - dest_url.c_str(), pool_size, slice_size, file_size); + SDK_LOG_INFO("download data,host=%s, path=%s, poolsize=%u, slice_size=%u, file_size=%" PRIu64, host.c_str(), + path.c_str(), pool_size, slice_size, file_size); std::vector vec_offset; vec_offset.resize(pool_size); @@ -1772,15 +1684,15 @@ CosResult ObjectOp::MultiThreadUpload( FileUploadTask** pptaskArr = new FileUploadTask*[pool_size]; for (int i = 0; i < pool_size; ++i) { pptaskArr[i] = - new FileUploadTask(dest_url, headers, params, req.GetConnTimeoutInms(), + new FileUploadTask(host, path, req.IsHttps(), m_op_util, headers, params, req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), handler, req.GetVerifyCert(), req.GetCaLocation(), req.GetSSLCtxCallback(), req.GetSSLCtxCbData()); } - SDK_LOG_DBG("upload data, url=%s, poolsize=%u, part_size=%" PRIu64 + SDK_LOG_DBG("upload data, host=%s, path=%s, poolsize=%u, part_size=%" PRIu64 ", file_size=%" PRIu64, - dest_url.c_str(), pool_size, part_size, file_size); + host.c_str(), path.c_str(), part_size, file_size); Poco::ThreadPool tp(pool_size); @@ -1879,15 +1791,15 @@ CosResult ObjectOp::MultiThreadUpload( if (req.CheckCRC64()) { // 如果已经计算了part的crc64值,只需要直接流式合并即可 if (ptask->GetCrc64Value() != 0) { - crc64_file = CRC64::CombineCRC(crc64_file, ptask->GetCrc64Value(), + crc64_file = CRC64::CombineCRC(crc64_file, ptask->GetCrc64Value(), static_cast(part_buf_info[task_index].len)); - SDK_LOG_DBG("Combine Crc64: %" PRIu64 ", Part Crc64: %" PRIu64, + SDK_LOG_DBG("Combine Crc64: %" PRIu64 ", Part Crc64: %" PRIu64, crc64_file, ptask->GetCrc64Value()); } else { // 两种情况都有可能: // 1、CheckPartCrc64()为false // 2、此part是断点续传已经上传的part - crc64_file = CRC64::CalcCRC(crc64_file, static_cast(part_buf_info[task_index].buf), + crc64_file = CRC64::CalcCRC(crc64_file, static_cast(part_buf_info[task_index].buf), part_buf_info[task_index].len); SDK_LOG_DBG("Calc Crc64: %" PRIu64, crc64_file) } @@ -2098,7 +2010,7 @@ uint64_t ObjectOp::GetContent(const std::string& src, void ObjectOp::FillUploadTask(const std::string& upload_id, const std::string& host, const std::string& path, unsigned char* file_content_buf, uint64_t len, - uint64_t part_number, FileUploadTask* task_ptr, + uint64_t part_number, FileUploadTask* task_ptr, bool sign_header_host, bool check_crc64) { std::map req_params; req_params.insert(std::make_pair("uploadId", upload_id)); @@ -2264,15 +2176,8 @@ CosResult ObjectOp::SelectObjectContent(const SelectObjectContentReq& req, CosResult ObjectOp::AppendObject(const AppendObjectReq& req, AppendObjectResp* resp) { - CosResult result = PutObject(static_cast(req), + return PutObject(static_cast(req), static_cast(resp)); - if (UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - result = PutObject(static_cast(req), - static_cast(resp), - nullptr, - COS_CHANGE_BACKUP_DOMAIN); - } - return result; } CosResult ObjectOp::PutLiveChannel(const PutLiveChannelReq& req, @@ -2410,11 +2315,7 @@ CosResult ObjectOp::ResumableGetObject(const GetObjectByFileReq& req, SetResultAndLogError( head_result, "failed to get object length before downloading object."); if (handler) { - if(!change_backup_domain && UseDefaultDomain() && RetryUtil::ShouldRetryWithChangeDomain(result)) { - handler->UpdateStatus(TransferStatus::RETRY, head_result); - } else { - handler->UpdateStatus(TransferStatus::FAILED, head_result); - } + handler->UpdateStatus(TransferStatus::FAILED, head_result); } return head_result; } @@ -2546,18 +2447,15 @@ CosResult ObjectOp::ResumableGetObject(const GetObjectByFileReq& req, file_content_buf[i] = new unsigned char[slice_size]; } - std::string dest_url = GetRealUrl(host, path, req.IsHttps()); FileDownTask** pptaskArr = new FileDownTask*[pool_size]; for (unsigned i = 0; i < pool_size; ++i) { pptaskArr[i] = - new FileDownTask(dest_url, headers, params, req.GetConnTimeoutInms(), + new FileDownTask(host, path, req.IsHttps(), m_op_util, headers, params, req.GetConnTimeoutInms(), req.GetRecvTimeoutInms(), handler); } - SDK_LOG_INFO( - "download data,url=%s, poolsize=%u, slice_size=%u, file_size=%" PRIu64 - ", last_offset=%" PRIu64, - dest_url.c_str(), pool_size, slice_size, file_size, resume_offset); + SDK_LOG_INFO("download data,host=%s, path=%s, poolsize=%u, slice_size=%u, file_size=%" PRIu64 + ", last_offset=%" PRIu64, host.c_str(), path.c_str(), pool_size, slice_size, file_size, resume_offset); std::vector vec_offset; vec_offset.resize(pool_size); @@ -2857,7 +2755,7 @@ CosResult ObjectOp::MoveObject(const MoveObjectReq& req, bool change_backup_doma // copy to dst object copy_result = Copy(copy_req, ©_resp, change_backup_domain); if (!copy_result.IsSucc()) { - SDK_LOG_ERR("failed to copy object from: %s to :%s", + SDK_LOG_ERR("failed to copy object from: %s to %s", req.GetSrcObjectName().c_str(), req.GetDstObjectName().c_str()); return copy_result; } diff --git a/src/util/base_op_util.cpp b/src/util/base_op_util.cpp new file mode 100644 index 0000000..044a070 --- /dev/null +++ b/src/util/base_op_util.cpp @@ -0,0 +1,113 @@ + +#include "util/base_op_util.h" +#include "cos_sys_config.h" +#include "util/codec_util.h" +#include "util/simple_dns_cache.h" +#include + +namespace qcloud_cos { +SimpleDnsCache& GetGlobalDnsCacheInstance() { + static SimpleDnsCache dns_cache(CosSysConfig::GetDnsCacheSize(), + CosSysConfig::GetDnsCacheExpireSeconds()); + return dns_cache; +} + +bool BaseOpUtil::ShouldChangeBackupDomain(const CosResult &result, const uint32_t &request_num, const bool is_ci_req) const { + if (is_ci_req) { + // 请求到万象的, 不切域名 + return false; + } + if (!CosSysConfig::GetRetryChangeDomain()) { + // 没开启开关, 不切域名 + return false; + } + if (!UseDefaultDomain()) { + // 未使用默认域名, 不切域名 + return false; + } + if (!result.GetXCosRequestId().empty()) { + // 响应中有x-cos-request-id, 说明请求到了 COS, 不切域名 + return false; + } + const int statusCode = result.GetHttpStatus(); + if (statusCode == 301 || statusCode == 302 || statusCode == 307) { + // 3xx 的响应码只要满足切换条件就切换域名, 不需要等最后一次重试 + return true; + } + if (request_num + 1 < m_config->GetMaxRetryTimes()) { + // 还没到最大重试次数, 不切域名 + return false; + } + return statusCode >= 500; +} + +bool BaseOpUtil::UseDefaultDomain() const { + if (m_config && m_config->GetSetIntranetOnce() && m_config->IsUseIntranet() && !m_config->GetIntranetAddr().empty() + || CosSysConfig::IsUseIntranet() && !CosSysConfig::GetIntranetAddr().empty() + || (!m_config->GetDestDomain().empty()) + || !CosSysConfig::GetDestDomain().empty() + || m_config->GetRegion() == "accelerate") { + return false; + } + return true; +} + +std::string BaseOpUtil::GetRealUrl(const std::string& host, const std::string& path, + bool is_https, bool is_generate_presigned_url) const { + // 1. host优先级,私有ip > 自定义域名 > DNS cache > 默认域名 + std::string dest_uri; + std::string dest_host = host; + std::string dest_path = path; + std::string dest_protocal = "http://"; // NOCA:HttpHardcoded(ignore) + if (is_https) { + dest_protocal = "https://"; + } + + if (dest_path.empty() || '/' != dest_path[0]) { + dest_path = "/" + dest_path; + } + + if (m_config && + m_config->GetSetIntranetOnce() && + m_config->IsUseIntranet() && + !m_config->GetIntranetAddr().empty() && !is_generate_presigned_url) { + dest_host = m_config->GetIntranetAddr(); + } else if (CosSysConfig::IsUseIntranet() && + !CosSysConfig::GetIntranetAddr().empty() && !is_generate_presigned_url) { + dest_host = CosSysConfig::GetIntranetAddr(); + } else if (m_config && + (!m_config->GetDestDomain().empty())) { + dest_host = m_config->GetDestDomain(); + } else if (!CosSysConfig::GetDestDomain().empty()) { + dest_host = CosSysConfig::GetDestDomain(); + } else if (CosSysConfig::GetUseDnsCache() && !is_generate_presigned_url) { + dest_host = GetGlobalDnsCacheInstance().Resolve(host); + } + + dest_uri = dest_protocal + dest_host + CodecUtil::EncodeKey(dest_path); + SDK_LOG_DBG("dest_uri: %s", dest_uri.c_str()); + return dest_uri; +} + +uint64_t BaseOpUtil::GetMaxRetryTimes() const +{ + return m_config->GetMaxRetryTimes(); +} + +void BaseOpUtil::SleepBeforeRetry(const uint32_t& request_num) const { + const uint32_t interval_ms = m_config->GetRetryIntervalMs() * (request_num + 1); + std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms)); +} + +std::string BaseOpUtil::ChangeHostSuffix(const std::string& host) { + const std::string old_suffix = ".myqcloud.com"; + const std::string new_suffix = ".tencentcos.cn"; + + const size_t suffix_pos = host.rfind(old_suffix); + if (suffix_pos != std::string::npos) { + std::string new_host = host.substr(0, suffix_pos) + new_suffix; + return new_host; + } + return host; +} +} // namespace qcloud_cos \ No newline at end of file diff --git a/src/util/codec_util.cpp b/src/util/codec_util.cpp index f32975f..3699661 100644 --- a/src/util/codec_util.cpp +++ b/src/util/codec_util.cpp @@ -140,13 +140,11 @@ std::string CodecUtil::HexToBin(const std::string& strHex) { return strBin; } // end of HexToBin -std::string CodecUtil::DigestToHex(const unsigned char *digest, size_t len) -{ +std::string CodecUtil::DigestToHex(const unsigned char *digest, size_t len) { static const char digits[] = "0123456789abcdef"; std::string result; result.reserve(len * 2); - for (size_t pos = 0; pos < len; pos++) - { + for (size_t pos = 0; pos < len; pos++) { unsigned char c = digest[pos]; result += digits[(c >> 4) & 0xF]; result += digits[c & 0xF]; diff --git a/src/util/http_sender.cpp b/src/util/http_sender.cpp index e8302e2..2de7d78 100644 --- a/src/util/http_sender.cpp +++ b/src/util/http_sender.cpp @@ -50,25 +50,6 @@ int HttpSender::SendRequest( return ret; } -int HttpSender::SendRequest( - const SharedTransferHandler& handler, const std::string& http_method, - const std::string& url_str, - const std::map& req_params, - const std::map& req_headers, - const std::string& req_body, uint64_t conn_timeout_in_ms, - uint64_t recv_timeout_in_ms, - std::map* resp_headers, std::ostream& resp_stream, - std::string* err_msg, bool is_check_md5, - bool is_verify_cert, const std::string& ca_location, - SSLCtxCallback ssl_ctx_cb, void *user_data) { - std::istringstream is(req_body); - int ret = SendRequest(handler, http_method, url_str, req_params, req_headers, - is, conn_timeout_in_ms, recv_timeout_in_ms, - resp_headers, resp_stream, err_msg, is_check_md5, - is_verify_cert, ca_location, ssl_ctx_cb, user_data); - return ret; -} - int HttpSender::SendRequest( const SharedTransferHandler& handler, const std::string& http_method, const std::string& url_str, diff --git a/src/util/retry_util.cpp b/src/util/retry_util.cpp deleted file mode 100644 index 1423fd0..0000000 --- a/src/util/retry_util.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "util/retry_util.h" -#include "cos_sys_config.h" - -namespace qcloud_cos { - - -bool RetryUtil::ShouldRetryWithChangeDomain(CosResult result) { - if ((result.GetHttpStatus()/100 != 2 && result.GetXCosRequestId().empty()) && CosSysConfig::GetRetryChangeDomain()) { - return true; - } - return false; -} - -} // namespace qcloud_cos \ No newline at end of file diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 8c7d183..1db7caf 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -6,6 +6,7 @@ file(GLOB object_response_test_src src/object_response_test.cpp) file(GLOB bucket_response_test_src src/bucket_response_test.cpp) file(GLOB util_test_src src/util_test.cpp) file(GLOB object_op_test_src src/object_op_test.cpp) +file(GLOB object_op_retry_test_src src/object_op_retry_test.cpp) file(GLOB bucket_op_test_src src/bucket_op_test.cpp) file(GLOB live_channel_test_src src/live_channel_test.cpp) file(GLOB async_op_test_src src/async_op_test.cpp) @@ -29,6 +30,9 @@ target_link_libraries(util-test cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIB add_executable(object-op-test ${object_op_test_src} ${common_src}) target_link_libraries(object-op-test cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS} ${GTEST_LIBS}) +add_executable(object-op-retry-test ${object_op_retry_test_src} ${common_src}) +target_link_libraries(object-op-retry-test cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS} ${GTEST_LIBS}) + add_executable(bucket-op-test ${bucket_op_test_src} ${common_src}) target_link_libraries(bucket-op-test cossdk ${POCO_LIBS} ${OPENSSL_LIBS} ${SYSTEM_LIBS} ${GTEST_LIBS}) @@ -50,6 +54,7 @@ add_executable(all-test ${bucket_response_test_src} ${util_test_src} ${object_op_test_src} + ${object_op_retry_test_src} ${bucket_op_test_src} ${live_channel_test_src} ${async_op_test_src} diff --git a/unittest/conf/config.json b/unittest/conf/config.json index 0764723..40a002a 100644 --- a/unittest/conf/config.json +++ b/unittest/conf/config.json @@ -17,5 +17,7 @@ "IsDomainSameToHost":false, "DestDomain":"", "IsUseIntranet":false, - "IntranetAddr":"" + "IntranetAddr":"", + "RetryIntervalMs":100, + "MaxRetryTimes": 3 } diff --git a/unittest/src/object_op_retry_test.cpp b/unittest/src/object_op_retry_test.cpp new file mode 100644 index 0000000..28bc666 --- /dev/null +++ b/unittest/src/object_op_retry_test.cpp @@ -0,0 +1,764 @@ + +#include + +#include +#include "Poco/MD5Engine.h" +#include "Poco/StreamCopier.h" +#include "cos_api.h" +#include "util/test_utils.h" +#include "util/file_util.h" +#include "util/simple_dns_cache.h" +#include "gtest/gtest.h" +#include "op/object_op.h" + + +namespace qcloud_cos { +class ObjectOpRetryTest : public ::testing::Test { + protected: + static void SetUpTestCase() { + m_client_without_retry= createCosClient(0); + m_client_with_retry= createCosClient(3); + m_bucket_name = GetEnvVar("ErrBucket") + "-" + GetEnvVar("ErrAppid"); + } + + static CosAPI* createCosClient(int max_retry_num) { + m_config = new CosConfig("./config.json"); + m_config->SetMaxRetryTimes(max_retry_num); + m_config->SetAccessKey(GetEnvVar("CPP_SDK_V5_ACCESS_KEY")); + m_config->SetSecretKey(GetEnvVar("CPP_SDK_V5_SECRET_KEY")); + m_config->SetRegion(GetEnvVar("ErrRegion")); + + return new CosAPI(*m_config); + } + + static void TearDownTestCase() { + delete m_client_with_retry; + delete m_client_without_retry; + delete m_config; + } + + protected: + static CosConfig* m_config; + static CosAPI* m_client_without_retry; + static CosAPI* m_client_with_retry; + static std::string m_bucket_name; +}; + +CosConfig* ObjectOpRetryTest::m_config; +CosAPI* ObjectOpRetryTest::m_client_without_retry; +CosAPI* ObjectOpRetryTest::m_client_with_retry; +std::string ObjectOpRetryTest::m_bucket_name; + +TEST_F(ObjectOpRetryTest, GetObjectFor2xxTest) { + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "200", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "204", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "206", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, GetObjectWithChangeDomainFor2xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "200", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "204", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "206", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, GetObjectWithoutRetryFor5xxTest) { + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "500", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_without_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(500, result.GetHttpStatus()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "503", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_without_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(503, result.GetHttpStatus()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "504", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_without_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(504, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, GetObjectWithRetryFor5xxTest) { + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "500", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "503", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "504", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, GetObjectWithRetryChangeDomainFor5xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "500", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "503", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "504", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, GetObjectWithRetryFor4xxTest) { + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "400", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(400, result.GetHttpStatus()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "403", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(403, result.GetHttpStatus()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "404", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(404, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, GetObjectWithoutRetryFor3xxTest) { + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "301", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_without_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(301, result.GetHttpStatus()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "302", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_without_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(302, result.GetHttpStatus()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "307", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_without_retry->GetObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(307, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, GetObjectWithRetryChangeDomainFor3xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "301", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "302", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "307", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, PutObjectWithoutRetryFor5xxTest) { + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "500", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_without_retry->PutObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(500, result.GetHttpStatus()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "503", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_without_retry->PutObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(503, result.GetHttpStatus()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "504", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_without_retry->PutObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(504, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, PutObjectWithRetryFor5xxTest) { + { + std::istringstream iss("put object test"); + PutObjectByStreamReq req(m_bucket_name, "500", iss); + + qcloud_cos::PutObjectByStreamResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "503", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "504", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, PutObjectWithRetryChangeDomainFor5xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "500", local_file); + + qcloud_cos::PutObjectByFileResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + std::istringstream iss("put object test"); + PutObjectByStreamReq req(m_bucket_name, "503", iss); + + qcloud_cos::PutObjectByStreamResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "504", local_file); + + qcloud_cos::PutObjectByFileResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, PutObjectWithoutRetryFor3xxTest) { + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "301", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_without_retry->PutObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(301, result.GetHttpStatus()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "302", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_without_retry->PutObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(302, result.GetHttpStatus()); + } + { + std::istringstream iss("put object test"); + PutObjectByStreamReq req(m_bucket_name, "307", iss); + + PutObjectByStreamResp resp; + CosResult result = m_client_without_retry->PutObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(307, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, PutObjectWithRetryChangeDomainFor3xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "301", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + std::string local_file = "./testfile"; + TestUtils::WriteRandomDatatoFile(local_file, 1024); + PutObjectByFileReq req(m_bucket_name, "302", local_file); + + PutObjectByFileResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + std::istringstream iss("put object test"); + PutObjectByStreamReq req(m_bucket_name, "307", iss); + + PutObjectByStreamResp resp; + CosResult result = m_client_with_retry->PutObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithoutRetryFor5xxTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "500"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(500, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "503"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(503, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "504"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(504, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryFor5xxTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "500"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "503"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + std::cout << "result.GetErrorMsg() = " << result.GetErrorMsg() << std::endl; + std::cout << "result.GetHttpStatus() = " << result.GetHttpStatus() << std::endl; + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "504"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryChangeDomainFor5xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "500"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "503"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + std::cout << "result.GetErrorMsg() = " << result.GetErrorMsg() << std::endl; + std::cout << "result.GetHttpStatus() = " << result.GetHttpStatus() << std::endl; + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "504"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithoutRetryFor4xxTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "400"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(400, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "403"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(403, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "404"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(404, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithoutRetryFor3xxTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "301"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(301, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "302"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(302, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "307"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_without_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(307, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryChangeDomainFor3xxTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "301"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "302"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "307"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, GetObjectFor2xxrTest) { + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "200r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "204r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "206r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, GetObjectWithChangeDomainFor2xxrTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "200r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "204r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "206r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, GetObjectWithRetryChangeDomainFor3xxrTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "301r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::GetObjectByFileReq req(m_bucket_name, "302r", "./test.txt"); + qcloud_cos::GetObjectByFileResp resp; + + qcloud_cos::CosResult result = m_client_with_retry->GetObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryFor4xxrTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "400r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(400, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "403r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(403, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "404r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(404, result.GetHttpStatus()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryChangeDomainFor4xxrTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "400r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(400, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "403r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(403, result.GetHttpStatus()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "404r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_FALSE(result.IsSucc()); + EXPECT_EQ(404, result.GetHttpStatus()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryFor5xxrTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "500r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "503r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + std::cout << "result.GetErrorMsg() = " << result.GetErrorMsg() << std::endl; + std::cout << "result.GetHttpStatus() = " << result.GetHttpStatus() << std::endl; + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "504r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryChangeDomainFor5xxrTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "500r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "503r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + std::cout << "result.GetErrorMsg() = " << result.GetErrorMsg() << std::endl; + std::cout << "result.GetHttpStatus() = " << result.GetHttpStatus() << std::endl; + } + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "504r"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryForTimeoutTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "timeout"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryForShutdownTest) { + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "shutdown"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryChangeDomainForTimeoutTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "timeout"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} + +TEST_F(ObjectOpRetryTest, HeadObjectWithRetryChangeDomainForShutdownTest) { + CosSysConfig::SetRetryChangeDomain(true); + { + qcloud_cos::HeadObjectReq req(m_bucket_name, "shutdown"); + qcloud_cos::HeadObjectResp resp; + qcloud_cos::CosResult result = m_client_with_retry->HeadObject(req, &resp); + EXPECT_TRUE(result.IsSucc()); + } + CosSysConfig::SetRetryChangeDomain(false); +} +} \ No newline at end of file diff --git a/unittest/src/object_op_test.cpp b/unittest/src/object_op_test.cpp index 21ce311..c5c3571 100644 --- a/unittest/src/object_op_test.cpp +++ b/unittest/src/object_op_test.cpp @@ -3545,7 +3545,7 @@ TEST_F(ObjectOpTest, ObjectOptest1){ object_op->GetTmpToken(); object_op->GetDestDomain(); ASSERT_TRUE(object_op->IsDefaultHost("xxxxx-12234.cos.zzzzz-wwww.myqcloud.com")); - object_op->ChangeHostSuffix("beijing.myqcloud.com"); + std::string local_file = "./testfile2"; TestUtils::WriteRandomDatatoFile(local_file, 1024); PutObjectByFileReq req(m_bucket_name, "test_object", local_file);