Skip to content

Commit b870c38

Browse files
committed
use RAII way to update allocated_file_size
1 parent f445548 commit b870c38

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

mooncake-store/include/replica.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "types.h"
1313
#include "allocator.h"
14+
#include "master_metric_manager.h"
15+
1416

1517
namespace mooncake {
1618

@@ -92,6 +94,15 @@ struct MemoryReplicaData {
9294
struct DiskReplicaData {
9395
std::string file_path;
9496
uint64_t object_size = 0;
97+
// Automatic update allocated_file_size via RAII
98+
DiskReplicaData(std::string file_path, uint64_t object_size)
99+
: file_path(std::move(file_path)), object_size(object_size) {
100+
MasterMetricManager::instance().inc_allocated_file_size(object_size);
101+
}
102+
103+
~DiskReplicaData() {
104+
MasterMetricManager::instance().dec_allocated_file_size(object_size);
105+
}
95106
};
96107

97108
struct MemoryDescriptor {

mooncake-store/src/master_service.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ auto MasterService::PutStart(const std::string& key,
410410
std::string file_path = ResolvePath(key);
411411
replicas.emplace_back(file_path, total_length,
412412
ReplicaStatus::PROCESSING);
413-
MasterMetricManager::instance().inc_allocated_file_size(total_length);
414413
}
415414

416415
std::vector<Replica::Descriptor> replica_list;
@@ -464,18 +463,7 @@ auto MasterService::PutRevoke(const std::string& key, ReplicaType replica_type)
464463
<< ", error=invalid_replica_status";
465464
return tl::make_unexpected(ErrorCode::INVALID_WRITE);
466465
}
467-
// When disk replica is enabled, update allocated_file_size
468-
if (use_disk_replica_ && replica_type == ReplicaType::DISK) {
469-
for (const auto& replica : metadata.replicas) {
470-
if (replica.is_memory_replica()) {
471-
continue;
472-
}
473-
auto disk_descriptor =
474-
replica.get_descriptor().get_disk_descriptor();
475-
MasterMetricManager::instance().dec_allocated_file_size(
476-
disk_descriptor.object_size);
477-
}
478-
}
466+
479467
metadata.EraseReplica(replica_type);
480468
if (metadata.IsValid() == false) {
481469
accessor.Erase();

0 commit comments

Comments
 (0)