Config.h 11.2 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

G
groot 已提交
18 19
#pragma once

Y
yudong.cai 已提交
20
#include <mutex>
S
starlord 已提交
21
#include <string>
Y
yudong.cai 已提交
22
#include <unordered_map>
S
starlord 已提交
23
#include <vector>
S
starlord 已提交
24

G
groot 已提交
25
#include "config/ConfigNode.h"
S
starlord 已提交
26
#include "utils/Status.h"
G
groot 已提交
27

J
jinhai 已提交
28
namespace milvus {
G
groot 已提交
29 30
namespace server {

31
/* server config */
S
starlord 已提交
32 33 34 35 36 37 38 39 40
static const char* CONFIG_SERVER = "server_config";
static const char* CONFIG_SERVER_ADDRESS = "address";
static const char* CONFIG_SERVER_ADDRESS_DEFAULT = "127.0.0.1";
static const char* CONFIG_SERVER_PORT = "port";
static const char* CONFIG_SERVER_PORT_DEFAULT = "19530";
static const char* CONFIG_SERVER_DEPLOY_MODE = "deploy_mode";
static const char* CONFIG_SERVER_DEPLOY_MODE_DEFAULT = "single";
static const char* CONFIG_SERVER_TIME_ZONE = "time_zone";
static const char* CONFIG_SERVER_TIME_ZONE_DEFAULT = "UTC+8";
G
groot 已提交
41

42
/* db config */
S
starlord 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
static const char* CONFIG_DB = "db_config";
static const char* CONFIG_DB_PRIMARY_PATH = "primary_path";
static const char* CONFIG_DB_PRIMARY_PATH_DEFAULT = "/tmp/milvus";
static const char* CONFIG_DB_SECONDARY_PATH = "secondary_path";
static const char* CONFIG_DB_SECONDARY_PATH_DEFAULT = "";
static const char* CONFIG_DB_BACKEND_URL = "backend_url";
static const char* CONFIG_DB_BACKEND_URL_DEFAULT = "sqlite://:@:/";
static const char* CONFIG_DB_ARCHIVE_DISK_THRESHOLD = "archive_disk_threshold";
static const char* CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT = "0";
static const char* CONFIG_DB_ARCHIVE_DAYS_THRESHOLD = "archive_days_threshold";
static const char* CONFIG_DB_ARCHIVE_DAYS_THRESHOLD_DEFAULT = "0";
static const char* CONFIG_DB_INSERT_BUFFER_SIZE = "insert_buffer_size";
static const char* CONFIG_DB_INSERT_BUFFER_SIZE_DEFAULT = "4";
S
starlord 已提交
56
static const char* CONFIG_DB_PRELOAD_TABLE = "preload_table";
G
groot 已提交
57

58
/* cache config */
S
starlord 已提交
59 60 61 62
static const char* CONFIG_CACHE = "cache_config";
static const char* CONFIG_CACHE_CPU_CACHE_CAPACITY = "cpu_cache_capacity";
static const char* CONFIG_CACHE_CPU_CACHE_CAPACITY_DEFAULT = "16";
static const char* CONFIG_CACHE_GPU_CACHE_CAPACITY = "gpu_cache_capacity";
63
static const char* CONFIG_CACHE_GPU_CACHE_CAPACITY_DEFAULT = "4";
S
starlord 已提交
64 65 66 67 68 69
static const char* CONFIG_CACHE_CPU_CACHE_THRESHOLD = "cpu_mem_threshold";
static const char* CONFIG_CACHE_CPU_CACHE_THRESHOLD_DEFAULT = "0.85";
static const char* CONFIG_CACHE_GPU_CACHE_THRESHOLD = "gpu_mem_threshold";
static const char* CONFIG_CACHE_GPU_CACHE_THRESHOLD_DEFAULT = "0.85";
static const char* CONFIG_CACHE_CACHE_INSERT_DATA = "cache_insert_data";
static const char* CONFIG_CACHE_CACHE_INSERT_DATA_DEFAULT = "false";
70 71

/* metric config */
S
starlord 已提交
72 73 74 75 76 77 78 79
static const char* CONFIG_METRIC = "metric_config";
static const char* CONFIG_METRIC_ENABLE_MONITOR = "enable_monitor";
static const char* CONFIG_METRIC_ENABLE_MONITOR_DEFAULT = "false";
static const char* CONFIG_METRIC_COLLECTOR = "collector";
static const char* CONFIG_METRIC_COLLECTOR_DEFAULT = "prometheus";
static const char* CONFIG_METRIC_PROMETHEUS = "prometheus_config";
static const char* CONFIG_METRIC_PROMETHEUS_PORT = "port";
static const char* CONFIG_METRIC_PROMETHEUS_PORT_DEFAULT = "8080";
Y
yu yunfeng 已提交
80

81
/* engine config */
S
starlord 已提交
82 83 84 85 86
static const char* CONFIG_ENGINE = "engine_config";
static const char* CONFIG_ENGINE_USE_BLAS_THRESHOLD = "use_blas_threshold";
static const char* CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT = "20";
static const char* CONFIG_ENGINE_OMP_THREAD_NUM = "omp_thread_num";
static const char* CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT = "0";
87 88
static const char* CONFIG_ENGINE_GPU_SEARCH_THRESHOLD = "gpu_search_threshold";
static const char* CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT = "1000";
Y
yu yunfeng 已提交
89

90
/* resource config */
S
starlord 已提交
91 92 93
static const char* CONFIG_RESOURCE = "resource_config";
static const char* CONFIG_RESOURCE_MODE = "mode";
static const char* CONFIG_RESOURCE_MODE_DEFAULT = "simple";
94
static const char* CONFIG_RESOURCE_SEARCH_RESOURCES = "search_resources";
95 96
static const char* CONFIG_RESOURCE_SEARCH_RESOURCES_DELIMITER = ",";
static const char* CONFIG_RESOURCE_SEARCH_RESOURCES_DEFAULT = "cpu,gpu0";
97 98
static const char* CONFIG_RESOURCE_INDEX_BUILD_DEVICE = "index_build_device";
static const char* CONFIG_RESOURCE_INDEX_BUILD_DEVICE_DEFAULT = "gpu0";
W
wxyu 已提交
99

100 101
const int32_t CPU_DEVICE_ID = -1;

102
class Config {
G
groot 已提交
103
 public:
S
starlord 已提交
104 105 106 107 108 109 110 111 112 113
    static Config&
    GetInstance();
    Status
    LoadConfigFile(const std::string& filename);
    Status
    ValidateConfig();
    Status
    ResetDefaultConfig();
    void
    PrintAll();
G
groot 已提交
114

Y
yudong.cai 已提交
115
 private:
S
starlord 已提交
116 117 118 119 120 121 122 123
    ConfigNode&
    GetConfigNode(const std::string& name);
    Status
    GetConfigValueInMem(const std::string& parent_key, const std::string& child_key, std::string& value);
    void
    SetConfigValueInMem(const std::string& parent_key, const std::string& child_key, const std::string& value);
    void
    PrintConfigSection(const std::string& config_node_name);
Y
yudong.cai 已提交
124 125 126

