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

chunkserver : fix bug in trash when collecting chunknum miss chunk in… #417

Merged
merged 1 commit into from
Jul 9, 2021
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
32 changes: 18 additions & 14 deletions src/chunkserver/trash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,27 +306,31 @@ bool Trash::IsWALFile(const std::string &fileName) {
}

uint32_t Trash::CountChunkNumInCopyset(const std::string &copysetPath) {
auto count = [&](const std::string& path) {
std::vector<std::string> chunks;
localFileSystem_->List(path, &chunks);
std::vector<std::string> files;
if (0 != localFileSystem_->List(copysetPath, &files)) {
LOG(ERROR) << "Trash failed to list files in " << copysetPath;
return 0;
}

uint32_t chunkNum = 0;
for (auto& chunk : chunks) {
// Traverse subdirectories
uint32_t chunkNum = 0;
for (auto &file : files) {
std::string filePath = copysetPath + "/" + file;
bool isDir = localFileSystem_->DirExists(filePath);
if (!isDir) {
// valid: chunkfile, snapshotfile, walfile
if (!(IsChunkOrSnapShotFile(chunk) || IsWALFile(chunk))) {
if (!(IsChunkOrSnapShotFile(file) ||
IsWALFile(file))) {
LOG(WARNING) << "Trash find a illegal file:"
<< chunk << " in " << path
<< ", filename: " << chunk;
<< file << " in " << copysetPath;
continue;
}
++chunkNum;
} else {
chunkNum += CountChunkNumInCopyset(filePath);
}

return chunkNum;
};

return count(copysetPath + "/" + RAFT_DATA_DIR)
+ count(copysetPath + "/" + RAFT_LOG_DIR);
}
return chunkNum;
}

} // namespace chunkserver
Expand Down
69 changes: 62 additions & 7 deletions test/chunkserver/trash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,6 @@ TEST_F(TrashTest, recycle_copyset_dir_list_err) {
EXPECT_CALL(*lfs, Mkdir(trashPath)).WillOnce(Return(0));
EXPECT_CALL(*lfs, Rename(dirPath, _, 0)).WillOnce(Return(0));
EXPECT_CALL(*lfs, List(_, _))
.WillOnce(Return(-1))
.WillOnce(Return(-1));
ASSERT_EQ(0, trash->RecycleCopySet(dirPath));
}
Expand All @@ -571,7 +570,6 @@ TEST_F(TrashTest, recycle_copyset_dir_ok) {
EXPECT_CALL(*lfs, Mkdir(trashPath)).WillOnce(Return(0));
EXPECT_CALL(*lfs, Rename(dirPath, _, 0)).WillOnce(Return(0));
EXPECT_CALL(*lfs, List(_, _))
.WillOnce(Return(0))
.WillOnce(Return(0));
ASSERT_EQ(0, trash->RecycleCopySet(dirPath));
}
Expand All @@ -586,6 +584,7 @@ TEST_F(TrashTest, DISABLED_test_concurrenct) {
TEST_F(TrashTest, test_chunk_num_statistic) {
std::string trashPath = "./runlog/trash_test0/trash";
std::vector<std::string> copysets{"4294967493.55555", "4294967494.55555"};
std::vector<std::string> dirs{"data", "log"};
std::vector<std::string> chunks1{"chunk_100", "chunk_101"};
std::vector<std::string> chunks2{"chunk_200_snap_1", "abc"};
std::vector<std::string> logfiles1{
Expand All @@ -594,14 +593,30 @@ TEST_F(TrashTest, test_chunk_num_statistic) {
std::vector<std::string> logfiles2{};

// (1) chunk exists when init
// trash/
Copy link
Contributor

Choose a reason for hiding this comment

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

Not include the scenario bug triggered? Like '/data/chunkserver1/recycler/8589936511.1625810976/raft_snapshot/temp/data/chunk_10000'

Copy link
Member Author

Choose a reason for hiding this comment

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

done

// 4294967493.55555/
// data/
// chunk_100, chunk_101 +2
// log/
// curve_log_10086_10087, +1
// curve_log_inprogress_10088, +1
// log_10083_10084,
// log_inprogress_10085
// 4294967494.55555/
// data/
// chunk_200_snap_1, abc +1
// log/

using item4list = struct{
std::string subdir;
std::vector<std::string>& names;
};
std::vector<item4list> action4List{
{ "", copysets },
{ "/4294967493.55555", dirs},
{ "/4294967493.55555/data", chunks1 },
{ "/4294967493.55555/log", logfiles1 },
{ "/4294967494.55555", dirs},
{ "/4294967494.55555/data", chunks2 },
{ "/4294967494.55555/log", logfiles2 },
};
Expand All @@ -611,24 +626,64 @@ TEST_F(TrashTest, test_chunk_num_statistic) {
.WillOnce(DoAll(SetArgPointee<1>(it.names), Return(0)));
}

EXPECT_CALL(*lfs, DirExists(_))
.WillOnce(Return(true)) // data
.WillOnce(Return(false)) // chunk_100
.WillOnce(Return(false)) // chunk_101
.WillOnce(Return(true)) // log
.WillOnce(Return(false)) // curve_log_10086_10087
.WillOnce(Return(false)) // curve_log_inprogress_10088_10088
.WillOnce(Return(false)) // log_10083_10084
.WillOnce(Return(false)) // log_inprogress_10085
.WillOnce(Return(true)) // data
.WillOnce(Return(false)) // chunk_200_snap_1
.WillOnce(Return(false)) // abc
.WillOnce(Return(true)); // log

trash->Init(ops);
ASSERT_EQ(5, trash->GetChunkNum());

// (2) recycle copyset
std::string copysetDir = "./runlog/trash_test0/copysets/4294967495";
// trash_test0/copysets/4294967495/
// data/
// chunk_333, chunk_444 +2
// log/
// curve_log_10086_10087 +1
// raft_snapshot/temp/data/
// chunk_10000 +1
//
std::string copysetDir = "/copysets/4294967495";
std::vector<std::string> dirs2{"data", "log", "raft_snapshot"};
EXPECT_CALL(*lfs, DirExists(_))
.WillOnce(Return(true))
.WillOnce(Return(false))
.WillOnce(Return(true)) // data
.WillOnce(Return(false))
.WillOnce(Return(false))
.WillOnce(Return(true)) // log
.WillOnce(Return(false))
.WillOnce(Return(true)) // raft_snapshot
.WillOnce(Return(true)) // temp
.WillOnce(Return(true)) // data
.WillOnce(Return(false));
std::string trashedCopysetDir;

std::string trashedCopysetDir = "/trash_test0/copysets/4294967495";
EXPECT_CALL(*lfs, Rename(copysetDir, _, 0))
.WillOnce(DoAll(SaveArg<1>(&trashedCopysetDir), Return(0)));
std::vector<std::string> chunks3{"chunk_333", "chunk_444"};
std::vector<std::string> logfiles3{"curve_log_10086_10087"};
std::vector<std::string> temp{"temp"};
std::vector<std::string> data{"data"};
std::vector<std::string> chunks4{"chunk_10000"};
EXPECT_CALL(*lfs, List(_, _))
.WillOnce(DoAll(SetArgPointee<1>(dirs2), Return(0)))
.WillOnce(DoAll(SetArgPointee<1>(chunks3), Return(0)))
.WillOnce(DoAll(SetArgPointee<1>(logfiles3), Return(0)));
.WillOnce(DoAll(SetArgPointee<1>(logfiles3), Return(0)))
.WillOnce(DoAll(SetArgPointee<1>(temp), Return(0)))
.WillOnce(DoAll(SetArgPointee<1>(data), Return(0)))
.WillOnce(DoAll(SetArgPointee<1>(chunks4), Return(0)));
ASSERT_EQ(0, trash->RecycleCopySet(copysetDir));
ASSERT_EQ(8, trash->GetChunkNum());
ASSERT_EQ(9, trash->GetChunkNum());

// (3) delete eligible copyset
std::string trashedCopysetName =
Expand Down Expand Up @@ -702,7 +757,7 @@ TEST_F(TrashTest, test_chunk_num_statistic) {
.Times(0);

trash->DeleteEligibleFileInTrash();
ASSERT_EQ(6, trash->GetChunkNum());
ASSERT_EQ(7, trash->GetChunkNum());
}

} // namespace chunkserver
Expand Down