Config.cpp 37.1 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 198

    /* resource config */
    std::string resource_mode;
    s = GetResourceConfigMode(resource_mode);
S
starlord 已提交
199 200 201
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
202

203 204 205 206 207 208 209 210
    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 已提交
211 212 213
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
214 215 216 217

    return Status::OK();
}

Y
yudong.cai 已提交
218 219 220 221 222 223
Status
Config::ResetDefaultConfig() {
    Status s;

    /* server config */
    s = SetServerConfigAddress(CONFIG_SERVER_ADDRESS_DEFAULT);
S
starlord 已提交
224 225 226
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
227 228

    s = SetServerConfigPort(CONFIG_SERVER_PORT_DEFAULT);
S
starlord 已提交
229 230 231
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
232 233

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

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

    /* db config */
    s = SetDBConfigPrimaryPath(CONFIG_DB_PRIMARY_PATH_DEFAULT);
S
starlord 已提交
245 246 247
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
248 249

    s = SetDBConfigSecondaryPath(CONFIG_DB_SECONDARY_PATH_DEFAULT);
S
starlord 已提交
250 251 252
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
253 254

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

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

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

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

    /* metric config */
    s = SetMetricConfigEnableMonitor(CONFIG_METRIC_ENABLE_MONITOR_DEFAULT);
S
starlord 已提交
276 277 278
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
279 280

    s = SetMetricConfigCollector(CONFIG_METRIC_COLLECTOR_DEFAULT);
S
starlord 已提交
281 282 283
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
284 285

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

    /* cache config */
Y
yudong.cai 已提交
291
    s = SetCacheConfigCpuCacheCapacity(CONFIG_CACHE_CPU_CACHE_CAPACITY_DEFAULT);
S
starlord 已提交
292 293 294
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
295

Y
yudong.cai 已提交
296
    s = SetCacheConfigCpuCacheThreshold(CONFIG_CACHE_CPU_CACHE_THRESHOLD_DEFAULT);
S
starlord 已提交
297 298 299
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
300

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

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

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

    /* engine config */
Y
yudong.cai 已提交
317
    s = SetEngineConfigUseBlasThreshold(CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT);
S
starlord 已提交
318 319 320
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
321 322

    s = SetEngineConfigOmpThreadNum(CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT);
S
starlord 已提交
323 324 325
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
326 327 328

    /* resource config */
    s = SetResourceConfigMode(CONFIG_RESOURCE_MODE_DEFAULT);
S
starlord 已提交
329 330 331
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
332

333 334 335 336 337
    s = SetResourceConfigIndexBuildDevice(CONFIG_RESOURCE_INDEX_BUILD_DEVICE_DEFAULT);
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
338 339 340
    return Status::OK();
}

Y
yudong.cai 已提交
341
void
S
starlord 已提交
342
Config::PrintConfigSection(const std::string& config_node_name) {
Y
yudong.cai 已提交
343 344 345
    std::cout << std::endl;
    std::cout << config_node_name << ":" << std::endl;
    if (config_map_.find(config_node_name) != config_map_.end()) {
S
starlord 已提交
346
        for (auto item : config_map_[config_node_name]) {
Y
yudong.cai 已提交
347 348
            std::cout << item.first << ": " << item.second << std::endl;
        }
Z
zhiru 已提交
349 350 351
    }
}

Y
yudong.cai 已提交
352 353 354 355 356 357 358 359 360 361 362
void
Config::PrintAll() {
    PrintConfigSection(CONFIG_SERVER);
    PrintConfigSection(CONFIG_DB);
    PrintConfigSection(CONFIG_CACHE);
    PrintConfigSection(CONFIG_METRIC);
    PrintConfigSection(CONFIG_ENGINE);
    PrintConfigSection(CONFIG_RESOURCE);
}

////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
363
Status
S
starlord 已提交
364
Config::CheckServerConfigAddress(const std::string& value) {
Y
yudong.cai 已提交
365
    if (!ValidationUtil::ValidateIpAddress(value).ok()) {
366 367
        std::string msg = "Invalid server IP address: " + value +
                          ". Possible reason: server_config.address is invalid in server_config.yaml.";
368
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
369
    }
Y
yudong.cai 已提交
370 371
    return Status::OK();
}
Z
zhiru 已提交
372

