Skip to content

Commit b36a0aa

Browse files
committed
curvefs/metaserver: fix trash bugs
1 parent 0c722ae commit b36a0aa

19 files changed

+244
-30
lines changed

curvefs/src/metaserver/copyset/copyset_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ bool CopysetNode::Start() {
170170
LOG(ERROR) << "Fail to init raft node, copyset: " << name_;
171171
return false;
172172
}
173-
173+
metaStore_->LoadDeletedInodes();
174174
LOG(INFO) << "Run copyset success, copyset: " << name_;
175175
return true;
176176
}

curvefs/src/metaserver/inode_manager.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,6 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
391391
}
392392
}
393393

394-
if (needAddTrash) {
395-
trash_->Add(old.inodeid(), old.dtime());
396-
--(*type2InodeNum_)[old.type()];
397-
}
398-
399394
const S3ChunkInfoMap &map2add = request.s3chunkinfoadd();
400395
const S3ChunkInfoList *list2add;
401396
VLOG(9) << "UpdateInode inode " << old.inodeid() << " map2add size "
@@ -446,10 +441,27 @@ MetaStatusCode InodeManager::UpdateInode(const UpdateInodeRequest& request,
446441
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
447442
}
448443
}
444+
445+
if (needAddTrash) {
446+
trash_->Add(old.inodeid(), old.dtime(), false);
447+
--(*type2InodeNum_)[old.type()];
448+
}
449+
449450
VLOG(9) << "UpdateInode success, " << request.ShortDebugString();
450451
return MetaStatusCode::OK;
451452
}
452453