    ///////////////////////////////////////////////////////////////////////////
    /* server config */
S
starlord 已提交
127 128 129 130 131 132 133 134
    Status
    CheckServerConfigAddress(const std::string& value);
    Status
    CheckServerConfigPort(const std::string& value);
    Status
    CheckServerConfigDeployMode(const std::string& value);
    Status
    CheckServerConfigTimeZone(const std::string& value);
Y
yudong.cai 已提交
135 136

    /* db config */
S
starlord 已提交
137 138 139 140 141 142 143 144 145 146 147 148
    Status
    CheckDBConfigPrimaryPath(const std::string& value);
    Status
    CheckDBConfigSecondaryPath(const std::string& value);
    Status
    CheckDBConfigBackendUrl(const std::string& value);
    Status
    CheckDBConfigArchiveDiskThreshold(const std::string& value);
    Status
    CheckDBConfigArchiveDaysThreshold(const std::string& value);
    Status
    CheckDBConfigInsertBufferSize(const std::string& value);
Y
yudong.cai 已提交
149 150

    /* metric config */
S
starlord 已提交
151 152 153 154 155 156
    Status
    CheckMetricConfigEnableMonitor(const std::string& value);
    Status
    CheckMetricConfigCollector(const std::string& value);
    Status
    CheckMetricConfigPrometheusPort(const std::string& value);
Y
yudong.cai 已提交
157 158