Y
yudong.cai 已提交
373
Status
S
starlord 已提交
374
Config::CheckServerConfigPort(const std::string& value) {
Y
yudong.cai 已提交
375
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
376 377
        std::string msg = "Port " + value + " is not a number. " +
                          "Possible reason: server_config.port in server_config.yaml is invalid.";
378
        return Status(SERVER_INVALID_ARGUMENT, msg);
379
    } else {
Y
yudong.cai 已提交
380
        int32_t port = std::stoi(value);
381
        if (!(port > 1024 && port < 65535)) {
382 383
            std::string msg = "Port " + value + " is not in range [1025, 65534]. " +
                              "Possible reason: server_config.port in server_config.yaml is invalid.";
384
            return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
385 386
        }
    }
Y
yudong.cai 已提交
387 388
    return Status::OK();
}
Z
zhiru 已提交
389

Y
yudong.cai 已提交
390
Status
S
starlord 已提交
391 392
Config::CheckServerConfigDeployMode(const std::string& value) {
    if (value != "single" && value != "cluster_readonly" && value != "cluster_writable") {
Y
yudong.cai 已提交
393
        return Status(SERVER_INVALID_ARGUMENT,
394 395
                      "Error: server_config.deploy_mode in server_config.yaml is not one of "
                      "single, cluster_readonly, and cluster_writable.");
396
    }
Y
yudong.cai 已提交
397 398
    return Status::OK();
}
399

Y
yudong.cai 已提交
400
Status
S
starlord 已提交
401
Config::CheckServerConfigTimeZone(const std::string& value) {
Y
yudong.cai 已提交
402 403
    if (value.length() <= 3) {
        return Status(SERVER_INVALID_ARGUMENT, "Invalid server config time_zone: " + value);
404
    } else {
Y
yudong.cai 已提交
405 406
        if (value.substr(0, 3) != "UTC") {
            return Status(SERVER_INVALID_ARGUMENT, "Invalid server config time_zone: " + value);
407 408
        } else {
            try {
Y
yudong.cai 已提交
409
                stoi(value.substr(3));
410
            } catch (...) {
Y
yudong.cai 已提交
411
                return Status(SERVER_INVALID_ARGUMENT, "Invalid server config time_zone: " + value);
412
            }
413 414
        }
    }
Y
yudong.cai 已提交
415
    return Status::OK();
Z
zhiru 已提交
416 417
}

S
starlord 已提交
418
Status
S
starlord 已提交
419
Config::CheckDBConfigPrimaryPath(const std::string& value) {
Y
yudong.cai 已提交
420
    if (value.empty()) {
421 422
        return Status(SERVER_INVALID_ARGUMENT,
                      "db_path is empty. Possible reason: db_config.db_path in server_config.yaml is empty.");
Z
zhiru 已提交
423
    }
Y
yudong.cai 已提交
424 425
    return Status::OK();
}
Z
zhiru 已提交
426

Y
yudong.cai 已提交
427
Status
S
starlord 已提交
428
Config::CheckDBConfigSecondaryPath(const std::string& value) {
Y
yudong.cai 已提交
429 430 431
    return Status::OK();
}

Y
yudong.cai 已提交
432
Status
S
starlord 已提交
433
Config::CheckDBConfigBackendUrl(const std::string& value) {
Y
yudong.cai 已提交
434
    if (!ValidationUtil::ValidateDbURI(value).ok()) {
435
        std::string msg =
436 437 438
            "Invalid db_backend_url: " + value +
            ". Possible reason: db_config.db_backend_url is invalid in server_config.yaml. " +
            "The correct format should be like sqlite://:@:/ or mysql://root:123456@127.0.0.1:3306/milvus.";
439
        return Status(SERVER_INVALID_ARGUMENT, "invalid db_backend_url: " + value);
Z
zhiru 已提交
440
    }
Y
yudong.cai 已提交
441 442
    return Status::OK();
}
Z
zhiru 已提交
443

