Config.cpp 42.5 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"
Z
Zhiru Zhu 已提交
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

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

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

Y
yudong.cai 已提交
128
    int64_t db_insert_buffer_size;
Y
yudong.cai 已提交
129
    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 166 167

    bool cache_insert_data;
    s = GetCacheConfigCacheInsertData(cache_insert_data);
S
starlord 已提交
168 169 170
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
171 172

    /* engine config */
Y
yudong.cai 已提交
173
    int64_t engine_use_blas_threshold;
Y
yudong.cai 已提交
174
    s = GetEngineConfigUseBlasThreshold(engine_use_blas_threshold);
S
starlord 已提交
175 176 177
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
178

Y
yudong.cai 已提交
179
    int64_t engine_omp_thread_num;
Y
yudong.cai 已提交
180
    s = GetEngineConfigOmpThreadNum(engine_omp_thread_num);
S
starlord 已提交
181 182 183
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
184

Y
yudong.cai 已提交
185
    int64_t engine_gpu_search_threshold;
186
    s = GetEngineConfigGpuSearchThreshold(engine_gpu_search_threshold);
W
wxyu 已提交
187 188 189 190
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
191 192
    /* gpu resource config */
#ifdef MILVUS_GPU_VERSION
193 194
    bool gpu_resource_enable;
    s = GetGpuResourceConfigEnable(gpu_resource_enable);
Y
yudong.cai 已提交
195 196 197 198 199 200
    if (!s.ok()) {
        return s;
    }

    int64_t resource_cache_capacity;
    s = GetGpuResourceConfigCacheCapacity(resource_cache_capacity);
S
starlord 已提交
201 202 203
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
204

Y
yudong.cai 已提交
205 206
    float resource_cache_threshold;
    s = GetGpuResourceConfigCacheThreshold(resource_cache_threshold);
S
starlord 已提交
207 208 209
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
210

Y
yudong.cai 已提交
211
    std::vector<int64_t> search_resources;
Y
yudong.cai 已提交
212
    s = GetGpuResourceConfigSearchResources(search_resources);
213 214 215 216
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
217
    std::vector<int64_t> index_build_resources;
Y
yudong.cai 已提交
218
    s = GetGpuResourceConfigBuildIndexResources(index_build_resources);
S
starlord 已提交
219 220 221
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
222
#endif
Y
yudong.cai 已提交
223 224 225 226

    return Status::OK();
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Y
yudong.cai 已提交
326
    s = SetEngineConfigGpuSearchThreshold(CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT);
S
starlord 已提交
327 328 329
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
330

Y
yudong.cai 已提交
331 332
    /* gpu resource config */
#ifdef MILVUS_GPU_VERSION
333
    s = SetGpuResourceConfigEnable(CONFIG_GPU_RESOURCE_ENABLE_DEFAULT);
S
starlord 已提交
334 335 336
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
337

Y
yudong.cai 已提交
338
    s = SetGpuResourceConfigCacheCapacity(CONFIG_GPU_RESOURCE_CACHE_CAPACITY_DEFAULT);
W
wxyu 已提交
339 340 341 342
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
343
    s = SetGpuResourceConfigCacheThreshold(CONFIG_GPU_RESOURCE_CACHE_THRESHOLD_DEFAULT);
S
starlord 已提交
344 345 346
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
347

Y
yudong.cai 已提交
348
    s = SetGpuResourceConfigSearchResources(CONFIG_GPU_RESOURCE_SEARCH_RESOURCES_DEFAULT);
Z
Zhiru Zhu 已提交
349 350 351 352
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
353
    s = SetGpuResourceConfigBuildIndexResources(CONFIG_GPU_RESOURCE_BUILD_INDEX_RESOURCES_DEFAULT);
354 355 356
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
357
#endif
358

Y
yudong.cai 已提交
359 360 361
    return Status::OK();
}

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

Y
yudong.cai 已提交
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);
Y
yudong.cai 已提交
380
    PrintConfigSection(CONFIG_GPU_RESOURCE);
Y
yudong.cai 已提交
381 382 383
}

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

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

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

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

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