    /* cache config */
S
starlord 已提交
159 160 161 162 163 164 165 166 167 168
    Status
    CheckCacheConfigCpuCacheCapacity(const std::string& value);
    Status
    CheckCacheConfigCpuCacheThreshold(const std::string& value);
    Status
    CheckCacheConfigGpuCacheCapacity(const std::string& value);
    Status
    CheckCacheConfigGpuCacheThreshold(const std::string& value);
    Status
    CheckCacheConfigCacheInsertData(const std::string& value);
Y
yudong.cai 已提交
169 170

    /* engine config */
S
starlord 已提交
171 172 173 174
    Status
    CheckEngineConfigUseBlasThreshold(const std::string& value);
    Status
    CheckEngineConfigOmpThreadNum(const std::string& value);
W
wxyu 已提交
175
    Status
176
    CheckEngineConfigGpuSearchThreshold(const std::string& value);
Y
yudong.cai 已提交
177 178

    /* resource config */
S
starlord 已提交
179 180 181
    Status
    CheckResourceConfigMode(const std::string& value);
    Status
182 183 184
    CheckResourceConfigSearchResources(const std::vector<std::string>& value);
    Status
    CheckResourceConfigIndexBuildDevice(const std::string& value);
Y
yudong.cai 已提交
185

S
starlord 已提交
186
    std::string
S
starlord 已提交
187
    GetConfigStr(const std::string& parent_key, const std::string& child_key, const std::string& default_value = "");
188
    std::string
189 190
    GetConfigSequenceStr(const std::string& parent_key, const std::string& child_key, const std::string& delim = ",",
                         const std::string& default_value = "");
191

Y
yudong.cai 已提交
192
 public:
193
    /* server config */
S
starlord 已提交
194 195 196 197 198 199 200 201
    Status
    GetServerConfigAddress(std::string& value);
    Status
    GetServerConfigPort(std::string& value);
    Status
    GetServerConfigDeployMode(std::string& value);
    Status
    GetServerConfigTimeZone(std::string& value);
202 203

    /* db config */
S
starlord 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
    Status
    GetDBConfigPrimaryPath(std::string& value);
    Status
    GetDBConfigSecondaryPath(std::string& value);
    Status
    GetDBConfigBackendUrl(std::string& value);
    Status
    GetDBConfigArchiveDiskThreshold(int32_t& value);
    Status
    GetDBConfigArchiveDaysThreshold(int32_t& value);
    Status
    GetDBConfigInsertBufferSize(int32_t& value);
    Status
S
starlord 已提交
217
    GetDBConfigPreloadTable(std::string& value);
218 219

    /* metric config */
S
starlord 已提交
220 221 222 223 224 225
    Status
    GetMetricConfigEnableMonitor(bool& value);
    Status
    GetMetricConfigCollector(std::string& value);
    Status
    GetMetricConfigPrometheusPort(std::string& value);
226 227

