This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
refactor(util): use flags to define configurations #275
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
068be55
utils: use flags to define configuration item
5c880ef
fix
8570fbc
fix ut
56d6b98
Merge branch 'master' into flags
2513a3e
Merge branch 'master' into flags
6903570
Merge branch 'master' into flags
acelyc111 ab67158
Update flags.cpp
c330e66
fix
f1ed82a
Merge branch 'master' into flags
ffc736e
fix
73a3e49
Merge branch 'master' into flags
acelyc111 7ded916
fix
ffe383f
Merge branch 'flags' of github.com:neverchanje/rdsn into flags
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2017-present, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <cstdint> | ||
#include <functional> | ||
|
||
// Example: | ||
// DSN_DEFINE_string("core", filename, "my_file.txt", "The file to read"); | ||
// DSN_DEFINE_validator(filename, [](const char *fname){ return is_file(fname); }); | ||
// auto fptr = file::open(FLAGS_filename, O_RDONLY | O_BINARY, 0); | ||
|
||
#define DSN_DECLARE_VARIABLE(type, name) extern type FLAGS_##name | ||
|
||
#define DSN_DECLARE_int32(name) DSN_DECLARE_VARIABLE(int32_t, name) | ||
#define DSN_DECLARE_uint32(name) DSN_DECLARE_VARIABLE(uint32_t, name) | ||
#define DSN_DECLARE_int64(name) DSN_DECLARE_VARIABLE(int64_t, name) | ||
#define DSN_DECLARE_uint64(name) DSN_DECLARE_VARIABLE(uint64_t, name) | ||
#define DSN_DECLARE_double(name) DSN_DECLARE_VARIABLE(double, name) | ||
#define DSN_DECLARE_bool(name) DSN_DECLARE_VARIABLE(bool, name) | ||
#define DSN_DECLARE_string(name) DSN_DECLARE_VARIABLE(const char *, name) | ||
|
||
#define DSN_DEFINE_VARIABLE(type, section, name, default_value, desc) \ | ||
type FLAGS_##name = default_value; \ | ||
static dsn::flag_registerer FLAGS_REG_##name(section, #name, desc, &FLAGS_##name) | ||
|
||
#define DSN_DEFINE_int32(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(int32_t, section, name, val, desc) | ||
#define DSN_DEFINE_uint32(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(uint32_t, section, name, val, desc) | ||
#define DSN_DEFINE_int64(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(int64_t, section, name, val, desc) | ||
#define DSN_DEFINE_uint64(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(uint64_t, section, name, val, desc) | ||
#define DSN_DEFINE_double(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(double, section, name, val, desc) | ||
#define DSN_DEFINE_bool(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(bool, section, name, val, desc) | ||
#define DSN_DEFINE_string(section, name, val, desc) \ | ||
DSN_DEFINE_VARIABLE(const char *, section, name, val, desc) | ||
|
||
// Convenience macro for the registration of a flag validator. | ||
// `validator` must be a std::function<bool(FLAG_TYPE)> and receives the flag value as argument, | ||
// returns true if validation passed. | ||
// The program corrupts if the validation failed. | ||
#define DSN_DEFINE_validator(name, validator) \ | ||
static auto FLAGS_VALIDATOR_FN_##name = validator; \ | ||
static const dsn::flag_validator FLAGS_VALIDATOR_##name(#name, []() { \ | ||
dassert(FLAGS_VALIDATOR_FN_##name(FLAGS_##name), "validation failed: %s", #name); \ | ||
}) | ||
|
||
namespace dsn { | ||
|
||
// An utility class that registers a flag upon initialization. | ||
class flag_registerer | ||
{ | ||
public: | ||
flag_registerer(const char *section, const char *name, const char *desc, int32_t *val); | ||
flag_registerer(const char *section, const char *name, const char *desc, uint32_t *val); | ||
flag_registerer(const char *section, const char *name, const char *desc, int64_t *val); | ||
flag_registerer(const char *section, const char *name, const char *desc, uint64_t *val); | ||
flag_registerer(const char *section, const char *name, const char *desc, double *val); | ||
flag_registerer(const char *section, const char *name, const char *desc, bool *val); | ||
flag_registerer(const char *section, const char *name, const char *desc, const char **val); | ||
}; | ||
|
||
// An utility class that registers a validator upon initialization. | ||
class flag_validator | ||
{ | ||
public: | ||
flag_validator(const char *name, std::function<void()>); | ||
}; | ||
|
||
// Loads all the flags from configuration. | ||
extern void flags_initialize(); | ||
|
||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright (c) 2017-present, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <dsn/utility/flags.h> | ||
#include <dsn/utility/config_api.h> | ||
#include <dsn/utility/singleton.h> | ||
#include <dsn/c/api_utilities.h> | ||
#include <boost/optional/optional.hpp> | ||
|
||
#include <map> | ||
|
||
namespace dsn { | ||
|
||
enum value_type | ||
{ | ||
FV_BOOL = 0, | ||
FV_INT32 = 1, | ||
FV_UINT32 = 2, | ||
FV_INT64 = 3, | ||
FV_UINT64 = 4, | ||
FV_DOUBLE = 5, | ||
FV_STRING = 6, | ||
FV_MAX_INDEX = 6, | ||
}; | ||
|
||
using validator_fn = std::function<void()>; | ||
|
||
class flag_data | ||
{ | ||
public: | ||
#define FLAG_DATA_LOAD_CASE(type, type_enum, suffix) \ | ||
case type_enum: \ | ||
value<type>() = dsn_config_get_value_##suffix(_section, _name, value<type>(), _desc); \ | ||
if (_validator) { \ | ||
_validator(); \ | ||
} \ | ||
break | ||
|
||
void load() | ||
{ | ||
switch (_type) { | ||
FLAG_DATA_LOAD_CASE(int32_t, FV_INT32, int64); | ||
FLAG_DATA_LOAD_CASE(int64_t, FV_INT64, int64); | ||
FLAG_DATA_LOAD_CASE(uint32_t, FV_UINT32, uint64); | ||
FLAG_DATA_LOAD_CASE(uint64_t, FV_UINT64, uint64); | ||
FLAG_DATA_LOAD_CASE(bool, FV_BOOL, bool); | ||
FLAG_DATA_LOAD_CASE(double, FV_DOUBLE, double); | ||
FLAG_DATA_LOAD_CASE(const char *, FV_STRING, string); | ||
} | ||
} | ||
|
||
flag_data(const char *section, const char *name, const char *desc, value_type type, void *val) | ||
: _type(type), _val(val), _section(section), _name(name), _desc(desc) | ||
{ | ||
} | ||
|
||
void set_validator(validator_fn &validator) { _validator = std::move(validator); } | ||
const validator_fn &validator() const { return _validator; } | ||
|
||
private: | ||
template <typename T> | ||
T &value() | ||
{ | ||
return *reinterpret_cast<T *>(_val); | ||
} | ||
|
||
private: | ||
const value_type _type; | ||
void *const _val; | ||
const char *_section; | ||
const char *_name; | ||
const char *_desc; | ||
validator_fn _validator; | ||
}; | ||
|
||
class flag_registry : public utils::singleton<flag_registry> | ||
{ | ||
public: | ||
void add_flag(const char *name, flag_data flag) { _flags.emplace(name, flag); } | ||
|
||
void add_validator(const char *name, validator_fn &validator) | ||
{ | ||
auto it = _flags.find(name); | ||
dassert(it != _flags.end(), "flag \"%s\" does not exist", name); | ||
flag_data &flag = it->second; | ||
if (!flag.validator()) { | ||
flag.set_validator(validator); | ||
} | ||
} | ||
|
||
void load_from_config() | ||
{ | ||
for (auto &kv : _flags) { | ||
flag_data &flag = kv.second; | ||
flag.load(); | ||
} | ||
} | ||
|
||
private: | ||
friend class utils::singleton<flag_registry>; | ||
flag_registry() = default; | ||
|
||
private: | ||
std::map<std::string, flag_data> _flags; | ||
}; | ||
|
||
#define FLAG_REG_CONSTRUCTOR(type, type_enum) \ | ||
flag_registerer::flag_registerer( \ | ||
const char *section, const char *name, const char *desc, type *val) \ | ||
{ \ | ||
flag_registry::instance().add_flag(name, flag_data(section, name, desc, type_enum, val)); \ | ||
} | ||
|
||
FLAG_REG_CONSTRUCTOR(int32_t, FV_INT32); | ||
FLAG_REG_CONSTRUCTOR(uint32_t, FV_UINT32); | ||
FLAG_REG_CONSTRUCTOR(int64_t, FV_INT64); | ||
FLAG_REG_CONSTRUCTOR(uint64_t, FV_UINT64); | ||
FLAG_REG_CONSTRUCTOR(bool, FV_BOOL); | ||
FLAG_REG_CONSTRUCTOR(double, FV_DOUBLE); | ||
FLAG_REG_CONSTRUCTOR(const char *, FV_STRING); | ||
|
||
flag_validator::flag_validator(const char *name, validator_fn validator) | ||
{ | ||
flag_registry::instance().add_validator(name, validator); | ||
} | ||
|
||
/*extern*/ void flags_initialize() { flag_registry::instance().load_from_config(); } | ||
|
||
} // namespace dsn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the name is
flag_registerer
, but notflag_register
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These class names are learned from gflags. If users are familiar with gflags, he will soon get started to use dsn flags.
See this: https://github.com/gflags/gflags/blob/master/src/gflags.h.in#L432.
"registerer" plays the role to register the flag, where "register" is a verb, not a noun.