Y
yudong.cai 已提交
445
Status
S
starlord 已提交
446
Config::CheckDBConfigSecondaryPath(const std::string& value) {
Y
yudong.cai 已提交
447 448 449
    return Status::OK();
}

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

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

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

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

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

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

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

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

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

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

Y
yudong.cai 已提交
560
        int64_t buffer_value;
Y
yudong.cai 已提交
561
        Status s = GetDBConfigInsertBufferSize(buffer_value);
S
starlord 已提交
562 563 564 565
        if (!s.ok()) {
            return s;
        }

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

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

Y
yudong.cai 已提交
594
Status
S
starlord 已提交
595
Config::CheckCacheConfigCacheInsertData(const std::string& value) {
Y
yudong.cai 已提交
596
    if (!ValidationUtil::ValidateStringIsBool(value).ok()) {
597
        std::string msg = "Invalid cache insert data option: " + value +
S
starlord 已提交
598
                          ". Possible reason: cache_config.cache_insert_data is not a boolean.";
599
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
600 601
    }
    return Status::OK();
Z
zhiru 已提交
602 603
}

S
starlord 已提交
604
Status
S
starlord 已提交
605
Config::CheckEngineConfigUseBlasThreshold(const std::string& value) {
Y
yudong.cai 已提交
606
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
607
        std::string msg = "Invalid use blas threshold: " + value +
S
starlord 已提交
608
                          ". Possible reason: engine_config.use_blas_threshold is not a positive integer.";
609
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
610
    }
Y
yudong.cai 已提交
611 612
    return Status::OK();
}
Z
zhiru 已提交
613

Y
yudong.cai 已提交
614
Status
S
starlord 已提交
615
Config::CheckEngineConfigOmpThreadNum(const std::string& value) {
Y
yudong.cai 已提交
616
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
617
        std::string msg = "Invalid omp thread num: " + value +
S
starlord 已提交
618
                          ". Possible reason: engine_config.omp_thread_num is not a positive integer.";
619
        return Status(SERVER_INVALID_ARGUMENT, msg);
S
starlord 已提交
620 621
    }

Y
yudong.cai 已提交
622 623
    int64_t omp_thread = std::stoll(value);
    int64_t sys_thread_cnt = 8;
S
starlord 已提交
624
    CommonUtil::GetSystemAvailableThreads(sys_thread_cnt);
Y
yudong.cai 已提交
625
    if (omp_thread > sys_thread_cnt) {
626
        std::string msg = "Invalid omp thread num: " + value +
S
starlord 已提交
627
                          ". Possible reason: engine_config.omp_thread_num exceeds system cpu cores.";
628
        return Status(SERVER_INVALID_ARGUMENT, msg);
Z
zhiru 已提交
629
    }
Y
yudong.cai 已提交
630
    return Status::OK();
Z
zhiru 已提交
631 632
}

W
wxyu 已提交
633
Status
634
Config::CheckEngineConfigGpuSearchThreshold(const std::string& value) {
W
wxyu 已提交
635
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
636 637
        std::string msg = "Invalid gpu search threshold: " + value +
                          ". Possible reason: engine_config.gpu_search_threshold is not a positive integer.";
W
wxyu 已提交
638 639 640 641 642
        return Status(SERVER_INVALID_ARGUMENT, msg);
    }
    return Status::OK();
}

S
starlord 已提交
643
Status
644
Config::CheckGpuResourceConfigEnable(const std::string& value) {
Y
yudong.cai 已提交
645
    if (!ValidationUtil::ValidateStringIsBool(value).ok()) {
646 647
        std::string msg =
            "Invalid gpu resource config: " + value + ". Possible reason: gpu_resource_config.enable is not a boolean.";
648
        return Status(SERVER_INVALID_ARGUMENT, msg);
W
wxyu 已提交
649
    }
Y
yudong.cai 已提交
650 651
    return Status::OK();
}
652

