Remove config order restriction

- Delay setLatencyMonitor to load custom commands config first
- Fix custom command container bug
This commit is contained in:
Yoon 2018-04-12 15:05:02 +09:00
parent dcdc9a1ba0
commit 9d1d1af5bf
6 changed files with 36 additions and 21 deletions

View File

@ -7,7 +7,6 @@
## }... ## }...
## } ## }
## This section must be defined before Latency monitor to support all commands
## Currently support maximum 16 custom commands ## Currently support maximum 16 custom commands
## Example: ## Example:

View File

@ -95,7 +95,6 @@ Include try.conf
################################### COMMAND #################################### ################################### COMMAND ####################################
## Custom command define, see command.conf ## Custom command define, see command.conf
## must be defined before Latency monitor
#Include command.conf #Include command.conf
################################### LATENCY #################################### ################################### LATENCY ####################################

View File

@ -190,15 +190,15 @@ void Command::init()
} }
} }
void Command::addCustomCommand(const Command *p) { void Command::addCustomCommand(const Command& c) {
if (Sentinel >= AvailableCommands) { if (Sentinel >= AvailableCommands) {
Throw(InitFail, "too many custom commands(>%d)", MaxCustomCommands); Throw(InitFail, "too many custom commands(>%d)", MaxCustomCommands);
} }
if (nullptr != find(p->name)) { if (nullptr != find(c.name)) {
Throw(InitFail, "custom command %s is duplicated", p->name); Throw(InitFail, "custom command %s is duplicated", c.name);
} }
CmdPool[Sentinel] = *p; CmdPool[Sentinel] = c;
CmdMap[p->name] = &CmdPool[Sentinel]; CmdMap[c.name] = &CmdPool[Sentinel];
Sentinel++; Sentinel++;
} }

View File

@ -251,7 +251,7 @@ public:
auto it = CmdMap.find(cmd); auto it = CmdMap.find(cmd);
return it == CmdMap.end() ? nullptr : it->second; return it == CmdMap.end() ? nullptr : it->second;
} }
static void addCustomCommand(const Command *pc); static void addCustomCommand(const Command& pc);
static int Sentinel; static int Sentinel;
private: private:
static const int MaxArgs = 100000000; static const int MaxArgs = 100000000;

View File

@ -35,11 +35,18 @@ bool ServerConf::parse(ServerConf& s, const char* str)
void CustomCommandConf::init(CustomCommandConf&c, const char* name, const int type) { void CustomCommandConf::init(CustomCommandConf&c, const char* name, const int type) {
c.name = name; c.name = name;
c.cmd.type = (Command::Type)type; c.type = type;
c.cmd.name = c.name.c_str(); c.minArgs = 2;
c.cmd.minArgs = 2; c.maxArgs = 2;
c.cmd.maxArgs = 2; c.mode = Command::Write;
c.cmd.mode = Command::Write; }
void CustomCommandConf::convert(const CustomCommandConf&c, Command &cmd) {
cmd.name = c.name.c_str();
cmd.minArgs = c.minArgs;
cmd.maxArgs = c.maxArgs;
cmd.mode = c.mode;
cmd.type = (Command::Type)c.type;
} }
Conf::Conf(): Conf::Conf():
@ -129,6 +136,7 @@ void Conf::setGlobal(const ConfParser::Node* node)
const ConfParser::Node* clusterServerPool = nullptr; const ConfParser::Node* clusterServerPool = nullptr;
const ConfParser::Node* sentinelServerPool = nullptr; const ConfParser::Node* sentinelServerPool = nullptr;
const ConfParser::Node* dataCenter = nullptr; const ConfParser::Node* dataCenter = nullptr;
const ConfParser::Node* latencyMonitor = nullptr;
for (auto p = node; p; p = p->next) { for (auto p = node; p; p = p->next) {
if (setStr(mName, "Name", p)) { if (setStr(mName, "Name", p)) {
} else if (setStr(mBind, "Bind", p)) { } else if (setStr(mBind, "Bind", p)) {
@ -157,8 +165,7 @@ void Conf::setGlobal(const ConfParser::Node* node)
} else if (setInt(mLogSample[LogLevel::Warn], "LogWarnSample", p)) { } else if (setInt(mLogSample[LogLevel::Warn], "LogWarnSample", p)) {
} else if (setInt(mLogSample[LogLevel::Error], "LogErrorSample", p)) { } else if (setInt(mLogSample[LogLevel::Error], "LogErrorSample", p)) {
} else if (strcasecmp(p->key.c_str(), "LatencyMonitor") == 0) { } else if (strcasecmp(p->key.c_str(), "LatencyMonitor") == 0) {
mLatencyMonitors.push_back(LatencyMonitorConf{}); latencyMonitor = p;
setLatencyMonitor(mLatencyMonitors.back(), p);
} else if (strcasecmp(p->key.c_str(), "Authority") == 0) { } else if (strcasecmp(p->key.c_str(), "Authority") == 0) {
authority = p; authority = p;
} else if (strcasecmp(p->key.c_str(), "ClusterServerPool") == 0) { } else if (strcasecmp(p->key.c_str(), "ClusterServerPool") == 0) {
@ -190,6 +197,10 @@ void Conf::setGlobal(const ConfParser::Node* node)
if (dataCenter) { if (dataCenter) {
setDataCenter(dataCenter); setDataCenter(dataCenter);
} }
if (latencyMonitor) {
mLatencyMonitors.push_back(LatencyMonitorConf{});
setLatencyMonitor(mLatencyMonitors.back(), latencyMonitor);
}
} }
static void setKeyPrefix(std::vector<std::string>& dat, const std::string& v) static void setKeyPrefix(std::vector<std::string>& dat, const std::string& v)
@ -381,17 +392,19 @@ void Conf::setCustomCommand(const ConfParser::Node* node)
CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel); CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel);
auto s = p->sub; auto s = p->sub;
for (;s ; s = s->next) { for (;s ; s = s->next) {
if (setInt(cc.cmd.minArgs, "MinArgs", s, 2)) { if (setInt(cc.minArgs, "MinArgs", s, 2)) {
} else if (setInt(cc.cmd.maxArgs, "MaxArgs", s, 2, 9999)) { } else if (setInt(cc.maxArgs, "MaxArgs", s, 2, 9999)) {
} else if (setCommandMode(cc.cmd.mode, "Mode", s)) { } else if (setCommandMode(cc.mode, "Mode", s)) {
} else { } else {
Throw(UnknownKey, "%s:%d unknown key %s", s->file, s->line, s->key.c_str()); Throw(UnknownKey, "%s:%d unknown key %s", s->file, s->line, s->key.c_str());
} }
} }
if (cc.cmd.maxArgs < cc.cmd.minArgs) { if (cc.maxArgs < cc.minArgs) {
Throw(InvalidValue, "%s:%d must be MaxArgs >= MinArgs", p->file, p->line); Throw(InvalidValue, "%s:%d must be MaxArgs >= MinArgs", p->file, p->line);
} }
Command::addCustomCommand(&cc.cmd); Command cmd;
CustomCommandConf::convert(cc, cmd);
Command::addCustomCommand(cmd);
} }
} }

View File

@ -97,9 +97,13 @@ struct LatencyMonitorConf
struct CustomCommandConf struct CustomCommandConf
{ {
std::string name; std::string name;
Command cmd; int type;
int minArgs;
int maxArgs;
int mode;
static void init(CustomCommandConf &c, const char* name, const int type); static void init(CustomCommandConf &c, const char* name, const int type);
static void convert(const CustomCommandConf&c, Command &cmd);
}; };
class Conf class Conf