Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
configuration: refactor configuration related APIs (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
shengofsun committed May 18, 2018
1 parent e9b1dde commit bda73a1
Show file tree
Hide file tree
Showing 23 changed files with 298 additions and 300 deletions.
41 changes: 0 additions & 41 deletions include/dsn/c/api_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,6 @@

#include <dsn/c/api_common.h>

/*!
@defgroup config Configuration Service
@ingroup service-api-utilities
Configuration Service (e.g., config.ini)
@{
*/

extern DSN_API const char *
dsn_config_get_value_string(const char *section, ///< [section]
const char *key, ///< key = value
const char *default_value, ///< if [section] key is not present
const char *dsptr ///< what it is for, as help-info in config
);
extern DSN_API bool dsn_config_get_value_bool(const char *section,
const char *key,
bool default_value,
const char *dsptr);
extern DSN_API uint64_t dsn_config_get_value_uint64(const char *section,
const char *key,
uint64_t default_value,
const char *dsptr);
extern DSN_API int64_t dsn_config_get_value_int64(const char *section,
const char *key,
int64_t default_value,
const char *dsptr);
extern DSN_API double dsn_config_get_value_double(const char *section,
const char *key,
double default_value,
const char *dsptr);
// return all section count (may greater than buffer_count)
extern DSN_API int dsn_config_get_all_sections(const char **buffers,
/*inout*/ int *buffer_count);
// return all key count (may greater than buffer_count)
extern DSN_API int dsn_config_get_all_keys(const char *section,
const char **buffers,
/*inout*/ int *buffer_count);
extern DSN_API void dsn_config_dump(const char *file);
/*@}*/

/*!
@defgroup logging Logging Service
@ingroup service-api-utilities
Expand Down
2 changes: 0 additions & 2 deletions include/dsn/service_api_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
#pragma once

#include <dsn/service_api_c.h>
//# include <dsn/utility/ports.h>
#include <dsn/tool-api/auto_codes.h>
#include <dsn/cpp/config_helper.h>
#include <dsn/cpp/serialization.h>
#include <dsn/cpp/serialization_helper/dsn.layer2.types.h>
#include <dsn/cpp/rpc_stream.h>
Expand Down
2 changes: 1 addition & 1 deletion include/dsn/tool-api/task_spec.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

#include <dsn/service_api_c.h>
#include <dsn/utility/utils.h>
#include <dsn/cpp/config_helper.h>
#include <dsn/utility/config_helper.h>
#include <dsn/utility/enum_helper.h>
#include <dsn/utility/customizable_id.h>
#include <dsn/utility/singleton_vector_store.h>
Expand Down
2 changes: 1 addition & 1 deletion include/dsn/tool-api/uri_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#pragma once

#include <algorithm>
#include <dsn/utility/configuration.h>
#include <dsn/utility/config_api.h>
#include <dsn/utility/synchronize.h>
#include <dsn/tool-api/rpc_address.h>
#include <dsn/tool-api/partition_resolver.h>
Expand Down
125 changes: 125 additions & 0 deletions include/dsn/utility/config_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Microsoft Corporation
*
* -=- Robust Distributed System Nucleus (rDSN) -=-
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

#include <string>
#include <vector>
#include <cstdint>

/// load a ini configuration file, and replace specific strings in file with arguments.
///
/// the rules of replacement is as follows:
/// 1. arguments are with format of k1=v1,k2=v2,k3=v3
/// 2. if a string if file is %k1%, it will be replaced with v1.
/// %k2%, %k3% will be replaced to v2 and v3 in a similar fashion
///
/// for example:
///
/// the file content:
/// "
/// [example_section]
/// %replace_key% = real_value
/// real_port = %host_port%
/// "
///
/// and the arguments "replace_key=key,host_port=8080
///
/// so the loaded config will be:
/// "
/// [example_section]
/// key = real_value
/// real_port = 8080
///
/// return true if load config file succeed, otherwise false
///
/// please call this function at the beginning of your process, otherwise non of the
/// dsn_config_xxx function works
///
/// the function is not thread safe.
bool dsn_config_load(const char *file, const char *arguments);

/// dump the global configuration
void dsn_config_dump(std::ostream &os);

/// the section format:
/// [section]
/// key = value
///
/// this function get the value of a key in some section.
/// if <key, value> is not present in section, then return default_value.
/// dsptr is the description of the key-value pairs.
///
/// this function is not thread safe if get/set are concurrently called
const char *dsn_config_get_value_string(const char *section,
const char *key,
const char *default_value,
const char *dsptr);

/// get value of a key in some section, the value should be "true" or "false"
/// this function is not thread safe if dsn_config_set is concurrently called
bool dsn_config_get_value_bool(const char *section,
const char *key,
bool default_value,
const char *dsptr);

/// get value of a key in some section, the value should be decimal in range of uint64_t
/// this function is not thread safe if dsn_config_set is concurrently called
uint64_t dsn_config_get_value_uint64(const char *section,
const char *key,
uint64_t default_value,
const char *dsptr);

/// get value of a key in some section, the value should be decimal in range of double
/// this function is not thread safe if dsn_config_set is concurrently called
double dsn_config_get_value_double(const char *section,
const char *key,
double default_value,
const char *dsptr);

/// get the names of all sections
/// this function is not thread safe if dsn_config_set is concurrently called
void dsn_config_get_all_sections(/*out*/ std::vector<std::string> &sections);

/// get the names of all sections
/// this function is not thread safe if dsn_config_set is concurrently called
void dsn_config_get_all_sections(/*out*/ std::vector<const char *> &sections);

/// get all keys in some specific section
/// this function is not thread safe if dsn_config_set is concurrently called
void dsn_config_get_all_keys(const char *section, /*out*/ std::vector<std::string> &keys);

