Config.cpp 38.4 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
#include <sys/stat.h>
Y
yudong.cai 已提交
19
#include <algorithm>
S
starlord 已提交
20
#include <iostream>
21
#include <regex>
S
starlord 已提交
22
#include <string>
S
starlord 已提交
23
#include <vector>
G
groot 已提交
24

25 26
#include "config/YamlConfigMgr.h"
#include "server/Config.h"
27 28
#include "utils/CommonUtil.h"
#include "utils/ValidationUtil.h"
G
groot 已提交
29

J
jinhai 已提交
30
namespace milvus {
G
groot 已提交
31 32
namespace server {

Y
yudong.cai 已提交
33
constexpr uint64_t GB = 1UL << 30;
34

S
starlord 已提交
35
Config&
36 37 38
Config::GetInstance() {
    static Config config_inst;
    return config_inst;
G
groot 已提交
39 40
}

S
starlord 已提交
41
Status
S
starlord 已提交
42
Config::LoadConfigFile(const std::string& filename) {
43
    if (filename.empty()) {
44
        return Status(SERVER_UNEXPECTED_ERROR, "No specified config file");
G
groot 已提交
45
    }
46 47 48 49 50

    struct stat file_stat;
    if (stat(filename.c_str(), &file_stat) != 0) {
        std::string str = "Config file not exist: " + filename;
        return Status(SERVER_FILE_NOT_FOUND, str);
G
groot 已提交
51 52 53
    }

    try {
54 55 56 57
        ConfigMgr* mgr = YamlConfigMgr::GetInstance();
        Status s = mgr->LoadConfigFile(filename);
        if (!s.ok()) {
            return s;
G
groot 已提交
58
        }
S
starlord 已提交
59
    } catch (YAML::Exception& e) {
60 61
        std::string str = "Exception occurs when loading config file: " + filename;
        return Status(SERVER_UNEXPECTED_ERROR, str);
G
groot 已提交
62 63
    }

S
starlord 已提交
64
    return Status::OK();
G
groot 已提交
65 66
}

Y
yudong.cai 已提交
67 68 69 70 71 72 73
Status
Config::ValidateConfig() {
    Status s;

    /* server config */
    std::string server_addr;
    s = GetServerConfigAddress(server_addr);
S
starlord 已提交
74 75 76
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
77 78 79

    std::string server_port;
    s = GetServerConfigPort(server_port);
S
starlord 已提交
80 81 82
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
83 84

    std::string server_mode;
Y
yudong.cai 已提交
85
    s = GetServerConfigDeployMode(server_mode);
S
starlord 已提交
86 87 88
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
89 90 91

    std::string server_time_zone;
    s = GetServerConfigTimeZone(server_time_zone);
S
starlord 已提交
92 93 94
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
95 96 97 98

    /* db config */
    std::string db_primary_path;
    s = GetDBConfigPrimaryPath(db_primary_path);
S
starlord 已提交
99 100 101
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
102 103 104

    std::string db_secondary_path;
    s = GetDBConfigSecondaryPath(db_secondary_path);
S
starlord 已提交
105 106 107
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
108 109 110

    std::string db_backend_url;
    s = GetDBConfigBackendUrl(db_backend_url);
S
starlord 已提交
111 112 113
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
114 115 116

    int32_t db_archive_disk_threshold;
    s = GetDBConfigArchiveDiskThreshold(db_archive_disk_threshold);
S
starlord 已提交
117 118 119
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
120 121 122

    int32_t db_archive_days_threshold;
    s = GetDBConfigArchiveDaysThreshold(db_archive_days_threshold);
S
starlord 已提交
123 124 125
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
126 127 128

    int32_t db_insert_buffer_size;
    s = GetDBConfigInsertBufferSize(db_insert_buffer_size);
S
starlord 已提交
129 130 131
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
132 133 134 135

    /* metric config */
    bool metric_enable_monitor;
    s = GetMetricConfigEnableMonitor(metric_enable_monitor);
S
starlord 已提交
136 137 138
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
139 140 141

    std::string metric_collector;
    s = GetMetricConfigCollector(metric_collector);
S
starlord 已提交
142 143 144
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
145 146 147

    std::string metric_prometheus_port;
    s = GetMetricConfigPrometheusPort(metric_prometheus_port);
S
starlord 已提交
148 149 150
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
151 152

    /* cache config */
W
wxyu 已提交
153
    int64_t cache_cpu_cache_capacity;
Y
yudong.cai 已提交
154
    s = GetCacheConfigCpuCacheCapacity(cache_cpu_cache_capacity);
S
starlord 已提交
155 156 157
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
158

Y
yudong.cai 已提交
159 160
    float cache_cpu_cache_threshold;
    s = GetCacheConfigCpuCacheThreshold(cache_cpu_cache_threshold);
S
starlord 已提交
161 162 163
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
164

W
wxyu 已提交
165
    int64_t cache_gpu_cache_capacity;
Y
yudong.cai 已提交
166
    s = GetCacheConfigGpuCacheCapacity(cache_gpu_cache_capacity);
S
starlord 已提交
167 168 169
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
170

Y
yudong.cai 已提交
171 172
    float cache_gpu_cache_threshold;
    s = GetCacheConfigGpuCacheThreshold(cache_gpu_cache_threshold);
S
starlord 已提交
173 174 175
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
176 177 178

    bool cache_insert_data;
    s = GetCacheConfigCacheInsertData(cache_insert_data);
S
starlord 已提交
179 180 181
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
182 183

    /* engine config */
Y
yudong.cai 已提交
184 185
    int32_t engine_use_blas_threshold;
    s = GetEngineConfigUseBlasThreshold(engine_use_blas_threshold);
S
starlord 已提交
186 187 188
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
189 190 191

    int32_t engine_omp_thread_num;
    s = GetEngineConfigOmpThreadNum(engine_omp_thread_num);
S
starlord 已提交
192 193 194
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
195

196 197
    int32_t engine_gpu_search_threshold;
    s = GetEngineConfigGpuSearchThreshold(engine_gpu_search_threshold);
W
wxyu 已提交
198 199 200 201
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
202 203 204
    /* resource config */
    std::string resource_mode;
    s = GetResourceConfigMode(resource_mode);
S
starlord 已提交
205 206 207
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
208

209 210 211 212 213 214 215 216
    std::vector<std::string> search_resources;
    s = GetResourceConfigSearchResources(search_resources);
    if (!s.ok()) {
        return s;
    }

    int32_t resource_index_build_device;
    s = GetResourceConfigIndexBuildDevice(resource_index_build_device);
S
starlord 已提交
217 218 219
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
220 221 222 223

    return Status::OK();
}

Y
yudong.cai 已提交
224 225 226 227 228 229
Status
Config::ResetDefaultConfig() {
    Status s;

    /* server config */
    s = SetServerConfigAddress(CONFIG_SERVER_ADDRESS_DEFAULT);
S
starlord 已提交
230 231 232
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
233 234

    s = SetServerConfigPort(CONFIG_SERVER_PORT_DEFAULT);
S
starlord 已提交
235 236 237
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
238 239

    s = SetServerConfigDeployMode(CONFIG_SERVER_DEPLOY_MODE_DEFAULT);
S
starlord 已提交
240 241 242
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
243 244

    s = SetServerConfigTimeZone(CONFIG_SERVER_TIME_ZONE_DEFAULT);
S
starlord 已提交
245 246 247
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
248 249 250

    /* db config */
    s = SetDBConfigPrimaryPath(CONFIG_DB_PRIMARY_PATH_DEFAULT);
S
starlord 已提交
251 252 253
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
254 255

    s = SetDBConfigSecondaryPath(CONFIG_DB_SECONDARY_PATH_DEFAULT);
S
starlord 已提交
256 257 258
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
259 260

    s = SetDBConfigBackendUrl(CONFIG_DB_BACKEND_URL_DEFAULT);
S
starlord 已提交
261 262 263
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
264 265

    s = SetDBConfigArchiveDiskThreshold(CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT);
S
starlord 已提交
266 267 268
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
269 270

    s = SetDBConfigArchiveDaysThreshold(CONFIG_DB_ARCHIVE_DAYS_THRESHOLD_DEFAULT);
S
starlord 已提交
271 272 273
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
274 275

    s = SetDBConfigInsertBufferSize(CONFIG_DB_INSERT_BUFFER_SIZE_DEFAULT);
S
starlord 已提交
276 277 278
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
279 280 281

    /* metric config */
    s = SetMetricConfigEnableMonitor(CONFIG_METRIC_ENABLE_MONITOR_DEFAULT);
S
starlord 已提交
282 283 284
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
285 286

    s = SetMetricConfigCollector(CONFIG_METRIC_COLLECTOR_DEFAULT);
S
starlord 已提交
287 288 289
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
290 291

    s = SetMetricConfigPrometheusPort(CONFIG_METRIC_PROMETHEUS_PORT_DEFAULT);
S
starlord 已提交
292 293 294
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
295 296

    /* cache config */
Y
yudong.cai 已提交
297
    s = SetCacheConfigCpuCacheCapacity(CONFIG_CACHE_CPU_CACHE_CAPACITY_DEFAULT);
S
starlord 已提交
298 299 300
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
301

Y
yudong.cai 已提交
302
    s = SetCacheConfigCpuCacheThreshold(CONFIG_CACHE_CPU_CACHE_THRESHOLD_DEFAULT);
S
starlord 已提交
303 304 305
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
306

Y
yudong.cai 已提交
307
    s = SetCacheConfigGpuCacheCapacity(CONFIG_CACHE_GPU_CACHE_CAPACITY_DEFAULT);
S
starlord 已提交
308 309 310
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
311

Y
yudong.cai 已提交
312
    s = SetCacheConfigGpuCacheThreshold(CONFIG_CACHE_GPU_CACHE_THRESHOLD_DEFAULT);
S
starlord 已提交
313 314 315
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
316 317

    s = SetCacheConfigCacheInsertData(CONFIG_CACHE_CACHE_INSERT_DATA_DEFAULT);
S
starlord 已提交
318 319 320
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
321 322

    /* engine config */
Y
yudong.cai 已提交
323
    s = SetEngineConfigUseBlasThreshold(CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT);
S
starlord 已提交
324 325 326
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
327 328

    s = SetEngineConfigOmpThreadNum(CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT);
S
starlord 已提交
329 330 331
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
332

333
    s = SetEngineConfigGpuSearchThreshold(CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT);
W
wxyu 已提交
334 335 336 337
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
338 339
    /* resource config */
    s = SetResourceConfigMode(CONFIG_RESOURCE_MODE_DEFAULT);
S
starlord 已提交
340 341 342
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
343

344 345 346 347 348
    s = SetResourceConfigIndexBuildDevice(CONFIG_RESOURCE_INDEX_BUILD_DEVICE_DEFAULT);
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
349 350 351
    return Status::OK();
}

Y
yudong.cai 已提交
352
void
S
starlord 已提交
353
Config::PrintConfigSection(const std::string& config_node_name) {
Y
yudong.cai 已提交
354 355 356
    std::cout << std::endl;
    std::cout << config_node_name << ":" << std::endl;
    if (config_map_.find(config_node_name) != config_map_.end()) {
S
starlord 已提交
357
        for (auto item : config_map_[config_node_name]) {
Y
yudong.cai 已提交
358 359
            std::cout << item.first << ": " << item.second << std::endl;
        }
Z
zhiru 已提交
360 361 362
    }
}

Y
yudong.cai 已提交
363 364 365 366 367 368 369 370 371 372 373
void
Config::PrintAll() {
    PrintConfigSection(CONFIG_SERVER);
    PrintConfigSection(CONFIG_DB);
    PrintConfigSection(CONFIG_CACHE);
    PrintConfigSection(CONFIG_METRIC);
    PrintConfigSection(CONFIG_ENGINE);
    PrintConfigSection(CONFIG_RESOURCE);
}

////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
374
Status
S
starlord 已提交
375
Config::CheckServerConfigAddress(const std::string& value) {
Y
yudong.cai 已提交
376
    if (!ValidationUtil::ValidateIpAddress(value).ok()) {
S
starlord 已提交
377 378
        std::string msg =
            "Invalid server IP address: " + value + ". Possible reason: server_config.address is invalid.";
379
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
380
    }
Y
yudong.cai 已提交
381 382
    return Status::OK();
}
Z
zhiru 已提交
383

Y
yudong.cai 已提交
384
Status
S
starlord 已提交
385
Config::CheckServerConfigPort(const std::string& value) {
Y
yudong.cai 已提交
386
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
S
starlord 已提交
387
        std::string msg = "Invalid server port: " + value + ". Possible reason: server_config.port is not a number.";
388
        return Status(SERVER_INVALID_ARGUMENT, msg);
389
    } else {
Y
yudong.cai 已提交
390
        int32_t port = std::stoi(value);
391
        if (!(port > 1024 && port < 65535)) {
S
starlord 已提交
392 393
            std::string msg = "Invalid server port: " + value +
                              ". Possible reason: server_config.port is not in range [1025, 65534].";
394
            return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
395 396
        }
    }
Y
yudong.cai 已提交
397 398
    return Status::OK();
}
Z
zhiru 已提交
399

Y
yudong.cai 已提交
400
Status
S
starlord 已提交
401 402
Config::CheckServerConfigDeployMode(const std::string& value) {
    if (value != "single" && value != "cluster_readonly" && value != "cluster_writable") {
Y
yudong.cai 已提交
403
        return Status(SERVER_INVALID_ARGUMENT,
S
starlord 已提交
404
                      "server_config.deploy_mode is not one of "
405
                      "single, cluster_readonly, and cluster_writable.");
406
    }
Y
yudong.cai 已提交
407 408
    return Status::OK();
}
409

Y
yudong.cai 已提交
410
Status
S
starlord 已提交
411
Config::CheckServerConfigTimeZone(const std::string& value) {
Y
yudong.cai 已提交
412
    if (value.length() <= 3) {
S
starlord 已提交
413
        return Status(SERVER_INVALID_ARGUMENT, "Invalid server_config.time_zone: " + value);
414
    } else {
Y
yudong.cai 已提交
415
        if (value.substr(0, 3) != "UTC") {
S
starlord 已提交
416
            return Status(SERVER_INVALID_ARGUMENT, "Invalid server_config.time_zone: " + value);
417 418
        } else {
            try {
Y
yudong.cai 已提交
419
                stoi(value.substr(3));
420
            } catch (...) {
S
starlord 已提交
421
                return Status(SERVER_INVALID_ARGUMENT, "Invalid server_config.time_zone: " + value);
422
            }
423 424
        }
    }
Y
yudong.cai 已提交
425
    return Status::OK();
Z
zhiru 已提交
426 427
}

S
starlord 已提交
428
Status
S
starlord 已提交
429
Config::CheckDBConfigPrimaryPath(const std::string& value) {
Y
yudong.cai 已提交
430
    if (value.empty()) {
S
starlord 已提交
431
        return Status(SERVER_INVALID_ARGUMENT, "db_config.db_path is empty.");
Z
zhiru 已提交
432
    }
Y
yudong.cai 已提交
433 434
    return Status::OK();
}
Z
zhiru 已提交
435

Y
yudong.cai 已提交
436
Status
S
starlord 已提交
437
Config::CheckDBConfigSecondaryPath(const std::string& value) {
Y
yudong.cai 已提交
438 439 440
    return Status::OK();
}

Y
yudong.cai 已提交
441
Status
S
starlord 已提交
442
Config::CheckDBConfigBackendUrl(const std::string& value) {
Y
yudong.cai 已提交
443
    if (!ValidationUtil::ValidateDbURI(value).ok()) {
444
        std::string msg =
S
starlord 已提交
445
            "Invalid backend url: " + value + ". Possible reason: db_config.db_backend_url is invalid. " +
446
            "The correct format should be like sqlite://:@:/ or mysql://root:123456@127.0.0.1:3306/milvus.";
447
        return Status(SERVER_INVALID_ARGUMENT, "invalid db_backend_url: " + value);
Z
zhiru 已提交
448
    }
Y
yudong.cai 已提交
449 450
    return Status::OK();
}
Z
zhiru 已提交
451

Y
yudong.cai 已提交
452
Status
S
starlord 已提交
453
Config::CheckDBConfigArchiveDiskThreshold(const std::string& value) {
Y
yudong.cai 已提交
454
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
455
        std::string msg = "Invalid archive disk threshold: " + value +
S
starlord 已提交
456
                          ". Possible reason: db_config.archive_disk_threshold is invalid.";
457
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
458
    }
Y
yudong.cai 已提交
459 460
    return Status::OK();
}
Z
zhiru 已提交
461

