Skip to content

Commit

Permalink
curvefs/metaserver: fix trash bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhongsong committed Dec 6, 2023
1 parent bfd5acb commit 914faf4
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 35 deletions.
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
12 changes: 7 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,6 +441,13 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}
}

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

VLOG(9) << "UpdateInode success, " << request.ShortDebugString();
return MetaStatusCode::OK;
}
Expand Down
68 changes: 66 additions & 2 deletions curvefs/src/metaserver/inode_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,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 +186,74 @@ MetaStatusCode InodeStorage::Insert(const Inode& inode, int64_t logIndex) {
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

MetaStatusCode InodeStorage::UpdateDeletingKey(
const Inode& inode, int64_t logIndex) {
WriteLockGuard lg(rwLock_);
Key4Inode key(inode.fsid(), inode.inodeid());
std::string skey = conv_.SerializeToString(key);
VLOG(9) << "update deleting key, " << inode.inodeid();
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;
}
auto rc = txn->HSet(table4DelInode_, skey , inode);
step = "insert inode ";
if (rc.ok()) {
rc = SetAppliedIndex(txn.get(), logIndex);
step = "Insert applied index to transaction";
}
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 != nullptr && !txn->Rollback().ok()) {
LOG(ERROR) << "Rollback delete inode transaction failed, status = "
<< rc.ToString();
}
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

