Config.cpp 40.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
#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
#include "utils/CommonUtil.h"
28
#include "utils/StringHelpFunctions.h"
29
#include "utils/ValidationUtil.h"
G
groot 已提交
30

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

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

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

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

    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 已提交
52 53 54
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

210 211 212 213 214 215 216 217
    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 已提交
218 219 220
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
221 222 223 224

    return Status::OK();
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

345 346 347 348 349
    s = SetResourceConfigSearchResources(CONFIG_RESOURCE_SEARCH_RESOURCES_DEFAULT);
    if (!s.ok()) {
        return s;
    }

350 351 352 353 354
    s = SetResourceConfigIndexBuildDevice(CONFIG_RESOURCE_INDEX_BUILD_DEVICE_DEFAULT);
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
355 356 357
    return Status::OK();
}

Y
yudong.cai 已提交
358
void
S
starlord 已提交
359
Config::PrintConfigSection(const std::string& config_node_name) {
Y
yudong.cai 已提交
360 361 362
    std::cout << std::endl;
    std::cout << config_node_name << ":" << std::endl;
    if (config_map_.find(config_node_name) != config_map_.end()) {
S
starlord 已提交
363
        for (auto item : config_map_[config_node_name]) {
Y
yudong.cai 已提交
364 365
            std::cout << item.first << ": " << item.second << std::endl;
        }
Z
zhiru 已提交
366 367 368
    }
}

Y
yudong.cai 已提交
369 370 371 372 373 374 375 376 377 378 379
void
Config::PrintAll() {
    PrintConfigSection(CONFIG_SERVER);
    PrintConfigSection(CONFIG_DB);
    PrintConfigSection(CONFIG_CACHE);
    PrintConfigSection(CONFIG_METRIC);
    PrintConfigSection(CONFIG_ENGINE);
    PrintConfigSection(CONFIG_RESOURCE);
}

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

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

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

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

S
starlord 已提交
433
Status
S
starlord 已提交
434
Config::CheckDBConfigPrimaryPath(const std::string& value) {
Y
yudong.cai 已提交
435
    if (value.empty()) {
S
starlord 已提交
436
        return Status(SERVER_INVALID_ARGUMENT, "db_config.db_path is empty.");
Z
zhiru 已提交
437
    }
Y
yudong.cai 已提交
438 439
    return Status::OK();
}
Z
zhiru 已提交
440

Y
yudong.cai 已提交
441
Status
S
starlord 已提交
442
Config::CheckDBConfigSecondaryPath(const std::string& value) {
Y
yudong.cai 已提交
443 444 445
    return Status::OK();
}

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

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

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

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

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

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

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

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

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

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

Y
yudong.cai 已提交
556 557
        int32_t buffer_value;
        Status s = GetDBConfigInsertBufferSize(buffer_value);
S
starlord 已提交
558 559 560 561
        if (!s.ok()) {
            return s;
        }

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

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

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

F
fishpenguin 已提交
604
        if (device_id == server::CPU_DEVICE_ID)
605 606
            return Status::OK();

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

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

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

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

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

    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)) {
671
        std::string msg = "Invalid omp thread num: " + value +
S
starlord 已提交
672
                          ". Possible reason: engine_config.omp_thread_num exceeds system cpu cores.";
673
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
674
    }
Y
yudong.cai 已提交
675
    return Status::OK();
Z
zhiru 已提交
676 677
}

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

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

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

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

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

725
    bool cpu_found = false, gpu_found = false;
726 727
    for (auto& device : value) {
        if (device == "cpu") {
728
            cpu_found = true;
729 730
            continue;
        }
731 732 733
        if (CheckGpuDevice(device).ok()) {
            gpu_found = true;
        } else {
734
            std::string msg = "Invalid search resource: " + device +
S
starlord 已提交
735
                              ". Possible reason: resource_config.search_resources does not match your hardware.";
736
            return Status(SERVER_INVALID_ARGUMENT, msg);
737 738
        }
    }
739 740 741

    if (cpu_found && !gpu_found) {
        std::string msg =
Y
yudong.cai 已提交
742
            "Invalid search resource. Possible reason: resource_config.search_resources has only CPU resource.";
743 744
        return Status(SERVER_INVALID_ARGUMENT, msg);
    }
745 746 747 748 749
    return Status::OK();
}

