Skip to content

Commit

Permalink
curvefs/client: fix s3 object will not be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhongsong committed Jul 3, 2023
1 parent 2dc5b4a commit b23c3e3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
16 changes: 15 additions & 1 deletion curvefs/src/metaserver/trash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ MetaStatusCode TrashImpl::DeleteInodeAndData(const TrashItem &item) {
clientAdaptorOption.chunkSize = s3Info.chunksize();
s3Adaptor_->Reinit(clientAdaptorOption, s3Info.ak(), s3Info.sk(),
s3Info.endpoint(), s3Info.bucketname());
ret = inodeStorage_->PaddingInodeS3ChunkInfo(item.fsId,
item.inodeId, inode.mutable_s3chunkinfomap());
if (ret != MetaStatusCode::OK) {
LOG(ERROR) << "GetInode chunklist fail, fsId = " << item.fsId
<< ", inodeId = " << item.inodeId
<< ", retCode = " << MetaStatusCode_Name(ret);
return ret;
}
if (inode.s3chunkinfomap().empty()) {
LOG(WARNING) << "GetInode chunklist empty, fsId = " << item.fsId
<< ", inodeId = " << item.inodeId;
return MetaStatusCode::NOT_FOUND;
}
VLOG(9) << "DeleteInodeAndData, inode: "
<< inode.ShortDebugString();
int retVal = s3Adaptor_->Delete(inode);
if (retVal != 0) {
LOG(ERROR) << "S3ClientAdaptor delete s3 data failed"
Expand All @@ -176,7 +191,6 @@ MetaStatusCode TrashImpl::DeleteInodeAndData(const TrashItem &item) {
return MetaStatusCode::S3_DELETE_ERR;
}
}

ret = inodeStorage_->Delete(Key4Inode(item.fsId, item.inodeId));
if (ret != MetaStatusCode::OK && ret != MetaStatusCode::NOT_FOUND) {
LOG(ERROR) << "Delete Inode fail, fsId = " << item.fsId
Expand Down
2 changes: 1 addition & 1 deletion curvefs/test/metaserver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ cc_test(
"mock_metaserver_s3.h",
"metaserver_s3_adaptor_test.h",
"metaserver_s3_adaptor_test.cpp",
"mock_metaserver_s3_adaptor.h",
"metaserver_s3_test.cpp",
"mock_s3compact_inode.h",
"s3compact_test.cpp",
Expand All @@ -46,6 +45,7 @@ cc_test(
"//curvefs/test/metaserver/storage:metaserver_storage_test_utils",
"//curvefs/test/metaserver/mock:metaserver_test_mock",
"@com_google_absl//absl/types:optional",
"//curvefs/test/client/rpcclient:rpcclient_test_mock",
],
)

Expand Down
65 changes: 58 additions & 7 deletions curvefs/test/metaserver/trash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "curvefs/src/metaserver/storage/rocksdb_storage.h"
#include "curvefs/test/metaserver/storage/utils.h"
#include "src/fs/ext4_filesystem_impl.h"
#include "curvefs/test/client/rpcclient/mock_mds_client.h"
#include "curvefs/test/metaserver/mock_metaserver_s3_adaptor.h"

using ::testing::AtLeast;
using ::testing::StrEq;
Expand All @@ -45,6 +47,7 @@ namespace {
auto localfs = curve::fs::Ext4FileSystemImpl::getInstance();
}

using ::curvefs::client::rpcclient::MockMdsClient;
using ::curvefs::metaserver::storage::KVStorage;
using ::curvefs::metaserver::storage::StorageOptions;
using ::curvefs::metaserver::storage::RocksDBStorage;
Expand Down Expand Up @@ -103,7 +106,29 @@ class TestTrash : public ::testing::Test {
inode.set_gid(0);
inode.set_mode(0);
inode.set_nlink(0);
inode.set_type(FsFileType::TYPE_FILE);
inode.set_type(FsFileType::TYPE_S3);
return inode;
}

Inode GenInodeHasChunks(uint32_t fsId, uint64_t inodeId) {
Inode inode;
inode.set_fsid(fsId);
inode.set_inodeid(inodeId);
inode.set_length(4096);
inode.set_ctime(0);
inode.set_ctime_ns(0);
inode.set_mtime(0);
inode.set_mtime_ns(0);
inode.set_atime(0);
inode.set_atime_ns(0);
inode.set_uid(0);
inode.set_gid(0);
inode.set_mode(0);
inode.set_nlink(0);
inode.set_type(FsFileType::TYPE_S3);

S3ChunkInfoList s3ChunkInfoList;
inode.mutable_s3chunkinfomap()->insert({0, s3ChunkInfoList});
return inode;
}

Expand All @@ -119,17 +144,18 @@ TEST_F(TestTrash, testAdd3ItemAndDelete) {
option.scanPeriodSec = 1;
option.expiredAfterSec = 1;

option.mdsClient = std::make_shared<MockMdsClient>();
option.s3Adaptor = std::make_shared<MockS3ClientAdaptor>();
trashManager_->Init(option);
trashManager_->Run();

auto trash1 = std::make_shared<TrashImpl>(inodeStorage_);
auto trash2 = std::make_shared<TrashImpl>(inodeStorage_);
trashManager_->Add(1, trash1);
trashManager_->Add(2, trash2);

inodeStorage_->Insert(GenInode(1, 1));
inodeStorage_->Insert(GenInode(1, 2));
inodeStorage_->Insert(GenInode(2, 1));
inodeStorage_->Insert(GenInodeHasChunks(1, 1));
inodeStorage_->Insert(GenInodeHasChunks(1, 2));
inodeStorage_->Insert(GenInodeHasChunks(2, 1));

ASSERT_EQ(inodeStorage_->Size(), 3);

Expand All @@ -138,17 +164,42 @@ TEST_F(TestTrash, testAdd3ItemAndDelete) {
trash2->Add(2, 1, 0);

std::this_thread::sleep_for(std::chrono::seconds(5));

std::list<TrashItem> list;

trashManager_->ListItems(&list);

ASSERT_EQ(0, list.size());

ASSERT_EQ(inodeStorage_->Size(), 0);

trashManager_->Fini();
}

TEST_F(TestTrash, testAdd3ItemAndNoDelete) {
TrashOption option;
option.scanPeriodSec = 1;
option.expiredAfterSec = 1;
option.mdsClient = std::make_shared<MockMdsClient>();
option.s3Adaptor = std::make_shared<MockS3ClientAdaptor>();
trashManager_->Init(option);
trashManager_->Run();

auto trash1 = std::make_shared<TrashImpl>(inodeStorage_);
trashManager_->Add(1, trash1);

inodeStorage_->Insert(GenInode(1, 1));
inodeStorage_->Insert(GenInode(1, 2));
inodeStorage_->Insert(GenInode(2, 1));
ASSERT_EQ(inodeStorage_->Size(), 3);
trash1->Add(1, 1, 0);
trash1->Add(1, 2, 0);
std::this_thread::sleep_for(std::chrono::seconds(5));
std::list<TrashItem> list;

trashManager_->ListItems(&list);
ASSERT_EQ(0, list.size());
ASSERT_EQ(inodeStorage_->Size(), 3);
trashManager_->Fini();
}

} // namespace metaserver
} // namespace curvefs

0 comments on commit b23c3e3

Please sign in to comment.