/// get all keys in some specific section
/// this function is not thread safe if dsn_config_set is concurrently called
void dsn_config_get_all_keys(const char *section, /*out*/ std::vector<const char *> &keys);

/// set value for a key of some section.
/// if the section doesn't exsit, a new one will be created.
/// if the key doesn't exist, a new one will be created
///
/// multiple concurrent set are thread safe.
///
/// any of dsn_config_get_xxx may corrupt if called concurrently with dsn_config_set
void dsn_config_set(const char *section, const char *key, const char *value, const char *dsptr);
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@
* THE SOFTWARE.
*/

/*
* Description:
* What is this file about?
*
* Revision history:
* xxxx-xx-xx, author, first version
* xxxx-xx-xx, author, fix bug about xxx
*/

#pragma once

#include <dsn/service_api_c.h>
#include <dsn/utility/config_api.h>
#include <dsn/utility/strings.h>

/// you can use following macros to implement a function called "read_config"
/// to initialize a structure from the configuration file quickly
///
/// please refer to "task_spec.h". it's a very good example

#define CONFIG_BEGIN(t_struct) \
inline bool read_config( \
const char *section, /*out*/ t_struct &val, t_struct *default_value = nullptr) \
Expand Down
20 changes: 0 additions & 20 deletions include/dsn/utility/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/*
* Description:
* configuration file interface
*
* Revision history:
* Mar., 2015, @imzhenyu (Zhenyu Guo), first version
* Jul., 2015, @imzhenyu (Zhenyu Guo), add parameter support
* xxxx-xx-xx, author, fix bug about xxx
*/

#pragma once

#include <memory>
Expand All @@ -48,12 +37,6 @@

namespace dsn {

class configuration;
typedef std::shared_ptr<configuration> configuration_ptr;
typedef void (*config_file_change_notifier)(configuration_ptr);

extern configuration_ptr get_main_config();

class configuration
{
public:
Expand Down Expand Up @@ -84,8 +67,6 @@ class configuration

void set(const char *section, const char *key, const char *value, const char *dsptr);

void register_config_change_notification(config_file_change_notifier notifier);

bool has_section(const char *section);

bool has_key(const char *section, const char *key);
Expand Down Expand Up @@ -130,7 +111,6 @@ class configuration
config_map _configs;

std::string _file_name;
std::list<config_file_change_notifier> _notifiers;
std::string _file_data;
bool _warning;
};
Expand Down
72 changes: 72 additions & 0 deletions src/core/core/config_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <fstream>
#include <dsn/utility/config_api.h>
#include <dsn/utility/configuration.h>

dsn::configuration g_config;

bool dsn_config_load(const char *file, const char *arguments)
{
return g_config.load(file, arguments);
}

void dsn_config_dump(std::ostream &os) { g_config.dump(os); }

const char *dsn_config_get_value_string(const char *section,
const char *key,
const char *default_value,
const char *dsptr)
{
return g_config.get_string_value(section, key, default_value, dsptr);
}

bool dsn_config_get_value_bool(const char *section,
const char *key,
bool default_value,
const char *dsptr)
{
return g_config.get_value<bool>(section, key, default_value, dsptr);
}

uint64_t dsn_config_get_value_uint64(const char *section,
const char *key,
uint64_t default_value,
const char *dsptr)
{
return g_config.get_value<uint64_t>(section, key, default_value, dsptr);
}

double dsn_config_get_value_double(const char *section,
const char *key,
double default_value,
const char *dsptr)
{
return g_config.get_value<double>(section, key, default_value, dsptr);
}

void dsn_config_get_all_sections(/*out*/ std::vector<std::string> &sections)
{
g_config.get_all_sections(sections);
}

void dsn_config_get_all_sections(/*out*/ std::vector<const char *> &sections)
{
g_config.get_all_section_ptrs(sections);
}

void dsn_config_get_all_keys(const char *section, std::vector<std::string> &keys)
{
std::vector<const char *> key_ptrs;
g_config.get_all_keys(section, key_ptrs);
for (const char *p : key_ptrs)
keys.emplace_back(std::string(p));
}

void dsn_config_get_all_keys(const char *section, /*out*/ std::vector<const char *> &keys)
{
g_config.get_all_keys(section, keys);
}

void dsn_config_set(const char *section, const char *key, const char *value, const char *dsptr)
{
g_config.set(section, key, value, dsptr);
}
26 changes: 15 additions & 11 deletions src/core/core/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,30 @@
* xxxx-xx-xx, author, fix bug about xxx
*/

#include <dsn/utility/configuration.h>
#include <dsn/utility/utils.h>
#include <dsn/utility/filesystem.h>
#include <dsn/utility/strings.h>
#include <cassert>
#include <errno.h>
#include <iostream>
#include <algorithm>

#include <dsn/utility/utils.h>
#include <dsn/utility/filesystem.h>
#include <dsn/utility/strings.h>
#include <dsn/utility/configuration.h>

namespace dsn {

configuration::configuration() { _warning = false; }

configuration::~configuration() {}
configuration::~configuration()
{
for (auto &section_kv : _configs) {
auto &section = section_kv.second;
for (auto &kv : section) {
delete kv.second;
}
}
_configs.clear();
}

// arguments: k1=v1;k2=v2;k3=v3; ...
// e.g.,
Expand Down Expand Up @@ -431,12 +441,6 @@ void configuration::set(const char *section, const char *key, const char *value,
_lock.unlock();
}

void configuration::register_config_change_notification(config_file_change_notifier notifier)
{
printf("ERROR: method register_config_change_notification() not implemented\n");
::abort();
}

bool configuration::has_section(const char *section)
{
auto it = _configs.find(section);
Expand Down

0 comments on commit bda73a1

Please sign in to comment.