Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curvefs/metaserver: fix trash bugs #2948

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion curvefs/src/metaserver/copyset/copyset_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ bool CopysetNode::Start() {
LOG(ERROR) << "Fail to init raft node, copyset: " << name_;
return false;
}

metaStore_->LoadDeletedInodes();
LOG(INFO) << "Run copyset success, copyset: " << name_;
return true;
}
Expand Down
22 changes: 17 additions & 5 deletions curvefs/src/metaserver/inode_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,6 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
}
}

if (needAddTrash) {
trash_->Add(old.inodeid(), old.dtime());
--(*type2InodeNum_)[old.type()];
}

const S3ChunkInfoMap &map2add = request.s3chunkinfoadd();
const S3ChunkInfoList *list2add;
VLOG(9) << "UpdateInode inode " << old.inodeid() << " map2add size "
Expand Down Expand Up @@ -446,10 +441,27 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}
}

if (needAddTrash) {
trash_->Add(old.inodeid(), old.dtime(), false);
--(*type2InodeNum_)[old.type()];
}

VLOG(9) << "UpdateInode success, " << request.ShortDebugString();
return MetaStatusCode::OK;
}

void InodeManager::LoadDeletedInodes() {
std::map<std::string, uint64_t> items;
inodeStorage_->LoadDeletedInodes(&items);
VLOG(3) << "build trash items size: " << items.size();
std::vector<std::string> names;
for (auto& iter : items) {
curve::common::SplitString(iter.first , ":", &names);
trash_->Add(std::stoull(names[names.size() - 1 ]), iter.second, true);
}
}

