Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,14 @@ class ShardAllocator final : public DfsAllocatorInterface {
std::string mount_path_;
uint64_t shard_capacity_ = 0;
uint64_t alignment_ = 4096;
// Readers retain an immutable topology snapshot. Expansion shares existing
// Readers use an immutable topology snapshot. Expansion shares existing
// ShardState objects and publishes the complete new topology in one step.
std::shared_ptr<const ShardList> shards_;
// Readers keep no reference, so superseded snapshots are never freed:
// published_shards_ (guarded by expansion_mutex_) holds every snapshot
// until destruction. Only Init and a growing ExpandShards publish, so each
// expansion retains 16 bytes (one shared_ptr) per shard.
std::atomic<const ShardList*> shards_{nullptr};
std::vector<std::unique_ptr<const ShardList>> published_shards_;
std::mutex expansion_mutex_;
std::unique_ptr<FileSystemAdapter> fs_adapter_;
bool eviction_enabled_ = true;
Expand Down
52 changes: 22 additions & 30 deletions mooncake-store/src/storage/distributed/shard_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ tl::expected<void, ErrorCode> ShardAllocator::Init(

std::vector<std::string> created_files;
try {
auto ready = std::make_shared<ShardList>();
auto ready = std::make_unique<ShardList>();
ready->reserve(target_count);
for (int i = 0; i < target_count; ++i) {
const auto existing = existing_paths.find(i);
const std::string path = existing == existing_paths.end()
Expand All @@ -142,9 +143,9 @@ tl::expected<void, ErrorCode> ShardAllocator::Init(
}
ready->push_back(std::move(*shard));
}
std::atomic_store_explicit(
&shards_, std::shared_ptr<const ShardList>(std::move(ready)),
std::memory_order_release);
published_shards_.push_back(std::move(ready));
shards_.store(published_shards_.back().get(),
std::memory_order_release);
} catch (const std::exception& error) {
CleanupCreatedFiles(created_files);
LOG(ERROR) << "Failed to initialize DFS shards: " << error.what();
Expand All @@ -155,8 +156,7 @@ tl::expected<void, ErrorCode> ShardAllocator::Init(
}

int ShardAllocator::GetShardCount() const {
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
return shards ? static_cast<int>(shards->size()) : 0;
}

Expand All @@ -165,8 +165,7 @@ tl::expected<int, ErrorCode> ShardAllocator::ExpandShards(int target_count) {
if (!initialized_.load(std::memory_order_acquire)) {
return tl::make_unexpected(ErrorCode::DFS_SERVICE_UNAVAILABLE);
}
const auto current =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* current = shards_.load(std::memory_order_acquire);
if (target_count <= 0 || target_count < static_cast<int>(current->size()) ||
shard_capacity_ > std::numeric_limits<uint64_t>::max() /
static_cast<uint64_t>(target_count)) {
Expand All @@ -176,7 +175,9 @@ tl::expected<int, ErrorCode> ShardAllocator::ExpandShards(int target_count) {

std::vector<std::string> created_files;
try {
auto ready = std::make_shared<ShardList>(*current);
auto ready = std::make_unique<ShardList>();
ready->reserve(target_count);
ready->assign(current->begin(), current->end());
for (int i = static_cast<int>(current->size()); i < target_count; ++i) {
const std::string path =
mount_path_ + "/dfs_shard_" + FormatShardIdx(i, 0) + ".data";
Expand All @@ -190,9 +191,9 @@ tl::expected<int, ErrorCode> ShardAllocator::ExpandShards(int target_count) {
}
ready->push_back(std::move(*shard));
}
std::atomic_store_explicit(
&shards_, std::shared_ptr<const ShardList>(std::move(ready)),
std::memory_order_release);
published_shards_.push_back(std::move(ready));
shards_.store(published_shards_.back().get(),
std::memory_order_release);
} catch (const std::exception& error) {
CleanupCreatedFiles(created_files);
LOG(ERROR) << "Failed to expand DFS shards: " << error.what();
Expand Down Expand Up @@ -271,8 +272,7 @@ tl::expected<DistributedFSDescriptor, ErrorCode> ShardAllocator::Allocate(
}

auto key_lock = LockKey(key);
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
const size_t preferred = std::hash<std::string>{}(key) % shards->size();
// A full preferred shard must not hide capacity in newly appended shards.
// Existing descriptors remain authoritative even when the modulo changes.
Expand Down Expand Up @@ -351,8 +351,7 @@ void ShardAllocator::UpdateAccess(const std::string& key,

uint64_t ShardAllocator::GetUsedBytes() const {
uint64_t used = 0;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
if (!shards) return 0;
for (const auto& shard : *shards) {
if (!shard) continue;
Expand All @@ -367,8 +366,7 @@ uint64_t ShardAllocator::GetUsedBytes() const {

uint64_t ShardAllocator::GetTotalCapacity() const {
uint64_t total = 0;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
if (!shards) return 0;
for (const auto& shard : *shards) {
if (shard) total += shard->capacity;
Expand All @@ -383,8 +381,7 @@ uint64_t ShardAllocator::GetFileCount() const {
void ShardAllocator::Free(uint64_t offset, uint64_t /*aligned_size*/,
int shard_idx, const std::string& key) {
if (!initialized_.load(std::memory_order_acquire)) return;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
if (shard_idx < 0 || shard_idx >= static_cast<int>(shards->size())) return;

auto& shard = *(*shards)[shard_idx];
Expand All @@ -410,8 +407,7 @@ void ShardAllocator::Free(uint64_t offset, uint64_t /*aligned_size*/,
void ShardAllocator::UpdateAccess(const std::string& key, int shard_idx,
uint64_t offset) {
if (!initialized_.load(std::memory_order_acquire)) return;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
if (shard_idx < 0 || shard_idx >= static_cast<int>(shards->size())) return;

auto& shard = *(*shards)[shard_idx];
Expand Down Expand Up @@ -440,8 +436,7 @@ ShardAllocator::PendingEviction ShardAllocator::PrepareEviction() {
PendingEviction pending(this);
if (!initialized_.load(std::memory_order_acquire)) return pending;

const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
for (size_t i = 0; i < shards->size(); ++i) {
auto& shard = *(*shards)[i];
{
Expand All @@ -455,8 +450,7 @@ ShardAllocator::PendingEviction ShardAllocator::PrepareEviction() {

void ShardAllocator::CommitPreparedEviction(PendingEviction&& pending) {
if (pending.owner_ != this) return;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);

const auto free_at =
std::chrono::steady_clock::now() + deferred_free_duration_;
Expand All @@ -482,8 +476,7 @@ void ShardAllocator::CommitPreparedEviction(PendingEviction&& pending) {

void ShardAllocator::RestorePreparedEviction(PendingEviction&& pending) {
if (pending.owner_ != this) return;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);

// Candidates were removed oldest-first. Restore in reverse order so the
// original relative LRU order is preserved.
Expand Down Expand Up @@ -520,8 +513,7 @@ void ShardAllocator::RestorePreparedEviction(PendingEviction&& pending) {
void ShardAllocator::ResolvePreparedEviction(
PendingEviction&& pending, const std::vector<bool>& accepted) {
if (pending.owner_ != this) return;
const auto shards =
std::atomic_load_explicit(&shards_, std::memory_order_acquire);
const auto* shards = shards_.load(std::memory_order_acquire);
if (accepted.size() != pending.prepared_.size()) {
LOG(ERROR) << "DFS eviction decision count " << accepted.size()
<< " does not match prepared candidate count "
Expand Down
Loading