Skip to content

Commit

Permalink
[feat]add cluster usage
Browse files Browse the repository at this point in the history
Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu committed Oct 25, 2023
1 parent 7f4a097 commit aff0ebf
Show file tree
Hide file tree
Showing 9 changed files with 1,160 additions and 16 deletions.
1,014 changes: 1,014 additions & 0 deletions curvefs/monitor/grafana/provisioning/dashboards/cluster.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions curvefs/src/mds/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cc_library(
"//curvefs/src/common:curvefs_common",
"//curvefs/src/mds/codec:fs_mds_codec",
"//curvefs/src/mds/idgenerator:fs_mds_idgenerator",
"//curvefs/src/mds/metric:fs_mds_metric",
"//src/common:curve_common",
"//src/common/concurrent:curve_concurrent",
"//src/idgenerator",
Expand Down
19 changes: 16 additions & 3 deletions curvefs/src/mds/fs_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ FSStatusCode FsManager::DeleteFs(const std::string& fsName) {
<< ", ret = " << FSStatusCode_Name(ret);
// do not abort deletion
}
FsMetric::GetInstance().DeleteFsUsage(fsName);
}

return FSStatusCode::OK;
Expand Down Expand Up @@ -832,7 +833,11 @@ FSStatusCode FsManager::UpdateFsInfo(
wrapper.SetCapacity(req->capacity());
}

return fsStorage_->Update(wrapper);
ret = fsStorage_->Update(wrapper);
if (ret == FSStatusCode::OK) {
FsMetric::GetInstance().SetCapacity(fsName, req->capacity());
}
return ret;
}