    /* cache config */
S
starlord 已提交
228
    Status
W
wxyu 已提交
229
    GetCacheConfigCpuCacheCapacity(int64_t& value);
S
starlord 已提交
230 231 232
    Status
    GetCacheConfigCpuCacheThreshold(float& value);
    Status
W
wxyu 已提交
233
    GetCacheConfigGpuCacheCapacity(int64_t& value);
S
starlord 已提交
234 235 236 237
    Status
    GetCacheConfigGpuCacheThreshold(float& value);
    Status
    GetCacheConfigCacheInsertData(bool& value);
238 239

    /* engine config */
S
starlord 已提交
240 241 242 243
    Status
    GetEngineConfigUseBlasThreshold(int32_t& value);
    Status
    GetEngineConfigOmpThreadNum(int32_t& value);
W
wxyu 已提交
244
    Status
245
    GetEngineConfigGpuSearchThreshold(int32_t& value);
246 247

    /* resource config */
S
starlord 已提交
248 249 250
    Status
    GetResourceConfigMode(std::string& value);
    Status
251 252 253
    GetResourceConfigSearchResources(std::vector<std::string>& value);
    Status
    GetResourceConfigIndexBuildDevice(int32_t& value);
Y
yudong.cai 已提交
254

Y
yudong.cai 已提交
255 256
 public:
    /* server config */
S
starlord 已提交
257 258 259 260 261 262 263 264
    Status
    SetServerConfigAddress(const std::string& value);
    Status
    SetServerConfigPort(const std::string& value);
    Status
    SetServerConfigDeployMode(const std::string& value);
    Status
    SetServerConfigTimeZone(const std::string& value);
Y
yudong.cai 已提交
265 266

    /* db config */
S
starlord 已提交
267 268 269 270 271 272 273 274 275 276 277 278
    Status
    SetDBConfigPrimaryPath(const std::string& value);
    Status
    SetDBConfigSecondaryPath(const std::string& value);
    Status
    SetDBConfigBackendUrl(const std::string& value);
    Status
    SetDBConfigArchiveDiskThreshold(const std::string& value);
    Status
    SetDBConfigArchiveDaysThreshold(const std::string& value);
    Status
    SetDBConfigInsertBufferSize(const std::string& value);
Y
yudong.cai 已提交
279 280

    /* metric config */
S
starlord 已提交
281 282 283 284 285 286
    Status
    SetMetricConfigEnableMonitor(const std::string& value);
    Status
    SetMetricConfigCollector(const std::string& value);
    Status
    SetMetricConfigPrometheusPort(const std::string& value);
Y
yudong.cai 已提交
287 288

    /* cache config */
S
starlord 已提交
289 290 291 292 293 294 295 296 297 298
    Status
    SetCacheConfigCpuCacheCapacity(const std::string& value);
    Status
    SetCacheConfigCpuCacheThreshold(const std::string& value);
    Status
    SetCacheConfigGpuCacheCapacity(const std::string& value);
    Status
    SetCacheConfigGpuCacheThreshold(const std::string& value);
    Status
    SetCacheConfigCacheInsertData(const std::string& value);
Y
yudong.cai 已提交
299 300

    /* engine config */
S
starlord 已提交
301 302 303 304
    Status
    SetEngineConfigUseBlasThreshold(const std::string& value);
    Status
    SetEngineConfigOmpThreadNum(const std::string& value);
W
wxyu 已提交
305
    Status
306
    SetEngineConfigGpuSearchThreshold(const std::string& value);
Y
yudong.cai 已提交
307 308

    /* resource config */
S
starlord 已提交
309 310
    Status
    SetResourceConfigMode(const std::string& value);
311
    Status
312 313
    SetResourceConfigSearchResources(const std::string& value);
    Status
314
    SetResourceConfigIndexBuildDevice(const std::string& value);
Y
yudong.cai 已提交
315

Y
yudong.cai 已提交
316 317 318
 private:
    std::unordered_map<std::string, std::unordered_map<std::string, std::string>> config_map_;
    std::mutex mutex_;
G
groot 已提交
319 320
};

S
starlord 已提交
321 322
}  // namespace server
}  // namespace milvus