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

G
groot 已提交
18 19 20

#include "DBWrapper.h"
#include "ServerConfig.h"
S
starlord 已提交
21
#include "db/DBFactory.h"
G
groot 已提交
22 23
#include "utils/CommonUtil.h"
#include "utils/Log.h"
S
starlord 已提交
24
#include "utils/StringHelpFunctions.h"
G
groot 已提交
25

26
#include <omp.h>
S
starlord 已提交
27
#include <faiss/utils.h>
28

G
groot 已提交
29 30 31 32 33
namespace zilliz {
namespace milvus {
namespace server {

DBWrapper::DBWrapper() {
S
starlord 已提交
34 35 36

}

S
starlord 已提交
37
Status DBWrapper::StartService() {
38
    //db config
S
starlord 已提交
39
    engine::DBOptions opt;
S
starlord 已提交
40
    ConfigNode& db_config = ServerConfig::GetInstance().GetConfig(CONFIG_DB);
41
    opt.meta.backend_uri = db_config.GetValue(CONFIG_DB_BACKEND_URL);
S
starlord 已提交
42
    std::string db_path = db_config.GetValue(CONFIG_DB_PATH);
G
groot 已提交
43
    opt.meta.path = db_path + "/db";
S
starlord 已提交
44

S
starlord 已提交
45
    std::string db_slave_path = db_config.GetValue(CONFIG_DB_SLAVE_PATH);
S
starlord 已提交
46 47
    StringHelpFunctions::SplitStringByDelimeter(db_slave_path, ";", opt.meta.slave_paths);

48
    // cache config
S
starlord 已提交
49
    ConfigNode& cache_config = ServerConfig::GetInstance().GetConfig(CONFIG_CACHE);
50 51
    opt.insert_cache_immediately_ =
        cache_config.GetBoolValue(CONFIG_CACHE_INSERT_IMMEDIATELY, std::stoi(CONFIG_CACHE_INSERT_IMMEDIATELY_DEFAULT));
S
starlord 已提交
52

Z
update  
zhiru 已提交
53
    ConfigNode& serverConfig = ServerConfig::GetInstance().GetConfig(CONFIG_SERVER);
54
    std::string mode = serverConfig.GetValue(CONFIG_SERVER_MODE, CONFIG_SERVER_MODE_DEFAULT);
Z
update  
zhiru 已提交
55
    if (mode == "single") {
S
starlord 已提交
56
        opt.mode = engine::DBOptions::MODE::SINGLE;
Z
update  
zhiru 已提交
57 58
    }
    else if (mode == "cluster") {
S
starlord 已提交
59
        opt.mode = engine::DBOptions::MODE::CLUSTER;
Z
update  
zhiru 已提交
60 61
    }
    else if (mode == "read_only") {
S
starlord 已提交
62
        opt.mode = engine::DBOptions::MODE::READ_ONLY;
Z
update  
zhiru 已提交
63 64
    }
    else {
S
starlord 已提交
65
        std::cerr << "ERROR: mode specified in server_config is not one of ['single', 'cluster', 'read_only']" << std::endl;
Z
update  
zhiru 已提交
66 67
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
68

69 70
    // engine config
    ConfigNode& engine_config = ServerConfig::GetInstance().GetConfig(CONFIG_ENGINE);
71 72
    int32_t omp_thread =
        engine_config.GetInt32Value(CONFIG_ENGINE_OMP_THREAD_NUM, std::stoi(CONFIG_ENGINE_OMP_THREAD_NUM_DEFAULT));
73 74 75
    if(omp_thread > 0) {
        omp_set_num_threads(omp_thread);
        SERVER_LOG_DEBUG << "Specify openmp thread number: " << omp_thread;
76 77 78 79 80 81
    } else {
        uint32_t sys_thread_cnt = 8;
        if(CommonUtil::GetSystemAvailableThreads(sys_thread_cnt)) {
            omp_thread = (int32_t)ceil(sys_thread_cnt*0.5);
            omp_set_num_threads(omp_thread);
        }
82
    }
83

84 85 86
    //init faiss global variable
    faiss::distance_compute_blas_threshold =
        engine_config.GetInt32Value(CONFIG_ENGINE_BLAS_THRESHOLD, std::stoi(CONFIG_ENGINE_BLAS_THRESHOLD_DEFAULT));
S
starlord 已提交
87

G
groot 已提交
88 89
    //set archive config
    engine::ArchiveConf::CriteriaT criterial;
90 91 92 93
    int64_t disk =
        db_config.GetInt64Value(CONFIG_DB_ARCHIVE_DISK_THRESHOLD, std::stoi(CONFIG_DB_ARCHIVE_DISK_THRESHOLD_DEFAULT));
    int64_t days =
        db_config.GetInt64Value(CONFIG_DB_ARCHIVE_DAYS_THRESHOLD, std::stoi(CONFIG_DB_ARCHIVE_DAYS_THRESHOLD_DEFAULT));
G
groot 已提交
94 95 96 97 98 99 100 101 102
    if(disk > 0) {
        criterial[engine::ARCHIVE_CONF_DISK] = disk;
    }
    if(days > 0) {
        criterial[engine::ARCHIVE_CONF_DAYS] = days;
    }
    opt.meta.archive_conf.SetCriterias(criterial);

    //create db root folder
S
starlord 已提交
103 104 105
    Status status = CommonUtil::CreateDirectory(opt.meta.path);
    if(!status.ok()) {
        std::cerr << "ERROR! Failed to create database root path: " << opt.meta.path << std::endl;
G
groot 已提交
106 107
        kill(0, SIGUSR1);
    }
G
groot 已提交
108

S
starlord 已提交
109
    for(auto& path : opt.meta.slave_paths) {
S
starlord 已提交
110 111 112
        status = CommonUtil::CreateDirectory(path);
        if(!status.ok()) {
            std::cerr << "ERROR! Failed to create database slave path: " << path << std::endl;
S
starlord 已提交
113 114 115 116
            kill(0, SIGUSR1);
        }
    }

117
    //create db instance
G
groot 已提交
118
    try {
S
starlord 已提交
119
        db_ = engine::DBFactory::Build(opt);
G
groot 已提交
120
    } catch(std::exception& ex) {
121
        std::cerr << "ERROR! Failed to open database: " << ex.what() << std::endl;
G
groot 已提交
122
        kill(0, SIGUSR1);
G
groot 已提交
123
    }
S
starlord 已提交
124 125 126

    db_->Start();

S
starlord 已提交
127
    return Status::OK();
G
groot 已提交
128 129
}

S
starlord 已提交
130
Status DBWrapper::StopService() {
S
starlord 已提交
131 132 133 134
    if(db_) {
        db_->Stop();
    }

S
starlord 已提交
135
    return Status::OK();
G
groot 已提交
136 137 138 139 140
}

}
}
}