Status
Config::CheckResourceConfigIndexBuildDevice(const std::string& value) {
Y
yudong.cai 已提交
750 751 752
    // if (value == "cpu") {
    //     return Status::OK();
    // }
753
    if (!CheckGpuDevice(value).ok()) {
754
        std::string msg = "Invalid index build device: " + value +
S
starlord 已提交
755
                          ". Possible reason: resource_config.index_build_device does not match your hardware.";
756
        return Status(SERVER_INVALID_ARGUMENT, msg);
G
groot 已提交
757
    }
Y
yudong.cai 已提交
758
    return Status::OK();
G
groot 已提交
759 760
}

Y
yudong.cai 已提交
761
////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
762 763
ConfigNode&
Config::GetConfigNode(const std::string& name) {
764
    ConfigMgr* mgr = YamlConfigMgr::GetInstance();
S
starlord 已提交
765
    ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
766
    return root_node.GetChild(name);
G
groot 已提交
767 768
}

Y
yudong.cai 已提交
769
Status
S
starlord 已提交
770
Config::GetConfigValueInMem(const std::string& parent_key, const std::string& child_key, std::string& value) {
Y
yudong.cai 已提交
771
    std::lock_guard<std::mutex> lock(mutex_);
Y
yudong.cai 已提交
772 773 774 775 776
    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 已提交
777
    return Status(SERVER_UNEXPECTED_ERROR, "key not exist");
Y
yudong.cai 已提交
778 779
}

Y
yudong.cai 已提交
780
void
S
starlord 已提交
781
Config::SetConfigValueInMem(const std::string& parent_key, const std::string& child_key, const std::string& value) {
Y
yudong.cai 已提交
782 783 784 785 786
    std::lock_guard<std::mutex> lock(mutex_);
    config_map_[parent_key][child_key] = value;
}

////////////////////////////////////////////////////////////////////////////////
Y
yudong.cai 已提交
787
std::string
S
starlord 已提交
788
Config::GetConfigStr(const std::string& parent_key, const std::string& child_key, const std::string& default_value) {
Y
yudong.cai 已提交
789
    std::string value;
S
starlord 已提交
790 791 792
    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 已提交
793
    }
Y
yudong.cai 已提交
794
    return value;
Y
yudong.cai 已提交
795 796
}

797
std::string
798 799
Config::GetConfigSequenceStr(const std::string& parent_key, const std::string& child_key, const std::string& delim,
                             const std::string& default_value) {
800 801 802
    std::string value;
    if (!GetConfigValueInMem(parent_key, child_key, value).ok()) {
        std::vector<std::string> sequence = GetConfigNode(parent_key).GetSequence(child_key);
803 804 805 806 807
        if (sequence.empty()) {
            value = default_value;
        } else {
            server::StringHelpFunctions::MergeStringWithDelimeter(sequence, delim, value);
        }
808 809 810 811 812
        SetConfigValueInMem(parent_key, child_key, value);
    }
    return value;
}

813
Status
S
starlord 已提交
814
Config::GetServerConfigAddress(std::string& value) {
S
starlord 已提交
815
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_ADDRESS, CONFIG_SERVER_ADDRESS_DEFAULT);
Y
yudong.cai 已提交
816
    return CheckServerConfigAddress(value);
817 818 819
}

Status
S
starlord 已提交
820
Config::GetServerConfigPort(std::string& value) {
S
starlord 已提交
821
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_PORT, CONFIG_SERVER_PORT_DEFAULT);
Y
yudong.cai 已提交
822
    return CheckServerConfigPort(value);
823 824 825
}

Status
S
starlord 已提交
826
Config::GetServerConfigDeployMode(std::string& value) {
S
starlord 已提交
827
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, CONFIG_SERVER_DEPLOY_MODE_DEFAULT);
Y
yudong.cai 已提交
828
    return CheckServerConfigDeployMode(value);
829 830 831
}

Status
S
starlord 已提交
832
Config::GetServerConfigTimeZone(std::string& value) {
S
starlord 已提交
833
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, CONFIG_SERVER_TIME_ZONE_DEFAULT);
Y
yudong.cai 已提交
834
    return CheckServerConfigTimeZone(value);
835 836 837
}

Status
S
starlord 已提交
838
Config::GetDBConfigPrimaryPath(std::string& value) {
S
starlord 已提交
839
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_PRIMARY_PATH, CONFIG_DB_PRIMARY_PATH_DEFAULT);
Y
yudong.cai 已提交
840
    return CheckDBConfigPrimaryPath(value);
841 842 843
}

Status
S
starlord 已提交
844
Config::GetDBConfigSecondaryPath(std::string& value) {
S
starlord 已提交
845
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_SECONDARY_PATH, CONFIG_DB_SECONDARY_PATH_DEFAULT);
Y
yudong.cai 已提交
846
    return Status::OK();