Y
yudong.cai 已提交
462
Status
S
starlord 已提交
463
Config::CheckDBConfigArchiveDaysThreshold(const std::string& value) {
Y
yudong.cai 已提交
464
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
465
        std::string msg = "Invalid archive days threshold: " + value +
466
                          ". Possible reason: db_config.archive_days_threshold is invalid.";
467
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
468
    }
Y
yudong.cai 已提交
469 470
    return Status::OK();
}
Z
zhiru 已提交
471

Y
yudong.cai 已提交
472
Status
S
starlord 已提交
473
Config::CheckDBConfigInsertBufferSize(const std::string& value) {
Y
yudong.cai 已提交
474
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
475
        std::string msg = "Invalid insert buffer size: " + value +
S
starlord 已提交
476
                          ". Possible reason: db_config.insert_buffer_size is not a positive integer.";
477
        return Status(SERVER_INVALID_ARGUMENT, msg);
478
    } else {
Y
yudong.cai 已提交
479
        int64_t buffer_size = std::stoi(value) * GB;
480 481
        if (buffer_size <= 0) {
            std::string msg = "Invalid insert buffer size: " + value +
S
starlord 已提交
482
                              ". Possible reason: db_config.insert_buffer_size is not a positive integer.";
483 484 485
            return Status(SERVER_INVALID_ARGUMENT, msg);
        }

S
starlord 已提交
486
        uint64_t total_mem = 0, free_mem = 0;
Z
zhiru 已提交
487
        CommonUtil::GetSystemMemInfo(total_mem, free_mem);
Y
yudong.cai 已提交
488
        if (buffer_size >= total_mem) {
S
starlord 已提交
489 490
            std::string msg = "Invalid insert buffer size: " + value +
                              ". Possible reason: db_config.insert_buffer_size exceeds system memory.";
491
            return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
492 493
        }
    }