Y
yudong.cai 已提交
653
Status
Y
yudong.cai 已提交
654 655 656 657
Config::CheckGpuResourceConfigCacheCapacity(const std::string& value) {
    if (!ValidationUtil::ValidateStringIsNumber(value).ok()) {
        std::string msg = "Invalid gpu cache capacity: " + value +
                          ". Possible reason: gpu_resource_config.cache_capacity is not a positive integer.";
658
        return Status(SERVER_INVALID_ARGUMENT, msg);
Y
yudong.cai 已提交
659
    } else {
Y
yudong.cai 已提交
660 661
        int64_t gpu_cache_capacity = std::stoll(value) * GB;
        std::vector<int64_t> gpu_ids;
Y
yudong.cai 已提交
662 663 664 665 666
        Status s = GetGpuResourceConfigBuildIndexResources(gpu_ids);
        if (!s.ok()) {
            return s;
        }

Y
yudong.cai 已提交
667
        for (int64_t gpu_id : gpu_ids) {
Y
yudong.cai 已提交
668 669 670 671 672 673 674 675 676 677 678 679
            size_t gpu_memory;
            if (!ValidationUtil::GetGpuMemory(gpu_id, gpu_memory).ok()) {
                std::string msg = "Fail to get GPU memory for GPU device: " + std::to_string(gpu_id);
                return Status(SERVER_UNEXPECTED_ERROR, msg);
            } else if (gpu_cache_capacity >= gpu_memory) {
                std::string msg = "Invalid gpu cache capacity: " + value +
                                  ". Possible reason: gpu_resource_config.cache_capacity exceeds GPU memory.";
                return Status(SERVER_INVALID_ARGUMENT, msg);
            } else if (gpu_cache_capacity > (double)gpu_memory * 0.9) {
                std::cerr << "Warning: gpu cache capacity value is too big" << std::endl;
            }
        }
W
wxyu 已提交
680
    }
Y
yudong.cai 已提交
681 682
    return Status::OK();
}
683

Y
yudong.cai 已提交
684
Status
Y
yudong.cai 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
Config::CheckGpuResourceConfigCacheThreshold(const std::string& value) {
    if (!ValidationUtil::ValidateStringIsFloat(value).ok()) {
        std::string msg = "Invalid gpu cache threshold: " + value +
                          ". Possible reason: gpu_resource_config.cache_threshold is not in range (0.0, 1.0].";
        return Status(SERVER_INVALID_ARGUMENT, msg);
    } else {
        float gpu_cache_threshold = std::stof(value);
        if (gpu_cache_threshold <= 0.0 || gpu_cache_threshold >= 1.0) {
            std::string msg = "Invalid gpu cache threshold: " + value +
                              ". Possible reason: gpu_resource_config.cache_threshold is not in range (0.0, 1.0].";
            return Status(SERVER_INVALID_ARGUMENT, msg);
        }
    }
    return Status::OK();
}

Status
CheckGpuResource(const std::string& value) {
Y
youny626 已提交
703 704
    std::string s = value;
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
Z
Zhiru Zhu 已提交
705

Y
yudong.cai 已提交
706
    const std::regex pat("gpu(\\d+)");
Z
Zhiru Zhu 已提交
707 708
    std::smatch m;
    if (!std::regex_match(s, m, pat)) {
Y
yudong.cai 已提交
709 710
        std::string msg = "Invalid gpu resource: " + value +
                          ". Possible reason: gpu_resource_config is not in the format of cpux or gpux";
711
        return Status(SERVER_INVALID_ARGUMENT, msg);
712 713
    }

Z
Zhiru Zhu 已提交
714 715 716
    if (s.compare(0, 3, "gpu") == 0) {
        int32_t gpu_index = std::stoi(s.substr(3));
        if (!ValidationUtil::ValidateGpuIndex(gpu_index).ok()) {
Y
yudong.cai 已提交
717 718
            std::string msg = "Invalid gpu resource: " + value +
                              ". Possible reason: gpu_resource_config does not match with the hardware.";
Z
Zhiru Zhu 已提交
719 720
            return Status(SERVER_INVALID_ARGUMENT, msg);
        }
721
    }
Z
Zhiru Zhu 已提交
722

723 724 725 726
    return Status::OK();
}