847 848 849
}

Status
S
starlord 已提交
850
Config::GetDBConfigBackendUrl(std::string& value) {
S
starlord 已提交
851
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_BACKEND_URL, CONFIG_DB_BACKEND_URL_DEFAULT);
Y
yudong.cai 已提交
852
    return CheckDBConfigBackendUrl(value);
853 854 855
}

Status
S
starlord 已提交
856
Config::GetDBConfigArchiveDiskThreshold(int32_t& value) {
S
starlord 已提交
857 858
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DISK_THRESHOLD, CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
859
    Status s = CheckDBConfigArchiveDiskThreshold(str);
S
starlord 已提交
860 861 862 863
    if (!s.ok()) {
        return s;
    }

864 865 866 867 868
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
869
Config::GetDBConfigArchiveDaysThreshold(int32_t& value) {
S
starlord 已提交
870 871
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
872
    Status s = CheckDBConfigArchiveDaysThreshold(str);
S
starlord 已提交
873 874 875 876
    if (!s.ok()) {
        return s;
    }

877 878 879 880 881
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
882
Config::GetDBConfigInsertBufferSize(int32_t& value) {
883
    std::string str = GetConfigStr(CONFIG_DB, CONFIG_DB_INSERT_BUFFER_SIZE, CONFIG_DB_INSERT_BUFFER_SIZE_DEFAULT);
Y
yudong.cai 已提交
884
    Status s = CheckDBConfigInsertBufferSize(str);
S
starlord 已提交
885 886 887 888
    if (!s.ok()) {
        return s;
    }

889 890 891 892
    value = std::stoi(str);
    return Status::OK();
}

S
starlord 已提交
893 894
Status
Config::GetDBConfigPreloadTable(std::string& value) {
S
starlord 已提交
895
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_PRELOAD_TABLE);
S
starlord 已提交
896 897 898
    return Status::OK();
}

899
Status
S
starlord 已提交
900
Config::GetMetricConfigEnableMonitor(bool& value) {
901
    std::string str = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_ENABLE_MONITOR, CONFIG_METRIC_ENABLE_MONITOR_DEFAULT);
Y
yudong.cai 已提交
902
    Status s = CheckMetricConfigEnableMonitor(str);
S
starlord 已提交
903 904 905 906
    if (!s.ok()) {
        return s;
    }

907 908 909 910 911 912
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

Status
S
starlord 已提交
913
Config::GetMetricConfigCollector(std::string& value) {
S
starlord 已提交
914
    value = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_COLLECTOR, CONFIG_METRIC_COLLECTOR_DEFAULT);
Y
yudong.cai 已提交
915
    return Status::OK();
916 917 918
}

Status
S
starlord 已提交
919
Config::GetMetricConfigPrometheusPort(std::string& value) {
S
starlord 已提交
920
    value = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_PROMETHEUS_PORT, CONFIG_METRIC_PROMETHEUS_PORT_DEFAULT);
Y
yudong.cai 已提交
921
    return CheckMetricConfigPrometheusPort(value);
922 923 924
}

Status
W
wxyu 已提交
925
Config::GetCacheConfigCpuCacheCapacity(int64_t& value) {
S
starlord 已提交
926 927
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_CAPACITY, CONFIG_CACHE_CPU_CACHE_CAPACITY_DEFAULT);
Y
yudong.cai 已提交
928
    Status s = CheckCacheConfigCpuCacheCapacity(str);
S
starlord 已提交
929 930 931 932
    if (!s.ok()) {
        return s;
    }

933 934 935 936 937
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
938
Config::GetCacheConfigCpuCacheThreshold(float& value) {
S
starlord 已提交
939 940
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_THRESHOLD, CONFIG_CACHE_CPU_CACHE_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
941
    Status s = CheckCacheConfigCpuCacheThreshold(str);
S
starlord 已提交
942 943 944 945
    if (!s.ok()) {
        return s;
    }

946 947 948 949 950
    value = std::stof(str);
    return Status::OK();
}

Status
W
wxyu 已提交
951
Config::GetCacheConfigGpuCacheCapacity(int64_t& value) {
S
starlord 已提交
952 953
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_CAPACITY, CONFIG_CACHE_GPU_CACHE_CAPACITY_DEFAULT);
Y
yudong.cai 已提交
954
    Status s = CheckCacheConfigGpuCacheCapacity(str);