Y
yudong.cai 已提交
494 495
    return Status::OK();
}
Z
zhiru 已提交
496

S
starlord 已提交
497
Status
S
starlord 已提交
498
Config::CheckMetricConfigEnableMonitor(const std::string& value) {
Y
yudong.cai 已提交
499
    if (!ValidationUtil::ValidateStringIsBool(value).ok()) {
500
        std::string msg =
S
starlord 已提交
501
            "Invalid metric config: " + value + ". Possible reason: metric_config.enable_monitor is not a boolean.";
502
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
503
    }
Y
yudong.cai 已提交
504 505
    return Status::OK();
}
Z
zhiru 已提交
506

Y
yudong.cai 已提交
507
Status
S
starlord 已提交
508
Config::CheckMetricConfigCollector(const std::string& value) {
Y
yudong.cai 已提交
509
    if (value != "prometheus") {
S
starlord 已提交
510 511
        std::string msg =
            "Invalid metric collector: " + value + ". Possible reason: metric_config.collector is invalid.";
512
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
513 514 515 516
    }
    return Status::OK();
}

Y
yudong.cai 已提交
517
Status
S
starlord 已提交
518
Config::CheckMetricConfigPrometheusPort(const std::string& value) {
Y
yudong.cai 已提交
519
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
S
starlord 已提交
520 521
        std::string msg = "Invalid metric port: " + value +
                          ". Possible reason: metric_config.prometheus_config.port is not in range [1025, 65534].";
Y
yudong.cai 已提交
522
        return Status(SERVER_INVALID_ARGUMENT, "Invalid metric config prometheus_port: " + value);
Z
zhiru 已提交
523
    }
Y
yudong.cai 已提交
524
    return Status::OK();
Z
zhiru 已提交
525 526
}

