DBWrapper.cpp 6.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.

S
starlord 已提交
18
#include <faiss/utils.h>
S
starlord 已提交
19
#include <omp.h>
J
JinHai-CN 已提交
20
#include <cmath>
S
starlord 已提交
21
#include <string>
22
#include <vector>
23

24 25 26 27 28 29 30
#include "db/DBFactory.h"
#include "server/Config.h"
#include "server/DBWrapper.h"
#include "utils/CommonUtil.h"
#include "utils/Log.h"
#include "utils/StringHelpFunctions.h"

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

S
starlord 已提交
34 35
Status
DBWrapper::StartService() {
36
    Config& config = Config::GetInstance();
37
    Status s;
38

S
starlord 已提交
39
    // db config
S
starlord 已提交
40
    engine::DBOptions opt;
41
    s = config.GetDBConfigBackendUrl(opt.meta_.backend_uri_);
S
starlord 已提交
42 43 44
    if (!s.ok()) {
        return s;
    }
45 46

    std::string path;
Y
yudong.cai 已提交
47
    s = config.GetDBConfigPrimaryPath(path);
S
starlord 已提交
48 49 50
    if (!s.ok()) {
        return s;
    }
51

52
    opt.meta_.path_ = path + "/db";
53 54

    std::string db_slave_path;
Y
yudong.cai 已提交
55
    s = config.GetDBConfigSecondaryPath(db_slave_path);
S
starlord 已提交
56
    if (!s.ok()) {
57 58
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
59
    }
60

S
starlord 已提交
61
    StringHelpFunctions::SplitStringByDelimeter(db_slave_path, ";", opt.meta_.slave_paths_);
S
starlord 已提交
62

63
    // cache config
64
    s = config.GetCacheConfigCacheInsertData(opt.insert_cache_immediately_);
S
starlord 已提交
65
    if (!s.ok()) {
66 67
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
68
    }
69 70

    std::string mode;
Y
yudong.cai 已提交
71
    s = config.GetServerConfigDeployMode(mode);
S
starlord 已提交
72
    if (!s.ok()) {
73 74
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
75
    }
S
starlord 已提交
76

Z
update  
zhiru 已提交
77
    if (mode == "single") {
S
starlord 已提交
78
        opt.mode_ = engine::DBOptions::MODE::SINGLE;
S
starlord 已提交
79
    } else if (mode == "cluster_readonly") {
Y
yudong.cai 已提交
80
        opt.mode_ = engine::DBOptions::MODE::CLUSTER_READONLY;
S
starlord 已提交
81
    } else if (mode == "cluster_writable") {
Y
yudong.cai 已提交
82
        opt.mode_ = engine::DBOptions::MODE::CLUSTER_WRITABLE;
S
starlord 已提交
83
    } else {
84 85
        std::cerr << "Error: server_config.deploy_mode in server_config.yaml is not one of "
                  << "single, cluster_readonly, and cluster_writable."
S
starlord 已提交
86
                  << std::endl;
Z
update  
zhiru 已提交
87 88
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
89

90
    // engine config
91 92
    int32_t omp_thread;
    s = config.GetEngineConfigOmpThreadNum(omp_thread);
S
starlord 已提交
93
    if (!s.ok()) {
94 95
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
96 97
    }

98
    if (omp_thread > 0) {
99 100
        omp_set_num_threads(omp_thread);
        SERVER_LOG_DEBUG << "Specify openmp thread number: " << omp_thread;
101 102
    } else {
        uint32_t sys_thread_cnt = 8;
S
starlord 已提交
103
        if (CommonUtil::GetSystemAvailableThreads(sys_thread_cnt)) {
S
starlord 已提交
104
            omp_thread = static_cast<int32_t>(ceil(sys_thread_cnt * 0.5));
105 106
            omp_set_num_threads(omp_thread);
        }
107
    }
108

S
starlord 已提交
109
    // init faiss global variable
Y
yudong.cai 已提交
110 111
    int32_t use_blas_threshold;
    s = config.GetEngineConfigUseBlasThreshold(use_blas_threshold);
S
starlord 已提交
112
    if (!s.ok()) {
113 114
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
115 116
    }

Y
yudong.cai 已提交
117
    faiss::distance_compute_blas_threshold = use_blas_threshold;
S
starlord 已提交
118

S
starlord 已提交
119
    // set archive config
G
groot 已提交
120
    engine::ArchiveConf::CriteriaT criterial;
121 122
    int32_t disk, days;
    s = config.GetDBConfigArchiveDiskThreshold(disk);
S
starlord 已提交
123
    if (!s.ok()) {
124 125
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
126 127
    }

Y
yudong.cai 已提交
128
    if (disk > 0) {
G
groot 已提交
129 130
        criterial[engine::ARCHIVE_CONF_DISK] = disk;
    }
131 132

    s = config.GetDBConfigArchiveDaysThreshold(days);
S
starlord 已提交
133
    if (!s.ok()) {
134 135
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
136 137
    }

Y
yudong.cai 已提交
138
    if (days > 0) {
G
groot 已提交
139 140
        criterial[engine::ARCHIVE_CONF_DAYS] = days;
    }
S
starlord 已提交
141
    opt.meta_.archive_conf_.SetCriterias(criterial);
G
groot 已提交
142

S
starlord 已提交
143
    // create db root folder
144 145 146 147 148
    s = CommonUtil::CreateDirectory(opt.meta_.path_);
    if (!s.ok()) {
        std::cerr << "Error: Failed to create database primary path: " << path
                  << ". Possible reason: db_config.primary_path is wrong in server_config.yaml or not available."
                  << std::endl;
G
groot 已提交
149 150
        kill(0, SIGUSR1);
    }
G
groot 已提交
151

S
starlord 已提交
152
    for (auto& path : opt.meta_.slave_paths_) {
153 154 155 156 157
        s = CommonUtil::CreateDirectory(path);
        if (!s.ok()) {
            std::cerr << "Error: Failed to create database secondary path: " << path
                      << ". Possible reason: db_config.secondary_path is wrong in server_config.yaml or not available."
                      << std::endl;
S
starlord 已提交
158 159 160 161
            kill(0, SIGUSR1);
        }
    }

S
starlord 已提交
162
    // create db instance
G
groot 已提交
163
    try {
S
starlord 已提交
164
        db_ = engine::DBFactory::Build(opt);
S
starlord 已提交
165
    } catch (std::exception& ex) {
166 167 168
        std::cerr << "Error: failed to open database: " << ex.what()
                << ". Possible reason: the meta system does not work."
                << std::endl;
G
groot 已提交
169
        kill(0, SIGUSR1);
G
groot 已提交
170
    }
S
starlord 已提交
171 172 173

    db_->Start();

S
starlord 已提交
174 175 176 177
    // preload table
    std::string preload_tables;
    s = config.GetDBConfigPreloadTable(preload_tables);
    if (!s.ok()) {
178 179
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
S
starlord 已提交
180 181 182 183 184 185 186 187 188
    }

    s = PreloadTables(preload_tables);
    if (!s.ok()) {
        std::cerr << "ERROR! Failed to preload tables: " << preload_tables << std::endl;
        std::cerr << s.ToString() << std::endl;
        kill(0, SIGUSR1);
    }

S
starlord 已提交
189
    return Status::OK();
G
groot 已提交
190 191
}

S
starlord 已提交
192 193
Status
DBWrapper::StopService() {
S
starlord 已提交
194
    if (db_) {
S
starlord 已提交
195 196 197
        db_->Stop();
    }

S
starlord 已提交
198
    return Status::OK();
G
groot 已提交
199 200
}

S
starlord 已提交
201 202
Status
DBWrapper::PreloadTables(const std::string& preload_tables) {
203 204 205 206
    if (preload_tables.empty()) {
        // do nothing
    } else if (preload_tables == "*") {
        // load all tables
S
starlord 已提交
207 208 209
        std::vector<engine::meta::TableSchema> table_schema_array;
        db_->AllTables(table_schema_array);

210
        for (auto& schema : table_schema_array) {
S
starlord 已提交
211 212 213 214 215 216 217 218
            auto status = db_->PreloadTable(schema.table_id_);
            if (!status.ok()) {
                return status;
            }
        }
    } else {
        std::vector<std::string> table_names;
        StringHelpFunctions::SplitStringByDelimeter(preload_tables, ",", table_names);
219
        for (auto& name : table_names) {
S
starlord 已提交
220 221 222 223 224 225 226 227 228 229
            auto status = db_->PreloadTable(name);
            if (!status.ok()) {
                return status;
            }
        }
    }

    return Status::OK();
}

S
starlord 已提交
230 231
}  // namespace server
}  // namespace milvus