DBWrapper.cpp 4.3 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"
G
groot 已提交
21
#include "db/DBFactory.h"
G
groot 已提交
22 23
#include "utils/CommonUtil.h"
#include "utils/Log.h"
G
groot 已提交
24
#include "utils/StringHelpFunctions.h"
G
groot 已提交
25

26 27
#include <omp.h>

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

DBWrapper::DBWrapper() {
G
groot 已提交
33 34 35

}

G
groot 已提交
36
ErrorCode DBWrapper::StartService() {
37
    //db config
G
groot 已提交
38
    zilliz::milvus::engine::Options opt;
G
groot 已提交
39 40 41
    ConfigNode& db_config = ServerConfig::GetInstance().GetConfig(CONFIG_DB);
    opt.meta.backend_uri = db_config.GetValue(CONFIG_DB_URL);
    std::string db_path = db_config.GetValue(CONFIG_DB_PATH);
G
groot 已提交
42
    opt.meta.path = db_path + "/db";
G
groot 已提交
43

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

47
    // cache config
G
groot 已提交
48 49 50
    ConfigNode& cache_config = ServerConfig::GetInstance().GetConfig(CONFIG_CACHE);
    opt.insert_cache_immediately_ = cache_config.GetBoolValue(CONFIG_INSERT_CACHE_IMMEDIATELY, false);

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

67 68
    // engine config
    ConfigNode& engine_config = ServerConfig::GetInstance().GetConfig(CONFIG_ENGINE);
69 70 71 72
    int32_t omp_thread = engine_config.GetInt32Value(CONFIG_OMP_THREAD_NUM, 0);
    if(omp_thread > 0) {
        omp_set_num_threads(omp_thread);
        SERVER_LOG_DEBUG << "Specify openmp thread number: " << omp_thread;
73 74 75 76 77 78
    } 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);
        }
79
    }
80

G
groot 已提交
81 82
    //set archive config
    engine::ArchiveConf::CriteriaT criterial;
G
groot 已提交
83 84
    int64_t disk = db_config.GetInt64Value(CONFIG_DB_ARCHIVE_DISK, 0);
    int64_t days = db_config.GetInt64Value(CONFIG_DB_ARCHIVE_DAYS, 0);
G
groot 已提交
85 86 87 88 89 90 91 92 93
    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
G
groot 已提交
94
    ErrorCode err = CommonUtil::CreateDirectory(opt.meta.path);
G
groot 已提交
95 96 97 98
    if(err != SERVER_SUCCESS) {
        std::cout << "ERROR! Failed to create database root path: " << opt.meta.path << std::endl;
        kill(0, SIGUSR1);
    }
G
groot 已提交
99

G
groot 已提交
100 101 102 103 104 105 106 107
    for(auto& path : opt.meta.slave_paths) {
        err = CommonUtil::CreateDirectory(path);
        if(err != SERVER_SUCCESS) {
            std::cout << "ERROR! Failed to create database slave path: " << path << std::endl;
            kill(0, SIGUSR1);
        }
    }

G
groot 已提交
108
    //create db instance
G
groot 已提交
109 110
    std::string msg = opt.meta.path;
    try {
G
groot 已提交
111
        db_ = engine::DBFactory::Build(opt);
G
groot 已提交
112 113 114 115
    } catch(std::exception& ex) {
        msg = ex.what();
    }

G
groot 已提交
116
    if(db_ == nullptr) {
G
groot 已提交
117
        std::cout << "ERROR! Failed to open database: " << msg << std::endl;
G
groot 已提交
118
        kill(0, SIGUSR1);
G
groot 已提交
119
    }
G
groot 已提交
120 121 122 123

    db_->Start();

    return SERVER_SUCCESS;
G
groot 已提交
124 125
}

G
groot 已提交
126
ErrorCode DBWrapper::StopService() {
G
groot 已提交
127 128 129 130 131
    if(db_) {
        db_->Stop();
    }

    return SERVER_SUCCESS;
G
groot 已提交
132 133 134 135 136
}

}
}
}