Status
Y
yudong.cai 已提交
727
Config::CheckGpuResourceConfigSearchResources(const std::vector<std::string>& value) {
Y
yudong.cai 已提交
728
    if (value.empty()) {
729
        std::string msg =
Y
yudong.cai 已提交
730 731
            "Invalid gpu search resource. "
            "Possible reason: gpu_resource_config.search_resources is empty.";
732
        return Status(SERVER_INVALID_ARGUMENT, msg);
733 734
    }

Z
Zhiru Zhu 已提交
735
    for (auto& resource : value) {
Y
yudong.cai 已提交
736
        auto status = CheckGpuResource(resource);
Z
Zhiru Zhu 已提交
737 738
        if (!status.ok()) {
            return Status(SERVER_INVALID_ARGUMENT, status.message());
739 740 741 742 743 744
        }
    }
    return Status::OK();
}

Status
Y
yudong.cai 已提交
745
Config::CheckGpuResourceConfigBuildIndexResources(const std::vector<std::string>& value) {
746 747
    if (value.empty()) {
        std::string msg =
Y
yudong.cai 已提交
748 749
            "Invalid gpu build index resource. "
            "Possible reason: gpu_resource_config.build_index_resources is empty.";
750 751 752 753
        return Status(SERVER_INVALID_ARGUMENT, msg);
    }

    for (auto& resource : value) {
Y
yudong.cai 已提交
754
        auto status = CheckGpuResource(resource);
755 756 757
        if (!status.ok()) {
            return Status(SERVER_INVALID_ARGUMENT, status.message());
        }
G
groot 已提交
758
    }
759

Y
yudong.cai 已提交
760
    return Status::OK();
G
groot 已提交
761 762
}

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

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

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

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

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

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

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

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

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

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

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

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

Status
Y
yudong.cai 已提交
858
Config::GetDBConfigArchiveDiskThreshold(int64_t& value) {
S
starlord 已提交
859 860
    std::string str =
        GetConfigStr(CONFIG_DB, CONFIG_DB_ARCHIVE_DISK_THRESHOLD, CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT);
Y
yudong.cai 已提交
861
    Status s = CheckDBConfigArchiveDiskThreshold(str);
S
starlord 已提交
862 863 864
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
865
    value = std::stoll(str);
866 867 868 869
    return Status::OK();
}

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

Status
Y
yudong.cai 已提交
882
Config::GetDBConfigInsertBufferSize(int64_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
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
888
    value = std::stoll(str);
889 890 891
    return Status::OK();
}

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

