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 2, 2023
1 parent dd8ce61 commit dc5b193
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 19 deletions.
4 changes: 2 additions & 2 deletions curvefs/src/metaserver/copyset/copyset_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ bool CopysetNode::Start() {
LOG(ERROR) << "Fail to init raft node, copyset: " << name_;
return false;
}

LOG(INFO) << "Run copyset success, copyset: " << name_;
metaStore_->LoadDeletedInodes();
LOG(INFO) << "Run copyset start success, copyset: " << name_;
return true;
}

Expand Down
17 changes: 10 additions & 7 deletions curvefs/src/metaserver/inode_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ MetaStatusCode InodeManager::DeleteInode(uint32_t fsId, uint64_t inodeId,
MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
int64_t logIndex) {
CHECK_APPLIED();
VLOG(9) << "update inode, fsid: " << request.fsid()
VLOG(0) << "update inode, fsid: " << request.fsid()
<< ", inodeid: " << request.inodeid();
NameLockGuard lg(inodeLock_,
GetInodeLockName(request.fsid(), request.inodeid()));
Expand Down Expand Up @@ -388,11 +388,6 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
}
}

if (s3NeedTrash) {
trash_->Add(old.fsid(), 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 @@ -443,7 +438,15 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}
}
VLOG(9) << "UpdateInode success, " << request.ShortDebugString();

if (s3NeedTrash) {
inodeStorage_->UpdateDeletingKey(old, logIndex);
// wuhongsong:需要打开
// trash_->Add(old.fsid(), old.inodeid(), old.dtime());
--(*type2InodeNum_)[old.type()];
}

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

Expand Down
67 changes: 65 additions & 2 deletions curvefs/src/metaserver/inode_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,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->HSetDeleting(table4Inode_, 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(0) << "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->HClearDeleting(table4Inode_, 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 +536,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 +556,6 @@ MetaStatusCode InodeStorage::Clear() {
<< s.ToString();
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

s = kvStorage_->HClear(table4InodeAuxInfo_);
if (!s.ok()) {
LOG(ERROR)
Expand Down
16 changes: 16 additions & 0 deletions curvefs/src/metaserver/inode_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "curvefs/src/metaserver/storage/utils.h"
#include "src/common/concurrent/rw_lock.h"

#define DELETING_PREFIX "deleting_"
namespace curvefs {
namespace metaserver {

Expand Down Expand Up @@ -76,6 +77,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 @@ -187,6 +198,11 @@ class InodeStorage {
MetaStatusCode GetAllBlockGroup(
std::vector<DeallocatableBlockGroup>* deallocatableBlockGroupVec);

/*
std::shared_ptr<KVStorage> GetKVStorage() {
return kvStorage_;
}
*/
private:
MetaStatusCode UpdateInodeS3MetaSize(Transaction txn, uint32_t fsId,
uint64_t inodeId, uint64_t size4add,
Expand Down
26 changes: 26 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,28 @@ 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::stoul(names[names.size() - 2 ]),
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
1 change: 1 addition & 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
70 changes: 70 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 @@ -218,6 +219,7 @@ Status RocksDBStorage::Get(const std::string& name,
if (s.ok() && !value->ParseFromString(svalue)) {
return Status::ParsedFailed();
}

return ToStorageStatus(s);
}

Expand All @@ -241,6 +243,47 @@ Status RocksDBStorage::Set(const std::string& name,
return ToStorageStatus(s);
}

Status RocksDBStorage::SetDeleting(const std::string& name,
const std::string& key,
const ValueType& value,
bool ordered) {
std::string svalue;
if (!inited_) {
return Status::DBClosed();
} else if (!value.SerializeToString(&svalue)) {
return Status::SerializedFailed();
}

auto handle = GetColumnFamilyHandle(ordered);
std::string ikey = ToInternalKey(name, key, ordered);
std::string deletingKey = "deleting_" + ikey;
VLOG(9) << "whs set deleting key = " << deletingKey
<< ", ikey " << ikey << ", " << options_.dataDir;
RocksDBPerfGuard guard(OP_PUT);
ROCKSDB_NAMESPACE::Status s = InTransaction_ ?
txn_->Put(handle, deletingKey, svalue) :
db_->Put(dbWriteOptions_, handle, deletingKey, svalue);
return ToStorageStatus(s);
}

Status RocksDBStorage::ClearDeleting(const std::string& name,
const std::string& key,
bool ordered) {
if (!inited_) {
return Status::DBClosed();
}
std::string ikey = ToInternalKey(name, key, ordered);
std::string deletingKey = "deleting_" + ikey;
VLOG(9) << "whs clear deleting key = " << deletingKey
<< ", ikey " << ikey << ", " << options_.dataDir;
auto handle = GetColumnFamilyHandle(ordered);
RocksDBPerfGuard guard(OP_DELETE);
ROCKSDB_NAMESPACE::Status s = InTransaction_ ?
txn_->Delete(handle, deletingKey) :
db_->Delete(dbWriteOptions_, handle, deletingKey);
return ToStorageStatus(s);
}

Status RocksDBStorage::Del(const std::string& name,
const std::string& key,
bool ordered) {
Expand Down Expand Up @@ -273,6 +316,14 @@ std::shared_ptr<Iterator> RocksDBStorage::GetAll(const std::string& name,
this, std::move(ikey), 0, status, ordered);
}

std::shared_ptr<Iterator> RocksDBStorage::GetPrefix(const std::string& prefix,
bool ordered) {
int status = inited_ ? 0 : -1;
return std::make_shared<RocksDBStorageIterator>(
this, std::move(prefix), 0, status, ordered);
}


size_t RocksDBStorage::Size(const std::string& name, bool ordered) {
auto iterator = GetAll(name, ordered);
if (iterator->Status() != 0) {
Expand Down Expand Up @@ -505,6 +556,25 @@ bool RocksDBStorage::Recover(const std::string& dir) {
return true;
}

void RocksDBStorage::LoadDeletedInodes(std::list<std::string>& item) {
LOG(INFO) << "LoadAll storage from: " << options_.dataDir;
std::string sprefix = "deleting_";
rocksdb::Iterator* it1 = db_->NewIterator(rocksdb::ReadOptions());
int counts = 0;
for (it1->SeekToFirst(); it1->Valid(); it1->Next()) {
std::string key = it1->key().ToString();
if (curve::common::StringStartWith(key, sprefix)) {
VLOG(3) << "load key: " << key;
item.push_back(key);
}
VLOG(3) << "load key: " << key;
counts++;
}
LOG(INFO) << "LoadAll storage from. size: " << item.size()
<< ", " << counts << ", " << options_.dataDir;
// GetPrefix(prefix, false);
}

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

0 comments on commit dc5b193

Please sign in to comment.