Commit 6937e08d authored by 蔡宇东's avatar 蔡宇东
Browse files

MS-574 add config check


Former-commit-id: ddd81268b95977be22e72eaf7bfa41e65a527ffe
parent 3135201d
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ Please mark all change in change log and use the ticket from JIRA.
- MS-562 - Add JobMgr and TaskCreator in Scheduler
- MS-566 - Refactor cmake
- MS-555 - Remove old scheduler
- MS-574 - Milvus configuration refactor
- MS-578 - Make sure milvus5.0 don't crack 0.3.1 data

## New Feature
+353 −618

File changed.

Preview size limit exceeded, changes collapsed.

+63 −35
Original line number Diff line number Diff line
@@ -96,63 +96,91 @@ static const char* CONFIG_RESOURCE_POOL = "pool";
class Config {
 public:
    static Config& GetInstance();

    Status LoadConfigFile(const std::string& filename);
    Status ValidateConfig();
    void PrintAll() const;
    void PrintAll();

 private:
    ConfigNode& GetConfigNode(const std::string& name);

    Status CheckServerConfig();
    Status CheckDBConfig();
    Status CheckMetricConfig();
    Status CheckCacheConfig();
    Status CheckEngineConfig();
    Status CheckResourceConfig();

    Status GetConfigValueInMem(const std::string& parent_key,
                               const std::string& child_key,
                               std::string& value);