int FsManager::IsExactlySameOrCreateUnComplete(const std::string& fsName,
Expand Down Expand Up @@ -904,7 +909,11 @@ FSStatusCode FsManager::UpdateFsUsedBytes(
auto ret = fsStorage_->GetFsUsage(fsName, &usage, true);
if (ret == FSStatusCode::NOT_FOUND) {
usage.set_usedbytes(deltaBytes);
return fsStorage_->SetFsUsage(fsName, usage);
ret = fsStorage_->SetFsUsage(fsName, usage);
if (ret == FSStatusCode::OK) {
FsMetric::GetInstance().SetFsUsage(fsName, usage);
}
return ret;
}

if (ret != FSStatusCode::OK) {
Expand All @@ -918,7 +927,11 @@ FSStatusCode FsManager::UpdateFsUsedBytes(
} else {
usage.set_usedbytes(usage.usedbytes() + deltaBytes);
}
return fsStorage_->SetFsUsage(fsName, usage);
ret = fsStorage_->SetFsUsage(fsName, usage);
if (ret == FSStatusCode::OK) {
FsMetric::GetInstance().SetFsUsage(fsName, usage);
}
return ret;
}

void FsManager::RefreshSession(const RefreshSessionRequest* request,
Expand Down
4 changes: 4 additions & 0 deletions curvefs/src/mds/fs_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <vector>

#include "curvefs/src/mds/codec/codec.h"
#include "curvefs/src/mds/metric/fs_metric.h"

namespace curvefs {
namespace mds {
Expand Down Expand Up @@ -409,6 +410,9 @@ bool PersisKVStorage::LoadAllFs() {
{
WriteLockGuard lock(fsUsageCacheMutex_);
fsUsageCache_[fsInfo.fsname()] = fsUsage;
FsMetric::GetInstance().SetFsUsage(fsInfo.fsname(), fsUsage);
FsMetric::GetInstance().SetCapacity(
fsInfo.fsname(), fsInfo.capacity());
}
}

Expand Down
49 changes: 42 additions & 7 deletions curvefs/src/mds/metric/fs_metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,62 @@ namespace curvefs {
namespace mds {

void FsMetric::OnMount(const std::string& fsname, const Mountpoint& mp) {
std::lock_guard<Mutex> lock(mtx_);
std::lock_guard<Mutex> lock(mountMtx_);

auto iter = metrics_.find(fsname);
if (iter == metrics_.end()) {
auto r = metrics_.emplace(fsname, new FsMountMetric(fsname));
auto iter = mountMetrics_.find(fsname);
if (iter == mountMetrics_.end()) {
auto r = mountMetrics_.emplace(fsname, new FsMountMetric(fsname));
iter = r.first;
}

iter->second->OnMount(mp);
}

void FsMetric::OnUnMount(const std::string& fsname, const Mountpoint& mp) {
std::lock_guard<Mutex> lock(mtx_);
std::lock_guard<Mutex> lock(mountMtx_);

auto iter = metrics_.find(fsname);
if (iter == metrics_.end()) {
auto iter = mountMetrics_.find(fsname);
if (iter == mountMetrics_.end()) {
return;
}

iter->second->OnUnMount(mp);
}

std::unordered_map<std::string, std::unique_ptr<FsUsageMetric>>::iterator
FsMetric::GetFsnameUsageMetricIter(const std::string& fsname) {
auto iter = usageMetrics_.find(fsname);
if (iter == usageMetrics_.end()) {
auto r = usageMetrics_.emplace(fsname, new FsUsageMetric(fsname));
if (!r.second) {
LOG(ERROR) << "insert fs usage metric failed, fsname = " << fsname;
return usageMetrics_.end();
}
iter = r.first;
}
return iter;
}

void FsMetric::SetFsUsage(const std::string& fsname, const FsUsage& usage) {
std::lock_guard<Mutex> lock(usageMtx_);
auto iter = GetFsnameUsageMetricIter(fsname);
if (iter != usageMetrics_.end()) {
iter->second->SetUsage(usage);
}
}

void FsMetric::SetCapacity(const std::string& fsname, uint64_t capacity) {
std::lock_guard<Mutex> lock(usageMtx_);
auto iter = GetFsnameUsageMetricIter(fsname);
if (iter != usageMetrics_.end()) {
iter->second->SetCapacity(capacity);
}
}

void FsMetric::DeleteFsUsage(const std::string& fsname) {
std::lock_guard<Mutex> lock(usageMtx_);
usageMetrics_.erase(fsname);
}

} // namespace mds
} // namespace curvefs
14 changes: 12 additions & 2 deletions curvefs/src/mds/metric/fs_metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class FsMetric {

void OnMount(const std::string& fsname, const Mountpoint& mp);
void OnUnMount(const std::string& fsname, const Mountpoint& mp);
void SetFsUsage(const std::string& fsname, const FsUsage& usage);
void SetCapacity(const std::string& fsname, uint64_t capacity);
void DeleteFsUsage(const std::string& fsname);

private:
FsMetric() = default;
Expand All @@ -51,9 +54,16 @@ class FsMetric {
FsMetric(const FsMetric&) = delete;
FsMetric& operator=(const FsMetric&) = delete;

std::unordered_map<std::string, std::unique_ptr<FsUsageMetric>>::iterator
GetFsnameUsageMetricIter(const std::string& fsname);

private:
Mutex mtx_;
std::unordered_map<std::string, std::unique_ptr<FsMountMetric>> metrics_;
Mutex mountMtx_;
std::unordered_map<std::string, std::unique_ptr<FsMountMetric>>
mountMetrics_;
Mutex usageMtx_;
std::unordered_map<std::string, std::unique_ptr<FsUsageMetric>>
usageMetrics_;
};

} // namespace mds
Expand Down
6 changes: 6 additions & 0 deletions curvefs/src/mds/metric/metric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
namespace curvefs {
namespace mds {

const std::string FsUsageMetric::prefix = "fs_usage_info"; // NOLINT

void FsMountMetric::OnMount(const Mountpoint& mp) {
std::string key = Key(mp);

Expand Down Expand Up @@ -68,5 +70,9 @@ std::string FsMountMetric::Key(const Mountpoint& mp) {
std::to_string(mp.port()) + "_" + mp.path();
}

void FsUsageMetric::SetUsage(const FsUsage& usage) {
usedBytes_.set_value(usage.usedbytes());
}

} // namespace mds
} // namespace curvefs
21 changes: 17 additions & 4 deletions curvefs/src/mds/metric/metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <bvar/bvar.h>

#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
Expand All @@ -34,7 +35,6 @@

namespace curvefs {
namespace mds {

// Metric for a filesystem
// includes filesystem mount number and filesystem mountpoint lists
class FsMountMetric {
Expand All @@ -59,15 +59,28 @@ class FsMountMetric {
// current number of fs mountpoints
bvar::Adder<int64_t> count_;

using MountPointMetric =
std::unordered_map<std::string,
std::unique_ptr<bvar::Status<std::string>>>;
using MountPointMetric = std::unordered_map<std::string,
std::unique_ptr<bvar::Status<std::string>>>;
// protect mps_
Mutex mtx_;

MountPointMetric mps_;
};

struct FsUsageMetric {
static const std::string prefix;

std::string fsname_;
bvar::Status<uint64_t> usedBytes_;
bvar::Status<uint64_t> capacityBytes_;

explicit FsUsageMetric(const std::string& fsname)
: fsname_(fsname), usedBytes_(prefix + "_fs_" + fsname + "_used", 0) {}

void SetUsage(const FsUsage& usage);
void SetCapacity(uint64_t capacity) { capacityBytes_.set_value(capacity); }
};

} // namespace mds
} // namespace curvefs

Expand Down
48 changes: 48 additions & 0 deletions curvefs/test/mds/mds_metric_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 NetEase Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* Project: curve
* Created Date: Fri Apr 21 2023
* Author: Xinlong-Chen
*/

#include <gtest/gtest.h>

#include "curvefs/proto/mds.pb.h"
#include "curvefs/src/mds/metric/fs_metric.h"

namespace curvefs {
namespace mds {

class MdsMetricTest : public ::testing::Test {
protected:
void SetUp() override {}
void TearDown() override {}
};

TEST_F(MdsMetricTest, UpdateFsUsage) {
FsUsage usage;
usage.set_usedbytes(100);
FsMetric::GetInstance().SetFsUsage("test", usage);
usage.set_usedbytes(200);
FsMetric::GetInstance().SetFsUsage("test", usage);
FsMetric::GetInstance().DeleteFsUsage("test");
FsMetric::GetInstance().DeleteFsUsage("test");
}

} // namespace mds
} // namespace curvefs

0 comments on commit aff0ebf

Please sign in to comment.