MetaStatusCode InodeManager::GetOrModifyS3ChunkInfo(
uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add,
const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap,
Expand Down
3 changes: 3 additions & 0 deletions curvefs/src/metaserver/inode_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <atomic>
#include <list>
#include <map>
#include <memory>
#include <string>
#include <vector>
Expand Down Expand Up @@ -96,6 +97,8 @@ class InodeManager {
MetaStatusCode UpdateInode(const UpdateInodeRequest& request,
int64_t logIndex);

void LoadDeletedInodes();

MetaStatusCode GetOrModifyS3ChunkInfo(
uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add,
const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap,
Expand Down
73 changes: 71 additions & 2 deletions curvefs/src/metaserver/inode_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ using ::curvefs::metaserver::storage::Prefix4ChunkIndexS3ChunkInfoList;
using ::curvefs::metaserver::storage::Prefix4InodeS3ChunkInfoList;
using ::curvefs::metaserver::storage::Prefix4InodeVolumeExtent;
using ::curvefs::metaserver::storage::Status;
using curvefs::metaserver::Time;

const char* InodeStorage::kInodeCountKey("count");

Expand All @@ -67,6 +68,7 @@ InodeStorage::InodeStorage(std::shared_ptr<KVStorage> kvStorage,
uint64_t nInode)
: kvStorage_(std::move(kvStorage)),
table4Inode_(nameGenerator->GetInodeTableName()),
table4DelInode_(nameGenerator->GetDelInodeTableName()),
table4S3ChunkInfo_(nameGenerator->GetS3ChunkInfoTableName()),
table4VolumeExtent_(nameGenerator->GetVolumeExtentTableName()),
table4InodeAuxInfo_(nameGenerator->GetInodeAuxInfoTableName()),
Expand Down Expand Up @@ -185,9 +187,78 @@ MetaStatusCode InodeStorage::Insert(const Inode& inode, int64_t logIndex) {
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

MetaStatusCode InodeStorage::AddDeletedInode(
const Key4Inode& keyInode, uint64_t dtime) {
WriteLockGuard lg(rwLock_);
std::string skey = conv_.SerializeToString(keyInode);
VLOG(9) << "update deleting key, " << keyInode.inodeId << ", " << dtime;
const char* step = "Begin transaction";
std::shared_ptr<storage::StorageTransaction> txn;
txn = kvStorage_->BeginTransaction();
if (txn == nullptr) {
LOG(ERROR) << "Begin transaction failed";
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}
Time dtimeInfo;
dtimeInfo.set_sec(dtime);
dtimeInfo.set_nsec(0);
auto rc = txn->HSet(table4DelInode_, skey, dtimeInfo);
step = "insert inode ";
if (rc.ok()) {
rc = txn->Commit();
step = "commit";
}
if (rc.ok()) {
VLOG(9) << "set deleting key ok";
return MetaStatusCode::OK;
}
LOG(ERROR) << step << "failed, status = " << rc.ToString();
if (!txn->Rollback().ok()) {
LOG(ERROR) << "Rollback delete inode transaction failed, status = "
<< rc.ToString();
}
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

MetaStatusCode InodeStorage::RemoveDeletedInode(const Key4Inode& key) {
WriteLockGuard lg(rwLock_);
std::string skey = conv_.SerializeToString(key);
VLOG(9) << "clear deleting key start, " << skey;
std::shared_ptr<storage::StorageTransaction> txn = nullptr;
const char* step = "Begin transaction";
txn = kvStorage_->BeginTransaction();
if (txn == nullptr) {
LOG(ERROR) << "Begin transaction failed";
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}
step = "Delete inode from transaction";
auto s = txn->HDel(table4DelInode_, skey);
if (s.ok()) {
step = "Delete inode";
s = txn->Commit();
}
if (s.ok()) {
VLOG(9) << "clear deleting key ok, " << skey;
return MetaStatusCode::OK;
}
LOG(ERROR) << step << " failed, status = " << s.ToString();
if (!txn->Rollback().ok()) {
LOG(ERROR) << "Rollback delete inode transaction failed, status = "
<< s.ToString();
}
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

void InodeStorage::LoadDeletedInodes(std::map<std::string, uint64_t> * inodes) {
VLOG(6) << "load deleted key start with: " << table4DelInode_;
kvStorage_->GetPrefix(inodes, table4DelInode_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parse of key and value can do here and return the final result map<inodeId, dtime> is ok.

VLOG(6) << "load deleted over";
}

MetaStatusCode InodeStorage::Get(const Key4Inode& key, Inode* inode) {
ReadLockGuard lg(rwLock_);
std::string skey = conv_.SerializeToString(key);

Status s = kvStorage_->HGet(table4Inode_, skey, inode);
if (s.ok()) {
return MetaStatusCode::OK;
Expand Down Expand Up @@ -471,7 +542,6 @@ MetaStatusCode InodeStorage::Clear() {
// because if we fail stop, we will replay
// raft logs and clear it again
WriteLockGuard lg(rwLock_);

Status s = kvStorage_->HClear(table4Inode_);
if (!s.ok()) {
LOG(ERROR) << "InodeStorage clear inode table failed, status = "
Expand All @@ -492,7 +562,6 @@ MetaStatusCode InodeStorage::Clear() {
<< s.ToString();
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

s = kvStorage_->HClear(table4InodeAuxInfo_);
if (!s.ok()) {
LOG(ERROR)
Expand Down
14 changes: 14 additions & 0 deletions curvefs/src/metaserver/inode_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <cstdint>
#include <functional>
#include <list>
#include <map>
#include <memory>
#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -76,6 +77,18 @@ class InodeStorage {
*/
MetaStatusCode Insert(const Inode& inode, int64_t logIndex);

/**
* @brief update deleting inode key in storage
* @param[in] inode: the inode want to update
* @param[in] logIndex: the index of raft log
* @return
*/
MetaStatusCode AddDeletedInode(const Key4Inode& inode, uint64_t dtime);

MetaStatusCode RemoveDeletedInode(const Key4Inode& key);

void LoadDeletedInodes(std::map<std::string, uint64_t> * inodes);

/**
* @brief get inode from storage
* @param[in] key: the key of inode want to get
Expand Down Expand Up @@ -237,6 +250,7 @@ class InodeStorage {
RWLock rwLock_;
std::shared_ptr<KVStorage> kvStorage_;
std::string table4Inode_;
std::string table4DelInode_;
std::string table4S3ChunkInfo_;
std::string table4VolumeExtent_;
std::string table4InodeAuxInfo_;
Expand Down
15 changes: 15 additions & 0 deletions curvefs/src/metaserver/metastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <glog/logging.h>
#include <sys/types.h>

#include <bitset>
#include <memory>
#include <thread> // NOLINT
#include <unordered_map>
Expand Down Expand Up @@ -60,6 +61,7 @@ using KVStorage = ::curvefs::metaserver::storage::KVStorage;
using Key4S3ChunkInfoList = ::curvefs::metaserver::storage::Key4S3ChunkInfoList;

using ::curvefs::metaserver::storage::MemoryStorage;
using ::curvefs::metaserver::storage::NameGenerator;
using ::curvefs::metaserver::storage::RocksDBStorage;
using ::curvefs::metaserver::storage::StorageOptions;

Expand Down Expand Up @@ -147,6 +149,7 @@ bool MetaStoreImpl::Load(const std::string &pathname) {
}

startCompacts();

return true;
}

Expand Down Expand Up @@ -933,5 +936,17 @@ bool MetaStoreImpl::InitStorage() {
return kvStorage_->Open();
}

void MetaStoreImpl::LoadDeletedInodes() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoadDeletedInodes is the responsibility of InodeManager, Each partition has one InodeManager and InodeStorage, you can load deleted inodes with InodeManager and add to trash_ in InodeManager by pass and the deleted inode loaded are all belong to this partition and trash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix

VLOG(6) << "load deleted inodes start.";
WriteLockGuard writeLockGuard(rwLock_);
MetaStatusCode status;
for (auto it = partitionMap_.begin(); it != partitionMap_.end(); it++) {
uint32_t partitionId = it->second->GetPartitionId();
VLOG(6) << "load deleted inodes, partitionId: " << partitionId;
it->second->LoadDeletedInodes();
}
VLOG(6) << "load deleted inodes end.";
}

} // namespace metaserver
} // namespace curvefs
2 changes: 2 additions & 0 deletions curvefs/src/metaserver/metastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class MetaStore {
virtual bool SaveData(const std::string& dir,
std::vector<std::string>* files) = 0;
virtual bool Clear() = 0;
virtual void LoadDeletedInodes() {}
virtual bool Destroy() = 0;
virtual MetaStatusCode CreatePartition(
const CreatePartitionRequest* request,
Expand Down Expand Up @@ -236,6 +237,7 @@ class MetaStoreImpl : public MetaStore {
std::vector<std::string>* files) override;
bool Clear() override;
bool Destroy() override;
void LoadDeletedInodes() override;

MetaStatusCode CreatePartition(const CreatePartitionRequest* request,
CreatePartitionResponse* response,
Expand Down
4 changes: 4 additions & 0 deletions curvefs/src/metaserver/partition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest& request,
return ret;
}

void Partition::LoadDeletedInodes() {
inodeManager_->LoadDeletedInodes();
}

MetaStatusCode Partition::GetOrModifyS3ChunkInfo(
uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add,
const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap,
Expand Down
2 changes: 2 additions & 0 deletions curvefs/src/metaserver/partition.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Partition {

MetaStatusCode GetInode(uint32_t fsId, uint64_t inodeId, Inode* inode);

void LoadDeletedInodes();

MetaStatusCode GetInodeAttr(uint32_t fsId, uint64_t inodeId,
InodeAttr* attr);

Expand Down
6 changes: 6 additions & 0 deletions curvefs/src/metaserver/storage/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <inttypes.h>
#include <glog/logging.h>

#include <bitset>
#include <cstring>
#include <string>
#include <vector>
Expand Down Expand Up @@ -57,6 +58,7 @@ static bool CompareType(const std::string& str, KEY_TYPE keyType) {

NameGenerator::NameGenerator(uint32_t partitionId)
: tableName4Inode_(Format(kTypeInode, partitionId)),
tableName4DelInode_(Format(kTypeDelInode, partitionId)),
tableName4DeallocatableIndoe_(
Format(kTypeDeallocatableInode, partitionId)),
tableName4DeallocatableBlockGroup_(
Expand All @@ -76,6 +78,10 @@ std::string NameGenerator::GetInodeTableName() const {
return tableName4Inode_;
}

std::string NameGenerator::GetDelInodeTableName() const {
return tableName4DelInode_;
}

std::string NameGenerator::GetDeallocatableInodeTableName() const {
return tableName4DeallocatableIndoe_;
}
Expand Down
7 changes: 5 additions & 2 deletions curvefs/src/metaserver/storage/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace curvefs {
namespace metaserver {

class MetaStoreFStream;

namespace storage {

enum KEY_TYPE : unsigned char {
Expand All @@ -52,7 +51,8 @@ enum KEY_TYPE : unsigned char {
kTypeInodeCount = 11,
kTypeDentryCount = 12,
kTypeTxLock = 13,
kTypeTxWrite = 14
kTypeTxWrite = 14,
kTypeDelInode = 15
};

// NOTE: you must generate all table name by NameGenerator class for
Expand All @@ -64,6 +64,8 @@ class NameGenerator {

std::string GetInodeTableName() const;

std::string GetDelInodeTableName() const;

std::string GetDeallocatableInodeTableName() const;

std::string GetS3ChunkInfoTableName() const;
Expand Down Expand Up @@ -99,6 +101,7 @@ class NameGenerator {

private:
std::string tableName4Inode_;
std::string tableName4DelInode_;
std::string tableName4DeallocatableIndoe_;
std::string tableName4DeallocatableBlockGroup_;
std::string tableName4S3ChunkInfo_;
Expand Down
Loading