Skip to content

Commit

Permalink
[feat] curvefs: merge two rpc into one when call makenode
Browse files Browse the repository at this point in the history
Signed-off-by: swj <[email protected]>
  • Loading branch information
201341 committed Sep 5, 2023
1 parent 6f5c85b commit e421f66
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 91 deletions.
2 changes: 2 additions & 0 deletions curvefs/proto/metaserver.proto
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ message CreateDentryRequest {
required uint32 copysetId = 2;
required uint32 partitionId = 3;
required Dentry dentry = 4;
optional uint64 time = 5;
optional uint32 time_ns = 6;
}

message CreateDentryResponse {
Expand Down
5 changes: 4 additions & 1 deletion curvefs/src/client/dentry_cache_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ CURVEFS_ERROR DentryCacheManagerImpl::GetDentry(uint64_t parent,

CURVEFS_ERROR DentryCacheManagerImpl::CreateDentry(const Dentry &dentry) {
std::string key = GetDentryCacheKey(dentry.parentinodeid(), dentry.name());
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
NameLockGuard lock(nameLock_, key);
MetaStatusCode ret = metaClient_->CreateDentry(dentry);
MetaStatusCode ret = metaClient_->CreateDentry(dentry,
now);
if (ret != MetaStatusCode::OK) {
LOG(ERROR) << "metaClient_ CreateDentry failed, MetaStatusCode = "
<< ret
Expand Down
40 changes: 0 additions & 40 deletions curvefs/src/client/fuse_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ CURVEFS_ERROR FuseClient::MakeNode(
if (type == FsFileType::TYPE_FILE || type == FsFileType::TYPE_S3) {
dentry.set_flag(DentryFlag::TYPE_FILE_FLAG);
}

ret = dentryManager_->CreateDentry(dentry);
if (ret != CURVEFS_ERROR::OK) {
LOG(ERROR) << "dentryManager_ CreateDentry fail, ret = " << ret
Expand All @@ -456,15 +455,6 @@ CURVEFS_ERROR FuseClient::MakeNode(
return ret;
}

ret = UpdateParentMCTimeAndNlink(parent, type, NlinkChange::kAddOne);
if (ret != CURVEFS_ERROR::OK) {
LOG(ERROR) << "UpdateParentMCTimeAndNlink failed"
<< ", parent: " << parent
<< ", name: " << name
<< ", type: " << type;
return ret;
}

VLOG(6) << "dentryManager_ CreateDentry success"
<< ", parent = " << parent << ", name = " << name
<< ", mode = " << mode;
Expand Down Expand Up @@ -620,7 +610,6 @@ CURVEFS_ERROR FuseClient::CreateManageNode(fuse_req_t req,
if (type == FsFileType::TYPE_FILE || type == FsFileType::TYPE_S3) {
dentry.set_flag(DentryFlag::TYPE_FILE_FLAG);
}

ret = dentryManager_->CreateDentry(dentry);
if (ret != CURVEFS_ERROR::OK) {
LOG(ERROR) << "dentryManager_ CreateDentry fail, ret = " << ret
Expand All @@ -636,15 +625,6 @@ CURVEFS_ERROR FuseClient::CreateManageNode(fuse_req_t req,
return ret;
}

ret = UpdateParentMCTimeAndNlink(parent, type, NlinkChange::kAddOne);
if (ret != CURVEFS_ERROR::OK) {
LOG(ERROR) << "UpdateParentMCTimeAndNlink failed"
<< ", parent: " << parent
<< ", name: " << name
<< ", type: " << type;
return ret;
}

VLOG(6) << "dentryManager_ CreateDentry success"
<< ", parent = " << parent << ", name = " << name
<< ", mode = " << mode;
Expand Down Expand Up @@ -1231,17 +1211,6 @@ CURVEFS_ERROR FuseClient::FuseOpSymlink(fuse_req_t req,
return ret;
}

ret = UpdateParentMCTimeAndNlink(parent, FsFileType::TYPE_SYM_LINK,
NlinkChange::kAddOne);
if (ret != CURVEFS_ERROR::OK) {
LOG(ERROR) << "UpdateParentMCTimeAndNlink failed"
<< ", link:" << link
<< ", parent: " << parent
<< ", name: " << name
<< ", type: " << FsFileType::TYPE_SYM_LINK;
return ret;
}

if (enableSumInDir_.load()) {
// update parent summary info
XAttr xattr;
Expand Down Expand Up @@ -1303,15 +1272,6 @@ CURVEFS_ERROR FuseClient::FuseOpLink(fuse_req_t req,
return ret;
}

ret = UpdateParentMCTimeAndNlink(newparent, type, NlinkChange::kAddOne);
if (ret != CURVEFS_ERROR::OK) {
LOG(ERROR) << "UpdateParentMCTimeAndNlink failed"
<< ", parent: " << newparent
<< ", name: " << newname
<< ", type: " << type;
return ret;
}

if (enableSumInDir_.load()) {
// update parent summary info
XAttr xattr;
Expand Down
5 changes: 4 additions & 1 deletion curvefs/src/client/rpcclient/metaserver_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ MetaStatusCode MetaServerClientImpl::ListDentry(uint32_t fsId, uint64_t inodeid,
return ConvertToMetaStatusCode(excutor.DoRPCTask());
}

MetaStatusCode MetaServerClientImpl::CreateDentry(const Dentry &dentry) {
MetaStatusCode MetaServerClientImpl::CreateDentry(const Dentry &dentry,
const struct timespec &now) {
auto task = RPCTask {
(void)taskExecutorDone;
metric_.createDentry.qps.count << 1;
Expand All @@ -255,6 +256,8 @@ MetaStatusCode MetaServerClientImpl::CreateDentry(const Dentry &dentry) {
d->set_txid(txId);
d->set_type(dentry.type());
request.set_allocated_dentry(d);
request.set_time(now.tv_sec);
request.set_time_ns(now.tv_nsec);
curvefs::metaserver::MetaServerService_Stub stub(channel);
stub.CreateDentry(cntl, &request, &response, nullptr);

Expand Down
6 changes: 4 additions & 2 deletions curvefs/src/client/rpcclient/metaserver_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class MetaServerClient {
bool onlyDir,
std::list<Dentry> *dentryList) = 0;

virtual MetaStatusCode CreateDentry(const Dentry &dentry) = 0;
virtual MetaStatusCode CreateDentry(const Dentry &dentry,
const struct timespec &now) = 0;

virtual MetaStatusCode DeleteDentry(uint32_t fsId, uint64_t inodeid,
const std::string &name,
Expand Down Expand Up @@ -200,7 +201,8 @@ class MetaServerClientImpl : public MetaServerClient {
bool onlyDir,
std::list<Dentry> *dentryList) override;

MetaStatusCode CreateDentry(const Dentry &dentry) override;
MetaStatusCode CreateDentry(const Dentry &dentry,
const struct timespec &now) override;

MetaStatusCode DeleteDentry(uint32_t fsId, uint64_t inodeid,
const std::string &name,
Expand Down
4 changes: 4 additions & 0 deletions curvefs/src/mds/metaserverclient/metaserver_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ FSStatusCode MetaserverClient::CreateDentry(
d->set_txid(0);
d->set_type(FsFileType::TYPE_DIRECTORY);
request.set_allocated_dentry(d);
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
request.set_time(now.tv_sec);
request.set_time_ns(now.tv_nsec);

auto fp = &MetaServerService_Stub::CreateDentry;
LeaderCtx ctx;
Expand Down
48 changes: 33 additions & 15 deletions curvefs/src/metaserver/inode_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,27 +471,34 @@ MetaStatusCode InodeManager::PaddingInodeS3ChunkInfo(int32_t fsId,
}

MetaStatusCode InodeManager::UpdateInodeWhenCreateOrRemoveSubNode(
uint32_t fsId, uint64_t inodeId, FsFileType type, bool isCreate) {
Dentry dentry,
uint64_t now,
uint32_t now_ns,
bool isCreate) {
uint64_t fsId = dentry.fsid();
uint64_t parentInodeId = dentry.parentinodeid();
FsFileType type = dentry.type();
MetaStatusCode ret = MetaStatusCode::OK;

VLOG(6) << "UpdateInodeWhenCreateOrRemoveSubNode, fsId = " << fsId
<< ", inodeId = " << inodeId
<< ", inodeId = " << parentInodeId
<< ", isCreate = " << isCreate;
NameLockGuard lg(inodeLock_, GetInodeLockName(fsId, inodeId));

Inode inode;
MetaStatusCode ret = inodeStorage_->Get(
Key4Inode(fsId, inodeId), &inode);
NameLockGuard lg(inodeLock_, GetInodeLockName(fsId, parentInodeId));
Inode parentInode;
ret = inodeStorage_->Get(
Key4Inode(fsId, parentInodeId), &parentInode);
if (ret != MetaStatusCode::OK) {
LOG(ERROR) << "GetInode fail, " << inode.ShortDebugString()
LOG(ERROR) << "GetInode fail, " << parentInode.ShortDebugString()
<< ", ret = " << MetaStatusCode_Name(ret);
return ret;
}

uint32_t oldNlink = inode.nlink();
uint32_t oldNlink = parentInode.nlink();
if (oldNlink == 0) {
LOG(ERROR)
<< "UpdateInodeWhenCreateOrRemoveSubNode already be deleted!"
<< " fsId: " << fsId
<< ", inodeId: " << inodeId
<< ", inodeId: " << parentInodeId
<< ", type: " << type
<< ", isCreate: " << isCreate;
// already be deleted
Expand All @@ -500,21 +507,32 @@ MetaStatusCode InodeManager::UpdateInodeWhenCreateOrRemoveSubNode(

if (FsFileType::TYPE_DIRECTORY == type) {
if (isCreate) {
inode.set_nlink(++oldNlink);
parentInode.set_nlink(++oldNlink);
} else {
inode.set_nlink(--oldNlink);
parentInode.set_nlink(--oldNlink);
}
}

ret = inodeStorage_->Update(inode);
// update mctime and changetime
if (now != 0) {
parentInode.set_mtime(now);
parentInode.set_ctime(now);
}

if (now_ns != 0) {
parentInode.set_mtime_ns(now_ns);
parentInode.set_ctime_ns(now_ns);
}

ret = inodeStorage_->Update(parentInode);
if (ret != MetaStatusCode::OK) {
LOG(ERROR) << "UpdateInode fail, " << inode.ShortDebugString()
LOG(ERROR) << "UpdateInode fail, " << parentInode.ShortDebugString()
<< ", ret = " << MetaStatusCode_Name(ret);
return ret;
}

VLOG(9) << "UpdateInodeWhenCreateOrRemoveSubNode success, "
<< inode.ShortDebugString();
<< parentInode.ShortDebugString();
return MetaStatusCode::OK;
}

Expand Down
7 changes: 5 additions & 2 deletions curvefs/src/metaserver/inode_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ class InodeManager {
S3ChunkInfoMap* m,
uint64_t limit = 0);

MetaStatusCode UpdateInodeWhenCreateOrRemoveSubNode(uint32_t fsId,
uint64_t inodeId, FsFileType type, bool isCreate);
MetaStatusCode UpdateInodeWhenCreateOrRemoveSubNode(
Dentry dentry,
uint64_t now,
uint32_t now_ns,
bool isCreate);

MetaStatusCode InsertInode(const Inode &inode);

Expand Down
5 changes: 4 additions & 1 deletion curvefs/src/metaserver/metastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,10 @@ MetaStatusCode MetaStoreImpl::CreateDentry(const CreateDentryRequest *request,
std::shared_ptr<Partition> partition;
GET_PARTITION_OR_RETURN(partition);

MetaStatusCode status = partition->CreateDentry(request->dentry());
MetaStatusCode status =
partition->CreateDentry(request->dentry(),
request->time(),
request->time_ns());
response->set_statuscode(status);
return status;
}
Expand Down
10 changes: 5 additions & 5 deletions curvefs/src/metaserver/partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,16 @@ Partition::Partition(PartitionInfo partition,
} \
} while (0)

MetaStatusCode Partition::CreateDentry(const Dentry& dentry) {
MetaStatusCode Partition::CreateDentry(const Dentry& dentry,
uint64_t now,
uint32_t now_ns) {
PRECHECK(dentry.fsid(), dentry.parentinodeid());

MetaStatusCode ret = dentryManager_->CreateDentry(dentry);
if (MetaStatusCode::OK == ret) {
if (dentry.has_type()) {
return inodeManager_->UpdateInodeWhenCreateOrRemoveSubNode(
dentry.fsid(), dentry.parentinodeid(),
dentry.type(), true);
dentry, now, now_ns, true);
} else {
LOG(ERROR) << "CreateDentry does not have type, "
<< dentry.ShortDebugString();
Expand Down Expand Up @@ -149,8 +150,7 @@ MetaStatusCode Partition::DeleteDentry(const Dentry& dentry) {
if (MetaStatusCode::OK == ret) {
if (dentry.has_type()) {
return inodeManager_->UpdateInodeWhenCreateOrRemoveSubNode(
dentry.fsid(), dentry.parentinodeid(),
dentry.type(), false);
dentry, 0, 0, false);
} else {
LOG(ERROR) << "DeleteDentry does not have type, "
<< dentry.ShortDebugString();
Expand Down
4 changes: 3 additions & 1 deletion curvefs/src/metaserver/partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ class Partition {
Partition() = default;

// dentry
MetaStatusCode CreateDentry(const Dentry& dentry);
MetaStatusCode CreateDentry(const Dentry& dentry,
uint64_t now,
uint32_t now_ns);

MetaStatusCode LoadDentry(const DentryVec& vec, bool merge);

Expand Down
3 changes: 2 additions & 1 deletion curvefs/test/client/mock_metaserver_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class MockMetaServerClient : public MetaServerClient {
const std::string &last, uint32_t count, bool onlyDir,
std::list<Dentry> *dentryList));

MOCK_METHOD1(CreateDentry, MetaStatusCode(const Dentry &dentry));
MOCK_METHOD2(CreateDentry, MetaStatusCode(const Dentry &dentry,
const struct timespec &now));

MOCK_METHOD4(DeleteDentry, MetaStatusCode(
uint32_t fsId, uint64_t inodeid, const std::string &name,
Expand Down
13 changes: 10 additions & 3 deletions curvefs/test/client/rpcclient/metaserver_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <brpc/controller.h>
#include <brpc/server.h>
#include <butil/iobuf.h>
#include <butil/time.h>
#include <google/protobuf/message.h>
#include <google/protobuf/service.h>
#include <gtest/gtest.h>
Expand Down Expand Up @@ -316,7 +317,9 @@ TEST_F(MetaServerClientImplTest, test_CreateDentry_rpc_error) {
.Times(1 + opt_.maxRetry)
.WillRepeatedly(Return(true));

MetaStatusCode status = metaserverCli_.CreateDentry(d);
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
auto status = metaserverCli_.CreateDentry(d, now);
ASSERT_EQ(MetaStatusCode::RPC_ERROR, status);
}

Expand All @@ -343,7 +346,9 @@ TEST_F(MetaServerClientImplTest, test_CreateDentry_create_dentry_ok) {
EXPECT_CALL(*mockMetacache_.get(), GetTarget(_, _, _, _))
.WillOnce(DoAll(SetArgPointee<2>(target_), Return(true)));

auto status = metaserverCli_.CreateDentry(d);
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
auto status = metaserverCli_.CreateDentry(d, now);
ASSERT_EQ(MetaStatusCode::OK, status);
}

Expand Down Expand Up @@ -380,7 +385,9 @@ TEST_F(MetaServerClientImplTest, test_CreateDentry_copyset_not_exist) {
EXPECT_CALL(*mockMetacache_.get(), GetTargetLeader(_, _))
.WillOnce(Return(true));

auto status = metaserverCli_.CreateDentry(d);
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
auto status = metaserverCli_.CreateDentry(d, now);
ASSERT_EQ(MetaStatusCode::OK, status);
}

Expand Down
6 changes: 3 additions & 3 deletions curvefs/test/client/test_dentry_cache_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ TEST_F(TestDentryCacheManager, CreateAndGetDentry) {
dentryExp.set_parentinodeid(parent);
dentryExp.set_inodeid(inodeid);

EXPECT_CALL(*metaClient_, CreateDentry(_))
EXPECT_CALL(*metaClient_, CreateDentry(_, _))
.WillOnce(Return(MetaStatusCode::UNKNOWN_ERROR))
.WillOnce(Return(MetaStatusCode::OK));

Expand All @@ -155,7 +155,7 @@ TEST_F(TestDentryCacheManager, CreateAndGetDentry) {
fsId_, parent, name, FsFileType::TYPE_FILE))
.WillOnce(Return(MetaStatusCode::OK));
dCacheManager_->DeleteDentry(parent, name, FsFileType::TYPE_FILE);
EXPECT_CALL(*metaClient_, CreateDentry(_))
EXPECT_CALL(*metaClient_, CreateDentry(_, _))
.WillOnce(Return(MetaStatusCode::OK));
EXPECT_CALL(*metaClient_, GetDentry(fsId_, parent, name, _))
.WillOnce(
Expand Down Expand Up @@ -251,7 +251,7 @@ TEST_F(TestDentryCacheManager, GetTimeOutDentry) {
dentryExp.set_parentinodeid(parent);
dentryExp.set_inodeid(inodeid);

EXPECT_CALL(*metaClient_, CreateDentry(_))
EXPECT_CALL(*metaClient_, CreateDentry(_, _))
.WillOnce(Return(MetaStatusCode::OK));

EXPECT_CALL(*metaClient_, GetDentry(fsId_, parent, name, _))
Expand Down
3 changes: 0 additions & 3 deletions curvefs/test/client/test_fuse_s3_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1558,9 +1558,6 @@ TEST_F(TestFuseS3Client, FuseOpCreate_EnableSummary) {
auto parentInodeWrapper = std::make_shared<InodeWrapper>(
parentInode, metaClient_);
EXPECT_CALL(*inodeManager_, GetInode(_, _))
.WillOnce(
DoAll(SetArgReferee<1>(parentInodeWrapper),
Return(CURVEFS_ERROR::OK)))
.WillOnce(
DoAll(SetArgReferee<1>(parentInodeWrapper),
Return(CURVEFS_ERROR::OK)));
Expand Down
Loading

0 comments on commit e421f66

Please sign in to comment.