898
Status
S
starlord 已提交
899
Config::GetMetricConfigEnableMonitor(bool& value) {
900
    std::string str = GetConfigStr(CONFIG_METRIC, CONFIG_METRIC_ENABLE_MONITOR, CONFIG_METRIC_ENABLE_MONITOR_DEFAULT);
Y
yudong.cai 已提交
901
    Status s = CheckMetricConfigEnableMonitor(str);
S
starlord 已提交
902 903 904
    if (!s.ok()) {
        return s;
    }
905 906 907 908 909 910
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

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

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

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

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

Status
S
starlord 已提交
947
Config::GetCacheConfigCacheInsertData(bool& value) {
S
starlord 已提交
948 949
    std::string str =
        GetConfigStr(CONFIG_CACHE, CONFIG_CACHE_CACHE_INSERT_DATA, CONFIG_CACHE_CACHE_INSERT_DATA_DEFAULT);
Y
yudong.cai 已提交
950
    Status s = CheckCacheConfigCacheInsertData(str);
S
starlord 已提交
951 952 953
    if (!s.ok()) {
        return s;
    }
954 955 956 957 958 959
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
}

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

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

W
wxyu 已提交
982
Status
Y
yudong.cai 已提交
983
Config::GetEngineConfigGpuSearchThreshold(int64_t& value) {
W
wxyu 已提交
984
    std::string str =
985 986
        GetConfigStr(CONFIG_ENGINE, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD_DEFAULT);
    Status s = CheckEngineConfigGpuSearchThreshold(str);
W
wxyu 已提交
987 988 989
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
990
    value = std::stoll(str);
W
wxyu 已提交
991 992 993
    return Status::OK();
}

994
Status
995 996 997
Config::GetGpuResourceConfigEnable(bool& value) {
    std::string str = GetConfigStr(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_ENABLE, CONFIG_GPU_RESOURCE_ENABLE_DEFAULT);
    Status s = CheckGpuResourceConfigEnable(str);
Y
yudong.cai 已提交
998 999 1000 1001 1002 1003
    if (!s.ok()) {
        return s;
    }
    std::transform(str.begin(), str.end(), str.begin(), ::tolower);
    value = (str == "true" || str == "on" || str == "yes" || str == "1");
    return Status::OK();
1004 1005 1006
}

Status
Y
yudong.cai 已提交
1007
Config::GetGpuResourceConfigCacheCapacity(int64_t& value) {
1008 1009
    bool gpu_resource_enable = false;
    Status s = GetGpuResourceConfigEnable(gpu_resource_enable);
Y
yudong.cai 已提交
1010 1011 1012
    if (!s.ok()) {
        return s;
    }
1013 1014
    if (!gpu_resource_enable) {
        std::string msg = "GPU not supported. Possible reason: gpu_resource_config.enable is set to false.";
Y
yudong.cai 已提交
1015 1016 1017 1018 1019 1020 1021 1022
        return Status(SERVER_UNSUPPORTED_ERROR, msg);
    }
    std::string str = GetConfigStr(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_CACHE_CAPACITY,
                                   CONFIG_GPU_RESOURCE_CACHE_CAPACITY_DEFAULT);
    s = CheckGpuResourceConfigCacheCapacity(str);
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1023
    value = std::stoll(str);
W
wxyu 已提交
1024 1025 1026
    return Status::OK();
}

1027
Status
Y
yudong.cai 已提交
1028
Config::GetGpuResourceConfigCacheThreshold(float& value) {
1029 1030
    bool gpu_resource_enable = false;
    Status s = GetGpuResourceConfigEnable(gpu_resource_enable);
1031 1032 1033
    if (!s.ok()) {
        return s;
    }
1034 1035
    if (!gpu_resource_enable) {
        std::string msg = "GPU not supported. Possible reason: gpu_resource_config.enable is set to false.";
Y
yudong.cai 已提交
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
        return Status(SERVER_UNSUPPORTED_ERROR, msg);
    }
    std::string str = GetConfigStr(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_CACHE_THRESHOLD,
                                   CONFIG_GPU_RESOURCE_CACHE_THRESHOLD_DEFAULT);
    s = CheckGpuResourceConfigCacheThreshold(str);
    if (!s.ok()) {
        return s;
    }
    value = std::stof(str);
    return Status::OK();
1046 1047 1048
}

Status
Y
yudong.cai 已提交
1049
Config::GetGpuResourceConfigSearchResources(std::vector<int64_t>& value) {
1050 1051
    bool gpu_resource_enable = false;
    Status s = GetGpuResourceConfigEnable(gpu_resource_enable);
Y
yudong.cai 已提交
1052 1053 1054
    if (!s.ok()) {
        return s;
    }
1055 1056
    if (!gpu_resource_enable) {
        std::string msg = "GPU not supported. Possible reason: gpu_resource_config.enable is set to false.";
Y
yudong.cai 已提交
1057 1058 1059 1060 1061 1062 1063 1064 1065
        return Status(SERVER_UNSUPPORTED_ERROR, msg);
    }
    std::string str = GetConfigSequenceStr(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_SEARCH_RESOURCES,
                                           CONFIG_GPU_RESOURCE_DELIMITER, CONFIG_GPU_RESOURCE_SEARCH_RESOURCES_DEFAULT);
    std::vector<std::string> res_vec;
    server::StringHelpFunctions::SplitStringByDelimeter(str, CONFIG_GPU_RESOURCE_DELIMITER, res_vec);
    s = CheckGpuResourceConfigSearchResources(res_vec);
    if (!s.ok()) {
        return s;
Y
youny626 已提交
1066
    }
Y
yudong.cai 已提交
1067
    for (std::string& res : res_vec) {
Y
yudong.cai 已提交
1068
        value.push_back(std::stoll(res.substr(3)));
Y
yudong.cai 已提交
1069 1070
    }
    return Status::OK();
1071 1072 1073
}

Status
Y
yudong.cai 已提交
1074
Config::GetGpuResourceConfigBuildIndexResources(std::vector<int64_t>& value) {
1075 1076
    bool gpu_resource_enable = false;
    Status s = GetGpuResourceConfigEnable(gpu_resource_enable);
Y
yudong.cai 已提交
1077 1078 1079
    if (!s.ok()) {
        return s;
    }
1080 1081
    if (!gpu_resource_enable) {
        std::string msg = "GPU not supported. Possible reason: gpu_resource_config.enable is set to false.";
Y
yudong.cai 已提交
1082 1083
        return Status(SERVER_UNSUPPORTED_ERROR, msg);
    }
1084
    std::string str =
Y
yudong.cai 已提交
1085 1086 1087 1088 1089 1090 1091 1092 1093
        GetConfigSequenceStr(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_BUILD_INDEX_RESOURCES,
                             CONFIG_GPU_RESOURCE_DELIMITER, CONFIG_GPU_RESOURCE_BUILD_INDEX_RESOURCES_DEFAULT);
    std::vector<std::string> res_vec;
    server::StringHelpFunctions::SplitStringByDelimeter(str, CONFIG_GPU_RESOURCE_DELIMITER, res_vec);
    s = CheckGpuResourceConfigBuildIndexResources(res_vec);
    if (!s.ok()) {
        return s;
    }
    for (std::string& res : res_vec) {
Y
yudong.cai 已提交
1094
        value.push_back(std::stoll(res.substr(3)));
Y
yudong.cai 已提交
1095
    }
1096
    return Status::OK();
Y
yudong.cai 已提交
1097
}
G
groot 已提交
1098

Y
yudong.cai 已提交
1099 1100 1101
///////////////////////////////////////////////////////////////////////////////
/* server config */
Status
S
starlord 已提交
1102
Config::SetServerConfigAddress(const std::string& value) {
Y
yudong.cai 已提交
1103
    Status s = CheckServerConfigAddress(value);
S
starlord 已提交
1104 1105 1106
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1107 1108 1109 1110 1111
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_ADDRESS, value);
    return Status::OK();
}

