|
| 1 | +// Copyright 2025 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <Common/FailPoint.h> |
| 16 | +#include <Common/Logger.h> |
| 17 | +#include <Storages/S3/Lifecycle.h> |
| 18 | +#include <Storages/S3/S3Common.h> |
| 19 | +#include <aws/s3/model/ExpirationStatus.h> |
| 20 | +#include <aws/s3/model/GetBucketLifecycleConfigurationRequest.h> |
| 21 | +#include <aws/s3/model/LifecycleConfiguration.h> |
| 22 | +#include <aws/s3/model/LifecycleExpiration.h> |
| 23 | +#include <aws/s3/model/LifecycleRule.h> |
| 24 | +#include <aws/s3/model/LifecycleRuleAndOperator.h> |
| 25 | +#include <aws/s3/model/PutBucketLifecycleConfigurationRequest.h> |
| 26 | +#include <aws/s3/model/Rule.h> |
| 27 | +#include <aws/s3/model/Tag.h> |
| 28 | + |
| 29 | +namespace DB::FailPoints |
| 30 | +{ |
| 31 | +extern const char force_set_lifecycle_resp[]; |
| 32 | +} // namespace DB::FailPoints |
| 33 | + |
| 34 | +namespace DB::S3 |
| 35 | +{ |
| 36 | + |
| 37 | +Aws::S3::Model::BucketLifecycleConfiguration genNewLifecycleConfig( |
| 38 | + const Aws::Vector<Aws::S3::Model::LifecycleRule> & existing_rules, |
| 39 | + Int32 expire_days, |
| 40 | + bool use_ali_oss_format) |
| 41 | +{ |
| 42 | + static_assert(TaggingObjectIsDeleted == "tiflash_deleted=true"); |
| 43 | + std::vector<Aws::S3::Model::Tag> filter_tags{ |
| 44 | + Aws::S3::Model::Tag().WithKey("tiflash_deleted").WithValue("true"), |
| 45 | + }; |
| 46 | + Aws::S3::Model::LifecycleRule rule; |
| 47 | + rule.WithStatus(Aws::S3::Model::ExpirationStatus::Enabled) |
| 48 | + .WithExpiration(Aws::S3::Model::LifecycleExpiration().WithDays(expire_days)) |
| 49 | + .WithID("tiflashgc"); |
| 50 | + if (!use_ali_oss_format) |
| 51 | + { |
| 52 | + // Reference: https://docs.aws.amazon.com/AmazonS3/latest/userguide/S3OutpostsLifecycleCLIJava.html |
| 53 | + rule.WithFilter(Aws::S3::Model::LifecycleRuleFilter().WithAnd( |
| 54 | + Aws::S3::Model::LifecycleRuleAndOperator().WithPrefix("").WithTags(filter_tags))); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + // Alibaba cloud oss format |
| 59 | + // Note that "Prefix" field is required |
| 60 | + // Reference: https://github.com/aliyun/aliyun-oss-cpp-sdk/blob/c42600fb0b2057494ae3b77b93afeff42dfba0a4/sdk/src/model/SetBucketLifecycleRequest.cc#L40-L44 |
| 61 | + rule.WithFilter(Aws::S3::Model::LifecycleRuleFilter().WithTag(filter_tags[0]).WithPrefix("")) // |
| 62 | + .SetAliOssFormat(true); |
| 63 | + } |
| 64 | + |
| 65 | + auto new_rules = existing_rules; |
| 66 | + new_rules.emplace_back(rule); |
| 67 | + Aws::S3::Model::BucketLifecycleConfiguration lifecycle_config; |
| 68 | + lifecycle_config.WithRules(new_rules); |
| 69 | + |
| 70 | + return lifecycle_config; |
| 71 | +} |
| 72 | + |
| 73 | +struct RuleInfo |
| 74 | +{ |
| 75 | + bool check_success = false; |
| 76 | + bool rule_has_been_set = false; |
| 77 | + Aws::Vector<Aws::S3::Model::LifecycleRule> rules; |
| 78 | +}; |
| 79 | + |
| 80 | +RuleInfo checkLifecycleRuleExist(const TiFlashS3Client & client) |
| 81 | +{ |
| 82 | + Aws::S3::Model::GetBucketLifecycleConfigurationRequest req; |
| 83 | + req.SetBucket(client.bucket()); |
| 84 | + auto outcome = client.GetBucketLifecycleConfiguration(req); |
| 85 | + if (!outcome.IsSuccess()) |
| 86 | + { |
| 87 | + const auto & error = outcome.GetError(); |
| 88 | + // The life cycle is not added at all |
| 89 | + if (error.GetErrorType() == Aws::S3::S3Errors::RESOURCE_NOT_FOUND |
| 90 | + || error.GetExceptionName() == "NoSuchLifecycleConfiguration") |
| 91 | + { |
| 92 | + return RuleInfo{.check_success = true, .rule_has_been_set = false, .rules = {}}; |
| 93 | + } |
| 94 | + |
| 95 | + LOG_ERROR( |
| 96 | + client.log, |
| 97 | + "GetBucketLifecycle fail, please check the bucket lifecycle configuration or create the lifecycle rule" |
| 98 | + " manually, bucket={} {}", |
| 99 | + client.bucket(), |
| 100 | + S3ErrorMessage(error)); |
| 101 | + return RuleInfo{.check_success = false, .rule_has_been_set = false, .rules = {}}; |
| 102 | + } |
| 103 | + |
| 104 | + auto res = outcome.GetResultWithOwnership(); |
| 105 | + auto old_rules = res.GetRules(); |
| 106 | + fiu_do_on(FailPoints::force_set_lifecycle_resp, { |
| 107 | + if (auto v = FailPointHelper::getFailPointVal(FailPoints::force_set_lifecycle_resp); v) |
| 108 | + { |
| 109 | + auto rules = std::any_cast<std::vector<Aws::S3::Model::LifecycleRule>>(*v); |
| 110 | + old_rules = rules; |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + bool lifecycle_rule_has_been_set = false; |
| 115 | + static_assert(TaggingObjectIsDeleted == "tiflash_deleted=true"); |
| 116 | + for (const auto & rule : old_rules) |
| 117 | + { |
| 118 | + if (rule.GetAliOssFormat()) |
| 119 | + LOG_INFO(client.log, "Found existing lifecycle rule in AliOSS format, rule_id={}", rule.GetID()); |
| 120 | + |
| 121 | + const auto & filt = rule.GetFilter(); |
| 122 | + |
| 123 | + std::optional<Aws::S3::Model::Tag> tag; |
| 124 | + if (!filt.AndHasBeenSet()) |
| 125 | + { |
| 126 | + // For AWS S3, filt.AndHasBeenSet() == false |
| 127 | + tag = filt.GetTag(); |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + // For minio filt.AndHasBeenSet() == true |
| 132 | + const auto & and_op = filt.GetAnd(); |
| 133 | + const auto & tags = and_op.GetTags(); |
| 134 | + if (tags.size() != 1 || !and_op.PrefixHasBeenSet() || !and_op.GetPrefix().empty()) |
| 135 | + { |
| 136 | + continue; |
| 137 | + } |
| 138 | + tag = tags[0]; |
| 139 | + } |
| 140 | + if (!tag) |
| 141 | + continue; |
| 142 | + if (tag->GetKey() == "tiflash_deleted" && tag->GetValue() == "true") |
| 143 | + { |
| 144 | + if (rule.GetStatus() == Aws::S3::Model::ExpirationStatus::Enabled) |
| 145 | + { |
| 146 | + lifecycle_rule_has_been_set = true; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + LOG_ERROR( |
| 151 | + client.log, |
| 152 | + "The lifecycle rule is added but not enabled, please check the bucket lifecycle " |
| 153 | + "configuration or create the lifecycle rule manually, " |
| 154 | + "rule_id={} rule_status={} tag.key={} tag.value={}", |
| 155 | + rule.GetID(), |
| 156 | + Aws::S3::Model::ExpirationStatusMapper::GetNameForExpirationStatus(rule.GetStatus()), |
| 157 | + tag->GetKey(), |
| 158 | + tag->GetValue()); |
| 159 | + } |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return RuleInfo{ |
| 165 | + .check_success = true, |
| 166 | + .rule_has_been_set = lifecycle_rule_has_been_set, |
| 167 | + .rules = std::move(old_rules), |
| 168 | + }; |
| 169 | +} |
| 170 | + |
| 171 | +// Ensure the lifecycle rule with filter `TaggingObjectIsDeleted` has been added. |
| 172 | +// The lifecycle rule is required when using S3GCMethod::Lifecycle to expire the |
| 173 | +// deleted objects automatically using the cloud platform lifecycle mechanism. |
| 174 | +// If the lifecycle rule not exist, try to add the rule with `expire_days` days |
| 175 | +// to expire the objects. |
| 176 | +// Return true if the rule has been added or already exists. |
| 177 | +bool ensureLifecycleRuleExist(const TiFlashS3Client & client, Int32 expire_days) |
| 178 | +{ |
| 179 | + auto rule_info = checkLifecycleRuleExist(client); |
| 180 | + if (!rule_info.check_success) |
| 181 | + { |
| 182 | + // Failed to check the lifecycle rule existence, return false |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + bool rule_has_been_set = rule_info.rule_has_been_set; |
| 187 | + auto & old_rules = rule_info.rules; |
| 188 | + if (rule_has_been_set) |
| 189 | + { |
| 190 | + LOG_INFO( |
| 191 | + client.log, |
| 192 | + "The lifecycle rule has been set, n_rules={} filter={}", |
| 193 | + old_rules.size(), |
| 194 | + TaggingObjectIsDeleted); |
| 195 | + return true; |
| 196 | + } |
| 197 | + |
| 198 | + LOG_INFO( |
| 199 | + client.log, |
| 200 | + "The lifecycle rule with filter \"{}\" has not been added, n_rules={}", |
| 201 | + TaggingObjectIsDeleted, |
| 202 | + old_rules.size()); |
| 203 | + |
| 204 | + // Try add the tiflash rule to lifecycle |
| 205 | + bool use_ali_oss_format = false; |
| 206 | + auto lifecycle_config = genNewLifecycleConfig(old_rules, expire_days, use_ali_oss_format); |
| 207 | + Aws::S3::Model::PutBucketLifecycleConfigurationRequest request; |
| 208 | + request.WithBucket(client.bucket()).WithLifecycleConfiguration(lifecycle_config); |
| 209 | + auto outcome = client.PutBucketLifecycleConfiguration(request); |
| 210 | + if (outcome.IsSuccess()) |
| 211 | + { |
| 212 | + LOG_INFO( |
| 213 | + client.log, |
| 214 | + "The lifecycle rule has been added, new_n_rules={} tag={} use_ali_oss_format={}", |
| 215 | + old_rules.size(), |
| 216 | + TaggingObjectIsDeleted, |
| 217 | + use_ali_oss_format); |
| 218 | + return true; |
| 219 | + } |
| 220 | + const auto & error = outcome.GetError(); |
| 221 | + LOG_WARNING( |
| 222 | + client.log, |
| 223 | + "Create lifecycle rule with tag filter \"{}\" failed, retrying with another format, bucket={} " |
| 224 | + "use_ali_oss_format={} {}", |
| 225 | + TaggingObjectIsDeleted, |
| 226 | + client.bucket(), |
| 227 | + use_ali_oss_format, |
| 228 | + S3ErrorMessage(error)); |
| 229 | + |
| 230 | + // Retry with another format |
| 231 | + use_ali_oss_format = true; |
| 232 | + lifecycle_config = genNewLifecycleConfig(old_rules, expire_days, use_ali_oss_format); |
| 233 | + request.WithBucket(client.bucket()).WithLifecycleConfiguration(lifecycle_config); |
| 234 | + outcome = client.PutBucketLifecycleConfiguration(request); |
| 235 | + if (outcome.IsSuccess()) |
| 236 | + { |
| 237 | + LOG_INFO( |
| 238 | + client.log, |
| 239 | + "The lifecycle rule has been added, new_n_rules={} tag={} use_ali_oss_format={}", |
| 240 | + old_rules.size(), |
| 241 | + TaggingObjectIsDeleted, |
| 242 | + use_ali_oss_format); |
| 243 | + return true; |
| 244 | + } |
| 245 | + |
| 246 | + // Still failed to create lifecycle rule, log an error |
| 247 | + LOG_ERROR( |
| 248 | + client.log, |
| 249 | + "Create lifecycle rule with tag filter \"{}\" failed, please check the bucket lifecycle configuration or " |
| 250 | + "create the lifecycle rule manually, bucket={} use_ali_oss_format={} {}", |
| 251 | + TaggingObjectIsDeleted, |
| 252 | + client.bucket(), |
| 253 | + use_ali_oss_format, |
| 254 | + S3ErrorMessage(error)); |
| 255 | + return false; |
| 256 | +} |
| 257 | + |
| 258 | +} // namespace DB::S3 |
0 commit comments