Skip to content

Commit

Permalink
curvefs/client: fix trash bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
wuhongsong committed Nov 28, 2023
1 parent 6098f55 commit f79eb25
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 15 deletions.
5 changes: 3 additions & 2 deletions curvefs/src/metaserver/copyset/copyset_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ bool CopysetNode::Init(const CopysetNodeOptions& options) {
}

bool CopysetNode::Start() {
VLOG(3) << "copyset is starting, copyset: " << name_;
if (!raftNode_) {
LOG(ERROR) << "RaftNode didn't created, copyset: " << name_;
return false;
Expand All @@ -170,8 +171,8 @@ bool CopysetNode::Start() {
LOG(ERROR) << "Fail to init raft node, copyset: " << name_;
return false;
}

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

Expand Down
18 changes: 11 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) << "whs 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,16 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}
}
VLOG(9) << "UpdateInode success, " << request.ShortDebugString();

if (s3NeedTrash) {
VLOG(0) << "whs add need trash, " << request.ShortDebugString();

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

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

Expand Down
42 changes: 40 additions & 2 deletions curvefs/src/metaserver/inode_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,46 @@ 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()) {
// delete key
// rc = DeleteInternal(txn.get(), key);
rc = txn->HDel(table4Inode_, skey);
step = "delete 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) << "update deleting key ok, " << inode.inodeid();
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::Get(const Key4Inode& key, Inode* inode) {
ReadLockGuard lg(rwLock_);
std::string skey = conv_.SerializeToString(key);
Expand Down Expand Up @@ -471,7 +511,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 +531,6 @@ MetaStatusCode InodeStorage::Clear() {
<< s.ToString();
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
}

s = kvStorage_->HClear(table4InodeAuxInfo_);
if (!s.ok()) {
LOG(ERROR)
Expand Down
9 changes: 9 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,14 @@ 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);

/**
* @brief get inode from storage
* @param[in] key: the key of inode want to get
Expand Down
34 changes: 33 additions & 1 deletion curvefs/src/metaserver/metastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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 @@ -87,6 +88,8 @@ MetaStoreImpl::MetaStoreImpl(copyset::CopysetNode *node,
storageOptions_(storageOptions) {}

bool MetaStoreImpl::Load(const std::string &pathname) {
LOG(ERROR) << "whs load start";

// Load from raft snap file to memory
WriteLockGuard writeLockGuard(rwLock_);
MetaStoreFStream fstream(&partitionMap_, kvStorage_,
Expand Down Expand Up @@ -147,6 +150,9 @@ bool MetaStoreImpl::Load(const std::string &pathname) {
}

startCompacts();


LOG(ERROR) << "whs load end";
return true;
}

Expand Down Expand Up @@ -859,7 +865,33 @@ bool MetaStoreImpl::InitStorage() {
return false;
}

return kvStorage_->Open();

if (!kvStorage_->Open()) {
return false;
}

return true;
}

void MetaStoreImpl::BuildTrashList() {

std::shared_ptr<NameGenerator> nameGen = std::make_shared<NameGenerator>(0);

std::shared_ptr<InodeStorage> inodeStorage =
std::make_shared<InodeStorage>(kvStorage_, nameGen, 1);

auto trash = std::make_shared<TrashImpl>(inodeStorage);

TrashManager::GetInstance().BuildAbortTrash(kvStorage_, trash);
}

void MetaStoreImpl::LoadAll() {
LOG(ERROR) << "InitStorage start";
// kvStorage_->LoadAll();
LOG(ERROR) << "InitStorage start01";

BuildTrashList();
LOG(ERROR) << "InitStorage start02";
}

} // namespace metaserver
Expand Down
4 changes: 4 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 LoadAll() {};
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 LoadAll() override;

MetaStatusCode CreatePartition(const CreatePartitionRequest* request,
CreatePartitionResponse* response,
Expand Down Expand Up @@ -351,6 +353,8 @@ class MetaStoreImpl : public MetaStore {
// REQUIRES: rwLock_ is held with write permission
bool ClearInternal();

void BuildTrashList();

private:
RWLock rwLock_; // protect partitionMap_
std::shared_ptr<KVStorage> kvStorage_;
Expand Down
52 changes: 51 additions & 1 deletion 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 @@ -187,7 +188,7 @@ std::string RocksDBStorage::ToInternalKey(const std::string& name,
std::ostringstream oss;
oss << iname << kDelimiter_ << key;
std::string ikey = oss.str();
VLOG(9) << "ikey = " << ikey << " (ordered = " << ordered
VLOG(0) << "whs ikey = " << ikey << " (ordered = " << ordered
<< ", name = " << name << ", key = " << key << ")"
<< ", size = " << ikey.size();
return ikey;
Expand Down Expand Up @@ -241,6 +242,32 @@ 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(0) << "whs set deleting key = " << deletingKey << ", ikey " << ikey;
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::Del(const std::string& name,
const std::string& key,
bool ordered) {
Expand Down Expand Up @@ -273,6 +300,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 +540,21 @@ bool RocksDBStorage::Recover(const std::string& dir) {
return true;
}

void RocksDBStorage::LoadAll(std::list<std::string>& item) {
LOG(INFO) << "LoadAll storage from";
std::string sprefix = "deleting_";
rocksdb::Iterator* it1 = db_->NewIterator(rocksdb::ReadOptions());
for (it1->SeekToFirst(); it1->Valid(); it1->Next()) {
std::string key = it1->key().ToString();
if (curve::common::StringStartWith(key, sprefix)) {
VLOG(9) << "whs recovery: " << key;
item.push_back(key);
}
LOG(ERROR) << "whs recovery: " << key;
}
// GetPrefix(prefix, false)
}

} // namespace storage
} // namespace metaserver
} // namespace curvefs
19 changes: 19 additions & 0 deletions curvefs/src/metaserver/storage/rocksdb_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class RocksDBStorage : public KVStorage, public StorageTransaction {

bool Close() override;

void LoadAll(std::list<std::string>& item) override;

STORAGE_TYPE Type() override;

StorageOptions GetStorageOptions() const override;
Expand All @@ -92,6 +94,10 @@ class RocksDBStorage : public KVStorage, public StorageTransaction {
const std::string& key,
const ValueType& value) override;

Status HSetDeleting(const std::string& name,
const std::string& key,
const ValueType& value) override;

Status HDel(const std::string& name, const std::string& key) override;

std::shared_ptr<Iterator> HGetAll(const std::string& name) override;
Expand All @@ -100,6 +106,8 @@ class RocksDBStorage : public KVStorage, public StorageTransaction {

Status HClear(const std::string& name) override;

std::shared_ptr<Iterator> GetPrefix(const std::string& prefix,
bool ordered) override;
// ordered
Status SGet(const std::string& name,
const std::string& key,
Expand Down Expand Up @@ -156,6 +164,11 @@ class RocksDBStorage : public KVStorage, public StorageTransaction {
const ValueType& value,
bool ordered);

Status SetDeleting(const std::string& name,
const std::string& key,
const ValueType& value,
bool ordered);

Status Del(const std::string& name,
const std::string& key,
bool ordered);
Expand Down Expand Up @@ -219,6 +232,12 @@ inline Status RocksDBStorage::HSet(const std::string& name,
return Set(name, key, value, false);
}

inline Status RocksDBStorage::HSetDeleting(const std::string& name,
const std::string& key,
const ValueType& value) {
return SetDeleting(name, key, value, false);
}

inline Status RocksDBStorage::HDel(const std::string& name,
const std::string& key) {
return Del(name, key, false);
Expand Down
Loading

0 comments on commit f79eb25

Please sign in to comment.