Skip to content

Commit

Permalink
file allocated size multiply replicas num
Browse files Browse the repository at this point in the history
Change-Id: Ifaa36136ca5063780a58612abc45c0e12cf9ec98
  • Loading branch information
charisu committed Jul 10, 2020
1 parent 5a29152 commit 54aa447
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 54 deletions.
4 changes: 4 additions & 0 deletions proto/nameserver2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ message GetAllocatedSizeRequest {

message GetAllocatedSizeResponse {
required StatusCode statusCode = 1;
// 文件或目录的分配大小
optional uint64 allocatedSize = 2;
// 物理上分配的时间,因为对分配了1GB空间的文件来说
// 如果是三副本,实际上相当于分配了3GB物理空间
optional uint64 physicalAllocatedSize = 3;
}

message ClientInfo {
Expand Down
38 changes: 30 additions & 8 deletions src/mds/nameserver2/curvefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using curve::common::TimeUtility;
using ::std::chrono::steady_clock;
using ::std::chrono::microseconds;
using curve::mds::topology::LogicalPool;

namespace curve {
namespace mds {
Expand Down Expand Up @@ -73,7 +74,8 @@ bool CurveFS::Init(std::shared_ptr<NameServerStorage> storage,
std::shared_ptr<FileRecordManager> fileRecordManager,
std::shared_ptr<AllocStatistic> allocStatistic,
const struct CurveFSOption &curveFSOptions,
std::shared_ptr<MdsRepo> repo) {
std::shared_ptr<MdsRepo> repo,
std::shared_ptr<Topology> topology) {
startTime_ = steady_clock::now();
storage_ = storage;
InodeIDGenerator_ = InodeIDGenerator;
Expand All @@ -85,6 +87,7 @@ bool CurveFS::Init(std::shared_ptr<NameServerStorage> storage,

defaultChunkSize_ = curveFSOptions.defaultChunkSize;
repo_ = repo;
topology_ = topology;

InitRootFile();

Expand Down Expand Up @@ -282,9 +285,17 @@ StatusCode CurveFS::GetFileInfo(const std::string & filename,
}
}

AllocatedSize& AllocatedSize::operator+=(const AllocatedSize& rhs) {
allocatedSize += rhs.allocatedSize;
physicalAllocatedSize += rhs.physicalAllocatedSize;
return *this;
}

StatusCode CurveFS::GetAllocatedSize(const std::string& fileName,
uint64_t* allocatedSize) {
AllocatedSize* allocatedSize) {
assert(allocatedSize != nullptr);
allocatedSize->allocatedSize = 0;
allocatedSize->physicalAllocatedSize = 0;
FileInfo fileInfo;
auto ret = GetFileInfo(fileName, &fileInfo);
if (ret != StatusCode::kOK) {
Expand All @@ -303,8 +314,7 @@ StatusCode CurveFS::GetAllocatedSize(const std::string& fileName,

StatusCode CurveFS::GetAllocatedSize(const std::string& fileName,
const FileInfo& fileInfo,
uint64_t* allocSize) {
*allocSize = 0;
AllocatedSize* allocSize) {
if (fileInfo.filetype() != curve::mds::FileType::INODE_DIRECTORY) {
return GetFileAllocSize(fileName, fileInfo, allocSize);
} else { // 如果是目录,则list dir,并递归计算每个文件的大小最后加起来
Expand All @@ -314,20 +324,32 @@ StatusCode CurveFS::GetAllocatedSize(const std::string& fileName,

StatusCode CurveFS::GetFileAllocSize(const std::string& fileName,
const FileInfo& fileInfo,
uint64_t* allocSize) {
AllocatedSize* allocSize) {
std::vector<PageFileSegment> segments;
auto listSegmentRet = storage_->ListSegment(fileInfo.id(), &segments);

if (listSegmentRet != StoreStatus::OK) {
return StatusCode::kStorageError;
}
*allocSize = fileInfo.segmentsize() * segments.size();
for (const auto& segment : segments) {
const auto & logicPoolId = segment.logicalpoolid();
LogicalPool logicPool;
if (!topology_->GetLogicalPool(logicPoolId, &logicPool)) {
LOG(ERROR) << "Get logical pool " << logicPoolId
<< " from topology failed!";
return StatusCode::KInternalError;
}
uint64_t replicasNum = logicPool.GetReplicaNum();
allocSize->physicalAllocatedSize +=
fileInfo.segmentsize() * replicasNum;
}
allocSize->allocatedSize = fileInfo.segmentsize() * segments.size();
return StatusCode::kOK;
}

StatusCode CurveFS::GetDirAllocSize(const std::string& fileName,
const FileInfo& fileInfo,
uint64_t* allocSize) {
AllocatedSize* allocSize) {
std::vector<FileInfo> files;
StatusCode ret = ReadDir(fileName, &files);
if (ret != StatusCode::kOK) {
Expand All @@ -341,7 +363,7 @@ StatusCode CurveFS::GetDirAllocSize(const std::string& fileName,
} else {
fullPathName = fileName + "/" + file.filename();
}
uint64_t size;
AllocatedSize size;
if (GetAllocatedSize(fullPathName, file, &size) != 0) {
std::cout << "Get allocated size of " << fullPathName
<< " fail!" << std::endl;
Expand Down
21 changes: 16 additions & 5 deletions src/mds/nameserver2/curvefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ struct CurveFSOption {
FileRecordOptions fileRecordOptions;
};

struct AllocatedSize {
// mds给文件分配的segment的大小
uint64_t allocatedSize;
// 实际会占用的底层空间
uint64_t physicalAllocatedSize;
AllocatedSize() : allocatedSize(0), physicalAllocatedSize(0) {}
AllocatedSize& operator+=(const AllocatedSize& rhs);
};

using ::curve::mds::DeleteSnapShotResponse;

bool InitRecycleBinDir(std::shared_ptr<NameServerStorage> storage);
Expand Down Expand Up @@ -72,7 +81,8 @@ class CurveFS {
std::shared_ptr<FileRecordManager> fileRecordManager,
std::shared_ptr<AllocStatistic> allocStatistic,
const struct CurveFSOption &curveFSOptions,
std::shared_ptr<MdsRepo> repo);
std::shared_ptr<MdsRepo> repo,
std::shared_ptr<Topology> topology);

/**
* @brief Run session manager
Expand Down Expand Up @@ -119,7 +129,7 @@ class CurveFS {
* @return 是否成功,成功返回StatusCode::kOK
*/
StatusCode GetAllocatedSize(const std::string& fileName,
uint64_t* allocatedSize);
AllocatedSize* allocatedSize);

/**
* @brief 删除文件
Expand Down Expand Up @@ -536,7 +546,7 @@ class CurveFS {
*/
StatusCode GetAllocatedSize(const std::string& fileName,
const FileInfo& fileInfo,
uint64_t* allocSize);
AllocatedSize* allocSize);

/**
* @brief 获取文件分配大小
Expand All @@ -547,7 +557,7 @@ class CurveFS {
*/
StatusCode GetFileAllocSize(const std::string& fileName,
const FileInfo& fileInfo,
uint64_t* allocSize);
AllocatedSize* allocSize);

/**
* @brief 获取目录分配大小
Expand All @@ -558,7 +568,7 @@ class CurveFS {
*/
StatusCode GetDirAllocSize(const std::string& fileName,
const FileInfo& fileInfo,
uint64_t* allocSize);
AllocatedSize* allocSize);

private:
FileInfo rootFileInfo_;
Expand All @@ -568,6 +578,7 @@ class CurveFS {
std::shared_ptr<FileRecordManager> fileRecordManager_;
std::shared_ptr<CleanManagerInterface> cleanManager_;
std::shared_ptr<AllocStatistic> allocStatistic_;
std::shared_ptr<Topology> topology_;
struct RootAuthOption rootAuthOptions_;

uint64_t defaultChunkSize_;
Expand Down
7 changes: 4 additions & 3 deletions src/mds/nameserver2/namespace_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,8 +1614,8 @@ void NameSpaceService::GetAllocatedSize(
<< ", GetAllocatedSize request, fileName = " << request->filename();

StatusCode retCode;
uint64_t allocatedSize;
retCode = kCurveFS.GetAllocatedSize(request->filename(), &allocatedSize);
AllocatedSize allocSize;
retCode = kCurveFS.GetAllocatedSize(request->filename(), &allocSize);
if (retCode != StatusCode::kOK) {
response->set_statuscode(retCode);
LOG(ERROR) << "logid = " << cntl->log_id()
Expand All @@ -1625,7 +1625,8 @@ void NameSpaceService::GetAllocatedSize(
return;
} else {
response->set_statuscode(StatusCode::kOK);
response->set_allocatedsize(allocatedSize);
response->set_allocatedsize(allocSize.allocatedSize);
response->set_physicalallocatedsize(allocSize.physicalAllocatedSize);
LOG(INFO) << "logid = " << cntl->log_id()
<< ", GetAllocatedSize ok, fileName = " << request->filename()
<< ", allocatedSize = " << response->allocatedsize() / kGB << "GB";
Expand Down
2 changes: 1 addition & 1 deletion src/mds/server/mds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ void MDS::InitCurveFS(const CurveFSOption& curveFSOptions) {
chunkSegmentAllocate, cleanManager_,
fileRecordManager,
segmentAllocStatistic_,
curveFSOptions, mdsRepo_))
curveFSOptions, mdsRepo_, topology_))
<< "init FileRecordManager fail";
LOG(INFO) << "init FileRecordManager success.";

Expand Down
6 changes: 5 additions & 1 deletion src/tools/mds_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ int MDSClient::GetFileInfo(const std::string &fileName,
}

int MDSClient::GetAllocatedSize(const std::string& fileName,
uint64_t* allocSize) {
uint64_t* allocSize,
uint64_t* physicalAllocSize) {
if (!allocSize) {
std::cout << "The argument is a null pointer!" << std::endl;
return -1;
Expand All @@ -146,6 +147,9 @@ int MDSClient::GetAllocatedSize(const std::string& fileName,
}
if (response.statuscode() == StatusCode::kOK) {
*allocSize = response.allocatedsize();
if (physicalAllocSize) {
*physicalAllocSize = response.physicalallocatedsize();
}
return 0;
}
std::cout << "GetAllocatedSize fail with errCode: "
Expand Down
4 changes: 3 additions & 1 deletion src/tools/mds_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ class MDSClient {
* @brief 获取文件或目录分配大小
* @param fileName 文件名
* @param[out] allocSize 文件或目录分配大小,返回值为0时有效
* @param[out] physicalAllocSize 文件或目录底层分配大小,返回值为0时有效
* @return 成功返回0,失败返回-1
*/
virtual int GetAllocatedSize(const std::string& fileName,
uint64_t* allocSize);
uint64_t* allocSize,
uint64_t* physicalAllocSize = nullptr);

/**
* @brief 将目录下所有的fileInfo列出来
Expand Down
5 changes: 3 additions & 2 deletions src/tools/namespace_tool_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ int NameSpaceToolCore::CreateFile(const std::string& fileName,
}

int NameSpaceToolCore::GetAllocatedSize(const std::string& fileName,
uint64_t* size) {
return client_->GetAllocatedSize(fileName, size);
uint64_t* allocSize,
uint64_t* phyAllocSize) {
return client_->GetAllocatedSize(fileName, allocSize, phyAllocSize);
}

int NameSpaceToolCore::GetFileSize(const std::string& fileName,
Expand Down
4 changes: 3 additions & 1 deletion src/tools/namespace_tool_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ class NameSpaceToolCore {
* @brief 计算文件或目录实际分配的空间
* @param fileName 文件名
* @param[out] allocSize 文件或目录已分配大小,返回值为0是有效
* @param[out] phyAllocSize 文件或目录底层实际分配的大小,返回值0时有效
* @return 成功返回0,失败返回-1
*/
virtual int GetAllocatedSize(const std::string& fileName,
uint64_t* allocSize);
uint64_t* allocSize,
uint64_t* phyAllocSize = nullptr);

/**
* @brief 返回文件或目录的中的文件的用户申请的大小
Expand Down
17 changes: 13 additions & 4 deletions src/tools/status_tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,21 @@ int StatusTool::SpaceCmd() {
std::cout << "GetSpaceInfo fail!" << std::endl;
return -1;
}
double logicalUsedRatio = static_cast<double>(spaceInfo.logicalUsed) /
double logicalUsedRatio = 0;
double physicalUsedRatio = 0;
double canBeRecycledRatio = 0;
if (spaceInfo.total != 0) {
logicalUsedRatio = static_cast<double>(spaceInfo.logicalUsed) /
spaceInfo.total;
double physicalUsedRatio = static_cast<double>(spaceInfo.physicalUsed) /
physicalUsedRatio = static_cast<double>(spaceInfo.physicalUsed) /
spaceInfo.total;
double canBeRecycledRatio = static_cast<double>(spaceInfo.canBeRecycled) /
}
if (spaceInfo.logicalUsed != 0) {
canBeRecycledRatio = static_cast<double>(spaceInfo.canBeRecycled) /
spaceInfo.logicalUsed;
}
std:: cout.setf(std::ios::fixed);
std::cout<< std::setprecision(2);
std::cout << std::setprecision(2);
std::cout << "total space = " << spaceInfo.total / mds::kGB << "GB"
<< ", logical used = " << spaceInfo.logicalUsed / mds::kGB << "GB"
<< "(" << logicalUsedRatio * 100 << "%, can be recycled = "
Expand Down Expand Up @@ -719,7 +726,9 @@ int StatusTool::GetSpaceInfo(SpaceInfo* spaceInfo) {
spaceInfo->physicalUsed += size;
}
// 通过NameSpace工具获取RecycleBin的大小
uint64_t allocSize;
res = nameSpaceToolCore_->GetAllocatedSize(curve::mds::RECYCLEBINDIR,
&allocSize,
&spaceInfo->canBeRecycled);
if (res != 0) {
std::cout << "GetAllocatedSize of RecycleBin fail!" << std::endl;
Expand Down
Loading

0 comments on commit 54aa447

Please sign in to comment.