Y
yudong.cai 已提交
444
Status
S
starlord 已提交
445
Config::CheckDBConfigArchiveDiskThreshold(const std::string& value) {
Y
yudong.cai 已提交
446
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
447 448
        std::string msg = "Invalid archive disk threshold: " + value +
                          "Possible reason: db_config.archive_disk_threshold in server_config.yaml is invalid.";
449
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
450
    }
Y
yudong.cai 已提交
451 452
    return Status::OK();
}
Z
zhiru 已提交
453

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

Y
yudong.cai 已提交
464
Status
S
starlord 已提交
465
Config::CheckDBConfigInsertBufferSize(const std::string& value) {
Y
yudong.cai 已提交
466
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
467 468 469
        std::string msg = "Invalid insert buffer size: " + value +
                          "Possible reason: db_config.insert_buffer_size in server_config.yaml "
                          "is not a positive integer.";
470
        return Status(SERVER_INVALID_ARGUMENT, msg);
471
    } else {
Y
yudong.cai 已提交
472
        int64_t buffer_size = std::stoi(value) * GB;
473 474 475 476 477 478 479
        if (buffer_size <= 0) {
            std::string msg = "Invalid insert buffer size: " + value +
                              "Possible reason: db_config.insert_buffer_size in server_config.yaml "
                              "is not a positive integer.";
            return Status(SERVER_INVALID_ARGUMENT, msg);
        }

S
starlord 已提交
480
        uint64_t total_mem = 0, free_mem = 0;
Z
zhiru 已提交
481
        CommonUtil::GetSystemMemInfo(total_mem, free_mem);
Y
yudong.cai 已提交
482
        if (buffer_size >= total_mem) {
483 484
            std::string msg =
                "Invalid insert buffer size: " + value + "Possible reason: insert buffer size exceeds system memory.";
485
            return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
486 487
        }
    }
Y
yudong.cai 已提交
488 489
    return Status::OK();
}
Z
zhiru 已提交
490

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

Y
yudong.cai 已提交
501
Status
S
starlord 已提交
502
Config::CheckMetricConfigCollector(const std::string& value) {
Y
yudong.cai 已提交
503
    if (value != "prometheus") {
504
        std::string msg = "Invalid metric config: " + value + "Possible reason: metric_config.collector is invalid.";
505
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
506 507 508 509
    }
    return Status::OK();
}

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

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

S
starlord 已提交
534
        uint64_t total_mem = 0, free_mem = 0;
Z
zhiru 已提交
535
        CommonUtil::GetSystemMemInfo(total_mem, free_mem);
536 537 538
        if (static_cast<uint64_t>(cpu_cache_capacity) >= total_mem) {
            std::string msg = "Invalid cpu cache capacity: " + value +
                              "Possible reason: Cache config cpu_cache_capacity exceeds system memory.";
539
            return Status(SERVER_INVALID_ARGUMENT, msg);
540
        } else if (static_cast<double>(cpu_cache_capacity) > static_cast<double>(total_mem * 0.9)) {
541
            std::cerr << "WARNING: cpu cache capacity value is too big" << std::endl;
Z
zhiru 已提交
542
        }
543

Y
yudong.cai 已提交
544 545
        int32_t buffer_value;
        Status s = GetDBConfigInsertBufferSize(buffer_value);
S
starlord 已提交
546 547 548 549
        if (!s.ok()) {
            return s;
        }

Y
yudong.cai 已提交
550
        int64_t insert_buffer_size = buffer_value * GB;
Y
yudong.cai 已提交
551
        if (insert_buffer_size + cpu_cache_capacity >= total_mem) {
552 553 554
            std::string msg = "Invalid cpu cache capacity: " + value +
                              "Possible reason: sum of cache_config.cpu_cache_capacity and "
                              "db_config.insert_buffer_size exceeds system memory.";
555
            return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
556 557
        }
    }
