Skip to content

Commit f0ad5e4

Browse files
rock-gitchuandew
authored andcommitted
[feat][mdsv2] Optimize create file.
1 parent 6b9fd4f commit f0ad5e4

File tree

13 files changed

+460
-124
lines changed

13 files changed

+460
-124
lines changed

src/client/vfs/meta/v2/filesystem.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,26 @@ Status MDSV2FileSystem::Create(ContextSPtr ctx, Ino parent,
298298
const std::string& name, uint32_t uid,
299299
uint32_t gid, uint32_t mode, int flags,
300300
Attr* attr, uint64_t fh) {
301-
auto status = MkNod(ctx, parent, name, uid, gid, mode, 0, attr);
301+
// auto status = MkNod(ctx, parent, name, uid, gid, mode, 0, attr);
302+
// if (!status.ok()) {
303+
// return status;
304+
// }
305+
306+
// return Open(ctx, attr->ino, flags, fh);
307+
308+
std::vector<std::string> session_ids;
309+
auto status = mds_client_->Create(ctx, parent, name, uid, gid, mode, flags,
310+
*attr, session_ids);
302311
if (!status.ok()) {
303312
return status;
304313
}
305314

306-
return Open(ctx, attr->ino, flags, fh);
315+
// add file session
316+
CHECK(!session_ids.empty()) << "session_ids is empty.";
317+
const auto& session_id = session_ids.front();
318+
auto file_session = file_session_map_.Put(attr->ino, fh, session_id);
319+
320+
return Status::OK();
307321
}
308322

