Skip to content

Commit aff0ebf

Browse files
committed
[feat]add cluster usage
Signed-off-by: Cyber-SiKu <Cyber-SiKu@outlook.com>
1 parent 7f4a097 commit aff0ebf

File tree

9 files changed

+1160
-16
lines changed

9 files changed

+1160
-16
lines changed

curvefs/monitor/grafana/provisioning/dashboards/cluster.json

Lines changed: 1014 additions & 0 deletions
Large diffs are not rendered by default.

curvefs/src/mds/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ cc_library(
3333
"//curvefs/src/common:curvefs_common",
3434
"//curvefs/src/mds/codec:fs_mds_codec",
3535
"//curvefs/src/mds/idgenerator:fs_mds_idgenerator",
36+
"//curvefs/src/mds/metric:fs_mds_metric",
3637
"//src/common:curve_common",
3738
"//src/common/concurrent:curve_concurrent",
3839
"//src/idgenerator",

curvefs/src/mds/fs_manager.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ FSStatusCode FsManager::DeleteFs(const std::string& fsName) {
592592
<< ", ret = " << FSStatusCode_Name(ret);
593593
// do not abort deletion
594594
}
595+
FsMetric::GetInstance().DeleteFsUsage(fsName);
595596
}
596597

597598
return FSStatusCode::OK;
@@ -832,7 +833,11 @@ FSStatusCode FsManager::UpdateFsInfo(
832833
wrapper.SetCapacity(req->capacity());
833834
}
834835

835-
return fsStorage_->Update(wrapper);
836+
ret = fsStorage_->Update(wrapper);
837+
if (ret == FSStatusCode::OK) {
838+
FsMetric::GetInstance().SetCapacity(fsName, req->capacity());
839+
}
840+
return ret;
836841
}
837842