Y
yudong.cai 已提交
558 559
    return Status::OK();
}
Z
zhiru 已提交
560

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

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

Z
zhiru 已提交
592
        size_t gpu_memory;
S
starlord 已提交
593
        if (!ValidationUtil::GetGpuMemory(gpu_index, gpu_memory).ok()) {
594 595
            std::string msg = "Fail to get GPU memory for GPU device: " + std::to_string(gpu_index);
            return Status(SERVER_UNEXPECTED_ERROR, msg);
596
        } else if (gpu_cache_capacity >= gpu_memory) {
597 598
            std::string msg = "Invalid gpu cache capacity: " + value +
                              "Possible reason: cache_config.gpu_cache_capacity exceeds GPU memory.";
599
            return Status(SERVER_INVALID_ARGUMENT, msg);
S
starlord 已提交
600
        } else if (gpu_cache_capacity > (double)gpu_memory * 0.9) {
601
            std::cerr << "Warning: gpu cache capacity value is too big" << std::endl;
Z
zhiru 已提交
602 603
        }
    }
Y
yudong.cai 已提交
604 605
    return Status::OK();
}
Z
zhiru 已提交
606

Y
yudong.cai 已提交
607
Status
S
starlord 已提交
608
Config::CheckCacheConfigGpuCacheThreshold(const std::string& value) {
Y
yudong.cai 已提交
609
    if (!ValidationUtil::ValidateStringIsFloat(value).ok()) {
610 611
        std::string msg = "Invalid gpu cache threshold: " + value +
                          "Possible reason: cache_config.gpu_cache_threshold is not in range (0.0, 1.0].";
612
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
613
    } else {
Y
yudong.cai 已提交
614 615
        float gpu_cache_threshold = std::stof(value);
        if (gpu_cache_threshold <= 0.0 || gpu_cache_threshold >= 1.0) {
616 617
            std::string msg = "Invalid gpu cache threshold: " + value +
                              "Possible reason: cache_config.gpu_cache_threshold is not in range (0.0, 1.0].";
618
            return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
619
        }
Z
zhiru 已提交
620
    }
Y
yudong.cai 已提交
621 622
    return Status::OK();
}
Z
zhiru 已提交
623

Y
yudong.cai 已提交
624
Status
S
starlord 已提交
625
Config::CheckCacheConfigCacheInsertData(const std::string& value) {
Y
yudong.cai 已提交
626
    if (!ValidationUtil::ValidateStringIsBool(value).ok()) {
627 628
        std::string msg = "Invalid cache insert option: " + value +
                          "Possible reason: cache_config.cache_insert_data is not a boolean.";
629
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
630 631
    }
    return Status::OK();
Z
zhiru 已提交
632 633
}

S
starlord 已提交
634
Status
S
starlord 已提交
635
Config::CheckEngineConfigUseBlasThreshold(const std::string& value) {
Y
yudong.cai 已提交
636
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
637 638
        std::string msg = "Invalid blas threshold: " + value +
                          "Possible reason: engine_config.use_blas_threshold is not a positive integer.";
639
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
640
    }
Y
yudong.cai 已提交
641 642
    return Status::OK();
}
Z
zhiru 已提交
643

Y
yudong.cai 已提交
644
Status
S
starlord 已提交
645
Config::CheckEngineConfigOmpThreadNum(const std::string& value) {
Y
yudong.cai 已提交
646
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
647 648
        std::string msg = "Invalid omp thread number: " + value +
                          "Possible reason: engine_config.omp_thread_num is not a positive integer.";
649
        return Status(SERVER_INVALID_ARGUMENT, msg);
S
starlord 已提交
650 651 652 653 654 655
    }

    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)) {
656 657
        std::string msg = "Invalid omp thread number: " + value +
                          "Possible reason: engine_config.omp_thread_num exceeds system cpu cores.";
658
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
659
    }
Y
yudong.cai 已提交
660
    return Status::OK();
Z
zhiru 已提交
661 662
}

