1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
// (C) 2013 Cybozu.
#include "config.hpp"
#include <cybozu/config_parser.hpp>
#include <cybozu/filesystem.hpp>
#include <cybozu/logger.hpp>
#include <unordered_map>
namespace {
const char VIRTUAL_IP[] = "virtual_ip";
const char PORT[] = "port";
const char REPL_PORT[] = "repl_port";
const char BIND_IP[] = "bind_ip";
const char MAX_CONNECTIONS[] = "max_connections";
const char TEMP_DIR[] = "temp_dir";
const char USER[] = "user";
const char GROUP[] = "group";
const char LOG_THRESHOLD[] = "log.threshold";
const char LOG_FILE[] = "log.file";
const char BUCKETS[] = "buckets";
const char MAX_DATA_SIZE[] = "max_data_size";
const char HEAP_DATA_LIMIT[] = "heap_data_limit";
const char MEMORY_LIMIT[] = "memory_limit";
const char REPL_BUFSIZE[] = "repl_buffer_size";
const char SECURE_ERASE[] = "secure_erase";
const char LOCK_MEMORY[] = "lock_memory";
const char WORKERS[] = "workers";
const char GC_INTERVAL[] = "gc_interval";
const char SLAVE_TIMEOUT[] = "slave_timeout";
const char COUNTER_ENABLE[] = "counter.enable";
const char COUNTER_PORT[] = "counter.port";
const char COUNTER_MAX_CONNECTIONS[] = "counter.max_connections";
const char COUNTER_BUCKETS[] = "counter.buckets";
const char COUNTER_STAT_INTERVAL[] = "counter.stat_interval";
std::unordered_map<std::string, cybozu::severity> THRESHOLDS {
{"error", cybozu::severity::error},
{"warning", cybozu::severity::warning},
{"info", cybozu::severity::info},
{"debug", cybozu::severity::debug}
};
inline std::size_t parse_unit(std::string& s, const char* cmd) {
std::size_t base = 1;
switch( s.back() ) {
case 'k':
case 'K':
base <<= 10;
break;
case 'm':
case 'M':
base <<= 20;
break;
case 'g':
case 'G':
base <<= 30;
break;
}
if( base != 1 )
s.pop_back();
int n = std::stoi( s );
if( n < 1 )
throw yrmcds::config::bad_config(cmd + std::string(" must be > 0"));
return base * static_cast<std::size_t>(n);
}
} // anonymous namespace
namespace yrmcds {
void counter_config::load(const cybozu::config_parser& cp) {
if( cp.exists(COUNTER_ENABLE) )
m_enable = cp.get_as_bool(COUNTER_ENABLE);
if( cp.exists(COUNTER_PORT) ) {
int n = cp.get_as_int(COUNTER_PORT);
if( n < 1 || n > 65535 )
throw config::bad_config("Bad port: " + cp.get(COUNTER_PORT));
m_port = static_cast<std::uint16_t>(n);
}
if( cp.exists(COUNTER_MAX_CONNECTIONS) ) {
int conns = cp.get_as_int(COUNTER_MAX_CONNECTIONS);
if( conns < 0 )
throw config::bad_config("counter.max_connections must be >= 0");
m_max_connections = conns;
}
if( cp.exists(COUNTER_BUCKETS) ) {
int buckets = cp.get_as_int(COUNTER_BUCKETS);
if( buckets < 1 )
throw config::bad_config("buckets must be > 0");
if( buckets < 10000 )
cybozu::logger::warning() << "Too small bucket count!";
m_buckets = buckets;
}
if( cp.exists(COUNTER_STAT_INTERVAL) ) {
int n = cp.get_as_int(COUNTER_STAT_INTERVAL);
if( n < 10 )
throw config::bad_config("counter.stat_interval must be >= 10");
m_stat_interval = n;
}
}
void config::load(const std::string& path) {
cybozu::config_parser cp(path);
if( cp.exists(VIRTUAL_IP) )
m_vip.parse(cp.get(VIRTUAL_IP));
if( cp.exists(PORT) ) {
int n = cp.get_as_int(PORT);
if( n < 1 || n > 65535 )
throw bad_config("Bad port: " + cp.get(PORT));
m_port = static_cast<std::uint16_t>(n);
}
if( cp.exists(REPL_PORT) ) {
int n = cp.get_as_int(REPL_PORT);
if( n < 1 || n > 65535 )
throw bad_config("Bad repl port: " + cp.get(REPL_PORT));
m_repl_port = static_cast<std::uint16_t>(n);
}
if( cp.exists(BIND_IP) ) {
for( auto& s: cybozu::tokenize(cp.get(BIND_IP), ' ') ) {
m_bind_ip.emplace_back(s);
}
}
if( cp.exists(MAX_CONNECTIONS) ) {
int conns = cp.get_as_int(MAX_CONNECTIONS);
if( conns < 0 )
throw bad_config("max_connections must be >= 0");
m_max_connections = conns;
}
if( cp.exists(TEMP_DIR) ) {
m_tempdir = cp.get(TEMP_DIR);
if( ! cybozu::is_dir(m_tempdir) )
throw bad_config("Not a directory: " + m_tempdir);
if( ! cybozu::is_writable(m_tempdir) )
throw bad_config("Directory not writable: " + m_tempdir);
}
if( cp.exists(USER) ) {
m_user = cp.get(USER);
}
if( cp.exists(GROUP) ) {
m_group = cp.get(GROUP);
}
if( cp.exists(LOG_THRESHOLD) ) {
auto it = THRESHOLDS.find(cp.get(LOG_THRESHOLD));
if( it == THRESHOLDS.end() )
throw bad_config("Invalid threshold: " + cp.get(LOG_THRESHOLD));
m_threshold = it->second;
}
if( cp.exists(LOG_FILE) ) {
m_logfile = cp.get(LOG_FILE);
if( m_logfile.size() == 0 || m_logfile[0] != '/' )
throw bad_config("Invalid log file: " + m_logfile);
}
if( cp.exists(BUCKETS) ) {
int buckets = cp.get_as_int(BUCKETS);
if( buckets < 1 )
throw bad_config("buckets must be > 0");
if( buckets < 10000 )
cybozu::logger::warning() << "Too small bucket count!";
m_buckets = buckets;
}
if( cp.exists(MAX_DATA_SIZE) ) {
std::string t = cp.get(MAX_DATA_SIZE);
if( t.empty() )
throw bad_config("max_data_size must not be empty");
m_max_data_size = parse_unit(t, MAX_DATA_SIZE);
}
if( cp.exists(HEAP_DATA_LIMIT) ) {
std::string t = cp.get(HEAP_DATA_LIMIT);
if( t.empty() )
throw bad_config("heap_data_limit must not be empty");
m_heap_data_limit = parse_unit(t, HEAP_DATA_LIMIT);
if( m_heap_data_limit < 4096 )
throw bad_config("too small heap_data_limit");
}
if( cp.exists(MEMORY_LIMIT) ) {
std::string t = cp.get(MEMORY_LIMIT);
if( t.empty() )
throw bad_config("memory_limit must not be empty");
m_memory_limit = parse_unit(t, MEMORY_LIMIT);
}
if( cp.exists(REPL_BUFSIZE) ) {
int bufs = cp.get_as_int(REPL_BUFSIZE);
if( bufs < 1 )
throw bad_config("repl_buffer_size must be > 0");
m_repl_bufsize = bufs;
}
if( cp.exists(SECURE_ERASE) ) {
m_secure_erase = cp.get_as_bool(SECURE_ERASE);
}
if( cp.exists(LOCK_MEMORY) ) {
m_lock_memory = cp.get_as_bool(LOCK_MEMORY);
}
if( cp.exists(WORKERS) ) {
int n = cp.get_as_int(WORKERS);
if( n < 1 )
throw bad_config("workers must be > 0");
if( n > MAX_WORKERS )
throw bad_config("workers must be <= " +
std::to_string(MAX_WORKERS));
m_workers = n;
}
if( cp.exists(GC_INTERVAL) ) {
int n = cp.get_as_int(GC_INTERVAL);
if( n < 1 )
throw bad_config("gc_interval must be > 0");
m_gc_interval = n;
}
if( cp.exists(SLAVE_TIMEOUT) ) {
int n = cp.get_as_int(SLAVE_TIMEOUT);
if( n < 1 )
throw bad_config("slave_timeout must be > 0");
m_slave_timeout = n;
}
m_counter_config.load(cp);
}
config g_config;
} // namespace yrmcds
|