ServerConfig.cpp 5.1 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
////////////////////////////////////////////////////////////////////////////////
// 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>

#include "config/IConfigMgr.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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
}

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 {
        IConfigMgr* mgr = const_cast<IConfigMgr*>(IConfigMgr::GetInstance());
        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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
ServerError ServerConfig::ValidateConfig() const {
    //server config validation
    ConfigNode server_config = GetConfig(CONFIG_SERVER);
    uint32_t gpu_index = (uint32_t)server_config.GetInt32Value(CONFIG_GPU_INDEX);
    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);
    uint64_t insert_buffer_size = (uint64_t)db_config.GetInt32Value(CONFIG_DB_INSERT_BUFFER_SIZE);
    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;
    }

    uint64_t index_building_threshold = (uint64_t)db_config.GetInt32Value(CONFIG_DB_INDEX_TRIGGER_SIZE);
    index_building_threshold *= MB;

    size_t gpu_mem = 0;
    ValidationUtil::GetGpuMemory(gpu_index, gpu_mem);
    if(index_building_threshold >= gpu_mem/3) {
        std::cout << "Warnning: index_building_threshold is greater than 1/3 of gpu memory, "
            << "some index type(such as IVFLAT) may cause cuda::bad_alloc() error" << std::endl;
    } else if(index_building_threshold >= gpu_mem) {
        std::cout << "Error: index_building_threshold execeed gpu memory" << std::endl;
        return SERVER_INVALID_ARGUMENT;
    }

    //cache config validation
    ConfigNode cache_config = GetConfig(CONFIG_CACHE);
    uint64_t cache_cap = (uint64_t)cache_config.GetInt64Value(CONFIG_CPU_CACHE_CAPACITY);
    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;
    }

    if(insert_buffer_size + cache_cap >= total_mem) if(cache_cap >= total_mem) {
        std::cout << "Error: sum of cpu_cache_capacity and insert_buffer_size execeed system memory" << std::endl;
        return SERVER_INVALID_ARGUMENT;
    }

    double free_percent = cache_config.GetDoubleValue(server::CACHE_FREE_PERCENT);
    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 已提交
120 121 122 123 124 125 126 127 128 129
void
ServerConfig::PrintAll() const {
    if(const IConfigMgr* mgr = IConfigMgr::GetInstance()) {
        std::string str = mgr->DumpString();
//        SERVER_LOG_INFO << "\n" << str;
        std::cout << "\n" << str << std::endl;
    }
}

ConfigNode
G
groot 已提交
130
ServerConfig::GetConfig(const std::string& name) const {
G
groot 已提交
131 132
    const IConfigMgr* mgr = IConfigMgr::GetInstance();
    const ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
133
    return root_node.GetChild(name);
G
groot 已提交
134 135 136
}

ConfigNode&
G
groot 已提交
137
ServerConfig::GetConfig(const std::string& name) {
G
groot 已提交
138 139
    IConfigMgr* mgr = IConfigMgr::GetInstance();
    ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
140
    return root_node.GetChild(name);
G
groot 已提交
141 142 143 144 145 146
}


}
}
}