S
starlord 已提交
663
Status
S
starlord 已提交
664
Config::CheckResourceConfigMode(const std::string& value) {
Y
yudong.cai 已提交
665
    if (value != "simple") {
666
        std::string msg = "Invalid resource mode: " + value + "Possible reason: resource_config.mode is invalid.";
667
        return Status(SERVER_INVALID_ARGUMENT, msg);
W
wxyu 已提交
668
    }
Y
yudong.cai 已提交
669 670
    return Status::OK();
}
671

Y
yudong.cai 已提交
672
Status
673 674 675 676
CheckGpuDevice(const std::string& value) {
    const std::regex pat("gpu(\\d+)");
    std::cmatch m;
    if (!std::regex_match(value.c_str(), m, pat)) {
677 678
        std::string msg = "Invalid gpu device: " + value +
                          "Possible reason: resource_config.search_resources does not match your hardware.";
679
        return Status(SERVER_INVALID_ARGUMENT, msg);
680 681 682 683
    }

    int32_t gpu_index = std::stoi(value.substr(3));
    if (!ValidationUtil::ValidateGpuIndex(gpu_index).ok()) {
684 685
        std::string msg = "Invalid gpu device: " + value +
                          "Possible reason: resource_config.search_resources does not match your hardware.";
686
        return Status(SERVER_INVALID_ARGUMENT, msg);
687 688 689 690 691 692
    }
    return Status::OK();
}

Status
Config::CheckResourceConfigSearchResources(const std::vector<std::string>& value) {
Y
yudong.cai 已提交
693
    if (value.empty()) {
694 695 696
        std::string msg =
            "Invalid search resource. "
            "Possible reason: resource_config.search_resources is empty.";
697
        return Status(SERVER_INVALID_ARGUMENT, msg);
698 699 700 701
    }

    for (auto& gpu_device : value) {
        if (!CheckGpuDevice(gpu_device).ok()) {
702 703 704
            std::string msg = "Invalid search resource: " + gpu_device +
                              "Possible reason: resource_config.search_resources does not match your hardware.";
            return Status(SERVER_INVALID_ARGUMENT, msg);
705 706 707 708 709 710 711 712
        }
    }
    return Status::OK();
}

Status
Config::CheckResourceConfigIndexBuildDevice(const std::string& value) {
    if (!CheckGpuDevice(value).ok()) {
713 714
        std::string msg = "Invalid index build device: " + value +
                          "Possible reason: resource_config.index_build_device does not match your hardware.";
715
        return Status(SERVER_INVALID_ARGUMENT, msg);
G
groot 已提交
716
    }
Y
yudong.cai 已提交
717
    return Status::OK();
G
groot 已提交
718 719
}

Y
yudong.cai 已提交
720
////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
721 722
ConfigNode&
Config::GetConfigNode(const std::string& name) {
723
    ConfigMgr* mgr = YamlConfigMgr::GetInstance();
S
starlord 已提交
724
    ConfigNode& root_node = mgr->GetRootNode();
G
groot 已提交
725
    return root_node.GetChild(name);
G
groot 已提交
726 727
}

Y
yudong.cai 已提交
728
Status
S
starlord 已提交
729
Config::GetConfigValueInMem(const std::string& parent_key, const std::string& child_key, std::string& value) {
Y
yudong.cai 已提交
730
    std::lock_guard<std::mutex> lock(mutex_);
Y
yudong.cai 已提交
731 732 733 734 735
    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 已提交
736
    return Status(SERVER_UNEXPECTED_ERROR, "key not exist");
Y
yudong.cai 已提交
737 738
}

Y
yudong.cai 已提交
739
void
S
starlord 已提交
740
Config::SetConfigValueInMem(const std::string& parent_key, const std::string& child_key, const std::string& value) {
Y
yudong.cai 已提交
741 742 743 744 745
    std::lock_guard<std::mutex> lock(mutex_);
    config_map_[parent_key][child_key] = value;
}

////////////////////////////////////////////////////////////////////////////////
Y
yudong.cai 已提交
746
std::string
S
starlord 已提交
747
Config::GetConfigStr(const std::string& parent_key, const std::string& child_key, const std::string& default_value) {
Y
yudong.cai 已提交
748
    std::string value;
S
starlord 已提交
749 750 751
    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 已提交
752
    }
