|
| 1 | +/** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * */ |
| 24 | +#include "shard_gc.h" |
| 25 | +#include "logger/logger.h" |
| 26 | +#include "posix_file.h" |
| 27 | + |
| 28 | +namespace UC::PosixStore { |
| 29 | + |
| 30 | +ShardGarbageCollector::~ShardGarbageCollector() { StopBackgroundCheck(); } |
| 31 | + |
| 32 | +Status ShardGarbageCollector::Setup(const SpaceLayout* layout, const ShardGCConfig& config) |
| 33 | +{ |
| 34 | + layout_ = layout; |
| 35 | + config_ = config; |
| 36 | + shards_ = layout_->RelativeRoots(); |
| 37 | + |
| 38 | + if (shards_.empty()) { return Status::InvalidParam("no shards available"); } |
| 39 | + if (config_.recyclePercent <= 0 || config_.recyclePercent > 1.0f) { |
| 40 | + return Status::InvalidParam("invalid recycle percent"); |
| 41 | + } |
| 42 | + if (config_.gcConcurrency == 0) { return Status::InvalidParam("invalid gc concurrency"); } |
| 43 | + |
| 44 | + auto success = gcPool_.SetWorkerFn([this](ShardTaskContext& ctx, auto&) { ProcessTask(ctx); }) |
| 45 | + .SetNWorker(config_.gcConcurrency) |
| 46 | + .Run(); |
| 47 | + |
| 48 | + if (!success) { return Status::Error("failed to start gc thread pool"); } |
| 49 | + |
| 50 | + UC_INFO( |
| 51 | + "ShardGC setup: recyclePercent={}, concurrency={}, maxFileCount={}, " |
| 52 | + "thresholdRatio={}, checkIntervalSec={}", |
| 53 | + config_.recyclePercent, config_.gcConcurrency, config_.maxFileCount, config_.thresholdRatio, |
| 54 | + config_.gcCheckIntervalSec); |
| 55 | + |
| 56 | + if (config_.maxFileCount > 0) { |
| 57 | + gcCheckWorker_ = std::thread(&ShardGarbageCollector::GCCheckLoop, this); |
| 58 | + while (!stop_.load() && ShouldTrigger()) { Execute(); } |
| 59 | + } |
| 60 | + |
| 61 | + return Status::OK(); |
| 62 | +} |
| 63 | + |
| 64 | +void ShardGarbageCollector::StopBackgroundCheck() |
| 65 | +{ |
| 66 | + { |
| 67 | + std::lock_guard<std::mutex> lock(gcCheckMtx_); |
| 68 | + stop_ = true; |
| 69 | + } |
| 70 | + gcCheckCv_.notify_all(); |
| 71 | + if (gcCheckWorker_.joinable()) { gcCheckWorker_.join(); } |
| 72 | +} |
| 73 | + |
| 74 | +void ShardGarbageCollector::GCCheckLoop() |
| 75 | +{ |
| 76 | + while (!stop_.load()) { |
| 77 | + { |
| 78 | + std::unique_lock<std::mutex> lock(gcCheckMtx_); |
| 79 | + gcCheckCv_.wait_for(lock, std::chrono::seconds(config_.gcCheckIntervalSec), |
| 80 | + [this] { return stop_.load(); }); |
| 81 | + } |
| 82 | + if (stop_.load()) { break; } |
| 83 | + while (!stop_.load() && ShouldTrigger()) { Execute(); } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void ShardGarbageCollector::Execute() |
| 88 | +{ |
| 89 | + std::shared_ptr<Latch> waiter; |
| 90 | + |
| 91 | + try { |
| 92 | + waiter = std::make_shared<Latch>(); |
| 93 | + } catch (const std::exception& e) { |
| 94 | + UC_ERROR("Failed to allocate latch: ", e.what()); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + waiter->Set(shards_.size()); |
| 99 | + |
| 100 | + for (const auto& shard : shards_) { |
| 101 | + gcPool_.Push({ShardTaskContext::Type::GC, shard, waiter, nullptr}); |
| 102 | + } |
| 103 | + |
| 104 | + waiter->Wait(); |
| 105 | +} |
| 106 | + |
| 107 | +bool ShardGarbageCollector::ShouldTrigger() |
| 108 | +{ |
| 109 | + auto sampleShards = layout_->SampleShards(config_.shardSampleRatio); |
| 110 | + if (sampleShards.empty()) { return false; } |
| 111 | + |
| 112 | + std::shared_ptr<Latch> waiter; |
| 113 | + |
| 114 | + try { |
| 115 | + waiter = std::make_shared<Latch>(); |
| 116 | + } catch (const std::exception& e) { |
| 117 | + UC_ERROR("Failed to allocate latch: {}", e.what()); |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + std::atomic<size_t> sampledFiles{0}; |
| 122 | + waiter->Set(sampleShards.size()); |
| 123 | + |
| 124 | + for (const auto& shard : sampleShards) { |
| 125 | + gcPool_.Push({ShardTaskContext::Type::SAMPLE, shard, waiter, &sampledFiles}); |
| 126 | + } |
| 127 | + |
| 128 | + waiter->Wait(); |
| 129 | + |
| 130 | + size_t avgFilesPerShard = sampledFiles.load() / sampleShards.size(); |
| 131 | + size_t thresholdFilesPerShard = config_.maxFileCount / shards_.size(); |
| 132 | + |
| 133 | + UC_INFO("GC sampling: avgFiles/shard={}, threshold={}", avgFilesPerShard, |
| 134 | + static_cast<size_t>(thresholdFilesPerShard * config_.thresholdRatio)); |
| 135 | + |
| 136 | + return avgFilesPerShard >= static_cast<size_t>(thresholdFilesPerShard * config_.thresholdRatio); |
| 137 | +} |
| 138 | + |
| 139 | +void ShardGarbageCollector::ProcessTask(ShardTaskContext& ctx) |
| 140 | +{ |
| 141 | + if (ctx.type == ShardTaskContext::Type::SAMPLE) { |
| 142 | + size_t count = layout_->CountFilesInShard(ctx.shard); |
| 143 | + ctx.sampledFiles->fetch_add(count, std::memory_order_relaxed); |
| 144 | + } else { |
| 145 | + auto filesToDelete = layout_->GetOldestFiles(ctx.shard, config_.recyclePercent, |
| 146 | + config_.maxRecycleCountPerShard); |
| 147 | + for (const auto& blockId : filesToDelete) { |
| 148 | + PosixFile{layout_->DataFilePath(blockId, false)}.Remove(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + ctx.waiter->Done(); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace UC::PosixStore |
0 commit comments