diff --git a/curvefs/monitor/grafana/provisioning/dashboards/cluster.json b/curvefs/monitor/grafana/provisioning/dashboards/cluster.json new file mode 100644 index 0000000000..2dcae0ec8e --- /dev/null +++ b/curvefs/monitor/grafana/provisioning/dashboards/cluster.json @@ -0,0 +1,1014 @@ +{ + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": { + "type": "grafana", + "uid": "-- Grafana --" + }, + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "description": "Curvefs cluster", + "editable": true, + "fiscalYearStartMonth": 0, + "graphTooltip": 0, + "id": 9, + "links": [], + "liveNow": false, + "panels": [ + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 0 + }, + "id": 2, + "panels": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + } + }, + "mappings": [], + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 16, + "w": 12, + "x": 0, + "y": 1 + }, + "id": 7, + "interval": "1s", + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "pieType": "pie", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": false + }, + "tooltip": { + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "10.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "disableTextWrap": false, + "editorMode": "code", + "exemplar": false, + "expr": "{__name__=~\"fs_usage_info_fs_[[fsName:regex]]_used\", job=\"mds\"}", + "fullMetaSearch": false, + "includeNullMetadata": true, + "instant": false, + "interval": "", + "legendFormat": "__auto", + "range": true, + "refId": "A", + "useBackend": false + } + ], + "title": "fs used", + "type": "piechart" + }, + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "displayName": "cluster total used", + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 1 + }, + "id": 5, + "interval": "1s", + "options": { + "legend": { + "calcs": [ + "mean", + "max", + "min" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "none" + } + }, + "pluginVersion": "10.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "sum({__name__=~\"fs_usage_info_fs_[[fsName:regex]]_used\",job=\"mds\"})", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "title": "cluster total used", + "type": "timeseries" + }, + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 10, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "insertNulls": false, + "lineInterpolation": "linear", + "lineStyle": { + "fill": "solid" + }, + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + }, + "unit": "bytes" + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 9 + }, + "id": 4, + "interval": "1s", + "options": { + "legend": { + "calcs": [ + "mean", + "max", + "min" + ], + "displayMode": "table", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "none" + } + }, + "pluginVersion": "10.1.4", + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "disableTextWrap": false, + "editorMode": "code", + "exemplar": false, + "expr": "{__name__=~\"fs_usage_info_fs_[[fsName:regex]]_used\", job=\"mds\"}", + "fullMetaSearch": false, + "includeNullMetadata": true, + "instant": false, + "interval": "", + "legendFormat": "__auto", + "range": true, + "refId": "A", + "useBackend": false + } + ], + "title": "fs used", + "type": "timeseries" + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "unit": "none" + }, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 17 + }, + "hiddenSeries": false, + "id": 8, + "interval": "1s", + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "10.1.4", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "{__name__=~\"topology_fs_id_[[fsId:regex]]_.*inode_num\",job=\"mds\"}", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "thresholds": [], + "timeRegions": [], + "title": "inode_num_by_type", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transformations": [ + { + "id": "sortBy", + "options": {} + } + ], + "type": "graph", + "xaxis": { + "mode": "time", + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "none", + "logBase": 1, + "show": true + }, + { + "format": "short", + "logBase": 1, + "show": true + } + ], + "yaxis": { + "align": false + } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "unit": "none" + }, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 17 + }, + "hiddenSeries": false, + "id": 13, + "interval": "1s", + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "10.1.4", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "sum({__name__=~\"topology_fs_id_[0-9]*_inode_num\",job=\"mds\"})", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "thresholds": [], + "timeRegions": [], + "title": "cluster inode", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transformations": [ + { + "id": "sortBy", + "options": {} + } + ], + "type": "graph", + "xaxis": { + "mode": "time", + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "none", + "logBase": 1, + "show": true + }, + { + "format": "short", + "logBase": 1, + "show": true + } + ], + "yaxis": { + "align": false + } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "unit": "none" + }, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 25 + }, + "hiddenSeries": false, + "id": 10, + "interval": "1s", + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "10.1.4", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "sum({__name__=~\"topology_.*_type_file_inode_num\",job=\"mds\"})", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "thresholds": [], + "timeRegions": [], + "title": "cluster file inode", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transformations": [ + { + "id": "sortBy", + "options": {} + } + ], + "type": "graph", + "xaxis": { + "mode": "time", + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "none", + "logBase": 1, + "show": true + }, + { + "format": "short", + "logBase": 1, + "show": true + } + ], + "yaxis": { + "align": false + } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "unit": "none" + }, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 25 + }, + "hiddenSeries": false, + "id": 9, + "interval": "1s", + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "10.1.4", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "sum({__name__=~\"topology_.*_type_directory_inode_num\",job=\"mds\"})", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "thresholds": [], + "timeRegions": [], + "title": "cluster directory inode", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transformations": [ + { + "id": "sortBy", + "options": {} + } + ], + "type": "graph", + "xaxis": { + "mode": "time", + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "none", + "logBase": 1, + "show": true + }, + { + "format": "short", + "logBase": 1, + "show": true + } + ], + "yaxis": { + "align": false + } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "unit": "none" + }, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 33 + }, + "hiddenSeries": false, + "id": 12, + "interval": "1s", + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "10.1.4", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "sum({__name__=~\"topology_.*_type_sym_link_inode_num\",job=\"mds\"})", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "thresholds": [], + "timeRegions": [], + "title": "cluster sym_link inode", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transformations": [ + { + "id": "sortBy", + "options": {} + } + ], + "type": "graph", + "xaxis": { + "mode": "time", + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "none", + "logBase": 1, + "show": true + }, + { + "format": "short", + "logBase": 1, + "show": true + } + ], + "yaxis": { + "align": false + } + }, + { + "aliasColors": {}, + "bars": false, + "dashLength": 10, + "dashes": false, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "fieldConfig": { + "defaults": { + "unit": "none" + }, + "overrides": [] + }, + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 33 + }, + "hiddenSeries": false, + "id": 11, + "interval": "1s", + "legend": { + "alignAsTable": true, + "avg": true, + "current": false, + "max": true, + "min": true, + "show": true, + "total": false, + "values": true + }, + "lines": true, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "alertThreshold": true + }, + "percentage": false, + "pluginVersion": "10.1.4", + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": false, + "steppedLine": false, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "editorMode": "code", + "exemplar": true, + "expr": "sum({__name__=~\"topology_.*_type_s3_inode_num\",job=\"mds\"})", + "interval": "", + "legendFormat": "", + "range": true, + "refId": "A" + } + ], + "thresholds": [], + "timeRegions": [], + "title": "cluster s3 inode", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "transformations": [ + { + "id": "sortBy", + "options": {} + } + ], + "type": "graph", + "xaxis": { + "mode": "time", + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "none", + "logBase": 1, + "show": true + }, + { + "format": "short", + "logBase": 1, + "show": true + } + ], + "yaxis": { + "align": false + } + } + ], + "title": "fs usage", + "type": "row" + } + ], + "refresh": "5s", + "schemaVersion": 38, + "style": "dark", + "tags": [], + "templating": { + "list": [ + { + "current": { + "selected": false, + "text": "All", + "value": "$__all" + }, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "definition": "{__name__=~\"fs_usage_info_fs_.*_used\",job=\"mds\"}", + "hide": 0, + "includeAll": true, + "label": "fsName", + "multi": false, + "name": "fsName", + "options": [], + "query": { + "query": "{__name__=~\"fs_usage_info_fs_.*_used\",job=\"mds\"}", + "refId": "PrometheusVariableQueryEditor-VariableQuery" + }, + "refresh": 1, + "regex": "/fs_usage_info_fs_(.*)_used.*/", + "skipUrlSync": false, + "sort": 0, + "type": "query" + }, + { + "current": { + "selected": false, + "text": "All", + "value": "$__all" + }, + "datasource": { + "type": "prometheus", + "uid": "PBFA97CFB590B2093" + }, + "definition": "{__name__=~\"topology_fs_id_[0-9]*_inode_num\",job=\"mds\"}", + "hide": 0, + "includeAll": true, + "label": "fsId", + "multi": false, + "name": "fsId", + "options": [], + "query": { + "query": "{__name__=~\"topology_fs_id_[0-9]*_inode_num\",job=\"mds\"}", + "refId": "PrometheusVariableQueryEditor-VariableQuery" + }, + "refresh": 1, + "regex": "/topology_fs_id_([0-9]*)_inode_num.*/", + "skipUrlSync": false, + "sort": 0, + "type": "query" + } + ] + }, + "time": { + "from": "now-1h", + "to": "now" + }, + "timepicker": {}, + "timezone": "", + "title": "cluster", + "uid": "c57406c1-8262-4bf8-85c4-f4f65f336f96", + "version": 1, + "weekStart": "" +} diff --git a/curvefs/src/mds/BUILD b/curvefs/src/mds/BUILD index bf46e8d63c..4a26b34bae 100644 --- a/curvefs/src/mds/BUILD +++ b/curvefs/src/mds/BUILD @@ -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", diff --git a/curvefs/src/mds/fs_manager.cpp b/curvefs/src/mds/fs_manager.cpp index 1fafbb1a1b..6ab8090946 100644 --- a/curvefs/src/mds/fs_manager.cpp +++ b/curvefs/src/mds/fs_manager.cpp @@ -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; @@ -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, @@ -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) { @@ -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, diff --git a/curvefs/src/mds/fs_storage.cpp b/curvefs/src/mds/fs_storage.cpp index 304e2a30f5..b38b9f9803 100644 --- a/curvefs/src/mds/fs_storage.cpp +++ b/curvefs/src/mds/fs_storage.cpp @@ -29,6 +29,7 @@ #include #include "curvefs/src/mds/codec/codec.h" +#include "curvefs/src/mds/metric/fs_metric.h" namespace curvefs { namespace mds { @@ -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()); } } diff --git a/curvefs/src/mds/metric/fs_metric.cpp b/curvefs/src/mds/metric/fs_metric.cpp index 56ae90b381..9b84c92645 100644 --- a/curvefs/src/mds/metric/fs_metric.cpp +++ b/curvefs/src/mds/metric/fs_metric.cpp @@ -26,11 +26,11 @@ namespace curvefs { namespace mds { void FsMetric::OnMount(const std::string& fsname, const Mountpoint& mp) { - std::lock_guard lock(mtx_); + std::lock_guard 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; } @@ -38,15 +38,50 @@ void FsMetric::OnMount(const std::string& fsname, const Mountpoint& mp) { } void FsMetric::OnUnMount(const std::string& fsname, const Mountpoint& mp) { - std::lock_guard lock(mtx_); + std::lock_guard 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>::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 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 lock(usageMtx_); + auto iter = GetFsnameUsageMetricIter(fsname); + if (iter != usageMetrics_.end()) { + iter->second->SetCapacity(capacity); + } +} + +void FsMetric::DeleteFsUsage(const std::string& fsname) { + std::lock_guard lock(usageMtx_); + usageMetrics_.erase(fsname); +} + } // namespace mds } // namespace curvefs diff --git a/curvefs/src/mds/metric/fs_metric.h b/curvefs/src/mds/metric/fs_metric.h index b5137efa88..6b8349e332 100644 --- a/curvefs/src/mds/metric/fs_metric.h +++ b/curvefs/src/mds/metric/fs_metric.h @@ -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; @@ -51,9 +54,16 @@ class FsMetric { FsMetric(const FsMetric&) = delete; FsMetric& operator=(const FsMetric&) = delete; + std::unordered_map>::iterator + GetFsnameUsageMetricIter(const std::string& fsname); + private: - Mutex mtx_; - std::unordered_map> metrics_; + Mutex mountMtx_; + std::unordered_map> + mountMetrics_; + Mutex usageMtx_; + std::unordered_map> + usageMetrics_; }; } // namespace mds diff --git a/curvefs/src/mds/metric/metric.cpp b/curvefs/src/mds/metric/metric.cpp index b948a1ec56..d828c2923f 100644 --- a/curvefs/src/mds/metric/metric.cpp +++ b/curvefs/src/mds/metric/metric.cpp @@ -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); @@ -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 diff --git a/curvefs/src/mds/metric/metric.h b/curvefs/src/mds/metric/metric.h index 947a57dcb3..53488a2f51 100644 --- a/curvefs/src/mds/metric/metric.h +++ b/curvefs/src/mds/metric/metric.h @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -34,7 +35,6 @@ namespace curvefs { namespace mds { - // Metric for a filesystem // includes filesystem mount number and filesystem mountpoint lists class FsMountMetric { @@ -59,15 +59,28 @@ class FsMountMetric { // current number of fs mountpoints bvar::Adder count_; - using MountPointMetric = - std::unordered_map>>; + using MountPointMetric = std::unordered_map>>; // protect mps_ Mutex mtx_; MountPointMetric mps_; }; +struct FsUsageMetric { + static const std::string prefix; + + std::string fsname_; + bvar::Status usedBytes_; + bvar::Status 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 diff --git a/curvefs/test/mds/mds_metric_test.cpp b/curvefs/test/mds/mds_metric_test.cpp new file mode 100644 index 0000000000..7377179e22 --- /dev/null +++ b/curvefs/test/mds/mds_metric_test.cpp @@ -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 + +#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