Skip to content

Commit

Permalink
add config metric
Browse files Browse the repository at this point in the history
Change-Id: I2c635c7adb97dbdb7f6997b8ad95e46e0bd05cea
  • Loading branch information
yangyaokai committed Nov 22, 2019
1 parent 51af137 commit f1b7ab1
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 44 deletions.
23 changes: 4 additions & 19 deletions src/chunkserver/chunkserver_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ int ChunkServerMetric::Fini() {
leaderCount_ = nullptr;
chunkLeft_ = nullptr;
copysetMetricMap_.clear();
configMetric_.clear();
hasInited_ = false;
return 0;
}
Expand Down Expand Up @@ -511,28 +510,14 @@ void ChunkServerMetric::DecreaseLeaderCount() {
*leaderCount_ << -1;
}

void ChunkServerMetric::UpdateConfigMetric(const common::Configuration& conf) {
void ChunkServerMetric::UpdateConfigMetric(common::Configuration* conf) {
if (!option_.collectMetric) {
return;
}

std::string prefix = Prefix() + "_config";
std::map<std::string, std::string> configs = conf.ListConfig();
for (auto& config : configs) {
std::string configKey = config.first;
std::string configValue = config.second;
auto it = configMetric_.find(configKey);
// 如果配置项不存在,则新建配置项
if (it == configMetric_.end()) {
ConfigItemPtr configItem =
std::make_shared<bvar::Status<std::string>>(prefix,
configKey,
nullptr);
configMetric_[configKey] = configItem;
}
// 更新配置项
configMetric_[configKey]->set_value(configValue);
}
std::string exposeName = Prefix() + "_config";
conf->ExposeMetric(exposeName);
conf->UpdateMetric();
}

} // namespace chunkserver
Expand Down
10 changes: 1 addition & 9 deletions src/chunkserver/chunkserver_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,6 @@ struct ChunkServerMetricOptions {

using CopysetMetricPtr = std::shared_ptr<CSCopysetMetric>;
using CopysetMetricMap = std::unordered_map<GroupId, CopysetMetricPtr>;
using ConfigItemPtr = std::shared_ptr<bvar::Status<std::string>>;
using ConfigMetricMap = std::unordered_map<std::string, ConfigItemPtr>;

class ChunkServerMetric : public Uncopyable {
public:
Expand Down Expand Up @@ -460,7 +458,7 @@ class ChunkServerMetric : public Uncopyable {
* 更新配置项数据
* @param conf: 配置内容
*/
void UpdateConfigMetric(const common::Configuration& conf);
void UpdateConfigMetric(common::Configuration* conf);

// 下列函数用户获取各项metric 指标
const IOMetricPtr GetReadMetric() const {
Expand Down Expand Up @@ -488,10 +486,6 @@ class ChunkServerMetric : public Uncopyable {
return chunkLeft_->get_value();
}

const ConfigMetricMap GetConfigMetric() const {
return configMetric_;
}

private:
ChunkServerMetric();

Expand All @@ -516,8 +510,6 @@ class ChunkServerMetric : public Uncopyable {
PassiveStatusPtr<uint32_t> chunkLeft_;
// 各复制组metric的映射表,用GroupId作为key
CopysetMetricMap copysetMetricMap_;
// chunkserver配置的metric
ConfigMetricMap configMetric_;
// 用于单例模式的自指指针
static ChunkServerMetric* self_;
};
Expand Down
32 changes: 32 additions & 0 deletions src/common/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/common/configuration.h"

#include <glog/logging.h>
#include <iostream>
#include <fstream>
#include <sstream>
Expand Down Expand Up @@ -61,6 +62,37 @@ std::string Configuration::DumpConfig() {
}


void Configuration::ExposeMetric(const std::string& exposeName) {
if (exposeName_.empty()) {
exposeName_ = exposeName;
} else {
LOG(WARNING) << "Config metric has been exposed.";
}
}

void Configuration::UpdateMetric() {
if (exposeName_.empty()) {
LOG(WARNING) << "Config metric is not been exposed, update failed.";
return;
}

for (auto& config : config_) {
std::string configKey = config.first;
std::string configValue = config.second;
auto it = configMetric_.find(configKey);
// 如果配置项不存在,则新建配置项
if (it == configMetric_.end()) {
ConfigItemPtr configItem = std::make_shared<StringStatus>();
configItem->ExposeAs(exposeName_, configKey);
configMetric_[configKey] = configItem;
}
// 更新配置项
configMetric_[configKey]->Set("conf_name", configKey);
configMetric_[configKey]->Set("conf_value", configValue);
configMetric_[configKey]->Update();
}
}

std::map<std::string, std::string> Configuration::ListConfig() const {
return config_;
}
Expand Down
22 changes: 22 additions & 0 deletions src/common/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

#include <string>
#include <map>
#include <memory>
#include <unordered_map>

#include "src/common/stringstatus.h"

#ifndef SRC_COMMON_CONFIGURATION_H_
#define SRC_COMMON_CONFIGURATION_H_

namespace curve {
namespace common {

using ConfigItemPtr = std::shared_ptr<StringStatus>;
using ConfigMetricMap = std::unordered_map<std::string, ConfigItemPtr>;

class Configuration {
public:
Configuration() {}
Expand All @@ -24,6 +31,17 @@ class Configuration {
bool SaveConfig();
std::string DumpConfig();
std::map<std::string, std::string> ListConfig() const;
/**
* 暴露config的metric供采集
* 如果metric已经暴露,则直接返回
* @param exposeName: 对外暴露的metric的名字
*/
void ExposeMetric(const std::string& exposeName);

/**
* 更新新的配置到metric
*/
void UpdateMetric();

void SetConfigPath(const std::string &path);
std::string GetConfigPath();
Expand Down Expand Up @@ -97,6 +115,10 @@ class Configuration {
private:
std::string confFile_;
std::map<std::string, std::string> config_;
// metric对外暴露的名字
std::string exposeName_;
// 每一个配置项使用单独的一个metric,用map管理
ConfigMetricMap configMetric_;
};

} // namespace common
Expand Down
4 changes: 3 additions & 1 deletion test/chunkserver/chunkserver_snapshot_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ static void ReadVerifyNotAvailable(PeerId leaderId,
LOG_IF(INFO, cntl.Failed()) << "error msg: "
<< cntl.ErrorCode() << " : "
<< cntl.ErrorText();
ASSERT_TRUE(cntl.Failed());
LOG(INFO) << "read: " << CHUNK_OP_STATUS_Name(response.status());
ASSERT_TRUE(cntl.Failed() ||
response.status() != CHUNK_OP_STATUS::CHUNK_OP_STATUS_SUCCESS);
}
}

Expand Down
31 changes: 16 additions & 15 deletions test/chunkserver/metrics_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,21 +429,24 @@ TEST_F(CSMetricTest, ConfigTest) {
conf.SetConfigPath(confFile_);
int ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
metric_->UpdateConfigMetric(conf);
ConfigMetricMap configs = metric_->GetConfigMetric();
// 验证配置信息
ASSERT_EQ(configs["chunksize"]->get_value(), "1234");
ASSERT_EQ(configs["timeout"]->get_value(), "100");
ASSERT_EQ(configs["port"], nullptr);
metric_->UpdateConfigMetric(&conf);

std::string prefix = "chunkserver_127_0_0_1_9401_config_";
ASSERT_STREQ(bvar::Variable::describe_exposed(prefix + "chunksize").c_str(),
"{\"conf_name\":\"chunksize\",\"conf_value\":\"1234\"}");
ASSERT_STREQ(bvar::Variable::describe_exposed(prefix + "timeout").c_str(),
"{\"conf_name\":\"timeout\",\"conf_value\":\"100\"}");
// 修改新增配置信息
conf.SetStringValue("chunksize", "4321");
conf.SetStringValue("port", "9999");
metric_->UpdateConfigMetric(conf);
// 验证修改后信息
configs = metric_->GetConfigMetric();
ASSERT_EQ(configs["chunksize"]->get_value(), "4321");
ASSERT_EQ(configs["timeout"]->get_value(), "100");
ASSERT_EQ(configs["port"]->get_value(), "9999");
metric_->UpdateConfigMetric(&conf);
// // 验证修改后信息
ASSERT_STREQ(bvar::Variable::describe_exposed(prefix + "chunksize").c_str(),
"{\"conf_name\":\"chunksize\",\"conf_value\":\"4321\"}");
ASSERT_STREQ(bvar::Variable::describe_exposed(prefix + "timeout").c_str(),
"{\"conf_name\":\"timeout\",\"conf_value\":\"100\"}");
ASSERT_STREQ(bvar::Variable::describe_exposed(prefix + "port").c_str(),
"{\"conf_name\":\"port\",\"conf_value\":\"9999\"}");
}

TEST_F(CSMetricTest, OnOffTest) {
Expand All @@ -460,7 +463,7 @@ TEST_F(CSMetricTest, OnOffTest) {
conf.SetConfigPath(confFile_);
int ret = conf.LoadConfig();
ASSERT_EQ(ret, true);
metric_->UpdateConfigMetric(conf);
metric_->UpdateConfigMetric(&conf);
}
// 初始化后获取所有指标项都为空
{
Expand All @@ -469,8 +472,6 @@ TEST_F(CSMetricTest, OnOffTest) {
ASSERT_EQ(metric_->GetCopysetCount(), 0);
ASSERT_EQ(metric_->GetLeaderCount(), 0);
ASSERT_EQ(metric_->GetChunkLeftCount(), 0);
ConfigMetricMap configs = metric_->GetConfigMetric();
ASSERT_EQ(configs.size(), 0);
}
// 创建copyset的metric返回成功,但实际并未创建
{
Expand Down
17 changes: 17 additions & 0 deletions test/common/configuration_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

#include "src/common/configuration.h"

Expand Down Expand Up @@ -310,5 +311,21 @@ TEST_F(ConfigurationTest, GetSetDoubleAndFloatValue) {
ASSERT_EQ(0.009f, outf);
}

TEST_F(ConfigurationTest, TestMetric) {
Configuration conf;
conf.SetIntValue("key1", 123);
conf.SetFloatValue("key2", 1.23);
conf.SetBoolValue("key3", true);

conf.ExposeMetric("conf_metric");
conf.UpdateMetric();
ASSERT_STREQ(bvar::Variable::describe_exposed("conf_metric_key1").c_str(),
"{\"conf_name\":\"key1\",\"conf_value\":\"123\"}");
ASSERT_STREQ(bvar::Variable::describe_exposed("conf_metric_key2").c_str(),
"{\"conf_name\":\"key2\",\"conf_value\":\"1.230000\"}");
ASSERT_STREQ(bvar::Variable::describe_exposed("conf_metric_key3").c_str(),
"{\"conf_name\":\"key3\",\"conf_value\":\"1\"}");
}

} // namespace common
} // namespace curve

0 comments on commit f1b7ab1

Please sign in to comment.