S
starlord 已提交
527
Status
S
starlord 已提交
528
Config::CheckCacheConfigCpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
529
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
530
        std::string msg = "Invalid cpu cache capacity: " + value +
S
starlord 已提交
531
                          ". Possible reason: cache_config.cpu_cache_capacity is not a positive integer.";
532
        return Status(SERVER_INVALID_ARGUMENT, msg);
533
    } else {
534 535 536
        int64_t cpu_cache_capacity = std::stoi(value) * GB;
        if (cpu_cache_capacity <= 0) {
            std::string msg = "Invalid cpu cache capacity: " + value +
S
starlord 已提交
537
                              ". Possible reason: cache_config.cpu_cache_capacity is not a positive integer.";
538 539 540
            return Status(SERVER_INVALID_ARGUMENT, msg);
        }

S
starlord 已提交
541
        uint64_t total_mem = 0, free_mem = 0;
Z
zhiru 已提交
542
        CommonUtil::GetSystemMemInfo(total_mem, free_mem);
543 544
        if (static_cast<uint64_t>(cpu_cache_capacity) >= total_mem) {
            std::string msg = "Invalid cpu cache capacity: " + value +
S
starlord 已提交
545
                              ". Possible reason: cache_config.cpu_cache_capacity exceeds system memory.";
546
            return Status(SERVER_INVALID_ARGUMENT, msg);
547
        } else if (static_cast<double>(cpu_cache_capacity) > static_cast<double>(total_mem * 0.9)) {
548
            std::cerr << "WARNING: cpu cache capacity value is too big" << std::endl;
Z
zhiru 已提交
549
        }
550

Y
yudong.cai 已提交
551 552
        int32_t buffer_value;
        Status s = GetDBConfigInsertBufferSize(buffer_value);
S
starlord 已提交
553 554 555 556
        if (!s.ok()) {
            return s;
        }

Y
yudong.cai 已提交
557
        int64_t insert_buffer_size = buffer_value * GB;
Y
yudong.cai 已提交
558
        if (insert_buffer_size + cpu_cache_capacity >= total_mem) {
559
            std::string msg = "Invalid cpu cache capacity: " + value +
S
starlord 已提交
560
                              ". Possible reason: sum of cache_config.cpu_cache_capacity and "
561
                              "db_config.insert_buffer_size exceeds system memory.";
562
            return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
563 564
        }
    }
Y
yudong.cai 已提交
565 566
    return Status::OK();
}
Z
zhiru 已提交
567

Y
yudong.cai 已提交
568
Status
S
starlord 已提交
569
Config::CheckCacheConfigCpuCacheThreshold(const std::string& value) {
Y
yudong.cai 已提交
570
    if (!ValidationUtil::ValidateStringIsFloat(value).ok()) {
571
        std::string msg = "Invalid cpu cache threshold: " + value +
S
starlord 已提交
572
                          ". Possible reason: cache_config.cpu_cache_threshold is not in range (0.0, 1.0].";
573
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
574
    } else {
Y
yudong.cai 已提交
575 576
        float cpu_cache_threshold = std::stof(value);
        if (cpu_cache_threshold <= 0.0 || cpu_cache_threshold >= 1.0) {
577
            std::string msg = "Invalid cpu cache threshold: " + value +
S
starlord 已提交
578
                              ". Possible reason: cache_config.cpu_cache_threshold is not in range (0.0, 1.0].";
579
            return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
580
        }
581
    }
Y
yudong.cai 已提交
582 583
    return Status::OK();
}
584

Y
yudong.cai 已提交
585
Status
S
starlord 已提交
586
Config::CheckCacheConfigGpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
587
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
588
        std::string msg = "Invalid gpu cache capacity: " + value +
S
starlord 已提交
589
                          ". Possible reason: cache_config.gpu_cache_capacity is not a positive integer.";
590
        return Status(SERVER_INVALID_ARGUMENT, msg);
591
    } else {
Y
yudong.cai 已提交
592 593
        uint64_t gpu_cache_capacity = std::stoi(value) * GB;
        int gpu_index;
594
        Status s = GetResourceConfigIndexBuildDevice(gpu_index);
S
starlord 已提交
595 596 597 598
        if (!s.ok()) {
            return s;
        }

599 600 601
        if (gpu_index == server::CPU_DEVICE_ID)
            return Status::OK();

Z
zhiru 已提交
602
        size_t gpu_memory;
S
starlord 已提交
603
        if (!ValidationUtil::GetGpuMemory(gpu_index, gpu_memory).ok()) {
604 605
            std::string msg = "Fail to get GPU memory for GPU device: " + std::to_string(gpu_index);
            return Status(SERVER_UNEXPECTED_ERROR, msg);
606
        } else if (gpu_cache_capacity >= gpu_memory) {
607
            std::string msg = "Invalid gpu cache capacity: " + value +
S
starlord 已提交
608
                              ". Possible reason: cache_config.gpu_cache_capacity exceeds GPU memory.";
609
            return Status(SERVER_INVALID_ARGUMENT, msg);
S
starlord 已提交
610
        } else if (gpu_cache_capacity > (double)gpu_memory * 0.9) {
611
            std::cerr << "Warning: gpu cache capacity value is too big" << std::endl;
Z
zhiru 已提交
612 613
        }
    }
Y
yudong.cai 已提交
614 615
    return Status::OK();
}
Z
zhiru 已提交
616

Y
yudong.cai 已提交
617
Status
S
starlord 已提交
618
Config::CheckCacheConfigGpuCacheThreshold(const std::string& value) {
Y
yudong.cai 已提交
619
    if (!ValidationUtil::ValidateStringIsFloat(value).ok()) {
620
        std::string msg = "Invalid gpu cache threshold: " + value +
S
starlord 已提交
621
                          ". Possible reason: cache_config.gpu_cache_threshold is not in range (0.0, 1.0].";
622
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
623
    } else {
Y
yudong.cai 已提交
624 625
        float gpu_cache_threshold = std::stof(value);
        if (gpu_cache_threshold <= 0.0 || gpu_cache_threshold >= 1.0) {
626
            std::string msg = "Invalid gpu cache threshold: " + value +
S
starlord 已提交
627
                              ". Possible reason: cache_config.gpu_cache_threshold is not in range (0.0, 1.0].";
628
            return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
629
        }
Z
zhiru 已提交
630
    }
Y
yudong.cai 已提交
631 632
    return Status::OK();
}
Z
zhiru 已提交
633

Y
yudong.cai 已提交
634
Status
S
starlord 已提交
635
Config::CheckCacheConfigCacheInsertData(const std::string& value) {
Y
yudong.cai 已提交
636
    if (!ValidationUtil::ValidateStringIsBool(value).ok()) {
637
        std::string msg = "Invalid cache insert data option: " + value +
S
starlord 已提交
638
                          ". Possible reason: cache_config.cache_insert_data is not a boolean.";
639
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
640 641
    }
    return Status::OK();
Z
zhiru 已提交
642 643
}

