ServerConfig.cpp 4.4 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include "ServerConfig.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

G
groot 已提交
14
#include "config/ConfigMgr.h"
G
groot 已提交
15 16
#include "utils/CommonUtil.h"
#include "utils/ValidationUtil.h"
G
groot 已提交
17 18

namespace zilliz {
J
jinhai 已提交
19
namespace milvus {
G
groot 已提交
20 21
namespace server {

G
groot 已提交
22 23 24
constexpr uint64_t MB = 1024*1024;
constexpr uint64_t GB = MB*1024;

G
groot 已提交
25
ServerConfig&
G
groot 已提交
26 27
ServerConfig::GetInstance() {
    static ServerConfig config;
G
groot 已提交
28
    return config;
G
groot 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
}

ServerError
ServerConfig::LoadConfigFile(const std::string& config_filename) {
    std::string filename = config_filename;
    if(filename.empty()){
        std::cout << "ERROR: a config file is required" << std::endl;
        exit(1);//directly exit program if config file not specified
    }
    struct stat directoryStat;
    int statOK = stat(filename.c_str(), &directoryStat);
    if (statOK != 0) {
        std::cout << "ERROR: " << filename << " not found!" << std::endl;
        exit(1);//directly exit program if config file not found
    }

    try {
G
groot 已提交
46
        ConfigMgr* mgr = const_cast<ConfigMgr*>(ConfigMgr::GetInstance());
G
groot 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60
        ServerError err = mgr->LoadConfigFile(filename);
        if(err != 0) {
            std::cout << "Server failed to load config file" << std::endl;
            exit(1);//directly exit program if the config file is illegal
        }
    }
    catch (YAML::Exception& e) {
        std::cout << "Server failed to load config file: " << std::endl;
        return SERVER_UNEXPECTED_ERROR;
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
61 62 63
ServerError ServerConfig::ValidateConfig() const {
    //server config validation
    ConfigNode server_config = GetConfig(CONFIG_SERVER);
64
    uint32_t gpu_index = (uint32_t)server_config.GetInt32Value(CONFIG_GPU_INDEX, 0);
G
groot 已提交
65 66 67 68 69 70 71 72 73 74
    if(ValidationUtil::ValidateGpuIndex(gpu_index) != SERVER_SUCCESS) {
        std::cout << "Error: invalid gpu_index " << std::to_string(gpu_index) << std::endl;
        return SERVER_INVALID_ARGUMENT;
    }

    //db config validation
    unsigned long total_mem = 0, free_mem = 0;
    CommonUtil::GetSystemMemInfo(total_mem, free_mem);

    ConfigNode db_config = GetConfig(CONFIG_DB);
75
    uint64_t insert_buffer_size = (uint64_t)db_config.GetInt32Value(CONFIG_DB_INSERT_BUFFER_SIZE, 4);
G
groot 已提交
76 77 78 79 80 81 82 83
    insert_buffer_size *= GB;
    if(insert_buffer_size >= total_mem) {
        std::cout << "Error: insert_buffer_size execeed system memory" << std::endl;
        return SERVER_INVALID_ARGUMENT;
    }

    //cache config validation
    ConfigNode cache_config = GetConfig(CONFIG_CACHE);
84
    uint64_t cache_cap = (uint64_t)cache_config.GetInt64Value(CONFIG_CPU_CACHE_CAPACITY, 16);
G
groot 已提交
85 86 87 88 89 90 91 92
    cache_cap *= GB;
    if(cache_cap >= total_mem) {
        std::cout << "Error: cpu_cache_capacity execeed system memory" << std::endl;
        return SERVER_INVALID_ARGUMENT;
    } if(cache_cap > (double)total_mem*0.9) {
        std::cout << "Warnning: cpu_cache_capacity value is too aggressive" << std::endl;
    }

93
    if(insert_buffer_size + cache_cap >= total_mem) {
G
groot 已提交
94 95 96 97
        std::cout << "Error: sum of cpu_cache_capacity and insert_buffer_size execeed system memory" << std::endl;
        return SERVER_INVALID_ARGUMENT;
    }

98
    double free_percent = cache_config.GetDoubleValue(server::CACHE_FREE_PERCENT, 0.85);
G
groot 已提交
99 100 101 102 103 104 105 106
    if(free_percent < std::numeric_limits<double>::epsilon() || free_percent > 1.0) {
        std::cout << "Error: invalid cache_free_percent " << std::to_string(free_percent) << std::endl;
        return SERVER_INVALID_ARGUMENT;
    }

    return SERVER_SUCCESS;
}

G
groot 已提交
107 108
void
ServerConfig::PrintAll() const {
G
groot 已提交
109
    if(const ConfigMgr* mgr = ConfigMgr::GetInstance()) {
G
groot 已提交
110 111 112 113 114 115 116
        std::string str = mgr->DumpString();
//        SERVER_LOG_INFO << "\n" << str;
        std::cout << "\n" << str << std::endl;
    }
}

ConfigNode
G
groot 已提交
117
ServerConfig::GetConfig(const std::string& name) const {
G
groot 已提交
118
    const ConfigMgr* mgr = ConfigMgr::GetInstance();
G
groot 已提交
119
    const ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
120
    return root_node.GetChild(name);
G
groot 已提交
121 122 123
}

ConfigNode&
G
groot 已提交
124
ServerConfig::GetConfig(const std::string& name) {
G
groot 已提交
125
    ConfigMgr* mgr = ConfigMgr::GetInstance();
G
groot 已提交
126
    ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
127
    return root_node.GetChild(name);
G
groot 已提交
128 129 130 131 132 133
}


}
}
}