Status
S
starlord 已提交
1112
Config::SetServerConfigPort(const std::string& value) {
Y
yudong.cai 已提交
1113
    Status s = CheckServerConfigPort(value);
S
starlord 已提交
1114 1115 1116
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1117 1118 1119 1120 1121
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_PORT, value);
    return Status::OK();
}

Status
S
starlord 已提交
1122
Config::SetServerConfigDeployMode(const std::string& value) {
Y
yudong.cai 已提交
1123
    Status s = CheckServerConfigDeployMode(value);
S
starlord 已提交
1124 1125 1126
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1127
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_DEPLOY_MODE, value);
Y
yudong.cai 已提交
1128 1129 1130 1131
    return Status::OK();
}

Status
S
starlord 已提交
1132
Config::SetServerConfigTimeZone(const std::string& value) {
Y
yudong.cai 已提交
1133
    Status s = CheckServerConfigTimeZone(value);
S
starlord 已提交
1134 1135 1136
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1137 1138 1139 1140 1141 1142
    SetConfigValueInMem(CONFIG_SERVER, CONFIG_SERVER_TIME_ZONE, value);
    return Status::OK();
}

/* db config */
Status
S
starlord 已提交
1143
Config::SetDBConfigPrimaryPath(const std::string& value) {
Y
yudong.cai 已提交
1144
    Status s = CheckDBConfigPrimaryPath(value);
S
starlord 已提交
1145 1146 1147
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1148
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_PRIMARY_PATH, value);
Y
yudong.cai 已提交
1149 1150 1151 1152
    return Status::OK();
}

Status
S
starlord 已提交
1153
Config::SetDBConfigSecondaryPath(const std::string& value) {
Y
yudong.cai 已提交
1154
    Status s = CheckDBConfigSecondaryPath(value);
S
starlord 已提交
1155 1156 1157
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1158
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_SECONDARY_PATH, value);
Y
yudong.cai 已提交
1159 1160 1161 1162
    return Status::OK();
}