S
starlord 已提交
644
Status
S
starlord 已提交
645
Config::CheckEngineConfigUseBlasThreshold(const std::string& value) {
Y
yudong.cai 已提交
646
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
647
        std::string msg = "Invalid use blas threshold: " + value +
S
starlord 已提交
648
                          ". Possible reason: engine_config.use_blas_threshold is not a positive integer.";
649
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
650
    }
Y
yudong.cai 已提交
651 652
    return Status::OK();
}
Z
zhiru 已提交
653

Y
yudong.cai 已提交
654
Status
S
starlord 已提交
655
Config::CheckEngineConfigOmpThreadNum(const std::string& value) {
Y
yudong.cai 已提交
656
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
657
        std::string msg = "Invalid omp thread num: " + value +
S
starlord 已提交
658
                          ". Possible reason: engine_config.omp_thread_num is not a positive integer.";
659
        return Status(SERVER_INVALID_ARGUMENT, msg);
S
starlord 已提交
660 661 662 663 664 665
    }

    int32_t omp_thread = std::stoi(value);
    uint32_t sys_thread_cnt = 8;
    CommonUtil::GetSystemAvailableThreads(sys_thread_cnt);
    if (omp_thread > static_cast<int32_t>(sys_thread_cnt)) {
666
        std::string msg = "Invalid omp thread num: " + value +
S
starlord 已提交
667
                          ". Possible reason: engine_config.omp_thread_num exceeds system cpu cores.";
668
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
669
    }
Y
yudong.cai 已提交
670
    return Status::OK();
Z
zhiru 已提交
671 672
}

W
wxyu 已提交
673
Status
674
Config::CheckEngineConfigGpuSearchThreshold(const std::string& value) {
W
wxyu 已提交
675
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
676 677
        std::string msg = "Invalid gpu search threshold: " + value +
                          ". Possible reason: engine_config.gpu_search_threshold is not a positive integer.";
W
wxyu 已提交
678 679 680 681 682
        return Status(SERVER_INVALID_ARGUMENT, msg);
    }
    return Status::OK();
}

S
starlord 已提交
683
Status
S
starlord 已提交
684
Config::CheckResourceConfigMode(const std::string& value) {
Y
yudong.cai 已提交
685
    if (value != "simple") {
S
starlord 已提交
686
        std::string msg = "Invalid resource mode: " + value + ". Possible reason: resource_config.mode is invalid.";
687
        return Status(SERVER_INVALID_ARGUMENT, msg);
W
wxyu 已提交
688
    }
Y
yudong.cai 已提交
689 690
    return Status::OK();
}
691

Y
yudong.cai 已提交
692
Status
693 694 695 696
CheckGpuDevice(const std::string& value) {
    const std::regex pat("gpu(\\d+)");
    std::cmatch m;
    if (!std::regex_match(value.c_str(), m, pat)) {
697
        std::string msg = "Invalid gpu device: " + value +
S
starlord 已提交
698
                          ". Possible reason: resource_config.search_resources does not match your hardware.";
699
        return Status(SERVER_INVALID_ARGUMENT, msg);
700 701 702 703
    }

    int32_t gpu_index = std::stoi(value.substr(3));
    if (!ValidationUtil::ValidateGpuIndex(gpu_index).ok()) {
704
        std::string msg = "Invalid gpu device: " + value +
S
starlord 已提交
705
                          ". Possible reason: resource_config.search_resources does not match your hardware.";
706
        return Status(SERVER_INVALID_ARGUMENT, msg);
707 708 709 710 711 712
    }
    return Status::OK();
}

Status
Config::CheckResourceConfigSearchResources(const std::vector<std::string>& value) {
Y
yudong.cai 已提交
713
    if (value.empty()) {
714 715 716
        std::string msg =
            "Invalid search resource. "
            "Possible reason: resource_config.search_resources is empty.";
717
        return Status(SERVER_INVALID_ARGUMENT, msg);
718 719
    }

720 721 722 723 724 725
    for (auto& device : value) {
        if (device == "cpu") {
            continue;
        }
        if (!CheckGpuDevice(device).ok()) {
            std::string msg = "Invalid search resource: " + device +
S
starlord 已提交
726
                              ". Possible reason: resource_config.search_resources does not match your hardware.";
727
            return Status(SERVER_INVALID_ARGUMENT, msg);
728 729 730 731 732 733 734
        }
    }
    return Status::OK();
}

Status
Config::CheckResourceConfigIndexBuildDevice(const std::string& value) {
735 736 737
    if (value == "cpu") {
        return Status::OK();
    }
738
    if (!CheckGpuDevice(value).ok()) {
739
        std::string msg = "Invalid index build device: " + value +
S
starlord 已提交
740
                          ". Possible reason: resource_config.index_build_device does not match your hardware.";
741
        return Status(SERVER_INVALID_ARGUMENT, msg);
G
groot 已提交
742
    }
Y
yudong.cai 已提交
743
    return Status::OK();
G
groot 已提交
744 745
}

Y
yudong.cai 已提交
746
////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
747 748
ConfigNode&
Config::GetConfigNode(const std::string& name) {
749
    ConfigMgr* mgr = YamlConfigMgr::GetInstance();
S
starlord 已提交
750
    ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
751
    return root_node.GetChild(name);
G
groot 已提交
752 753
}

Y
yudong.cai 已提交
754
Status
S
starlord 已提交
755
Config::GetConfigValueInMem(const std::string& parent_key, const std::string& child_key, std::string& value) {
Y
yudong.cai 已提交
756
    std::lock_guard<std::mutex> lock(mutex_);
Y
yudong.cai 已提交
757 758 759 760 761
    if (config_map_.find(parent_key) != config_map_.end() &&
        config_map_[parent_key].find(child_key) != config_map_[parent_key].end()) {
        value = config_map_[parent_key][child_key];
        return Status::OK();
    }
S
starlord 已提交
762
    return Status(SERVER_UNEXPECTED_ERROR, "key not exist");
Y
yudong.cai 已提交
763 764
}

Y
yudong.cai 已提交
765
void
S
starlord 已提交
766
Config::SetConfigValueInMem(const std::string& parent_key, const std::string& child_key, const std::string& value) {
Y
yudong.cai 已提交
767 768 769 770 771
    std::lock_guard<std::mutex> lock(mutex_);
    config_map_[parent_key][child_key] = value;
}