Y
yudong.cai 已提交
753
    return value;
Y
yudong.cai 已提交
754 755
}

756
Status
S
starlord 已提交
757
Config::GetServerConfigAddress(std::string& value) {
S
starlord 已提交
758
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_ADDRESS, CONFIG_SERVER_ADDRESS_DEFAULT);
Y
yudong.cai 已提交
759
    return CheckServerConfigAddress(value);
760 761 762
}

Status
S
starlord 已提交
763
Config::GetServerConfigPort(std::string& value) {
S
starlord 已提交
764
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_PORT, CONFIG_SERVER_PORT_DEFAULT);
Y
yudong.cai 已提交
765
    return CheckServerConfigPort(value);
766 767 768
}

Status
S
starlord 已提交
769
Config::GetServerConfigDeployMode(std::string& value) {
S
starlord 已提交
770
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, CONFIG_SERVER_DEPLOY_MODE_DEFAULT);
Y
yudong.cai 已提交
771
    return CheckServerConfigDeployMode(value);
772 773 774
}

Status
S
starlord 已提交
775
Config::GetServerConfigTimeZone(std::string& value) {
S
starlord 已提交
776
    value = GetConfigStr(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, CONFIG_SERVER_TIME_ZONE_DEFAULT);
Y
yudong.cai 已提交
777
    return CheckServerConfigTimeZone(value);
778 779 780
}

Status
S
starlord 已提交
781
Config::GetDBConfigPrimaryPath(std::string& value) {
S
starlord 已提交
782
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_PRIMARY_PATH, CONFIG_DB_PRIMARY_PATH_DEFAULT);
Y
yudong.cai 已提交
783
    return CheckDBConfigPrimaryPath(value);
784 785 786
}

Status
S
starlord 已提交
787
Config::GetDBConfigSecondaryPath(std::string& value) {
S
starlord 已提交
788
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_SECONDARY_PATH, CONFIG_DB_SECONDARY_PATH_DEFAULT);
Y
yudong.cai 已提交
789
    return Status::OK();
790 791 792
}

Status
S
starlord 已提交
793
Config::GetDBConfigBackendUrl(std::string& value) {
S
starlord 已提交
794
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_BACKEND_URL, CONFIG_DB_BACKEND_URL_DEFAULT);
Y
yudong.cai 已提交
795
    return CheckDBConfigBackendUrl(value);
796 797 798
}

Status
S
starlord 已提交
799
Config::GetDBConfigArchiveDiskThreshold(int32_t& value) {
S
starlord 已提交
800 801
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DISK_THRESHOLD, CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
802
    Status s = CheckDBConfigArchiveDiskThreshold(str);
S
starlord 已提交
803 804 805 806
    if (!s.ok()) {
        return s;
    }

807 808 809 810 811
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
812
Config::GetDBConfigArchiveDaysThreshold(int32_t& value) {
S
starlord 已提交
813 814
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
815
    Status s = CheckDBConfigArchiveDaysThreshold(str);
S
starlord 已提交
816 817 818 819
    if (!s.ok()) {
        return s;
    }

820 821 822 823 824
    value = std::stoi(str);
    return Status::OK();
}

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

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

S
starlord 已提交
836 837
Status
Config::GetDBConfigPreloadTable(std::string& value) {
S
starlord 已提交
838
    value = GetConfigStr(CONFIG_DB, CONFIG_DB_PRELOAD_TABLE);
S
starlord 已提交
839 840 841
    return Status::OK();
}

842
Status
S
starlord 已提交
843
Config::GetMetricConfigEnableMonitor(bool& value) {
844
    std::string str = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_ENABLE_MONITOR, CONFIG_METRIC_ENABLE_MONITOR_DEFAULT);
Y
yudong.cai 已提交
845
    Status s = CheckMetricConfigEnableMonitor(str);
S
starlord 已提交
846 847 848 849
    if (!s.ok()) {
        return s;
    }

850 851 852 853 854 855
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

Status
S
starlord 已提交
856
Config::GetMetricConfigCollector(std::string& value) {
S
starlord 已提交
857
    value = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_COLLECTOR, CONFIG_METRIC_COLLECTOR_DEFAULT);
Y
yudong.cai 已提交
858
    return Status::OK();
859 860 861
}