838843
int FsManager::IsExactlySameOrCreateUnComplete(const std::string& fsName,
@@ -904,7 +909,11 @@ FSStatusCode FsManager::UpdateFsUsedBytes(
904909
auto ret = fsStorage_->GetFsUsage(fsName, &usage, true);
905910
if (ret == FSStatusCode::NOT_FOUND) {
906911
usage.set_usedbytes(deltaBytes);
907-
return fsStorage_->SetFsUsage(fsName, usage);
912+
ret = fsStorage_->SetFsUsage(fsName, usage);
913+
if (ret == FSStatusCode::OK) {
914+
FsMetric::GetInstance().SetFsUsage(fsName, usage);
915+
}
916+
return ret;
908917
}
909918

910919
if (ret != FSStatusCode::OK) {
@@ -918,7 +927,11 @@ FSStatusCode FsManager::UpdateFsUsedBytes(
918927
} else {
919928
usage.set_usedbytes(usage.usedbytes() + deltaBytes);
920929
}
921-
return fsStorage_->SetFsUsage(fsName, usage);
930+
ret = fsStorage_->SetFsUsage(fsName, usage);
931+
if (ret == FSStatusCode::OK) {
932+
FsMetric::GetInstance().SetFsUsage(fsName, usage);
933+
}
934+
return ret;
922935
}
923936

924937
void FsManager::RefreshSession(const RefreshSessionRequest* request,

curvefs/src/mds/fs_storage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <vector>
3030

3131
#include "curvefs/src/mds/codec/codec.h"
32+
#include "curvefs/src/mds/metric/fs_metric.h"
3233

3334
namespace curvefs {
3435
namespace mds {
@@ -409,6 +410,9 @@ bool PersisKVStorage::LoadAllFs() {
409410
{
410411
WriteLockGuard lock(fsUsageCacheMutex_);
411412
fsUsageCache_[fsInfo.fsname()] = fsUsage;
413+
FsMetric::GetInstance().SetFsUsage(fsInfo.fsname(), fsUsage);
414+
FsMetric::GetInstance().SetCapacity(
415+
fsInfo.fsname(), fsInfo.capacity());
412416
}
413417
}
414418

curvefs/src/mds/metric/fs_metric.cpp

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,62 @@ namespace curvefs {
2626
namespace mds {
2727

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

31-
auto iter = metrics_.find(fsname);
32-
if (iter == metrics_.end()) {
33-
auto r = metrics_.emplace(fsname, new FsMountMetric(fsname));
31+
auto iter = mountMetrics_.find(fsname);
32+
if (iter == mountMetrics_.end()) {
33+
auto r = mountMetrics_.emplace(fsname, new FsMountMetric(fsname));
3434
iter = r.first;
3535
}
3636

3737
iter->second->OnMount(mp);
3838
}
3939

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

43-
auto iter = metrics_.find(fsname);
44-
if (iter == metrics_.end()) {
43+
auto iter = mountMetrics_.find(fsname);
44+
if (iter == mountMetrics_.end()) {
4545
return;
4646
}
4747

4848
iter->second->OnUnMount(mp);
4949
}
5050

51+
std::unordered_map<std::string, std::unique_ptr<FsUsageMetric>>::iterator
52+
FsMetric::GetFsnameUsageMetricIter(const std::string& fsname) {
53+
auto iter = usageMetrics_.find(fsname);
54+
if (iter == usageMetrics_.end()) {
55+
auto r = usageMetrics_.emplace(fsname, new FsUsageMetric(fsname));
56+
if (!r.second) {
57+
LOG(ERROR) << "insert fs usage metric failed, fsname = " << fsname;
58+
return usageMetrics_.end();
59+
}
60+
iter = r.first;
61+
}
62+
return iter;
63+
}
64+
65+
void FsMetric::SetFsUsage(const std::string& fsname, const FsUsage& usage) {
66+
std::lock_guard<Mutex> lock(usageMtx_);
67+
auto iter = GetFsnameUsageMetricIter(fsname);
68+
if (iter != usageMetrics_.end()) {
69+
iter->second->SetUsage(usage);
70+
}
71+
}
72+
73+
void FsMetric::SetCapacity(const std::string& fsname, uint64_t capacity) {
74+
std::lock_guard<Mutex> lock(usageMtx_);
75+
auto iter = GetFsnameUsageMetricIter(fsname);
76+
if (iter != usageMetrics_.end()) {
77+
iter->second->SetCapacity(capacity);
78+
}
79+
}
80+
81+
void FsMetric::DeleteFsUsage(const std::string& fsname) {
82+
std::lock_guard<Mutex> lock(usageMtx_);
83+
usageMetrics_.erase(fsname);
84+
}
85+
5186
} // namespace mds
5287
} // namespace curvefs

curvefs/src/mds/metric/fs_metric.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class FsMetric {
4343

4444
void OnMount(const std::string& fsname, const Mountpoint& mp);
4545
void OnUnMount(const std::string& fsname, const Mountpoint& mp);
46+
void SetFsUsage(const std::string& fsname, const FsUsage& usage);
47+
void SetCapacity(const std::string& fsname, uint64_t capacity);
48+
void DeleteFsUsage(const std::string& fsname);
4649

4750
private:
4851
FsMetric() = default;
@@ -51,9 +54,16 @@ class FsMetric {
5154
FsMetric(const FsMetric&) = delete;
5255
FsMetric& operator=(const FsMetric&) = delete;
5356

57+
std::unordered_map<std::string, std::unique_ptr<FsUsageMetric>>::iterator
58+
GetFsnameUsageMetricIter(const std::string& fsname);
59+
5460
private:
55-
Mutex mtx_;
56-
std::unordered_map<std::string, std::unique_ptr<FsMountMetric>> metrics_;
61+
Mutex mountMtx_;
62+
std::unordered_map<std::string, std::unique_ptr<FsMountMetric>>
63+
mountMetrics_;
64+
Mutex usageMtx_;
65+
std::unordered_map<std::string, std::unique_ptr<FsUsageMetric>>
66+
usageMetrics_;
5767
};
5868

5969
} // namespace mds

curvefs/src/mds/metric/metric.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
namespace curvefs {
3535
namespace mds {
3636

37+
const std::string FsUsageMetric::prefix = "fs_usage_info"; // NOLINT
38+
3739
void FsMountMetric::OnMount(const Mountpoint& mp) {
3840
std::string key = Key(mp);
3941

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

73+
void FsUsageMetric::SetUsage(const FsUsage& usage) {
74+
usedBytes_.set_value(usage.usedbytes());
75+
}
76+
7177
} // namespace mds
7278
} // namespace curvefs

curvefs/src/mds/metric/metric.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <bvar/bvar.h>
2727

28+
#include <cstdint>
2829
#include <memory>
2930
#include <string>
3031
#include <unordered_map>
@@ -34,7 +35,6 @@
3435

3536
namespace curvefs {
3637
namespace mds {
37-
3838
// Metric for a filesystem
3939
// includes filesystem mount number and filesystem mountpoint lists
4040
class FsMountMetric {
@@ -59,15 +59,28 @@ class FsMountMetric {
5959
// current number of fs mountpoints
6060
bvar::Adder<int64_t> count_;
6161

62-
using MountPointMetric =
63-
std::unordered_map<std::string,
64-
std::unique_ptr<bvar::Status<std::string>>>;
62+
using MountPointMetric = std::unordered_map<std::string,
63+
std::unique_ptr<bvar::Status<std::string>>>;
6564
// protect mps_
6665
Mutex mtx_;
6766

6867
MountPointMetric mps_;
6968
};
7069

70+
struct FsUsageMetric {
71+
static const std::string prefix;
72+
73+
std::string fsname_;
74+
bvar::Status<uint64_t> usedBytes_;
75+
bvar::Status<uint64_t> capacityBytes_;
76+
77+
explicit FsUsageMetric(const std::string& fsname)
78+
: fsname_(fsname), usedBytes_(prefix + "_fs_" + fsname + "_used", 0) {}
79+
80+
void SetUsage(const FsUsage& usage);
81+
void SetCapacity(uint64_t capacity) { capacityBytes_.set_value(capacity); }
82+
};
83+
7184
} // namespace mds
7285
} // namespace curvefs
7386

curvefs/test/mds/mds_metric_test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2023 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: curve
19+
* Created Date: Fri Apr 21 2023
20+
* Author: Xinlong-Chen
21+
*/
22+
23+
#include <gtest/gtest.h>
24+
25+
#include "curvefs/proto/mds.pb.h"
26+
#include "curvefs/src/mds/metric/fs_metric.h"
27+
28+
namespace curvefs {
29+
namespace mds {
30+
31+
class MdsMetricTest : public ::testing::Test {
32+
protected:
33+
void SetUp() override {}
34+
void TearDown() override {}
35+
};
36+
37+
TEST_F(MdsMetricTest, UpdateFsUsage) {
38+
FsUsage usage;
39+
usage.set_usedbytes(100);
40+
FsMetric::GetInstance().SetFsUsage("test", usage);
41+
usage.set_usedbytes(200);
42+
FsMetric::GetInstance().SetFsUsage("test", usage);
43+
FsMetric::GetInstance().DeleteFsUsage("test");
44+
FsMetric::GetInstance().DeleteFsUsage("test");
45+
}
46+
47+
} // namespace mds
48+
} // namespace curvefs

0 commit comments

Comments
 (0)