////////////////////////////////////////////////////////////////////////////////
Y
yudong.cai 已提交
772
std::string
S
starlord 已提交
773
Config::GetConfigStr(const std::string& parent_key, const std::string& child_key, const std::string& default_value) {
Y
yudong.cai 已提交
774
    std::string value;
S
starlord 已提交
775 776 777
    if (!GetConfigValueInMem(parent_key, child_key, value).ok()) {
        value = GetConfigNode(parent_key).GetValue(child_key, default_value);
        SetConfigValueInMem(parent_key, child_key, value);
Y
yudong.cai 已提交
778
    }
Y
yudong.cai 已提交
779
    return value;
Y
yudong.cai 已提交
780 781
}

782
Status
S
starlord 已提交
783
Config::GetServerConfigAddress(std::string& value) {
S
starlord 已提交
784
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_ADDRESS, CONFIG_SERVER_ADDRESS_DEFAULT);
Y
yudong.cai 已提交
785
    return CheckServerConfigAddress(value);
786 787 788
}

Status
S
starlord 已提交
789
Config::GetServerConfigPort(std::string& value) {
S
starlord 已提交
790
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_PORT, CONFIG_SERVER_PORT_DEFAULT);
Y
yudong.cai 已提交
791
    return CheckServerConfigPort(value);
792 793 794
}

Status
S
starlord 已提交
795
Config::GetServerConfigDeployMode(std::string& value) {
S
starlord 已提交
796
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, CONFIG_SERVER_DEPLOY_MODE_DEFAULT);
Y
yudong.cai 已提交
797
    return CheckServerConfigDeployMode(value);
798 799 800
}

Status
S
starlord 已提交
801
Config::GetServerConfigTimeZone(std::string& value) {
S
starlord 已提交
802
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, CONFIG_SERVER_TIME_ZONE_DEFAULT);
Y
yudong.cai 已提交
803
    return CheckServerConfigTimeZone(value);
804 805 806
}

Status
S
starlord 已提交
807
Config::GetDBConfigPrimaryPath(std::string& value) {
S
starlord 已提交
808
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_PRIMARY_PATH, CONFIG_DB_PRIMARY_PATH_DEFAULT);
Y
yudong.cai 已提交
809
    return CheckDBConfigPrimaryPath(value);
810 811 812
}

Status
S
starlord 已提交
813
Config::GetDBConfigSecondaryPath(std::string& value) {
S
starlord 已提交
814
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_SECONDARY_PATH, CONFIG_DB_SECONDARY_PATH_DEFAULT);
Y
yudong.cai 已提交
815
    return Status::OK();
816 817 818
}

Status
S
starlord 已提交
819
Config::GetDBConfigBackendUrl(std::string& value) {
S
starlord 已提交
820
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_BACKEND_URL, CONFIG_DB_BACKEND_URL_DEFAULT);
Y
yudong.cai 已提交
821
    return CheckDBConfigBackendUrl(value);
822 823 824
}

Status
S
starlord 已提交
825
Config::GetDBConfigArchiveDiskThreshold(int32_t& value) {
S
starlord 已提交
826 827
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DISK_THRESHOLD, CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
828
    Status s = CheckDBConfigArchiveDiskThreshold(str);
S
starlord 已提交
829 830 831 832
    if (!s.ok()) {
        return s;
    }

833 834 835 836 837
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
838
Config::GetDBConfigArchiveDaysThreshold(int32_t& value) {
S
starlord 已提交
839 840
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
841
    Status s = CheckDBConfigArchiveDaysThreshold(str);
S
starlord 已提交
842 843 844 845
    if (!s.ok()) {
        return s;
    }

846 847 848 849 850
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
851
Config::GetDBConfigInsertBufferSize(int32_t& value) {
852
    std::string str = GetConfigStr(CONFIG_DB, CONFIG_DB_INSERT_BUFFER_SIZE, CONFIG_DB_INSERT_BUFFER_SIZE_DEFAULT);
Y
yudong.cai 已提交
853
    Status s = CheckDBConfigInsertBufferSize(str);
S
starlord 已提交
854 855 856 857
    if (!s.ok()) {
        return s;
    }

858 859 860 861
    value = std::stoi(str);
    return Status::OK();
}

S
starlord 已提交
862 863
Status
Config::GetDBConfigPreloadTable(std::string& value) {
S
starlord 已提交
864
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_PRELOAD_TABLE);
S
starlord 已提交
865 866 867
    return Status::OK();
}

868
Status
S
starlord 已提交
869
Config::GetMetricConfigEnableMonitor(bool& value) {
870
    std::string str = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_ENABLE_MONITOR, CONFIG_METRIC_ENABLE_MONITOR_DEFAULT);
Y
yudong.cai 已提交
871
    Status s = CheckMetricConfigEnableMonitor(str);
S
starlord 已提交
872 873 874 875
    if (!s.ok()) {
        return s;
    }

876 877 878 879 880 881
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

Status
S
starlord 已提交
882
Config::GetMetricConfigCollector(std::string& value) {
S
starlord 已提交
883
    value = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_COLLECTOR, CONFIG_METRIC_COLLECTOR_DEFAULT);
Y
yudong.cai 已提交
884
    return Status::OK();
885 886 887
}

Status
S
starlord 已提交
888
Config::GetMetricConfigPrometheusPort(std::string& value) {
S
starlord 已提交
889
    value = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_PROMETHEUS_PORT, CONFIG_METRIC_PROMETHEUS_PORT_DEFAULT);
Y
yudong.cai 已提交
890
    return CheckMetricConfigPrometheusPort(value);
891 892 893
}

Status
W
wxyu 已提交
894
Config::GetCacheConfigCpuCacheCapacity(int64_t& value) {
S
starlord 已提交
895 896
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_CAPACITY, CONFIG_CACHE_CPU_CACHE_CAPACITY_DEFAULT);
Y
yudong.cai 已提交
897
    Status s = CheckCacheConfigCpuCacheCapacity(str);
S
starlord 已提交
898 899 900 901
    if (!s.ok()) {
        return s;
    }

902 903 904 905 906
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
907
Config::GetCacheConfigCpuCacheThreshold(float& value) {
S
starlord 已提交
908 909
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_THRESHOLD, CONFIG_CACHE_CPU_CACHE_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
910
    Status s = CheckCacheConfigCpuCacheThreshold(str);
S
starlord 已提交
911 912 913 914
    if (!s.ok()) {
        return s;
    }

915 916 917 918 919
    value = std::stof(str);
    return Status::OK();
}

Status
W
wxyu 已提交
920
Config::GetCacheConfigGpuCacheCapacity(int64_t& value) {
S
starlord 已提交
921 922
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_CAPACITY, CONFIG_CACHE_GPU_CACHE_CAPACITY_DEFAULT);
Y
yudong.cai 已提交
923
    Status s = CheckCacheConfigGpuCacheCapacity(str);
S
starlord 已提交
924 925 926 927
    if (!s.ok()) {
        return s;
    }