    Status SetConfigValueInMem(const std::string& parent_key,
    void   SetConfigValueInMem(const std::string& parent_key,
                               const std::string& child_key,
                               std::string& value);

 private:
    void   PrintConfigSection(const std::string& config_node_name);

    ///////////////////////////////////////////////////////////////////////////
    /* server config */
    Status CheckServerConfigAddress(std::string& value);
    Status CheckServerConfigPort(std::string& value);
    Status CheckServerConfigMode(std::string& value);
    Status CheckServerConfigTimeZone(std::string& value);

    /* db config */
    Status CheckDBConfigPath(const std::string& value);
    Status CheckDBConfigSlavePath(const std::string& value);
    Status CheckDBConfigBackendUrl(const std::string& value);
    Status CheckDBConfigArchiveDiskThreshold(const std::string& value);
    Status CheckDBConfigArchiveDaysThreshold(const std::string& value);
    Status CheckDBConfigBufferSize(const std::string& value);
    Status CheckDBConfigBuildIndexGPU(const std::string& value);

    /* metric config */
    Status CheckMetricConfigAutoBootup(const std::string& value);
    Status CheckMetricConfigPrometheusPort(const std::string& value);

    /* cache config */
    Status CheckCacheConfigCpuMemCapacity(const std::string& value);
    Status CheckCacheConfigCpuMemThreshold(const std::string& value);
    Status CheckCacheConfigGpuMemCapacity(const std::string& value);
    Status CheckCacheConfigGpuMemThreshold(const std::string& value);
    Status CheckCacheConfigCacheInsertData(const std::string& value);

    /* engine config */
    Status CheckEngineConfigBlasThreshold(const std::string& value);
    Status CheckEngineConfigOmpThreadNum(const std::string& value);

    /* resource config */
    Status CheckResourceConfigMode(const std::string& value);
    Status CheckResourceConfigPool(const std::vector<std::string>& value);

    ///////////////////////////////////////////////////////////////////////////
    /* server config */
    Status GetServerConfigStrAddress(std::string& value);
    Status GetServerConfigStrPort(std::string& value);
    Status GetServerConfigStrMode(std::string& value);
    Status GetServerConfigStrTimeZone(std::string& value);
    std::string GetServerConfigStrAddress();
    std::string GetServerConfigStrPort();
    std::string GetServerConfigStrMode();
    std::string GetServerConfigStrTimeZone();

    /* db config */
    Status GetDBConfigStrPath(std::string& value);
    Status GetDBConfigStrSlavePath(std::string& value);
    Status GetDBConfigStrBackendUrl(std::string& value);
    Status GetDBConfigStrArchiveDiskThreshold(std::string& value);
    Status GetDBConfigStrArchiveDaysThreshold(std::string& value);
    Status GetDBConfigStrBufferSize(std::string& value);
    Status GetDBConfigStrBuildIndexGPU(std::string& value);
    std::string GetDBConfigStrPath();
    std::string GetDBConfigStrSlavePath();
    std::string GetDBConfigStrBackendUrl();
    std::string GetDBConfigStrArchiveDiskThreshold();
    std::string GetDBConfigStrArchiveDaysThreshold();
    std::string GetDBConfigStrBufferSize();
    std::string GetDBConfigStrBuildIndexGPU();

    /* metric config */
    Status GetMetricConfigStrAutoBootup(std::string& value);
    Status GetMetricConfigStrCollector(std::string& value);
    Status GetMetricConfigStrPrometheusPort(std::string& value);
    std::string GetMetricConfigStrAutoBootup();
    std::string GetMetricConfigStrCollector();
    std::string GetMetricConfigStrPrometheusPort();

    /* cache config */
    Status GetCacheConfigStrCpuMemCapacity(std::string& value);
    Status GetCacheConfigStrCpuMemThreshold(std::string& value);
    Status GetCacheConfigStrGpuMemCapacity(std::string& value);
    Status GetCacheConfigStrGpuMemThreshold(std::string& value);
    Status GetCacheConfigStrCacheInsertData(std::string& value);
    std::string GetCacheConfigStrCpuMemCapacity();
    std::string GetCacheConfigStrCpuMemThreshold();
    std::string GetCacheConfigStrGpuMemCapacity();
    std::string GetCacheConfigStrGpuMemThreshold();
    std::string GetCacheConfigStrCacheInsertData();

    /* engine config */
    Status GetEngineConfigStrBlasThreshold(std::string& value);
    Status GetEngineConfigStrOmpThreadNum(std::string& value);
    std::string GetEngineConfigStrBlasThreshold();
    std::string GetEngineConfigStrOmpThreadNum();

    /* resource config */
    Status GetResourceConfigStrMode(std::string& value);
    std::string GetResourceConfigStrMode();

 public:
    /* server config */
+3 −5
Original line number Diff line number Diff line
@@ -235,14 +235,12 @@ Server::Stop() {

ErrorCode
Server::LoadConfig() {
    Config& server_config = Config::GetInstance();
    server_config.LoadConfigFile(config_filename_);
    auto status = server_config.ValidateConfig();
    if (!status.ok()) {
    Config& config = Config::GetInstance();
    Status s = config.LoadConfigFile(config_filename_);
    if (!s.ok()) {
        std::cerr << "Failed to load config file: " << config_filename_ << std::endl;
        exit(0);
    }

    return SERVER_SUCCESS;
}

+21 −19
Original line number Diff line number Diff line
@@ -206,38 +206,40 @@ ValidationUtil::ValidateIpAddress(const std::string &ip_address) {
}

Status
ValidationUtil::ValidateStringIsNumber(const std::string &string) {
    if (!string.empty() && std::all_of(string.begin(), string.end(), ::isdigit)) {
        return Status::OK();
ValidationUtil::ValidateStringIsNumber(const std::string &str) {
    if (str.empty() || !std::all_of(str.begin(), str.end(), ::isdigit)) {
        return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
    }
    else {
        return Status(SERVER_INVALID_ARGUMENT, "Not a number");
    try {
        int32_t value = std::stoi(str);
    } catch(...) {
        return Status(SERVER_INVALID_ARGUMENT, "Invalid number");
    }
    return Status::OK();
}

Status
ValidationUtil::ValidateStringIsBool(std::string &str) {
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    if (str == "true" || str == "on" || str == "yes" || str == "1" ||
        str == "false" || str == "off" || str == "no" || str == "0" ||
        str.empty()) {
ValidationUtil::ValidateStringIsBool(const std::string &str) {
    std::string s = str;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    if (s == "true" || s == "on" || s == "yes" || s == "1" ||
        s == "false" || s == "off" || s == "no" || s == "0" ||
        s.empty()) {
        return Status::OK();
    }
    else {
        return Status(SERVER_INVALID_ARGUMENT, "Not a boolean: " + str);
        return Status(SERVER_INVALID_ARGUMENT, "Invalid boolean: " + str);
    }
}

Status
ValidationUtil::ValidateStringIsDouble(const std::string &str, double &val) {
    char *end = nullptr;
    val = std::strtod(str.c_str(), &end);
    if (end != str.c_str() && *end == '\0' && val != HUGE_VAL) {
        return Status::OK();
    }
    else {
        return Status(SERVER_INVALID_ARGUMENT, "Not a double value: " + str);
ValidationUtil::ValidateStringIsFloat(const std::string &str) {
    try {
        float val = std::stof(str);
    } catch(...) {
        return Status(SERVER_INVALID_ARGUMENT, "Invalid float: " + str);
    }
    return Status::OK();
}

Status
Loading