[optimize] move value_dump_str_ from ObConfigItem to ObConfigVersionItem

This commit is contained in:
obdev
2023-05-30 06:11:15 +00:00
committed by ob-robot
parent 5af9d6c36d
commit 54f01814ec
4 changed files with 75 additions and 54 deletions

View File

@ -41,12 +41,6 @@ int ObBaseConfig::init()
if (OB_UNLIKELY(inited_)) {
LOG_ERROR("init twice", K(inited_));
ret = OB_INIT_TWICE;
} else if (OB_ISNULL(config_file_buf1_= static_cast<char *>(ob_malloc(buf_len, "ConfigBuf")))) {
LOG_ERROR("allocate memory for buffer fail", K(config_file_buf1_), K(buf_len));
ret = OB_ALLOCATE_MEMORY_FAILED;
} else if (OB_ISNULL(config_file_buf2_= static_cast<char *>(ob_malloc(buf_len, "ConfigBuf")))) {
LOG_ERROR("allocate memory for buffer fail", K(config_file_buf2_), K(buf_len));
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
inited_ = true;
}
@ -55,16 +49,6 @@ int ObBaseConfig::init()
void ObBaseConfig::destroy()
{
if (NULL != config_file_buf1_) {
ob_free(config_file_buf1_);
config_file_buf1_ = NULL;
}
if (NULL != config_file_buf2_) {
ob_free(config_file_buf2_);
config_file_buf2_ = NULL;
}
inited_ = false;
}
@ -117,6 +101,8 @@ int ObBaseConfig::load_from_buffer(const char *config_str, const int64_t config_
char *saveptr = NULL;
char *token = NULL;
int64_t pos =0;
char *config_file_buf = NULL;
const int64_t buf_len= OB_MAX_CONFIG_LENGTH;
if (OB_UNLIKELY(!inited_)) {
LOG_ERROR("Config has not been initialized");
@ -124,22 +110,22 @@ int ObBaseConfig::load_from_buffer(const char *config_str, const int64_t config_
} else if (NULL == config_str || config_str_len <= 0) {
LOG_ERROR("invalid argument", K(config_str), K(config_str_len));
ret = OB_INVALID_ARGUMENT;
} else if (OB_ISNULL(config_file_buf2_)) {
LOG_ERROR("config_file_buf2_ is NULL", K(config_file_buf2_));
ret = OB_ERR_UNEXPECTED;
} else if (OB_ISNULL(config_file_buf = static_cast<char *>(ob_malloc(buf_len, "ConfigBuf")))) {
LOG_ERROR("allocate memory for buffer fail", K(config_file_buf), K(buf_len));
ret = OB_ALLOCATE_MEMORY_FAILED;
} else {
config_file_buf2_[0] = '\0';
config_file_buf[0] = '\0';
const int64_t buf_size = OB_MAX_CONFIG_LENGTH;
if (config_str_len > (buf_size - 1)) {
LOG_ERROR("extra config is too long!", K(config_str_len), K(buf_size));
ret = OB_BUF_NOT_ENOUGH;
} else if (OB_FAIL(databuff_printf(config_file_buf2_, buf_size, pos, "%.*s",
} else if (OB_FAIL(databuff_printf(config_file_buf, buf_size, pos, "%.*s",
static_cast<int>(config_str_len), config_str))) {
LOG_ERROR("copy config string fail", KR(ret), K(config_file_buf2_), K(buf_size), K(pos), K(config_str_len),
LOG_ERROR("copy config string fail", KR(ret), K(config_file_buf), K(buf_size), K(pos), K(config_str_len),
K(config_str));
} else {
token = strtok_r(config_file_buf2_, ",\n", &saveptr);
token = strtok_r(config_file_buf, ",\n", &saveptr);
while (NULL != token && OB_SUCCESS == ret) {
char *saveptr_one = NULL;
const char *name = NULL;
@ -170,6 +156,12 @@ int ObBaseConfig::load_from_buffer(const char *config_str, const int64_t config_
}
}
}
if (OB_LIKELY(NULL != config_file_buf)) {
ob_free(config_file_buf);
config_file_buf = NULL;
}
return ret;
}
@ -179,6 +171,8 @@ int ObBaseConfig::load_from_file(const char *config_file,
{
int ret = OB_SUCCESS;
FILE *fp = NULL;
char *config_file_buf = NULL;
const int64_t buf_len= OB_MAX_CONFIG_LENGTH;
if (OB_UNLIKELY(! inited_)) {
LOG_ERROR("ObLogConfig has not been initialized");
@ -186,16 +180,16 @@ int ObBaseConfig::load_from_file(const char *config_file,
} else if (OB_ISNULL(config_file)) {
LOG_ERROR("invalid argument", K(config_file));
ret = OB_INVALID_ARGUMENT;
} else if (OB_ISNULL(config_file_buf1_)) {
LOG_ERROR("config_file_buf1_ is NULL", K(config_file_buf1_));
ret = OB_ERR_UNEXPECTED;
} else if (OB_ISNULL(config_file_buf = static_cast<char *>(ob_malloc(buf_len, "ConfigBuf")))) {
LOG_ERROR("allocate memory for buffer fail", K(config_file_buf), K(buf_len));
ret = OB_ALLOCATE_MEMORY_FAILED;
} else if (NULL == (fp = fopen(config_file, "rb"))) {
ret = OB_IO_ERROR;
LOG_ERROR("can't open file", K(config_file), KR(ret), KERRNOMSG(errno));
} else {
config_file_buf1_[0] = '\0';
config_file_buf[0] = '\0';
int64_t buffer_size = OB_MAX_CONFIG_LENGTH;
int64_t read_len = fread(config_file_buf1_, 1, buffer_size - 1, fp);
int64_t read_len = fread(config_file_buf, 1, buffer_size - 1, fp);
if (0 != ferror(fp)) {
ret = OB_IO_ERROR;
@ -210,9 +204,9 @@ int ObBaseConfig::load_from_file(const char *config_file,
ret = OB_SIZE_OVERFLOW;
} else {
// end with '\0'
config_file_buf1_[read_len] = '\0';
config_file_buf[read_len] = '\0';
if (OB_FAIL(load_from_buffer(config_file_buf1_, read_len, version, check_name))) {
if (OB_FAIL(load_from_buffer(config_file_buf, read_len, version, check_name))) {
LOG_ERROR("load config fail", KR(ret), K(config_file), K(version), K(check_name),
K(read_len));
} else {
@ -226,6 +220,11 @@ int ObBaseConfig::load_from_file(const char *config_file,
fp = NULL;
}
if (OB_LIKELY(NULL != config_file_buf)) {
ob_free(config_file_buf);
config_file_buf = NULL;
}
return ret;
}

View File

@ -64,8 +64,7 @@ public:
typedef ObArray<ConfigItem> ConfigItemArray;
public:
ObBaseConfig() : config_file_buf1_(NULL), config_file_buf2_(NULL)
{}
ObBaseConfig() {}
int init();
void destroy();
int check_all();
@ -74,11 +73,6 @@ public:
const int64_t version = 0, const bool check_name = false);
int load_from_file(const char *config_file, const int64_t version = 0, const bool check_name = false);
int dump2file(const char *config_file) const;
protected:
// for load_from_file
char *config_file_buf1_;
// for load_from_buffer
char *config_file_buf2_;
private:
bool inited_;
static const int64_t OB_MAX_CONFIG_LENGTH = 5 * 1024 * 1024; // 5M

View File

@ -58,12 +58,11 @@ const char *log_archive_encryption_algorithm_values[] =
// ObConfigItem
ObConfigItem::ObConfigItem()
: ck_(NULL), version_(0), dumped_version_(0), inited_(false), initial_value_set_(false),
value_updated_(false), dump_value_updated_(false), value_valid_(false), name_str_(nullptr), info_str_(nullptr),
value_updated_(false), value_valid_(false), name_str_(nullptr), info_str_(nullptr),
range_str_(nullptr), lock_()
{
MEMSET(value_str_, 0, sizeof(value_str_));
MEMSET(value_reboot_str_, 0, sizeof(value_reboot_str_));
MEMSET(value_dump_str_, 0, sizeof(value_dump_str_));
}
ObConfigItem::~ObConfigItem()
@ -989,11 +988,13 @@ ObConfigVersionItem::ObConfigVersionItem(ObConfigContainer *container,
const char *range,
const char *info,
const ObParameterAttr attr)
: dump_value_updated_(false)
{
if (OB_LIKELY(NULL != container)) {
container->set_refactored(ObConfigStringKey(name), this, 1);
}
init(scope_info, name, def, range, info, attr);
MEMSET(value_dump_str_, 0, sizeof(value_dump_str_));
}
ObConfigVersionItem::ObConfigVersionItem(ObConfigContainer *container,
@ -1002,11 +1003,13 @@ ObConfigVersionItem::ObConfigVersionItem(ObConfigContainer *container,
const char *def,
const char *info,
const ObParameterAttr attr)
: dump_value_updated_(false)
{
if (OB_LIKELY(NULL != container)) {
container->set_refactored(ObConfigStringKey(name), this, 1);
}
init(scope_info, name, def, "", info, attr);
MEMSET(value_dump_str_, 0, sizeof(value_dump_str_));
}
bool ObConfigVersionItem::set(const char *str)

View File

@ -113,20 +113,18 @@ public:
ret = databuff_printf(value_reboot_str_, sizeof(value_reboot_str_), pos, "%s", str);
return ret == OB_SUCCESS;
}
bool set_dump_value(const char *str)
virtual bool set_dump_value(const char *str)
{
int64_t pos = 0;
int ret = OB_SUCCESS;
ret = databuff_printf(value_dump_str_, sizeof(value_dump_str_), pos, "%s", str);
return ret == OB_SUCCESS;
UNUSED(str);
return false;
}
void set_dump_value_updated()
virtual void set_dump_value_updated()
{
dump_value_updated_ = true;
// do nothing
}
bool dump_value_updated() const
virtual bool dump_value_updated() const
{
return dump_value_updated_;
return false;
}
void set_value_updated()
{
@ -156,13 +154,11 @@ public:
ObLatchRGuard rd_guard(const_cast<ObLatch&>(lock_), ObLatchIds::CONFIG_LOCK);
return value_str_;
}
const char *spfile_str() const
virtual const char *spfile_str() const
{
const char *ret = nullptr;
ObLatchRGuard rd_guard(const_cast<ObLatch&>(lock_), ObLatchIds::CONFIG_LOCK);
if (dump_value_updated()) {
ret = value_dump_str_;
} else if (reboot_effective() && is_initial_value_set()) {
if (reboot_effective() && is_initial_value_set()) {
ret = value_reboot_str_;
} else {
ret = value_str_;
@ -215,11 +211,9 @@ protected:
bool inited_;
bool initial_value_set_;
bool value_updated_;
bool dump_value_updated_;
bool value_valid_;
char value_str_[OB_MAX_CONFIG_VALUE_LEN];
char value_reboot_str_[OB_MAX_CONFIG_VALUE_LEN];
char value_dump_str_[OB_MAX_CONFIG_VALUE_LEN];
const char* name_str_;
const char* info_str_;
const char* range_str_;
@ -871,12 +865,43 @@ public:
virtual ObConfigItemType get_config_item_type() const {
return ObConfigItemType::OB_CONF_ITEM_TYPE_VERSION;
}
bool set_dump_value(const char *str) override
{
int64_t pos = 0;
int ret = OB_SUCCESS;
ret = databuff_printf(value_dump_str_, sizeof(value_dump_str_), pos, "%s", str);
return ret == OB_SUCCESS;
}
void set_dump_value_updated() override
{
dump_value_updated_ = true;
}
bool dump_value_updated() const override
{
return dump_value_updated_;
}
const char *spfile_str() const override
{
const char *ret = nullptr;
ObLatchRGuard rd_guard(const_cast<ObLatch&>(lock_), ObLatchIds::CONFIG_LOCK);
if (dump_value_updated()) {
ret = value_dump_str_;
} else if (reboot_effective() && is_initial_value_set()) {
ret = value_reboot_str_;
} else {
ret = value_str_;
}
return ret;
}
ObConfigVersionItem &operator = (int64_t value);
protected:
virtual bool set(const char *str) override;
virtual int64_t parse(const char *str, bool &valid) const override;
char value_dump_str_[32]; // 32 is enough for version like 4.2.0.0
bool dump_value_updated_;
private:
DISALLOW_COPY_AND_ASSIGN(ObConfigVersionItem);
};