Status
S
starlord 已提交
862
Config::GetMetricConfigPrometheusPort(std::string& value) {
S
starlord 已提交
863
    value = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_PROMETHEUS_PORT, CONFIG_METRIC_PROMETHEUS_PORT_DEFAULT);
Y
yudong.cai 已提交
864
    return CheckMetricConfigPrometheusPort(value);
865 866 867
}

Status
W
wxyu 已提交
868
Config::GetCacheConfigCpuCacheCapacity(int64_t& value) {
S
starlord 已提交
869 870
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_CAPACITY, CONFIG_CACHE_CPU_CACHE_CAPACITY_DEFAULT);
Y
yudong.cai 已提交
871
    Status s = CheckCacheConfigCpuCacheCapacity(str);
S
starlord 已提交
872 873 874 875
    if (!s.ok()) {
        return s;
    }

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

Status
S
starlord 已提交
881
Config::GetCacheConfigCpuCacheThreshold(float& value) {
S
starlord 已提交
882 883
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_THRESHOLD, CONFIG_CACHE_CPU_CACHE_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
884
    Status s = CheckCacheConfigCpuCacheThreshold(str);
S
starlord 已提交
885 886 887 888
    if (!s.ok()) {
        return s;
    }

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

Status
W
wxyu 已提交
894
Config::GetCacheConfigGpuCacheCapacity(int64_t& value) {
S
starlord 已提交
895 896
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_CAPACITY, CONFIG_CACHE_GPU_CACHE_CAPACITY_DEFAULT);
Y
yudong.cai 已提交
897
    Status s = CheckCacheConfigGpuCacheCapacity(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::GetCacheConfigGpuCacheThreshold(float& value) {
S
starlord 已提交
908 909
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_GPU_CACHE_THRESHOLD, CONFIG_CACHE_GPU_CACHE_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
910
    Status s = CheckCacheConfigGpuCacheThreshold(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
S
starlord 已提交
920
Config::GetCacheConfigCacheInsertData(bool& value) {
S
starlord 已提交
921 922
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CACHE_INSERT_DATA, CONFIG_CACHE_CACHE_INSERT_DATA_DEFAULT);
Y
yudong.cai 已提交
923
    Status s = CheckCacheConfigCacheInsertData(str);
S
starlord 已提交
924 925 926 927
    if (!s.ok()) {
        return s;
    }

928 929 930 931 932 933
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

Status
S
starlord 已提交
934
Config::GetEngineConfigUseBlasThreshold(int32_t& value) {
S
starlord 已提交
935 936
    std::string str =
        GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_USE_BLAS_THRESHOLD, CONFIG_ENGINE_USE_BLAS_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
937
    Status s = CheckEngineConfigUseBlasThreshold(str);
S
starlord 已提交
938 939 940 941
    if (!s.ok()) {
        return s;
    }

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

Status
S
starlord 已提交
947
Config::GetEngineConfigOmpThreadNum(int32_t& value) {
948
    std::string str = GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_OMP_THREAD_NUM, CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT);
Y
yudong.cai 已提交
949
    Status s = CheckEngineConfigOmpThreadNum(str);
S
starlord 已提交
950 951 952 953
    if (!s.ok()) {
        return s;
    }

954 955 956 957 958
    value = std::stoi(str);
    return Status::OK();
}

Status
S
starlord 已提交
959
Config::GetResourceConfigMode(std::string& value) {
S
starlord 已提交
960
    value = GetConfigStr(CONFIG_RESOURCE, CONFIG_RESOURCE_MODE, CONFIG_RESOURCE_MODE_DEFAULT);
Y
yudong.cai 已提交
961
    return CheckResourceConfigMode(value);
962 963 964
}

Status
965
Config::GetResourceConfigSearchResources(std::vector<std::string>& value) {
966
    ConfigNode resource_config = GetConfigNode(CONFIG_RESOURCE);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
    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;
    }

    value = std::stoi(str.substr(3));
    return Status::OK();
Y
yudong.cai 已提交
982
}
G
groot 已提交
983

Y
yudong.cai 已提交
984 985 986
///////////////////////////////////////////////////////////////////////////////
/* server config */
Status
S
starlord 已提交
987
Config::SetServerConfigAddress(const std::string& value) {
Y
yudong.cai 已提交
988
    Status s = CheckServerConfigAddress(value);
S
starlord 已提交
989 990 991 992
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
993 994 995 996 997
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_ADDRESS, value);
    return Status::OK();
}

Status
S
starlord 已提交
998
Config::SetServerConfigPort(const std::string& value) {
Y
yudong.cai 已提交
999
    Status s = CheckServerConfigPort(value);
S
starlord 已提交
1000 1001 1002 1003
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1004 1005 1006 1007 1008
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_PORT, value);
    return Status::OK();
}

Status
S
starlord 已提交
1009
Config::SetServerConfigDeployMode(const std::string& value) {
Y
yudong.cai 已提交
1010
    Status s = CheckServerConfigDeployMode(value);
S
starlord 已提交
1011 1012 1013 1014
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1015
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, value);
Y
yudong.cai 已提交
1016 1017 1018 1019
    return Status::OK();
}