928 929 930 931 932
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
933
Config::GetCacheConfigGpuCacheThreshold(float& value) {
S
starlord 已提交
934 935
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_THRESHOLD, CONFIG_CACHE_GPU_CACHE_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
936
    Status s = CheckCacheConfigGpuCacheThreshold(str);
S
starlord 已提交
937 938 939 940
    if (!s.ok()) {
        return s;
    }

941 942 943 944 945
    value = std::stof(str);
    return Status::OK();
}

Status
S
starlord 已提交
946
Config::GetCacheConfigCacheInsertData(bool& value) {
S
starlord 已提交
947 948
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CACHE_INSERT_DATA, CONFIG_CACHE_CACHE_INSERT_DATA_DEFAULT);
Y
yudong.cai 已提交
949
    Status s = CheckCacheConfigCacheInsertData(str);
S
starlord 已提交
950 951 952 953
    if (!s.ok()) {
        return s;
    }

954 955 956 957 958 959
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

Status
S
starlord 已提交
960
Config::GetEngineConfigUseBlasThreshold(int32_t& value) {
S
starlord 已提交
961 962
    std::string str =
        GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_USE_BLAS_THRESHOLD, CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
963
    Status s = CheckEngineConfigUseBlasThreshold(str);
S
starlord 已提交
964 965 966 967
    if (!s.ok()) {
        return s;
    }

968 969 970 971 972
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
973
Config::GetEngineConfigOmpThreadNum(int32_t& value) {
974
    std::string str = GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_OMP_THREAD_NUM, CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT);
Y
yudong.cai 已提交
975
    Status s = CheckEngineConfigOmpThreadNum(str);
S
starlord 已提交
976 977 978 979
    if (!s.ok()) {
        return s;
    }

980 981 982 983
    value = std::stoi(str);
    return Status::OK();
}

W
wxyu 已提交
984
Status
985
Config::GetEngineConfigGpuSearchThreshold(int32_t& value) {
W
wxyu 已提交
986
    std::string str =
987 988
        GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT);
    Status s = CheckEngineConfigGpuSearchThreshold(str);
W
wxyu 已提交
989 990 991 992 993 994 995 996
    if (!s.ok()) {
        return s;
    }

    value = std::stoi(str);
    return Status::OK();
}

997
Status
S
starlord 已提交
998
Config::GetResourceConfigMode(std::string& value) {
S
starlord 已提交
999
    value = GetConfigStr(CONFIG_RESOURCE, CONFIG_RESOURCE_MODE, CONFIG_RESOURCE_MODE_DEFAULT);
Y
yudong.cai 已提交
1000
    return CheckResourceConfigMode(value);
1001 1002 1003
}

Status
1004
Config::GetResourceConfigSearchResources(std::vector<std::string>& value) {
1005
    ConfigNode resource_config = GetConfigNode(CONFIG_RESOURCE);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    value = resource_config.GetSequence(CONFIG_RESOURCE_SEARCH_RESOURCES);
    return CheckResourceConfigSearchResources(value);
}

Status
Config::GetResourceConfigIndexBuildDevice(int32_t& value) {
    std::string str =
        GetConfigStr(CONFIG_RESOURCE, CONFIG_RESOURCE_INDEX_BUILD_DEVICE, CONFIG_RESOURCE_INDEX_BUILD_DEVICE_DEFAULT);
    Status s = CheckResourceConfigIndexBuildDevice(str);
    if (!s.ok()) {
        return s;
    }

1019 1020 1021 1022 1023 1024
    if (str == "cpu") {
        value = CPU_DEVICE_ID;
    } else {
        value = std::stoi(str.substr(3));
    }

1025
    return Status::OK();
Y
yudong.cai 已提交
1026
}
G
groot 已提交
1027

Y
yudong.cai 已提交
1028 1029 1030
///////////////////////////////////////////////////////////////////////////////
/* server config */
Status
S
starlord 已提交
1031
Config::SetServerConfigAddress(const std::string& value) {
Y
yudong.cai 已提交
1032
    Status s = CheckServerConfigAddress(value);
S
starlord 已提交
1033 1034 1035 1036
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1037 1038 1039 1040 1041
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_ADDRESS, value);
    return Status::OK();
}

Status
S
starlord 已提交
1042
Config::SetServerConfigPort(const std::string& value) {
Y
yudong.cai 已提交
1043
    Status s = CheckServerConfigPort(value);
S
starlord 已提交
1044 1045 1046 1047
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1048 1049 1050 1051 1052
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_PORT, value);
    return Status::OK();
}

Status
S
starlord 已提交
1053
Config::SetServerConfigDeployMode(const std::string& value) {
Y
yudong.cai 已提交
1054
    Status s = CheckServerConfigDeployMode(value);
S
starlord 已提交
1055 1056 1057 1058
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1059
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, value);
Y
yudong.cai 已提交
1060 1061 1062 1063
    return Status::OK();
}

Status
S
starlord 已提交
1064
Config::SetServerConfigTimeZone(const std::string& value) {
Y
yudong.cai 已提交
1065
    Status s = CheckServerConfigTimeZone(value);
S
starlord 已提交
1066 1067 1068 1069
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1070 1071 1072 1073 1074 1075
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, value);
    return Status::OK();
}

/* db config */
Status
S
starlord 已提交
1076
Config::SetDBConfigPrimaryPath(const std::string& value) {
Y
yudong.cai 已提交
1077
    Status s = CheckDBConfigPrimaryPath(value);
S
starlord 已提交
1078 1079 1080 1081
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1082
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_PRIMARY_PATH, value);
Y
yudong.cai 已提交
1083 1084 1085 1086
    return Status::OK();
}

Status
S
starlord 已提交
1087
Config::SetDBConfigSecondaryPath(const std::string& value) {
Y
yudong.cai 已提交
1088
    Status s = CheckDBConfigSecondaryPath(value);
S
starlord 已提交
1089 1090 1091 1092
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1093
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_SECONDARY_PATH, value);
Y
yudong.cai 已提交
1094 1095 1096 1097
    return Status::OK();
}

Status
S
starlord 已提交
1098
Config::SetDBConfigBackendUrl(const std::string& value) {
Y
yudong.cai 已提交
1099
    Status s = CheckDBConfigBackendUrl(value);
S
starlord 已提交
1100 1101 1102 1103
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1104 1105 1106 1107 1108
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_BACKEND_URL, value);
    return Status::OK();
}

Status
S
starlord 已提交
1109
Config::SetDBConfigArchiveDiskThreshold(const std::string& value) {
Y
yudong.cai 已提交
1110
    Status s = CheckDBConfigArchiveDiskThreshold(value);
S
starlord 已提交
1111 1112 1113 1114
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1115 1116 1117 1118 1119
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_ARCHIVE_DISK_THRESHOLD, value);
    return Status::OK();
}