Status
S
starlord 已提交
1163
Config::SetDBConfigBackendUrl(const std::string& value) {
Y
yudong.cai 已提交
1164
    Status s = CheckDBConfigBackendUrl(value);
S
starlord 已提交
1165 1166 1167
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1168 1169 1170 1171 1172
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_BACKEND_URL, value);
    return Status::OK();
}

Status
S
starlord 已提交
1173
Config::SetDBConfigArchiveDiskThreshold(const std::string& value) {
Y
yudong.cai 已提交
1174
    Status s = CheckDBConfigArchiveDiskThreshold(value);
S
starlord 已提交
1175 1176 1177
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1178 1179 1180 1181 1182
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_ARCHIVE_DISK_THRESHOLD, value);
    return Status::OK();
}

Status
S
starlord 已提交
1183
Config::SetDBConfigArchiveDaysThreshold(const std::string& value) {
Y
yudong.cai 已提交
1184
    Status s = CheckDBConfigArchiveDaysThreshold(value);
S
starlord 已提交
1185 1186 1187
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1188 1189 1190 1191 1192
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_ARCHIVE_DAYS_THRESHOLD, value);
    return Status::OK();
}

Status
S
starlord 已提交
1193
Config::SetDBConfigInsertBufferSize(const std::string& value) {
Y
yudong.cai 已提交
1194
    Status s = CheckDBConfigInsertBufferSize(value);
S
starlord 已提交
1195 1196 1197
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1198
    SetConfigValueInMem(CONFIG_DB, CONFIG_DB_INSERT_BUFFER_SIZE, value);
Y
yudong.cai 已提交
1199 1200 1201 1202 1203
    return Status::OK();
}

/* metric config */
Status
S
starlord 已提交
1204
Config::SetMetricConfigEnableMonitor(const std::string& value) {
Y
yudong.cai 已提交
1205
    Status s = CheckMetricConfigEnableMonitor(value);
S
starlord 已提交
1206 1207 1208
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1209
    SetConfigValueInMem(CONFIG_METRIC, CONFIG_METRIC_ENABLE_MONITOR, value);
Y
yudong.cai 已提交
1210 1211 1212 1213
    return Status::OK();
}

Status
S
starlord 已提交
1214
Config::SetMetricConfigCollector(const std::string& value) {
Y
yudong.cai 已提交
1215
    Status s = CheckMetricConfigCollector(value);
S
starlord 已提交
1216 1217 1218
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1219
    SetConfigValueInMem(CONFIG_METRIC, CONFIG_METRIC_COLLECTOR, value);
Y
yudong.cai 已提交
1220 1221 1222 1223
    return Status::OK();
}

Status
S
starlord 已提交
1224
Config::SetMetricConfigPrometheusPort(const std::string& value) {
Y
yudong.cai 已提交
1225
    Status s = CheckMetricConfigPrometheusPort(value);
S
starlord 已提交
1226 1227 1228
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1229
    SetConfigValueInMem(CONFIG_METRIC, CONFIG_METRIC_PROMETHEUS_PORT, value);
Y
yudong.cai 已提交
1230 1231 1232 1233 1234
    return Status::OK();
}

/* cache config */
Status
S
starlord 已提交
1235
Config::SetCacheConfigCpuCacheCapacity(const std::string& value) {
Y
yudong.cai 已提交
1236
    Status s = CheckCacheConfigCpuCacheCapacity(value);
S
starlord 已提交
1237 1238 1239
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1240
    SetConfigValueInMem(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_CAPACITY, value);
Y
yudong.cai 已提交
1241 1242 1243 1244
    return Status::OK();
}

Status
S
starlord 已提交
1245
Config::SetCacheConfigCpuCacheThreshold(const std::string& value) {
Y
yudong.cai 已提交
1246
    Status s = CheckCacheConfigCpuCacheThreshold(value);
S
starlord 已提交
1247 1248 1249
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1250
    SetConfigValueInMem(CONFIG_CACHE, CONFIG_CACHE_CPU_CACHE_THRESHOLD, value);
Y
yudong.cai 已提交
1251 1252 1253 1254
    return Status::OK();
}