MetaStatusCode InodeStorage::ClearDelKey(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;
}

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 +537,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 +557,6 @@ MetaStatusCode InodeStorage::Clear() {
<< s.ToString();
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

s = kvStorage_->HClear(table4InodeAuxInfo_);
if (!s.ok()) {
LOG(ERROR)
Expand Down
11 changes: 11 additions & 0 deletions curvefs/src/metaserver/inode_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ 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 UpdateDeletingKey(const Inode& inode, int64_t logIndex);

MetaStatusCode ClearDelKey(const Key4Inode& key);

/**
* @brief get inode from storage
* @param[in] key: the key of inode want to get
Expand Down Expand Up @@ -237,6 +247,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
25 changes: 25 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 @@ -866,5 +869,27 @@ bool MetaStoreImpl::InitStorage() {
return kvStorage_->Open();
}

void MetaStoreImpl::LoadDeletedInodes() {
VLOG(3) << "LoadDeletedInodes start";
std::list<std::string> items;
kvStorage_->LoadDeletedInodes(&items);
VLOG(3) << "build trash items size: " << items.size();
std::vector<std::string> names;
for (auto iter : items) {
curve::common::SplitString(iter , ":", &names);
char* tmp = const_cast<char*>(names[2].c_str());
uint32_t partitionId = *reinterpret_cast<uint32_t*>(tmp);
VLOG(3) << "partitionId is: " << partitionId;
std::shared_ptr<Trash> trash =
TrashManager::GetInstance().GetTrash(partitionId);
if (nullptr == trash) {
LOG(INFO) << "trash is null: " << partitionId;
continue;
}
trash->Add(std::stoull(names[names.size() - 1 ]), 0, true);
}
VLOG(3) << "LoadDeletedInodes over.";
}

} // 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 @@ -223,6 +224,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
18 changes: 18 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 @@ -54,6 +55,7 @@ static bool CompareType(const std::string& str, KEY_TYPE keyType) {

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

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

std::string NameGenerator::GetDeallocatableInodeTableName() const {
return tableName4DeallocatableIndoe_;
}
Expand Down Expand Up @@ -123,6 +129,13 @@ std::string NameGenerator::Format(KEY_TYPE type, uint32_t partitionId) {
return absl::StrCat(type, kDelimiter, absl::string_view(buf, sizeof(buf)));
}

std::string NameGenerator::FormatDel(KEY_TYPE type, uint32_t partitionId) {
char buf[sizeof(partitionId)];
std::memcpy(buf, reinterpret_cast<char*>(&partitionId), sizeof(buf));
return absl::StrCat(DELETEPREFIXINKV, type,
kDelimiter, absl::string_view(buf, sizeof(buf)));
}

Key4Inode::Key4Inode() : fsId(0), inodeId(0) {}

Key4Inode::Key4Inode(uint32_t fsId, uint64_t inodeId)
Expand All @@ -146,6 +159,11 @@ bool Key4Inode::ParseFromString(const std::string& value) {
StringToUl(items[1], &fsId) && StringToUll(items[2], &inodeId);
}

void Key4Inode::GetPrefix(bool ordered, std::string* value) {
*value = absl::StrCat(std::to_string(
ordered), ":" , DELETEPREFIXINKV);
}

std::string Prefix4AllInode::SerializeToString() const {
return absl::StrCat(keyType_, ":");
}
Expand Down
11 changes: 10 additions & 1 deletion curvefs/src/metaserver/storage/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ namespace curvefs {
namespace metaserver {

class MetaStoreFStream;

namespace storage {

#define DELETEPREFIXINKV "deleting:"

enum KEY_TYPE : unsigned char {
kTypeInode = 1,
kTypeS3ChunkInfo = 2,
Expand All @@ -61,6 +62,8 @@ class NameGenerator {

std::string GetInodeTableName() const;

std::string GetDelInodeTableName() const;

std::string GetDeallocatableInodeTableName() const;

std::string GetS3ChunkInfoTableName() const;
Expand All @@ -87,9 +90,12 @@ class NameGenerator {

private:
std::string Format(KEY_TYPE type, uint32_t partitionId);
std::string FormatDel(KEY_TYPE type, uint32_t partitionId);


private:
std::string tableName4Inode_;
std::string tableName4DelInode_;
std::string tableName4DeallocatableIndoe_;
std::string tableName4DeallocatableBlockGroup_;
std::string tableName4S3ChunkInfo_;
Expand All @@ -108,6 +114,7 @@ class StorageKey {

virtual std::string SerializeToString() const = 0;
virtual bool ParseFromString(const std::string& value) = 0;
virtual void GetPrefix(bool ordered, std::string* value) {}
};

/* rules for key serialization:
Expand Down Expand Up @@ -142,6 +149,8 @@ class Key4Inode : public StorageKey {

bool ParseFromString(const std::string& value) override;

void GetPrefix(bool ordered, std::string* value) override;

public:
static const KEY_TYPE keyType_ = kTypeInode;

Expand Down
26 changes: 26 additions & 0 deletions curvefs/src/metaserver/storage/rocksdb_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <iostream>
#include <unordered_map>

#include "src/common/string_util.h"
#include "src/common/timeutility.h"
#include "curvefs/src/metaserver/storage/utils.h"
#include "curvefs/src/metaserver/storage/storage.h"
Expand Down Expand Up @@ -209,6 +210,7 @@ Status RocksDBStorage::Get(const std::string& name,
ROCKSDB_NAMESPACE::Status s;
std::string svalue;
std::string ikey = ToInternalKey(name, key, ordered);
VLOG(9) << "Get key: " << ikey << ", " << options_.dataDir;
auto handle = GetColumnFamilyHandle(ordered);
{
RocksDBPerfGuard guard(OP_GET);
Expand All @@ -218,6 +220,7 @@ Status RocksDBStorage::Get(const std::string& name,
if (s.ok() && !value->ParseFromString(svalue)) {
return Status::ParsedFailed();
}

return ToStorageStatus(s);
}

Expand All @@ -234,6 +237,7 @@ Status RocksDBStorage::Set(const std::string& name,

auto handle = GetColumnFamilyHandle(ordered);
std::string ikey = ToInternalKey(name, key, ordered);
VLOG(9) << "set key: " << ikey << ", " << options_.dataDir;
RocksDBPerfGuard guard(OP_PUT);
ROCKSDB_NAMESPACE::Status s = InTransaction_ ?
txn_->Put(handle, ikey, svalue) :
Expand All @@ -250,6 +254,7 @@ Status RocksDBStorage::Del(const std::string& name,

std::string ikey = ToInternalKey(name, key, ordered);
auto handle = GetColumnFamilyHandle(ordered);
VLOG(9) << "del key: " << ikey << ", " << options_.dataDir;
RocksDBPerfGuard guard(OP_DELETE);
ROCKSDB_NAMESPACE::Status s = InTransaction_ ?
txn_->Delete(handle, ikey) :
Expand Down Expand Up @@ -505,6 +510,27 @@ bool RocksDBStorage::Recover(const std::string& dir) {
return true;
}

void RocksDBStorage::LoadDeletedInodes(std::list<std::string>* item) {
std::string sprefix;
Key4Inode key;
key.GetPrefix(0, &sprefix);

LOG(INFO) << "LoadAll storage from: "
<< options_.dataDir << ", " << sprefix;

int counts = 0;
rocksdb::Iterator* it = db_->NewIterator(rocksdb::ReadOptions());
for (it->Seek(sprefix); it->Valid() &&
it->key().starts_with(sprefix); it->Next()) {
std::string key = it->key().ToString();
item->push_back(key);
VLOG(3) << "load key: " << key << ", " << key.size();
counts++;
}
LOG(INFO) << "LoadAll storage from. size: " << item->size()
<< ", " << counts << ", " << options_.dataDir;
}

} // namespace storage
} // namespace metaserver
} // namespace curvefs
Loading

0 comments on commit 914faf4

Please sign in to comment.