454+
void InodeManager::LoadDeletedInodes() {
455+
std::map<std::string, uint64_t> items;
456+
inodeStorage_->LoadDeletedInodes(&items);
457+
VLOG(3) << "build trash items size: " << items.size();
458+
std::vector<std::string> names;
459+
for (auto iter : items) {
460+
curve::common::SplitString(iter.first , ":", &names);
461+
trash_->Add(std::stoull(names[names.size() - 1 ]), iter.second, true);
462+
}
463+
}
464+
453465
MetaStatusCode InodeManager::GetOrModifyS3ChunkInfo(
454466
uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add,
455467
const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap,

curvefs/src/metaserver/inode_manager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <atomic>
2929
#include <list>
30+
#include <map>
3031
#include <memory>
3132
#include <string>
3233
#include <vector>
@@ -96,6 +97,8 @@ class InodeManager {
9697
MetaStatusCode UpdateInode(const UpdateInodeRequest& request,
9798
int64_t logIndex);
9899

100+
void LoadDeletedInodes();
101+
99102
MetaStatusCode GetOrModifyS3ChunkInfo(
100103
uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add,
101104
const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap,

curvefs/src/metaserver/inode_storage.cpp

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ using ::curvefs::metaserver::storage::Prefix4ChunkIndexS3ChunkInfoList;
5757
using ::curvefs::metaserver::storage::Prefix4InodeS3ChunkInfoList;
5858
using ::curvefs::metaserver::storage::Prefix4InodeVolumeExtent;
5959
using ::curvefs::metaserver::storage::Status;
60+
using curvefs::metaserver::Time;
6061

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

@@ -67,6 +68,7 @@ InodeStorage::InodeStorage(std::shared_ptr<KVStorage> kvStorage,
6768
uint64_t nInode)
6869
: kvStorage_(std::move(kvStorage)),
6970
table4Inode_(nameGenerator->GetInodeTableName()),
71+
table4DelInode_(nameGenerator->GetDelInodeTableName()),
7072
table4S3ChunkInfo_(nameGenerator->GetS3ChunkInfoTableName()),
7173
table4VolumeExtent_(nameGenerator->GetVolumeExtentTableName()),
7274
table4InodeAuxInfo_(nameGenerator->GetInodeAuxInfoTableName()),
@@ -185,9 +187,78 @@ MetaStatusCode InodeStorage::Insert(const Inode& inode, int64_t logIndex) {
185187
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
186188
}
187189

190+
MetaStatusCode InodeStorage::AddDeletedInode(
191+
const Key4Inode& keyInode, uint64_t dtime) {
192+
WriteLockGuard lg(rwLock_);
193+
std::string skey = conv_.SerializeToString(keyInode);
194+
VLOG(9) << "update deleting key, " << keyInode.inodeId << ", " << dtime;
195+
const char* step = "Begin transaction";
196+
std::shared_ptr<storage::StorageTransaction> txn;
197+
txn = kvStorage_->BeginTransaction();
198+
if (txn == nullptr) {
199+
LOG(ERROR) << "Begin transaction failed";
200+
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
201+
}
202+
Time dtimeInfo;
203+
dtimeInfo.set_sec(dtime);
204+
dtimeInfo.set_nsec(0);
205+
auto rc = txn->HSet(table4DelInode_, skey, dtimeInfo);
206+
step = "insert inode ";
207+
if (rc.ok()) {
208+
rc = txn->Commit();
209+
step = "commit";
210+
}
211+
if (rc.ok()) {
212+
VLOG(9) << "set deleting key ok";
213+
return MetaStatusCode::OK;
214+
}
215+
LOG(ERROR) << step << "failed, status = " << rc.ToString();
216+
if (!txn->Rollback().ok()) {
217+
LOG(ERROR) << "Rollback delete inode transaction failed, status = "
218+
<< rc.ToString();
219+
}
220+
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
221+
}
222+
223+
MetaStatusCode InodeStorage::RemoveDeletedInode(const Key4Inode& key) {
224+
WriteLockGuard lg(rwLock_);
225+
std::string skey = conv_.SerializeToString(key);
226+
VLOG(9) << "clear deleting key start, " << skey;
227+
std::shared_ptr<storage::StorageTransaction> txn = nullptr;
228+
const char* step = "Begin transaction";
229+
txn = kvStorage_->BeginTransaction();
230+
if (txn == nullptr) {
231+
LOG(ERROR) << "Begin transaction failed";
232+
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
233+
}
234+
step = "Delete inode from transaction";
235+
auto s = txn->HDel(table4DelInode_, skey);
236+
if (s.ok()) {
237+
step = "Delete inode";
238+
s = txn->Commit();
239+
}
240+
if (s.ok()) {
241+
VLOG(9) << "clear deleting key ok, " << skey;
242+
return MetaStatusCode::OK;
243+
}
244+
LOG(ERROR) << step << " failed, status = " << s.ToString();
245+
if (!txn->Rollback().ok()) {
246+
LOG(ERROR) << "Rollback delete inode transaction failed, status = "
247+
<< s.ToString();
248+
}
249+
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
250+
}
251+
252+
void InodeStorage::LoadDeletedInodes(std::map<std::string, uint64_t> * inodes) {
253+
VLOG(6) << "load deleted key start with: " << table4DelInode_;
254+
kvStorage_->GetPrefix(inodes, table4DelInode_);
255+
VLOG(6) << "oad deleted over";
256+
}
257+
188258
MetaStatusCode InodeStorage::Get(const Key4Inode& key, Inode* inode) {
189259
ReadLockGuard lg(rwLock_);
190260
std::string skey = conv_.SerializeToString(key);
261+
191262
Status s = kvStorage_->HGet(table4Inode_, skey, inode);
192263
if (s.ok()) {
193264
return MetaStatusCode::OK;
@@ -471,7 +542,6 @@ MetaStatusCode InodeStorage::Clear() {
471542
// because if we fail stop, we will replay
472543
// raft logs and clear it again
473544
WriteLockGuard lg(rwLock_);
474-
475545
Status s = kvStorage_->HClear(table4Inode_);
476546
if (!s.ok()) {
477547
LOG(ERROR) << "InodeStorage clear inode table failed, status = "
@@ -492,7 +562,6 @@ MetaStatusCode InodeStorage::Clear() {
492562
<< s.ToString();
493563
return MetaStatusCode::STORAGE_INTERNAL_ERROR;
494564
}
495-
496565
s = kvStorage_->HClear(table4InodeAuxInfo_);
497566
if (!s.ok()) {
498567
LOG(ERROR)

curvefs/src/metaserver/inode_storage.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cstdint>
2828
#include <functional>
2929
#include <list>
30+
#include <map>
3031
#include <memory>
3132
#include <string>
3233
#include <unordered_map>
@@ -76,6 +77,18 @@ class InodeStorage {
7677
*/
7778
MetaStatusCode Insert(const Inode& inode, int64_t logIndex);
7879

80+
/**
81+
* @brief update deleting inode key in storage
82+
* @param[in] inode: the inode want to update
83+
* @param[in] logIndex: the index of raft log
84+
* @return
85+
*/
86+
MetaStatusCode AddDeletedInode(const Key4Inode& inode, uint64_t dtime);
87+
88+
MetaStatusCode RemoveDeletedInode(const Key4Inode& key);
89+
90+
void LoadDeletedInodes(std::map<std::string, uint64_t> * inodes);
91+
7992
/**
8093
* @brief get inode from storage
8194
* @param[in] key: the key of inode want to get
@@ -237,6 +250,7 @@ class InodeStorage {
237250
RWLock rwLock_;
238251
std::shared_ptr<KVStorage> kvStorage_;
239252
std::string table4Inode_;
253+
std::string table4DelInode_;
240254
std::string table4S3ChunkInfo_;
241255
std::string table4VolumeExtent_;
242256
std::string table4InodeAuxInfo_;

curvefs/src/metaserver/metastore.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <glog/logging.h>
2626
#include <sys/types.h>
2727

28+
#include <bitset>
2829
#include <memory>
2930
#include <thread> // NOLINT
3031
#include <unordered_map>
@@ -60,6 +61,7 @@ using KVStorage = ::curvefs::metaserver::storage::KVStorage;
6061
using Key4S3ChunkInfoList = ::curvefs::metaserver::storage::Key4S3ChunkInfoList;
6162

6263
using ::curvefs::metaserver::storage::MemoryStorage;
64+
using ::curvefs::metaserver::storage::NameGenerator;
6365
using ::curvefs::metaserver::storage::RocksDBStorage;
6466
using ::curvefs::metaserver::storage::StorageOptions;
6567

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

149151
startCompacts();
152+
150153
return true;
151154
}
152155

@@ -933,5 +936,17 @@ bool MetaStoreImpl::InitStorage() {
933936
return kvStorage_->Open();
934937
}
935938

939+
void MetaStoreImpl::LoadDeletedInodes() {
940+
VLOG(6) << "load deleted inodes start.";
941+
WriteLockGuard writeLockGuard(rwLock_);
942+
MetaStatusCode status;
943+
for (auto it = partitionMap_.begin(); it != partitionMap_.end(); it++) {
944+
uint32_t partitionId = it->second->GetPartitionId();
945+
VLOG(6) << "load deleted inodes, partitionId: " << partitionId;
946+
it->second->LoadDeletedInodes();
947+
}
948+
VLOG(6) << "load deleted inodes end.";
949+
}
950+
936951
} // namespace metaserver
937952
} // namespace curvefs

curvefs/src/metaserver/metastore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class MetaStore {
117117
virtual bool SaveData(const std::string& dir,
118118
std::vector<std::string>* files) = 0;
119119
virtual bool Clear() = 0;
120+
virtual void LoadDeletedInodes() {}
120121
virtual bool Destroy() = 0;
121122
virtual MetaStatusCode CreatePartition(
122123
const CreatePartitionRequest* request,
@@ -236,6 +237,7 @@ class MetaStoreImpl : public MetaStore {
236237
std::vector<std::string>* files) override;
237238
bool Clear() override;
238239
bool Destroy() override;
240+
void LoadDeletedInodes() override;
239241

240242
MetaStatusCode CreatePartition(const CreatePartitionRequest* request,
241243
CreatePartitionResponse* response,

curvefs/src/metaserver/partition.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ MetaStatusCode Partition::UpdateInode(const UpdateInodeRequest& request,
376376
return ret;
377377
}
378378

379+
void Partition::LoadDeletedInodes() {
380+
inodeManager_->LoadDeletedInodes();
381+
}
382+
379383
MetaStatusCode Partition::GetOrModifyS3ChunkInfo(
380384
uint32_t fsId, uint64_t inodeId, const S3ChunkInfoMap& map2add,
381385
const S3ChunkInfoMap& map2del, bool returnS3ChunkInfoMap,

curvefs/src/metaserver/partition.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class Partition {
109109

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

112+
void LoadDeletedInodes();
113+
112114
MetaStatusCode GetInodeAttr(uint32_t fsId, uint64_t inodeId,
113115
InodeAttr* attr);
114116

curvefs/src/metaserver/storage/converter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <inttypes.h>
2424
#include <glog/logging.h>
2525

26+
#include <bitset>
2627
#include <cstring>
2728
#include <string>
2829
#include <vector>
@@ -57,6 +58,7 @@ static bool CompareType(const std::string& str, KEY_TYPE keyType) {
5758

5859
NameGenerator::NameGenerator(uint32_t partitionId)
5960
: tableName4Inode_(Format(kTypeInode, partitionId)),
61+
tableName4DelInode_(Format(kTypeDelInode, partitionId)),
6062
tableName4DeallocatableIndoe_(
6163
Format(kTypeDeallocatableInode, partitionId)),
6264
tableName4DeallocatableBlockGroup_(
@@ -76,6 +78,10 @@ std::string NameGenerator::GetInodeTableName() const {
7678
return tableName4Inode_;
7779
}
7880

81+
std::string NameGenerator::GetDelInodeTableName() const {
82+
return tableName4DelInode_;
83+
}
84+
7985
std::string NameGenerator::GetDeallocatableInodeTableName() const {
8086
return tableName4DeallocatableIndoe_;
8187
}

curvefs/src/metaserver/storage/converter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace curvefs {
3434
namespace metaserver {
3535

3636
class MetaStoreFStream;
37-
3837
namespace storage {
3938

4039
enum KEY_TYPE : unsigned char {
@@ -52,7 +51,8 @@ enum KEY_TYPE : unsigned char {
5251
kTypeInodeCount = 11,
5352
kTypeDentryCount = 12,
5453
kTypeTxLock = 13,
55-
kTypeTxWrite = 14
54+
kTypeTxWrite = 14,
55+
kTypeDelInode = 15
5656
};
5757

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

6565
std::string GetInodeTableName() const;
6666

67+
std::string GetDelInodeTableName() const;
68+
6769
std::string GetDeallocatableInodeTableName() const;
6870

6971
std::string GetS3ChunkInfoTableName() const;
@@ -99,6 +101,7 @@ class NameGenerator {
99101

100102
private:
101103
std::string tableName4Inode_;
104+
std::string tableName4DelInode_;
102105
std::string tableName4DeallocatableIndoe_;
103106
std::string tableName4DeallocatableBlockGroup_;
104107
std::string tableName4S3ChunkInfo_;

0 commit comments

Comments
 (0)