-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathhf3fs_sdk.cc
More file actions
390 lines (334 loc) · 13.3 KB
/
Copy pathhf3fs_sdk.cc
File metadata and controls
390 lines (334 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "kv_cache_manager/client/src/internal/sdk/hf3fs_sdk.h"
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "kv_cache_manager/client/src/internal/sdk/deadline_util.h"
#include "kv_cache_manager/client/src/internal/sdk/hf3fs_gpu_util_alias.h"
#include "kv_cache_manager/client/src/internal/sdk/hf3fs_mempool.h"
#include "kv_cache_manager/common/logger.h"
namespace kv_cache_manager {
Hf3fsSdk::~Hf3fsSdk() {
KVCM_LOG_INFO("Hf3fsSdk destructor");
ReleaseIovHandle(read_iov_handle_);
ReleaseIovHandle(write_iov_handle_);
}
ClientErrorCode Hf3fsSdk::Init(const std::shared_ptr<SdkBackendConfig> &sdk_backend_config,
const std::shared_ptr<StorageConfig> &storage_config) {
// delete remaining iov shm to avoid shm space not enough
DeleteRemainingIovShm();
auto hf3fs_config = std::dynamic_pointer_cast<Hf3fsSdkConfig>(sdk_backend_config);
if (!hf3fs_config) {
KVCM_LOG_WARN("init 3fs sdk failed, unexpected config type: %d",
static_cast<uint8_t>(sdk_backend_config->type()));
return ER_INVALID_SDKBACKEND_CONFIG;
}
KVCM_LOG_INFO("init 3fs sdk, config: %s", hf3fs_config->ToString().c_str());
if (!CheckConfig(*hf3fs_config)) {
KVCM_LOG_WARN("3fs init failed, check init params failed");
return ER_INVALID_SDKBACKEND_CONFIG;
}
config_ = hf3fs_config;
auto gpu_util = std::make_shared<Hf3fsGpuUtil>();
if (!gpu_util->Init()) {
KVCM_LOG_WARN("dist storage 3fs init failed, gpu util init failed");
return ER_SDKINIT_ERROR;
}
usrbio_api_ = std::make_shared<Hf3fsUsrbioApi>();
// read iov
if (!InitIovHandle(read_iov_handle_, config_->read_iov_block_size(), config_->read_iov_size(), gpu_util)) {
KVCM_LOG_WARN("init read iov handle failed");
return ER_SDKINIT_ERROR;
}
// write iov
if (!InitIovHandle(write_iov_handle_, config_->write_iov_block_size(), config_->write_iov_size(), gpu_util)) {
KVCM_LOG_WARN("init write iov handle failed");
ReleaseIovHandle(read_iov_handle_);
return ER_SDKINIT_ERROR;
}
// TODO(LXQ): metrics
return ER_OK;
}
ClientErrorCode
Hf3fsSdk::Get(const std::vector<DataStorageUri> &remote_uris, const BlockBuffers &local_buffers, int64_t deadline_ms) {
if (remote_uris.size() != local_buffers.size()) {
KVCM_LOG_WARN(
"get failed, size mismatch, uris size: %lu, buffers size: %lu", remote_uris.size(), local_buffers.size());
return ER_INVALID_PARAMS;
}
// 逐 block 准入检查:deadline 已过期则不再为后续 block
// 创建 Hf3fsUsrbioClient / 发起 I/O。无 deadline(0)时 DeadlineExpired() 恒为 false。
for (size_t i = 0; i < remote_uris.size(); ++i) {
if (DeadlineExpired(deadline_ms)) {
KVCM_LOG_WARN("get skipped, deadline expired, path: %s, done blocks: %zu/%zu, remaining blocks skipped "
"to protect caller buffer",
remote_uris[i].GetPath().c_str(),
i,
remote_uris.size());
return ER_SDK_TIMEOUT;
}
if (Get(remote_uris[i], local_buffers[i], deadline_ms) != ER_OK) {
return ER_SDKREAD_ERROR;
}
}
return ER_OK;
}
ClientErrorCode Hf3fsSdk::Get(const DataStorageUri &uri, const BlockBuffer &block_buffer, int64_t deadline_ms) const {
if (block_buffer.iovs.empty()) {
return ER_OK;
}
const auto path = uri.GetPath();
if (path.empty()) {
KVCM_LOG_WARN("get failed, path is empty or not exists: %s", path.c_str());
return ER_INVALID_PARAMS;
}
const auto offset_opt = GetFileOffset(uri);
if (!offset_opt.has_value()) {
return ER_INVALID_PARAMS;
}
auto iovs = block_buffer.iovs;
iovs.insert(iovs.begin(), Iov{MemoryType::CPU, nullptr, offset_opt.value(), true});
auto usrbio_client = std::make_shared<Hf3fsUsrbioClient>(
BuildHf3fsFileConfig(path), read_iov_handle_, write_iov_handle_, usrbio_api_);
if (!usrbio_client->Read(iovs, deadline_ms)) {
KVCM_LOG_WARN("get failed, read failed, path: %s", path.c_str());
return ER_SDKREAD_ERROR;
}
return ER_OK;
}
ClientErrorCode Hf3fsSdk::Put(const std::vector<DataStorageUri> &remote_uris,
const BlockBuffers &local_buffers,
std::shared_ptr<std::vector<DataStorageUri>> actual_remote_uris,
int64_t deadline_ms) {
if (remote_uris.size() != local_buffers.size()) {
KVCM_LOG_WARN(
"put failed, size mismatch, uris size: %lu, buffers size: %lu", remote_uris.size(), local_buffers.size());
return ER_INVALID_PARAMS;
}
if (Alloc(remote_uris, *actual_remote_uris) != ER_OK) {
return ER_SDKALLOC_ERROR;
}
// 逐 block 准入检查:deadline 已过期则不再为后续 block
// 创建 Hf3fsUsrbioClient / 发起 I/O。无 deadline(0)时 DeadlineExpired() 恒为 false。
for (size_t i = 0; i < remote_uris.size(); ++i) {
if (DeadlineExpired(deadline_ms)) {
KVCM_LOG_WARN("put skipped, deadline expired, path: %s, done blocks: %zu/%zu, remaining blocks skipped",
remote_uris[i].GetPath().c_str(),
i,
remote_uris.size());
return ER_SDK_TIMEOUT;
}
if (Put(remote_uris[i], local_buffers[i], deadline_ms) != ER_OK) {
return ER_SDKWRITE_ERROR;
}
}
return ER_OK;
}
ClientErrorCode Hf3fsSdk::Put(const DataStorageUri &uri, const BlockBuffer &block_buffer, int64_t deadline_ms) const {
if (block_buffer.iovs.empty()) {
return ER_OK;
}
const auto path = uri.GetPath();
if (path.empty()) {
KVCM_LOG_WARN("put failed, path is empty");
return ER_INVALID_PARAMS;
}
const auto offset_opt = GetFileOffset(uri);
if (!offset_opt.has_value()) {
return ER_INVALID_PARAMS;
}
auto iovs = block_buffer.iovs;
iovs.insert(iovs.begin(), Iov{MemoryType::CPU, nullptr, offset_opt.value(), true});
auto usrbio_client = std::make_shared<Hf3fsUsrbioClient>(
BuildHf3fsFileConfig(path), read_iov_handle_, write_iov_handle_, usrbio_api_);
if (!usrbio_client->Write(iovs, deadline_ms)) {
KVCM_LOG_WARN("put failed, write failed, path: %s", path.c_str());
return ER_SDKWRITE_ERROR;
}
return ER_OK;
}
ClientErrorCode Hf3fsSdk::Alloc(const std::vector<DataStorageUri> &remote_uris,
std::vector<DataStorageUri> &alloc_uris) {
alloc_uris = remote_uris;
for (const auto &uri : remote_uris) {
const auto path = uri.GetPath();
if (path.empty()) {
KVCM_LOG_WARN("alloc failed, path is empty");
return ER_SDKALLOC_ERROR;
}
if (!CreateDir(std::filesystem::path(path).parent_path())) {
return ER_SDKALLOC_ERROR;
}
}
return ER_OK;
}
bool Hf3fsSdk::CheckConfig(const Hf3fsSdkConfig &hf3fs_config) const {
// TODO(LXQ): maybe need to move to Hf3fsSdkConfig::Validate()
const auto &mountpoint = hf3fs_config.mountpoint();
if (mountpoint.empty()) {
KVCM_LOG_WARN("init failed, 3fs mountpoint is empty");
return false;
}
if (!std::filesystem::exists(mountpoint)) {
KVCM_LOG_WARN("init failed, 3fs mountpoint not exists: %s", mountpoint.c_str());
return false;
}
const auto &root_dir = hf3fs_config.root_dir();
if (root_dir.empty()) {
KVCM_LOG_WARN("init failed, 3fs root dir is empty");
return false;
}
const auto root_dir_path = std::filesystem::path(mountpoint) / root_dir;
if (!std::filesystem::exists(root_dir_path)) {
KVCM_LOG_WARN("init failed, 3fs root dir not exists: %s", root_dir_path.string().c_str());
return false;
}
return true;
}
void Hf3fsSdk::DeleteRemainingIovShm() const {
// 删除旧的 shm iov , 避免 shm 空间不够用
namespace fs = std::filesystem;
const std::string shm_path = "/dev/shm/";
const std::string prefix = "hf3fs-iov-";
try {
fs::path dir(shm_path);
if (!fs::exists(dir)) {
return;
}
if (!fs::is_directory(dir)) {
return;
}
const auto threshold = std::chrono::seconds(300); // 5min
const auto now = fs::file_time_type::clock::now();
for (const auto &entry : fs::directory_iterator(dir)) {
if (!fs::is_regular_file(entry.status())) {
continue;
}
std::string filename = entry.path().filename().string();
if (filename.find(prefix) != 0) {
continue;
}
const auto age = now - fs::last_write_time(entry.path());
if (age > threshold) {
try {
KVCM_LOG_INFO("remove old shm iov file: %s", filename.c_str());
fs::remove(entry.path());
} catch (const fs::filesystem_error &e) {
KVCM_LOG_WARN(
"found exception when remove old iov file: %s, exception: %s", filename.c_str(), e.what());
}
}
}
} catch (const fs::filesystem_error &e) {
KVCM_LOG_WARN("found exception when remove old iov file, exception: %s", e.what());
}
}
bool Hf3fsSdk::InitIovHandle(Hf3fsIovHandle &handle,
size_t iov_block_size,
size_t iov_size,
const std::shared_ptr<Hf3fsGpuUtil> &gpu_util) const {
if (iov_block_size != 0 && iov_size % iov_block_size != 0) {
iov_size = (iov_size / iov_block_size + 1) * iov_block_size;
}
auto iov = CreateIov(config_->mountpoint(), iov_size, iov_block_size);
if (iov == nullptr) {
KVCM_LOG_WARN("create iov failed, iov size: %zu, iov block size: %zu", iov_size, iov_block_size);
return false;
}
auto mempool = std::make_shared<Hf3fsMempool>(iov->base, iov_size, iov_block_size);
if (!mempool->Init()) {
KVCM_LOG_WARN("mempool init failed, iov base: %p, iov size: %zu, iov block size: %zu",
iov->base,
iov_size,
iov_block_size);
DestroyIov(iov);
return false;
}
if (gpu_util != nullptr && !gpu_util->RegisterHost(iov->base, iov_size)) {
KVCM_LOG_WARN("gpu register iov failed, iov base: %p, expect iov size: %zu, actual iov size: %zu",
iov->base,
iov_size,
iov->size);
DestroyIov(iov);
return false;
}
handle.iov = iov;
handle.iov_size = iov_size;
handle.iov_block_size = iov_block_size;
handle.iov_mempool = mempool;
handle.gpu_util = gpu_util;
return true;
}
void Hf3fsSdk::ReleaseIovHandle(Hf3fsIovHandle &iov_handle) {
if (iov_handle.iov != nullptr) {
if (iov_handle.gpu_util) {
iov_handle.gpu_util->UnregisterHost(iov_handle.iov->base);
}
DestroyIov(iov_handle.iov);
iov_handle.iov = nullptr;
}
iov_handle.iov_mempool.reset();
iov_handle.gpu_util.reset();
}
struct hf3fs_iov *Hf3fsSdk::CreateIov(const std::string &mountpoint, size_t iov_size, size_t iov_block_size) const {
if (mountpoint.empty()) {
KVCM_LOG_WARN("create iov failed, mountpoint is empty");
return nullptr;
}
if (iov_size <= 0) {
KVCM_LOG_WARN("create iov failed, iov size is invalid: %zu", iov_size);
return nullptr;
}
auto iov = new struct hf3fs_iov();
auto ret = usrbio_api_->Hf3fsIovCreate(iov, mountpoint.c_str(), iov_size, iov_block_size, -1);
if (ret != 0) {
KVCM_LOG_WARN("3fs iov create failed, errno: %s, mountpoint: %s, iov size: %lu, iov block size: %lu",
strerror(-ret),
mountpoint.c_str(),
iov_size,
iov_block_size);
delete iov;
iov = nullptr;
return nullptr;
}
return iov;
}
void Hf3fsSdk::DestroyIov(struct hf3fs_iov *iov) const {
if (iov == nullptr) {
return;
}
usrbio_api_->Hf3fsIovDestroy(iov);
delete iov;
}
Hf3fsFileConfig Hf3fsSdk::BuildHf3fsFileConfig(const std::string &filepath) const {
Hf3fsFileConfig config;
config.filepath = filepath;
config.mountpoint = config_->mountpoint();
return config;
}
bool Hf3fsSdk::CreateDir(const std::filesystem::path &dir) const {
if (std::filesystem::exists(dir)) {
return true;
}
std::error_code ec;
std::filesystem::create_directories(dir, ec);
if (ec) {
KVCM_LOG_WARN("create directory failed, dir: %s, error: %s", dir.string().c_str(), ec.message().c_str());
return false;
}
return true;
}
std::optional<size_t> Hf3fsSdk::GetFileOffset(const DataStorageUri &uri) const {
uint64_t blkid = 0;
size_t size = 0;
uri.GetParamAs<uint64_t>("blkid", blkid);
uri.GetParamAs<size_t>("size", size);
if (size == 0) {
KVCM_LOG_WARN("get file offset failed, uri size is 0, uri: %s", uri.ToUriString().c_str());
return std::nullopt;
}
return blkid * size;
}
} // namespace kv_cache_manager