S
starlord 已提交
955 956 957 958
    if (!s.ok()) {
        return s;
    }

959 960 961 962 963
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
964
Config::GetCacheConfigGpuCacheThreshold(float& value) {
S
starlord 已提交
965 966
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_THRESHOLD, CONFIG_CACHE_GPU_CACHE_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
967
    Status s = CheckCacheConfigGpuCacheThreshold(str);
S
starlord 已提交
968 969 970 971
    if (!s.ok()) {
        return s;
    }

972 973 974 975 976
    value = std::stof(str);
    return Status::OK();
}

Status
S
starlord 已提交
977
Config::GetCacheConfigCacheInsertData(bool& value) {
S
starlord 已提交
978 979
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CACHE_INSERT_DATA, CONFIG_CACHE_CACHE_INSERT_DATA_DEFAULT);
Y
yudong.cai 已提交
980
    Status s = CheckCacheConfigCacheInsertData(str);
S
starlord 已提交
981 982 983 984
    if (!s.ok()) {
        return s;
    }

985 986 987 988 989 990
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

Status
S
starlord 已提交
991
Config::GetEngineConfigUseBlasThreshold(int32_t& value) {
S
starlord 已提交
992 993
    std::string str =
        GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_USE_BLAS_THRESHOLD, CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
994
    Status s = CheckEngineConfigUseBlasThreshold(str);
S
starlord 已提交
995 996 997 998
    if (!s.ok()) {
        return s;
    }

999 1000 1001 1002 1003
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
1004
Config::GetEngineConfigOmpThreadNum(int32_t& value) {
1005
    std::string str = GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_OMP_THREAD_NUM, CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT);
Y
yudong.cai 已提交
1006
    Status s = CheckEngineConfigOmpThreadNum(str);
S
starlord 已提交
1007 1008 1009 1010
    if (!s.ok()) {
        return s;
    }

1011 1012 1013 1014
    value = std::stoi(str);
    return Status::OK();
}

W
wxyu 已提交
1015
Status
1016
Config::GetEngineConfigGpuSearchThreshold(int32_t& value) {
W
wxyu 已提交
1017
    std::string str =
1018 1019
        GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT);
    Status s = CheckEngineConfigGpuSearchThreshold(str);
W
wxyu 已提交
1020 1021 1022 1023 1024 1025 1026 1027
    if (!s.ok()) {
        return s;
    }

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

1028
Status
S
starlord 已提交
1029
Config::GetResourceConfigMode(std::string& value) {
S
starlord 已提交
1030
    value = GetConfigStr(CONFIG_RESOURCE, CONFIG_RESOURCE_MODE, CONFIG_RESOURCE_MODE_DEFAULT);
Y
yudong.cai 已提交
1031
    return CheckResourceConfigMode(value);
1032 1033 1034
}

Status
1035
Config::GetResourceConfigSearchResources(std::vector<std::string>& value) {
1036 1037 1038
    std::string str =
        GetConfigSequenceStr(CONFIG_RESOURCE, CONFIG_RESOURCE_SEARCH_RESOURCES,
                             CONFIG_RESOURCE_SEARCH_RESOURCES_DELIMITER, CONFIG_RESOURCE_SEARCH_RESOURCES_DEFAULT);
1039
    server::StringHelpFunctions::SplitStringByDelimeter(str, CONFIG_RESOURCE_SEARCH_RESOURCES_DELIMITER, value);
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    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;
    }

1052 1053 1054 1055 1056 1057
    if (str == "cpu") {
        value = CPU_DEVICE_ID;
    } else {
        value = std::stoi(str.substr(3));
    }

1058
    return Status::OK();
Y
yudong.cai 已提交
1059
}
G
groot 已提交
1060

Y
yudong.cai 已提交
1061 1062 1063
///////////////////////////////////////////////////////////////////////////////
/* server config */
Status
S
starlord 已提交
1064
Config::SetServerConfigAddress(const std::string& value) {
Y
yudong.cai 已提交
1065
    Status s = CheckServerConfigAddress(value);
S
starlord 已提交
1066 1067 1068 1069
    if (!s.ok()) {
        return s;
    }

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

Status
S
starlord 已提交
1075
Config::SetServerConfigPort(const std::string& value) {
Y
yudong.cai 已提交
1076
    Status s = CheckServerConfigPort(value);
S
starlord 已提交
1077 1078 1079 1080
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1081 1082 1083 1084 1085
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_PORT, value);
    return Status::OK();
}

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

