-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.cpp
192 lines (174 loc) · 6.15 KB
/
main.cpp
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
#include <unistd.h>
#include <csignal>
#include <map>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "core/globals.hpp"
#include "core/command.hpp"
#include "core/server.hpp"
#include "utils/logging.hpp"
#include "utils/address.hpp"
#include "utils/random.hpp"
#include "utils/string.h"
#include "backtracpp/sig-handler.h"
namespace {
class Configuration {
std::map<std::string, std::string> _config;
void _parse_opt(int argc, char* argv[])
{
int ch;
opterr = 0;
while ((ch = getopt(argc, argv, "b:n:t:r:R:")) != EOF) {
switch (ch) {
case 'b':
_config["bind"] = optarg;
break;
case 'n':
_config["node"] = optarg;
break;
case 't':
_config["thread"] = optarg;
break;
case 'r':
_config["read-slave"] = optarg;
break;
case 'R':
_config["read-slave-filter"] = optarg;
break;
default:
std::cerr << "Invalid option." << std::endl;
exit(1);
}
}
}
public:
Configuration(int argc, char* argv[])
{
std::ifstream conf_file(argv[0]);
if (!conf_file.good()) {
std::cerr << "Fail to read config file " << argv[0] << std::endl;
exit(1);
}
std::string line;
while (!conf_file.eof()) {
std::getline(conf_file, line);
if (line.empty()) {
continue;
}
std::vector<std::string> kv(util::split_str(line, " ", true));
if (kv.size() != 2) {
std::cerr << "Invalid configure line " << line << std::endl;
exit(1);
}
_config[kv[0]] = kv[1];
}
_parse_opt(argc, argv);
}
bool contains(std::string const& k) const
{
return _config.find(k) != _config.end();
}
std::string const& get(std::string const& k) const
{
try {
return _config.at(k);
} catch (std::out_of_range&) {
throw std::runtime_error("Not configured: " + k);
}
}
std::string get(std::string const& k, std::string def) const
{
try {
return _config.at(k);
} catch (std::out_of_range&) {
return def;
}
}
};
void exit_on_int(int)
{
LOG(INFO) << "C-c Exit.";
exit(0);
}
void run(Configuration const& config)
{
if (config.get("read-slave", "") == "yes") {
LOG(INFO) << "Readonly proxy, use slaves for reading if possible";
cerb::Server::send_readonly_for_each_conn();
cerb::stats_set_read_slave();
cerb::SlotMap::select_slave_if_possible(config.get("read-slave-filter", ""));
} else {
LOG(INFO) << "Writable proxy";
cerb::Command::allow_write_commands();
}
if (config.get("cluster-require-full-coverage", "") == "no") {
LOG(INFO) << "Proxy won't require full slots coverage.";
cerb_global::set_cluster_req_full_cov(false);
}
int slow_poll_ms = util::atoi(config.get("slow-poll-elapse-ms", "50"));
if (slow_poll_ms <= 0) {
LOG(ERROR) << "Invalid slow poll elapse";
exit(1);
}
cerb_global::slow_poll_elapse = std::chrono::milliseconds(slow_poll_ms);
int bind_port = util::atoi(config.get("bind"));
int thread_count = util::atoi(config.get("thread", "1"));
if (thread_count <= 0) {
LOG(ERROR) << "Invalid thread count";
exit(1);
}
if (config.contains("node")) {
cerb_global::set_remotes(util::Address::from_hosts_ports(config.get("node")));
} else {
LOG(WARNING) << "Remote is not set in config file; to set it by command,"
" use `SETREMOTES <host> <port>' in a redis-cli prompt";
}
for (int i = 0; i < thread_count; ++i) {
cerb_global::all_threads.push_back(cerb::ListenThread(bind_port));
}
for (auto& t: cerb_global::all_threads) {
t.run();
}
LOG(INFO) << "Started; listen to port " << bind_port
<< " thread=" << thread_count;
for (auto& t: cerb_global::all_threads) {
t.join();
}
}
}
int main(int argc, char* argv[])
{
std::cerr << "Cerberus version " VERSION
" Copyright (c) HunanTV Platform developers" << std::endl;
if (argc == 1) {
std::cerr << "Usage:" << std::endl;
std::cerr << " cerberus CONFIG_FILE [ARGS]" << std::endl;
std::cerr << " where ARGS could be" << std::endl;
std::cerr << " -b PORT : port number for listening" << std::endl;
std::cerr << " -n NODE : initial redis node" << std::endl;
std::cerr << " -t THREAD : thread count" << std::endl;
std::cerr << " -r READONLY : if the proxy is readonly,"
" value shall be `no' or `yes'" << std::endl;
std::cerr << " -R SLAVE_HOST_BEGINNING : (if READONLY set to `yes')"
" if multiple slaves replicating one master,"
" use the one whose host starts with this pattern" << std::endl;
std::cerr << " Options passed by command line will override"
" those in the config file" << std::endl;
return 1;
}
Configuration config(argc - 1, argv + 1);
signal(SIGINT, exit_on_int);
logging::init();
util::random_init();
trac::trace_on_seg_fault();
trac::trace_on_fpe();
try {
run(config);
return 0;
} catch (std::runtime_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}