309323
Status MDSV2FileSystem::MkNod(ContextSPtr ctx, Ino parent,

src/client/vfs/meta/v2/mds_client.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,51 @@ Status MDSClient::Lookup(ContextSPtr ctx, Ino parent, const std::string& name,
292292
return Status::OK();
293293
}
294294

295+
Status MDSClient::Create(ContextSPtr ctx, Ino parent, const std::string& name,
296+
uint32_t uid, uint32_t gid, uint32_t mode, int flag,
297+
Attr& out_attr,
298+
std::vector<std::string>& session_ids) {
299+
CHECK(fs_id_ != 0) << "fs_id is invalid.";
300+
301+
auto get_mds_fn = [this, parent]() -> MDSMeta {
302+
return GetMdsByParent(parent);
303+
};
304+
305+
pb::mdsv2::BatchCreateRequest request;
306+
pb::mdsv2::BatchCreateResponse response;
307+
308+
SetAncestorInContext(request, parent);
309+
310+
request.set_fs_id(fs_id_);
311+
request.set_parent(parent);
312+
auto* param = request.add_params();
313+
314+
param->set_name(name);
315+
param->set_mode(mode);
316+
param->set_flag(flag);
317+
param->set_uid(uid);
318+
param->set_gid(gid);
319+
param->set_rdev(0);
320+
param->set_length(0);
321+
322+
auto status = SendRequest(ctx, get_mds_fn, "MDSService", "BatchCreate",
323+
request, response);
324+
if (!status.ok()) {
325+
return status;
326+
}
327+
328+
CHECK(!response.inodes().empty()) << "inodes is empty.";
329+
const auto& inode = response.inodes().at(0);
330+
331+
parent_memo_->Upsert(inode.ino(), parent, inode.version());
332+
parent_memo_->UpsertVersion(parent, response.parent_version());
333+
334+
out_attr = Helper::ToAttr(inode);
335+
session_ids = mdsv2::Helper::PbRepeatedToVector(response.session_ids());
336+
337+
return Status::OK();
338+
}
339+
295340
Status MDSClient::MkNod(ContextSPtr ctx, Ino parent, const std::string& name,
296341
uint32_t uid, uint32_t gid, mode_t mode, dev_t rdev,
297342
Attr& out_attr) {

src/client/vfs/meta/v2/mds_client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class MDSClient {
9090
Status Lookup(ContextSPtr ctx, Ino parent, const std::string& name,
9191
Attr& out_attr);
9292

93+
Status Create(ContextSPtr ctx, Ino parent, const std::string& name,
94+
uint32_t uid, uint32_t gid, uint32_t mode, int flag,
95+
Attr& out_attr, std::vector<std::string>& session_ids);
9396
Status MkNod(ContextSPtr ctx, Ino parent, const std::string& name,
9497
uint32_t uid, uint32_t gid, mode_t mode, dev_t rdev,
9598
Attr& out_attr);

src/mdsv2/common/runnable.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mdsv2/common/helper.h"
2828
#include "mdsv2/common/logging.h"
2929
#include "mdsv2/common/synchronization.h"
30+
#include "mdsv2/common/time.h"
3031

3132
namespace dingofs {
3233
namespace mdsv2 {
@@ -54,10 +55,10 @@ int ExecuteRoutine(void* meta,
5455
}
5556

5657
if (BAIDU_LIKELY(!iter.is_queue_stopped())) {
57-
int64_t start_time = Helper::TimestampMs();
58+
Duration duration;
5859
(*iter)->Run();
59-
DINGO_LOG(DEBUG) << fmt::format("[execqueue][type({})] run task elapsed time {}(ms).", (*iter)->Type(),
60-
Helper::TimestampMs() - start_time);
60+
DINGO_LOG(DEBUG) << fmt::format("[execqueue][type({})] run task elapsed time {}us.", (*iter)->Type(),
61+
duration.ElapsedUs());
6162
} else {
6263
DINGO_LOG(INFO) << fmt::format("[execqueue][type({})] task is stopped.", (*iter)->Type());
6364
}

src/mdsv2/common/runnable.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ class WorkerSet {
190190

191191
std::atomic<uint32_t> worker_no_generator_{0};
192192

193-
bool use_pthread_;
193+
const bool use_pthread_;
194+
195+
const uint32_t worker_num_{0};
196+
const int64_t max_pending_task_count_{0};
194197

195-
uint32_t worker_num_{0};
196-
int64_t max_pending_task_count_{0};
197198
std::atomic<int64_t> pending_task_count_{0};
198199

199200
// Notify

src/mdsv2/filesystem/file_session.cc

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ DECLARE_uint32(mds_txn_max_retry_times);
3636

3737
static const std::string kFileSessionCacheCountMetricsName = "dingofs_{}_file_session_cache_count";
3838

39-
static FileSessionPtr NewFileSession(uint32_t fs_id, Ino ino, const std::string& client_id) {
39+
static FileSessionSPtr NewFileSession(uint32_t fs_id, Ino ino, const std::string& client_id) {
4040
auto file_session = std::make_shared<FileSessionEntry>();
4141

4242
file_session->set_fs_id(fs_id);
@@ -51,7 +51,7 @@ static FileSessionPtr NewFileSession(uint32_t fs_id, Ino ino, const std::string&
5151
FileSessionCache::FileSessionCache(uint32_t fs_id)
5252
: count_metrics_(fmt::format(kFileSessionCacheCountMetricsName, fs_id)) {}
5353

54-
bool FileSessionCache::Put(FileSessionPtr file_session) {
54+
bool FileSessionCache::Put(FileSessionSPtr file_session) {
5555
utils::WriteLockGuard guard(lock_);
5656
auto key = Key{.ino = file_session->ino(), .session_id = file_session->session_id()};
5757
auto it = file_session_map_.find(key);
@@ -66,7 +66,7 @@ bool FileSessionCache::Put(FileSessionPtr file_session) {
6666
return true;
6767
}
6868

69-
void FileSessionCache::Upsert(FileSessionPtr file_session) {
69+
void FileSessionCache::Upsert(FileSessionSPtr file_session) {
7070
utils::WriteLockGuard guard(lock_);
7171

7272
auto key = Key{.ino = file_session->ino(), .session_id = file_session->session_id()};
@@ -105,7 +105,7 @@ void FileSessionCache::Delete(uint64_t ino) {
105105
}
106106
}
107107

108-
FileSessionPtr FileSessionCache::Get(uint64_t ino, const std::string& session_id) {
108+
FileSessionSPtr FileSessionCache::Get(uint64_t ino, const std::string& session_id) {
109109
utils::ReadLockGuard guard(lock_);
110110

111111
auto key = Key{.ino = ino, .session_id = session_id};
@@ -114,12 +114,12 @@ FileSessionPtr FileSessionCache::Get(uint64_t ino, const std::string& session_id
114114
return it != file_session_map_.end() ? it->second : nullptr;
115115
}
116116

117-
std::vector<FileSessionPtr> FileSessionCache::Get(uint64_t ino) {
117+
std::vector<FileSessionSPtr> FileSessionCache::Get(uint64_t ino) {
118118
utils::ReadLockGuard guard(lock_);
119119

120120
auto key = Key{.ino = ino, .session_id = ""};
121121

122-
std::vector<FileSessionPtr> file_sessions;
122+
std::vector<FileSessionSPtr> file_sessions;
123123
for (auto it = file_session_map_.upper_bound(key); it != file_session_map_.end(); ++it) {
124124
if (it->first.ino != ino) {
125125
break;
@@ -148,17 +148,16 @@ bool FileSessionCache::IsExist(uint64_t ino, const std::string& session_id) {
148148
FileSessionManager::FileSessionManager(uint32_t fs_id, OperationProcessorSPtr operation_processor)
149149
: fs_id_(fs_id), file_session_cache_(fs_id), operation_processor_(operation_processor) {}
150150

151-
Status FileSessionManager::Create(uint64_t ino, const std::string& client_id, FileSessionPtr& file_session) {
152-
file_session = NewFileSession(fs_id_, ino, client_id);
151+
FileSessionSPtr FileSessionManager::Create(uint64_t ino, const std::string& client_id) const {
152+
return NewFileSession(fs_id_, ino, client_id);
153+
}
153154

154-
// add to cache
155+
void FileSessionManager::Put(FileSessionSPtr file_session) {
155156
CHECK(file_session_cache_.Put(file_session))
156-
<< fmt::format("[filesession] put file session fail, {}/{}", ino, client_id);
157-
158-
return Status::OK();
157+
<< fmt::format("[filesession] put file session fail, {}/{}", file_session->ino(), file_session->client_id());
159158
}
160159

161-
FileSessionPtr FileSessionManager::Get(uint64_t ino, const std::string& session_id, bool just_cache) {
160+
FileSessionSPtr FileSessionManager::Get(uint64_t ino, const std::string& session_id, bool just_cache) {
162161
auto file_session = file_session_cache_.Get(ino, session_id);
163162
if (file_session != nullptr) {
164163
return file_session;
@@ -180,7 +179,7 @@ FileSessionPtr FileSessionManager::Get(uint64_t ino, const std::string& session_
180179
return file_session;
181180
}
182181

183-
std::vector<FileSessionPtr> FileSessionManager::Get(uint64_t ino, bool just_cache) {
182+
std::vector<FileSessionSPtr> FileSessionManager::Get(uint64_t ino, bool just_cache) {
184183
auto file_sessions = file_session_cache_.Get(ino);
185184
if (!file_sessions.empty()) {
186185
return file_sessions;
@@ -241,7 +240,7 @@ Status FileSessionManager::Delete(uint64_t ino) {
241240
return Status::OK();
242241
}
243242

244-
Status FileSessionManager::GetFileSessionsFromStore(uint64_t ino, std::vector<FileSessionPtr>& file_sessions) {
243+
Status FileSessionManager::GetFileSessionsFromStore(uint64_t ino, std::vector<FileSessionSPtr>& file_sessions) {
245244
Trace trace;
246245
ScanFileSessionOperation operation(trace, fs_id_, ino, [&](const FileSessionEntry& file_session) -> bool {
247246
file_sessions.push_back(std::make_shared<FileSessionEntry>(file_session));
@@ -258,7 +257,7 @@ Status FileSessionManager::GetFileSessionsFromStore(uint64_t ino, std::vector<Fi
258257
}
259258

260259
Status FileSessionManager::GetFileSessionFromStore(uint64_t ino, const std::string& session_id,
261-
FileSessionPtr& file_session) {
260+
FileSessionSPtr& file_session) {
262261
Trace trace;
263262
GetFileSessionOperation operation(trace, fs_id_, ino, session_id);
264263

src/mdsv2/filesystem/file_session.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
namespace dingofs {
2929
namespace mdsv2 {
3030

31-
using FileSessionPtr = std::shared_ptr<FileSessionEntry>;
31+
using FileSessionSPtr = std::shared_ptr<FileSessionEntry>;
3232

3333
// cache file session
3434
class FileSessionCache {
@@ -48,20 +48,20 @@ class FileSessionCache {
4848
}
4949
};
5050

51-
bool Put(FileSessionPtr file_session);
52-
void Upsert(FileSessionPtr file_session);
51+
bool Put(FileSessionSPtr file_session);
52+
void Upsert(FileSessionSPtr file_session);
5353
void Delete(uint64_t ino, const std::string& session_id);
5454
void Delete(uint64_t ino);
5555

56-
FileSessionPtr Get(uint64_t ino, const std::string& session_id);
57-
std::vector<FileSessionPtr> Get(uint64_t ino);
56+
FileSessionSPtr Get(uint64_t ino, const std::string& session_id);
57+
std::vector<FileSessionSPtr> Get(uint64_t ino);
5858
bool IsExist(uint64_t ino);
5959
bool IsExist(uint64_t ino, const std::string& session_id);
6060

6161
private:
6262
utils::RWLock lock_;
6363
// ino/session_id -> file_session
64-
std::map<Key, FileSessionPtr> file_session_map_;
64+
std::map<Key, FileSessionSPtr> file_session_map_;
6565

6666
// statistics
6767
bvar::Adder<int64_t> count_metrics_;
@@ -86,20 +86,21 @@ class FileSessionManager {
8686
return std::make_unique<FileSessionManager>(fs_id, operation_processor);
8787
}
8888

89-
Status Create(uint64_t ino, const std::string& client_id, FileSessionPtr& file_session);
89+
FileSessionSPtr Create(uint64_t ino, const std::string& client_id) const;
90+
void Put(FileSessionSPtr file_session);
9091
Status IsExist(uint64_t ino, bool just_cache, bool& is_exist);
9192
Status Delete(uint64_t ino, const std::string& session_id);
9293
Status Delete(uint64_t ino);
9394

94-
FileSessionPtr Get(uint64_t ino, const std::string& session_id, bool just_cache = false);
95-
std::vector<FileSessionPtr> Get(uint64_t ino, bool just_cache = false);
95+
FileSessionSPtr Get(uint64_t ino, const std::string& session_id, bool just_cache = false);
96+
std::vector<FileSessionSPtr> Get(uint64_t ino, bool just_cache = false);
9697
Status GetAll(std::vector<FileSessionEntry>& file_sessions);
9798

9899
FileSessionCache& GetFileSessionCache() { return file_session_cache_; }
99100

100101
private:
101-
Status GetFileSessionsFromStore(uint64_t ino, std::vector<FileSessionPtr>& file_sessions);
102-
Status GetFileSessionFromStore(uint64_t ino, const std::string& session_id, FileSessionPtr& file_session);
102+
Status GetFileSessionsFromStore(uint64_t ino, std::vector<FileSessionSPtr>& file_sessions);
103+
Status GetFileSessionFromStore(uint64_t ino, const std::string& session_id, FileSessionSPtr& file_session);
103104
Status IsExistFromStore(uint64_t ino, bool& is_exist);
104105

105106
uint32_t fs_id_;

0 commit comments

Comments
 (0)