Y
yudong.cai 已提交
1092
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, value);
Y
yudong.cai 已提交
1093 1094 1095 1096
    return Status::OK();
}

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

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

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

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

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

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

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

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

Status
S
starlord 已提交
1142
Config::SetDBConfigArchiveDiskThreshold(const std::string& value) {
Y
yudong.cai 已提交
1143
    Status s = CheckDBConfigArchiveDiskThreshold(value);
S
starlord 已提交
1144 1145 1146 1147
    if (!s.ok()) {
        return s;
    }

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

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

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

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

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

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

1182
    SetConfigValueInMem(CONFIG_METRIC, CONFIG_METRIC_ENABLE_MONITOR, value);
Y
yudong.cai 已提交
1183 1184 1185 1186
    return Status::OK();
}

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

1193
    SetConfigValueInMem(CONFIG_METRIC, CONFIG_METRIC_COLLECTOR, value);
Y
yudong.cai 已提交
1194 1195 1196 1197
    return Status::OK();
}

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

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

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

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

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

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

Status
S
starlord 已提交
1232
Config::SetCacheConfigGpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
1233
    Status s = CheckCacheConfigGpuCacheCapacity(value);
S
starlord 已提交
1234 1235 1236 1237
    if (!s.ok()) {
        return s;
    }

1238
    SetConfigValueInMem(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_CAPACITY, value);
Y
yudong.cai 已提交
1239 1240 1241 1242
    return Status::OK();
}

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

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

Status
S
starlord 已提交
1254
Config::SetCacheConfigCacheInsertData(const std::string& value) {
Y
yudong.cai 已提交
1255
    Status s = CheckCacheConfigCacheInsertData(value);
S
starlord 已提交
1256 1257 1258 1259
    if (!s.ok()) {
        return s;
    }

1260
    SetConfigValueInMem(CONFIG_CACHE, CONFIG_CACHE_CACHE_INSERT_DATA, value);
Y
yudong.cai 已提交
1261 1262 1263 1264 1265
    return Status::OK();
}

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

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

Status
S
starlord 已提交
1277
Config::SetEngineConfigOmpThreadNum(const std::string& value) {
Y
yudong.cai 已提交
1278
    Status s = CheckEngineConfigOmpThreadNum(value);
S
starlord 已提交
1279 1280 1281 1282
    if (!s.ok()) {
        return s;
    }

1283
    SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_OMP_THREAD_NUM, value);
Y
yudong.cai 已提交
1284 1285 1286
    return Status::OK();
}

W
wxyu 已提交
1287
Status
1288 1289
Config::SetEngineConfigGpuSearchThreshold(const std::string& value) {
    Status s = CheckEngineConfigGpuSearchThreshold(value);
W
wxyu 已提交
1290 1291 1292 1293
    if (!s.ok()) {
        return s;
    }

1294
    SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD, value);
W
wxyu 已提交
1295 1296 1297
    return Status::OK();
}

Y
yudong.cai 已提交
1298 1299
/* resource config */
Status
S
starlord 已提交
1300
Config::SetResourceConfigMode(const std::string& value) {
Y
yudong.cai 已提交
1301
    Status s = CheckResourceConfigMode(value);
S
starlord 已提交
1302 1303 1304 1305
    if (!s.ok()) {
        return s;
    }

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
    SetConfigValueInMem(CONFIG_RESOURCE, CONFIG_RESOURCE_MODE, value);
    return Status::OK();
}

Status
Config::SetResourceConfigSearchResources(const std::string& value) {
    std::vector<std::string> res_vec;
    server::StringHelpFunctions::SplitStringByDelimeter(value, CONFIG_RESOURCE_SEARCH_RESOURCES_DELIMITER, res_vec);

    Status s = CheckResourceConfigSearchResources(res_vec);
    if (!s.ok()) {
        return s;
    }

    SetConfigValueInMem(CONFIG_RESOURCE, CONFIG_RESOURCE_SEARCH_RESOURCES, value);
Y
yudong.cai 已提交
1321 1322 1323
    return Status::OK();
}

1324 1325 1326 1327 1328 1329 1330
Status
Config::SetResourceConfigIndexBuildDevice(const std::string& value) {
    Status s = CheckResourceConfigIndexBuildDevice(value);
    if (!s.ok()) {
        return s;
    }

1331
    SetConfigValueInMem(CONFIG_RESOURCE, CONFIG_RESOURCE_INDEX_BUILD_DEVICE, value);
1332 1333 1334
    return Status::OK();
}

S
starlord 已提交
1335 1336
}  // namespace server
}  // namespace milvus