Status
S
starlord 已提交
1255
Config::SetCacheConfigCacheInsertData(const std::string& value) {
Y
yudong.cai 已提交
1256
    Status s = CheckCacheConfigCacheInsertData(value);
S
starlord 已提交
1257 1258 1259
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
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
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1271
    SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_USE_BLAS_THRESHOLD, value);
Y
yudong.cai 已提交
1272 1273 1274 1275
    return Status::OK();
}

Status
S
starlord 已提交
1276
Config::SetEngineConfigOmpThreadNum(const std::string& value) {
Y
yudong.cai 已提交
1277
    Status s = CheckEngineConfigOmpThreadNum(value);
S
starlord 已提交
1278 1279 1280
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1281
    SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_OMP_THREAD_NUM, value);
Y
yudong.cai 已提交
1282 1283 1284
    return Status::OK();
}

W
wxyu 已提交
1285
Status
1286 1287
Config::SetEngineConfigGpuSearchThreshold(const std::string& value) {
    Status s = CheckEngineConfigGpuSearchThreshold(value);
W
wxyu 已提交
1288 1289 1290
    if (!s.ok()) {
        return s;
    }
Z
Zhiru Zhu 已提交
1291
    SetConfigValueInMem(CONFIG_ENGINE, CONFIG_ENGINE_GPU_SEARCH_THRESHOLD, value);
W
wxyu 已提交
1292 1293 1294
    return Status::OK();
}

Y
yudong.cai 已提交
1295
/* gpu resource config */
Y
yudong.cai 已提交
1296
Status
1297 1298
Config::SetGpuResourceConfigEnable(const std::string& value) {
    Status s = CheckGpuResourceConfigEnable(value);
S
starlord 已提交
1299 1300 1301
    if (!s.ok()) {
        return s;
    }
1302
    SetConfigValueInMem(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_ENABLE, value);
Z
Zhiru Zhu 已提交
1303 1304 1305 1306
    return Status::OK();
}

Status
Y
yudong.cai 已提交
1307 1308
Config::SetGpuResourceConfigCacheCapacity(const std::string& value) {
    Status s = CheckGpuResourceConfigCacheCapacity(value);
Z
Zhiru Zhu 已提交
1309 1310 1311
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1312 1313 1314
    SetConfigValueInMem(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_CACHE_CAPACITY, value);
    return Status::OK();
}
Z
Zhiru Zhu 已提交
1315

Y
yudong.cai 已提交
1316 1317 1318 1319 1320 1321 1322
Status
Config::SetGpuResourceConfigCacheThreshold(const std::string& value) {
    Status s = CheckGpuResourceConfigCacheThreshold(value);
    if (!s.ok()) {
        return s;
    }
    SetConfigValueInMem(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_CACHE_THRESHOLD, value);
Y
yudong.cai 已提交
1323 1324 1325
    return Status::OK();
}

1326
Status
Y
yudong.cai 已提交
1327
Config::SetGpuResourceConfigSearchResources(const std::string& value) {
1328
    std::vector<std::string> res_vec;
Y
yudong.cai 已提交
1329 1330
    server::StringHelpFunctions::SplitStringByDelimeter(value, CONFIG_GPU_RESOURCE_DELIMITER, res_vec);
    Status s = CheckGpuResourceConfigSearchResources(res_vec);
1331 1332 1333
    if (!s.ok()) {
        return s;
    }
Y
yudong.cai 已提交
1334
    SetConfigValueInMem(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_SEARCH_RESOURCES, value);
1335 1336 1337
    return Status::OK();
}

Y
yudong.cai 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346
Status
Config::SetGpuResourceConfigBuildIndexResources(const std::string& value) {
    std::vector<std::string> res_vec;
    server::StringHelpFunctions::SplitStringByDelimeter(value, CONFIG_GPU_RESOURCE_DELIMITER, res_vec);
    Status s = CheckGpuResourceConfigBuildIndexResources(res_vec);
    if (!s.ok()) {
        return s;
    }
    SetConfigValueInMem(CONFIG_GPU_RESOURCE, CONFIG_GPU_RESOURCE_BUILD_INDEX_RESOURCES, value);
1347
    return Status::OK();
1348
}  // namespace server
1349

S
starlord 已提交
1350 1351
}  // namespace server
}  // namespace milvus