diff --git a/conf/command.conf b/conf/command.conf index ac6ac01..9176913 100644 --- a/conf/command.conf +++ b/conf/command.conf @@ -7,7 +7,6 @@ ## }... ## } -## This section must be defined before Latency monitor to support all commands ## Currently support maximum 16 custom commands ## Example: diff --git a/conf/predixy.conf b/conf/predixy.conf index a9f3ae1..522b951 100644 --- a/conf/predixy.conf +++ b/conf/predixy.conf @@ -95,7 +95,6 @@ Include try.conf ################################### COMMAND #################################### ## Custom command define, see command.conf -## must be defined before Latency monitor #Include command.conf ################################### LATENCY #################################### diff --git a/src/Command.cpp b/src/Command.cpp index 7f3b9f5..0e3fc0b 100644 --- a/src/Command.cpp +++ b/src/Command.cpp @@ -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++; } diff --git a/src/Command.h b/src/Command.h index 77ad1b9..b4d346f 100644 --- a/src/Command.h +++ b/src/Command.h @@ -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; diff --git a/src/Conf.cpp b/src/Conf.cpp index 2bb8bd2..3ed1941 100644 --- a/src/Conf.cpp +++ b/src/Conf.cpp @@ -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& 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); } } diff --git a/src/Conf.h b/src/Conf.h index 1b9e002..4e94659 100644 --- a/src/Conf.h +++ b/src/Conf.h @@ -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