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
## Example:

View File

@ -95,7 +95,6 @@ Include try.conf
################################### COMMAND ####################################
## Custom command define, see command.conf
## must be defined before Latency monitor
#Include command.conf
################################### 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) {
Throw(InitFail, "too many custom commands(>%d)", MaxCustomCommands);
}
if (nullptr != find(p->name)) {
Throw(InitFail, "custom command %s is duplicated", p->name);
if (nullptr != find(c.name)) {
Throw(InitFail, "custom command %s is duplicated", c.name);
}
CmdPool[Sentinel] = *p;
CmdMap[p->name] = &CmdPool[Sentinel];
CmdPool[Sentinel] = c;
CmdMap[c.name] = &CmdPool[Sentinel];
Sentinel++;
}

View File

@ -251,7 +251,7 @@ public:
auto it = CmdMap.find(cmd);
return it == CmdMap.end() ? nullptr : it->second;
}
static void addCustomCommand(const Command *pc);
static void addCustomCommand(const Command& pc);
static int Sentinel;
private:
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) {
c.name = name;
c.cmd.type = (Command::Type)type;
c.cmd.name = c.name.c_str();
c.cmd.minArgs = 2;
c.cmd.maxArgs = 2;
c.cmd.mode = Command::Write;
c.type = type;
c.minArgs = 2;
c.maxArgs = 2;
c.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():
@ -129,6 +136,7 @@ void Conf::setGlobal(const ConfParser::Node* node)
const ConfParser::Node* clusterServerPool = nullptr;
const ConfParser::Node* sentinelServerPool = nullptr;
const ConfParser::Node* dataCenter = nullptr;
const ConfParser::Node* latencyMonitor = nullptr;
for (auto p = node; p; p = p->next) {
if (setStr(mName, "Name", 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::Error], "LogErrorSample", p)) {
} else if (strcasecmp(p->key.c_str(), "LatencyMonitor") == 0) {
mLatencyMonitors.push_back(LatencyMonitorConf{});
setLatencyMonitor(mLatencyMonitors.back(), p);
latencyMonitor = p;
} else if (strcasecmp(p->key.c_str(), "Authority") == 0) {
authority = p;
} else if (strcasecmp(p->key.c_str(), "ClusterServerPool") == 0) {
@ -190,6 +197,10 @@ void Conf::setGlobal(const ConfParser::Node* node)
if (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)
@ -381,17 +392,19 @@ void Conf::setCustomCommand(const ConfParser::Node* node)
CustomCommandConf::init(cc, p->key.c_str(), Command::Sentinel);
auto s = p->sub;
for (;s ; s = s->next) {
if (setInt(cc.cmd.minArgs, "MinArgs", s, 2)) {
} else if (setInt(cc.cmd.maxArgs, "MaxArgs", s, 2, 9999)) {
} else if (setCommandMode(cc.cmd.mode, "Mode", s)) {
if (setInt(cc.minArgs, "MinArgs", s, 2)) {
} else if (setInt(cc.maxArgs, "MaxArgs", s, 2, 9999)) {
} else if (setCommandMode(cc.mode, "Mode", s)) {
} else {
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);
}
Command::addCustomCommand(&cc.cmd);
Command cmd;
CustomCommandConf::convert(cc, cmd);
Command::addCustomCommand(cmd);
}
}

View File

@ -97,9 +97,13 @@ struct LatencyMonitorConf
struct CustomCommandConf
{
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 convert(const CustomCommandConf&c, Command &cmd);
};
class Conf