Skip to content

Commit f064a1b

Browse files
committed
[Store]: Support mounting file segments
1 parent b53ecdf commit f064a1b

File tree

7 files changed

+244
-23
lines changed

7 files changed

+244
-23
lines changed

mooncake-store/include/allocator.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ class AllocatedBuffer {
5353
struct Descriptor;
5454

5555
AllocatedBuffer(std::shared_ptr<BufferAllocatorBase> allocator,
56-
std::string segment_name, void* buffer_ptr,
57-
std::size_t size,
56+
std::string segment_name, FileBufferID file_id,
57+
void* buffer_ptr, std::size_t size,
5858
std::optional<offset_allocator::OffsetAllocationHandle>&&
5959
offset_handle = std::nullopt)
6060
: allocator_(std::move(allocator)),
6161
segment_name_(std::move(segment_name)),
62+
file_id_(file_id),
6263
buffer_ptr_(buffer_ptr),
6364
size_(size),
6465
offset_handle_(std::move(offset_handle)) {}
@@ -92,17 +93,20 @@ class AllocatedBuffer {
9293
// Represents the serializable state
9394
struct Descriptor {
9495
std::string segment_name_;
96+
FileBufferID file_id_;
9597
uint64_t size_;
9698
uintptr_t buffer_address_;
9799
BufStatus status_;
98-
YLT_REFL(Descriptor, segment_name_, size_, buffer_address_, status_);
100+
YLT_REFL(Descriptor, segment_name_, file_id_, size_, buffer_address_,
101+
status_);
99102
};
100103

101104
void mark_complete() { status = BufStatus::COMPLETE; }
102105

103106
private:
104107
std::weak_ptr<BufferAllocatorBase> allocator_;
105108
std::string segment_name_;
109+
FileBufferID file_id_;
106110
BufStatus status{BufStatus::INIT};
107111
void* buffer_ptr_{nullptr};
108112
std::size_t size_{0};
@@ -152,7 +156,8 @@ class CachelibBufferAllocator
152156
: public BufferAllocatorBase,
153157
public std::enable_shared_from_this<CachelibBufferAllocator> {
154158
public:
155-
CachelibBufferAllocator(std::string segment_name, size_t base, size_t size);
159+
CachelibBufferAllocator(std::string segment_name, size_t base, size_t size,
160+
FileBufferID file_id = 0);
156161

157162
~CachelibBufferAllocator() override;
158163

@@ -170,6 +175,7 @@ class CachelibBufferAllocator
170175
const size_t base_;
171176
const size_t total_size_;
172177
std::atomic_size_t cur_size_;
178+
const FileBufferID file_id_;
173179

174180
// metrics - removed allocated_bytes_ member
175181
// ylt::metric::gauge_t* allocated_bytes_{nullptr};
@@ -189,7 +195,8 @@ class OffsetBufferAllocator
189195
: public BufferAllocatorBase,
190196
public std::enable_shared_from_this<OffsetBufferAllocator> {
191197
public:
192-
OffsetBufferAllocator(std::string segment_name, size_t base, size_t size);
198+
OffsetBufferAllocator(std::string segment_name, size_t base, size_t size,
199+
FileBufferID file_id = 0);
193200

194201
~OffsetBufferAllocator() override;
195202

@@ -207,6 +214,7 @@ class OffsetBufferAllocator
207214
const size_t base_;
208215
const size_t total_size_;
209216
std::atomic_size_t cur_size_;
217+
const FileBufferID file_id_;
210218

211219
// offset allocator implementation
212220
std::shared_ptr<offset_allocator::OffsetAllocator> offset_allocator_;

mooncake-store/include/client.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ class Client {
179179
tl::expected<void, ErrorCode> UnmountSegment(const void* buffer,
180180
size_t size);
181181

182+
/**
183+
* @brief Register a file to master for allocation
184+
* @param path The file path
185+
* @return ErrorCode indicating success/failure
186+
*/
187+
tl::expected<void, ErrorCode> MountFileSegment(const std::string& path);
188+
189+
/**
190+
* @brief Unregisters a file segment from master
191+
* @param path File path to unregister
192+
* @return ErrorCode indicating success/failure
193+
*/
194+
tl::expected<void, ErrorCode> UnmountFileSegment(const std::string& path);
195+
182196
/**
183197
* @brief Registers memory buffer with TransferEngine for data transfer
184198
* @param addr Memory address to register

mooncake-store/include/types.h

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Replica;
4545
using ObjectKey = std::string;
4646
using Version = uint64_t;
4747
using SegmentId = int64_t;
48+
using FileBufferID = uint32_t;
4849
using TaskID = int64_t;
4950
using BufHandleList = std::vector<std::shared_ptr<AllocatedBuffer>>;
5051
// using ReplicaList = std::vector<ReplicaInfo>;
@@ -164,20 +165,61 @@ const static uint64_t kMaxSliceSize =
164165
facebook::cachelib::Slab::kSize - 16; // should be lower than limit
165166

166167
/**
167-
* @brief Represents a contiguous memory region
168+
* @brief Type of segments.
169+
*/
170+
enum class SegmentType {
171+
UNKNOWN = -1,
172+
MEMORY,
173+
FILE,
174+
};
175+
176+
/**
177+
* @brief Stream operator for SegmentType
178+
*/
179+
inline std::ostream& operator<<(std::ostream& os,
180+
const SegmentType& type) noexcept {
181+
static const std::unordered_map<SegmentType, std::string_view> type_strings{
182+
{SegmentType::UNKNOWN, "UNKNOWN"},
183+
{SegmentType::MEMORY, "MEMORY"},
184+
{SegmentType::FILE, "FILE"}};
185+
186+
os << (type_strings.count(type) ? type_strings.at(type) : "UNKNOWN");
187+
return os;
188+
}
189+
190+
/**
191+
* @brief Represents a contiguous storage region, could be memory or file.
168192
*/
169193
struct Segment {
170194
UUID id{0, 0};
195+
SegmentType type{SegmentType::UNKNOWN};
171196
std::string name{}; // The name of the segment, also might be the
172197
// hostname of the server that owns the segment
173198
uintptr_t base{0};
174199
size_t size{0};
200+
// For a file segment, this will be the path of the file.
201+
std::string path{};
202+
// For a file segment, this will be the id of the file buffer.
203+
FileBufferID file_id{0};
175204
Segment() = default;
176205
Segment(const UUID& id, const std::string& name, uintptr_t base,
177206
size_t size)
178-
: id(id), name(name), base(base), size(size) {}
207+
: id(id),
208+
type(SegmentType::MEMORY),
209+
name(name),
210+
base(base),
211+
size(size) {}
212+
Segment(const UUID& id, const std::string& name, uintptr_t base,
213+
size_t size, const std::string& path, FileBufferID file_id)
214+
: id(id),
215+
type(SegmentType::FILE),
216+
name(name),
217+
base(base),
218+
size(size),
219+
path(path),
220+
file_id(file_id) {}
179221
};
180-
YLT_REFL(Segment, id, name, base, size);
222+
YLT_REFL(Segment, id, type, name, base, size, path, file_id);
181223

182224
/**
183225
* @brief Client status from the master's perspective

mooncake-store/src/allocator.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,32 @@ AllocatedBuffer::~AllocatedBuffer() {
2323

2424
// Implementation of get_descriptor
2525
AllocatedBuffer::Descriptor AllocatedBuffer::get_descriptor() const {
26-
return {segment_name_, static_cast<uint64_t>(size()),
26+
return {segment_name_, file_id_, static_cast<uint64_t>(size()),
2727
reinterpret_cast<uintptr_t>(buffer_ptr_), status};
2828
}
2929

3030
// Define operator<< using public accessors or get_descriptor if appropriate
3131
std::ostream& operator<<(std::ostream& os, const AllocatedBuffer& buffer) {
3232
return os << "AllocatedBuffer: { "
3333
<< "segment_name: " << buffer.segment_name_ << ", "
34+
<< "file_id: " << buffer.file_id_ << ", "
3435
<< "size: " << buffer.size() << ", "
3536
<< "status: " << buffer.status << ", "
3637
<< "buffer_ptr: " << static_cast<void*>(buffer.data()) << " }";
3738
}
3839

3940
// Removed allocated_bytes parameter and member initialization
4041
CachelibBufferAllocator::CachelibBufferAllocator(std::string segment_name,
41-
size_t base, size_t size)
42+
size_t base, size_t size,
43+
FileBufferID file_id)
4244
: segment_name_(segment_name),
4345
base_(base),
4446
total_size_(size),
45-
cur_size_(0) {
47+
cur_size_(0),
48+
file_id_(file_id) {
4649
VLOG(1) << "initializing_buffer_allocator segment_name=" << segment_name
4750
<< " base_address=" << reinterpret_cast<void*>(base)
48-
<< " size=" << size;
51+
<< " size=" << size << " file_id=" << file_id;
4952

5053
// Calculate the size of the header region.
5154
header_region_size_ =
@@ -107,7 +110,7 @@ std::unique_ptr<AllocatedBuffer> CachelibBufferAllocator::allocate(
107110
cur_size_.fetch_add(size);
108111
MasterMetricManager::instance().inc_allocated_size(size);
109112
return std::make_unique<AllocatedBuffer>(shared_from_this(), segment_name_,
110-
buffer, size);
113+
file_id_, buffer, size);
111114
}
112115

113116
void CachelibBufferAllocator::deallocate(AllocatedBuffer* handle) {
@@ -133,14 +136,16 @@ void CachelibBufferAllocator::deallocate(AllocatedBuffer* handle) {
133136

134137
// OffsetBufferAllocator implementation
135138
OffsetBufferAllocator::OffsetBufferAllocator(std::string segment_name,
136-
size_t base, size_t size)
139+
size_t base, size_t size,
140+
FileBufferID file_id)
137141
: segment_name_(segment_name),
138142
base_(base),
139143
total_size_(size),
140-
cur_size_(0) {
144+
cur_size_(0),
145+
file_id_(file_id) {
141146
VLOG(1) << "initializing_offset_buffer_allocator segment_name="
142147
<< segment_name << " base_address=" << reinterpret_cast<void*>(base)
143-
<< " size=" << size;
148+
<< " size=" << size << " file_id=" << file_id;
144149

145150
try {
146151
// 1k <= init_capacity <= 64k
@@ -196,7 +201,7 @@ std::unique_ptr<AllocatedBuffer> OffsetBufferAllocator::allocate(size_t size) {
196201
// Create a custom AllocatedBuffer that manages the
197202
// OffsetAllocationHandle
198203
allocated_buffer = std::make_unique<AllocatedBuffer>(
199-
shared_from_this(), segment_name_, buffer_ptr, size,
204+
shared_from_this(), segment_name_, file_id_, buffer_ptr, size,
200205
std::move(allocation_handle));
201206
VLOG(1) << "allocation_succeeded size=" << size
202207
<< " segment=" << segment_name_ << " address=" << buffer_ptr;

0 commit comments

Comments
 (0)