Status
S
starlord 已提交
1120
Config::SetDBConfigArchiveDaysThreshold(const std::string& value) {
Y
yudong.cai 已提交
1121
    Status s = CheckDBConfigArchiveDaysThreshold(value);
S
starlord 已提交
1122 1123 1124 1125
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1126 1127 1128 1129 1130
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD, value);
    return Status::OK();
}

Status
S
starlord 已提交
1131
Config::SetDBConfigInsertBufferSize(const std::string& value) {
Y
yudong.cai 已提交
1132
    Status s = CheckDBConfigInsertBufferSize(value);
S
starlord 已提交
1133 1134 1135 1136
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1137
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_INSERT_BUFFER_SIZE, value);
Y
yudong.cai 已提交
1138 1139 1140 1141 1142
    return Status::OK();
}

/* metric config */
Status
S
starlord 已提交
1143
Config::SetMetricConfigEnableMonitor(const std::string& value) {
Y
yudong.cai 已提交
1144
    Status s = CheckMetricConfigEnableMonitor(value);
S
starlord 已提交
1145 1146 1147 1148
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1149
    SetConfigValueInMem(CONFIG_DB, CONFIG_METRIC_ENABLE_MONITOR, value);
Y
yudong.cai 已提交
1150 1151 1152 1153
    return Status::OK();
}

Status
S
starlord 已提交
1154
Config::SetMetricConfigCollector(const std::string& value) {
Y
yudong.cai 已提交
1155
    Status s = CheckMetricConfigCollector(value);
S
starlord 已提交
1156 1157 1158 1159
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1160 1161 1162 1163 1164
    SetConfigValueInMem(CONFIG_DB, CONFIG_METRIC_COLLECTOR, value);
    return Status::OK();
}

Status
S
starlord 已提交
1165
Config::SetMetricConfigPrometheusPort(const std::string& value) {
Y
yudong.cai 已提交
1166
    Status s = CheckMetricConfigPrometheusPort(value);
S
starlord 已提交
1167 1168 1169 1170
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1171 1172 1173 1174 1175 1176
    SetConfigValueInMem(CONFIG_DB, CONFIG_METRIC_PROMETHEUS_PORT, value);
    return Status::OK();
}

/* cache config */
Status
S
starlord 已提交
1177
Config::SetCacheConfigCpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
1178
    Status s = CheckCacheConfigCpuCacheCapacity(value);
S
starlord 已提交
1179 1180 1181 1182
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1183
    SetConfigValueInMem(CONFIG_DB, CONFIG_CACHE_CPU_CACHE_CAPACITY, value);
Y
yudong.cai 已提交
1184 1185 1186 1187
    return Status::OK();
}

Status
S
starlord 已提交
1188
Config::SetCacheConfigCpuCacheThreshold(const std::string& value) {
Y
yudong.cai 已提交
1189
    Status s = CheckCacheConfigCpuCacheThreshold(value);
S
starlord 已提交
1190 1191 1192 1193
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1194
    SetConfigValueInMem(CONFIG_DB, CONFIG_CACHE_CPU_CACHE_THRESHOLD, value);
Y
yudong.cai 已提交
1195 1196 1197 1198
    return Status::OK();
}

Status
S
starlord 已提交
1199
Config::SetCacheConfigGpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
1200
    Status s = CheckCacheConfigGpuCacheCapacity(value);
S
starlord 已提交
1201 1202 1203 1204
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1205
    SetConfigValueInMem(CONFIG_DB, CONFIG_CACHE_GPU_CACHE_CAPACITY, value);
Y
yudong.cai 已提交
1206 1207 1208 1209
    return Status::OK();
}

Status
S
starlord 已提交
1210
Config::SetCacheConfigGpuCacheThreshold(const std::string& value) {
Y
yudong.cai 已提交
1211
    Status s = CheckCacheConfigGpuCacheThreshold(value);
S
starlord 已提交
1212 1213 1214 1215
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1216
    SetConfigValueInMem(CONFIG_DB, CONFIG_CACHE_GPU_CACHE_THRESHOLD, value);
Y
yudong.cai 已提交
1217 1218 1219 1220
    return Status::OK();
}

Status
S
starlord 已提交
1221
Config::SetCacheConfigCacheInsertData(const std::string& value) {
Y
yudong.cai 已提交
1222
    Status s = CheckCacheConfigCacheInsertData(value);
S
starlord 已提交
1223 1224 1225 1226
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1227 1228 1229 1230 1231 1232
    SetConfigValueInMem(CONFIG_DB, CONFIG_CACHE_CACHE_INSERT_DATA, value);
    return Status::OK();
}

/* engine config */
Status
S
starlord 已提交
1233
Config::SetEngineConfigUseBlasThreshold(const std::string& value) {
Y
yudong.cai 已提交
1234
    Status s = CheckEngineConfigUseBlasThreshold(value);
S
starlord 已提交
1235 1236 1237 1238
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1239
    SetConfigValueInMem(CONFIG_DB, CONFIG_ENGINE_USE_BLAS_THRESHOLD, value);
Y
yudong.cai 已提交
1240 1241 1242 1243
    return Status::OK();
}

Status
S
starlord 已提交
1244
Config::SetEngineConfigOmpThreadNum(const std::string& value) {
Y
yudong.cai 已提交
1245
    Status s = CheckEngineConfigOmpThreadNum(value);
S
starlord 已提交
1246 1247 1248 1249
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1250 1251 1252 1253
    SetConfigValueInMem(CONFIG_DB, CONFIG_ENGINE_OMP_THREAD_NUM, value);
    return Status::OK();
}

W
wxyu 已提交
1254
Status
1255 1256
Config::SetEngineConfigGpuSearchThreshold(const std::string& value) {
    Status s = CheckEngineConfigGpuSearchThreshold(value);
W
wxyu 已提交
1257 1258 1259 1260
    if (!s.ok()) {
        return s;
    }

1261
    SetConfigValueInMem(CONFIG_DB, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD, value);
W
wxyu 已提交
1262 1263 1264
    return Status::OK();
}

Y
yudong.cai 已提交
1265 1266
/* resource config */
Status
S
starlord 已提交
1267
Config::SetResourceConfigMode(const std::string& value) {
Y
yudong.cai 已提交
1268
    Status s = CheckResourceConfigMode(value);
S
starlord 已提交
1269 1270 1271 1272
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1273 1274 1275 1276
    SetConfigValueInMem(CONFIG_DB, CONFIG_RESOURCE_MODE, value);
    return Status::OK();
}

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
Status
Config::SetResourceConfigIndexBuildDevice(const std::string& value) {
    Status s = CheckResourceConfigIndexBuildDevice(value);
    if (!s.ok()) {
        return s;
    }

    SetConfigValueInMem(CONFIG_DB, CONFIG_RESOURCE_INDEX_BUILD_DEVICE, value);
    return Status::OK();
}

S
starlord 已提交
1288 1289
}  // namespace server
}  // namespace milvus