Status
S
starlord 已提交
1020
Config::SetServerConfigTimeZone(const std::string& value) {
Y
yudong.cai 已提交
1021
    Status s = CheckServerConfigTimeZone(value);
S
starlord 已提交
1022 1023 1024 1025
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1026 1027 1028 1029 1030 1031
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, value);
    return Status::OK();
}

/* db config */
Status
S
starlord 已提交
1032
Config::SetDBConfigPrimaryPath(const std::string& value) {
Y
yudong.cai 已提交
1033
    Status s = CheckDBConfigPrimaryPath(value);
S
starlord 已提交
1034 1035 1036 1037
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
1038
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_PRIMARY_PATH, value);
Y
yudong.cai 已提交
1039 1040 1041 1042
    return Status::OK();
}

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

Y
yudong.cai 已提交
1049
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_SECONDARY_PATH, value);
Y
yudong.cai 已提交
1050 1051 1052 1053
    return Status::OK();
}

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

Y
yudong.cai 已提交
1060 1061 1062 1063 1064
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_BACKEND_URL, value);
    return Status::OK();
}

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

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

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

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

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

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

/* metric config */
Status
S
starlord 已提交
1099
Config::SetMetricConfigEnableMonitor(const std::string& value) {
Y
yudong.cai 已提交
1100
    Status s = CheckMetricConfigEnableMonitor(value);
S
starlord 已提交
1101 1102 1103 1104
    if (!s.ok()) {
        return s;
    }

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

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

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

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

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

/* cache config */
Status
S
starlord 已提交
1133
Config::SetCacheConfigCpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
1134
    Status s = CheckCacheConfigCpuCacheCapacity(value);
S
starlord 已提交
1135 1136 1137 1138
    if (!s.ok()) {
        return s;
    }

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

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

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

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

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

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

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

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

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

/* engine config */
Status
S
starlord 已提交
1189
Config::SetEngineConfigUseBlasThreshold(const std::string& value) {
Y
yudong.cai 已提交
1190
    Status s = CheckEngineConfigUseBlasThreshold(value);
S
starlord 已提交
1191 1192 1193 1194
    if (!s.ok()) {
        return s;
    }

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

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

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

/* resource config */
Status
S
starlord 已提交
1212
Config::SetResourceConfigMode(const std::string& value) {
Y
yudong.cai 已提交
1213
    Status s = CheckResourceConfigMode(value);
S
starlord 已提交
1214 1215 1216 1217
    if (!s.ok()) {
        return s;
    }

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

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
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 已提交
1233 1234
}  // namespace server
}  // namespace milvus