DBWrapper.cpp 3.4 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6 7 8 9 10
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/

#include "DBWrapper.h"
#include "ServerConfig.h"
#include "utils/CommonUtil.h"
#include "utils/Log.h"
S
starlord 已提交
11
#include "utils/StringHelpFunctions.h"
G
groot 已提交
12 13 14 15 16 17 18 19 20 21 22

namespace zilliz {
namespace milvus {
namespace server {

DBWrapper::DBWrapper() {
    zilliz::milvus::engine::Options opt;
    ConfigNode& config = ServerConfig::GetInstance().GetConfig(CONFIG_DB);
    opt.meta.backend_uri = config.GetValue(CONFIG_DB_URL);
    std::string db_path = config.GetValue(CONFIG_DB_PATH);
    opt.meta.path = db_path + "/db";
S
starlord 已提交
23 24 25 26

    std::string db_slave_path = config.GetValue(CONFIG_DB_SLAVE_PATH);
    StringHelpFunctions::SplitStringByDelimeter(db_slave_path, ";", opt.meta.slave_paths);

G
groot 已提交
27 28 29 30
    int64_t index_size = config.GetInt64Value(CONFIG_DB_INDEX_TRIGGER_SIZE);
    if(index_size > 0) {//ensure larger than zero, unit is MB
        opt.index_trigger_size = (size_t)index_size * engine::ONE_MB;
    }
31 32 33
    int64_t insert_buffer_size = config.GetInt64Value(CONFIG_DB_INSERT_BUFFER_SIZE, 4);
    if (insert_buffer_size >= 1) {
        opt.insert_buffer_size = insert_buffer_size * engine::ONE_GB;
Z
zhiru 已提交
34
    }
35 36
    else {
        std::cout << "ERROR: insert_buffer_size should be at least 1 GB" << std::endl;
Z
zhiru 已提交
37 38
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
39

Z
update  
zhiru 已提交
40
    ConfigNode& serverConfig = ServerConfig::GetInstance().GetConfig(CONFIG_SERVER);
Z
update  
zhiru 已提交
41 42 43 44 45 46 47 48 49 50 51
    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 已提交
52 53 54
        std::cout << "ERROR: mode specified in server_config is not one of ['single', 'cluster', 'read_only']" << std::endl;
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
55

G
groot 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68
    //set archive config
    engine::ArchiveConf::CriteriaT criterial;
    int64_t disk = config.GetInt64Value(CONFIG_DB_ARCHIVE_DISK, 0);
    int64_t days = config.GetInt64Value(CONFIG_DB_ARCHIVE_DAYS, 0);
    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 已提交
69 70 71 72 73
    ServerError err = CommonUtil::CreateDirectory(opt.meta.path);
    if(err != SERVER_SUCCESS) {
        std::cout << "ERROR! Failed to create database root path: " << opt.meta.path << std::endl;
        kill(0, SIGUSR1);
    }
G
groot 已提交
74

S
starlord 已提交
75 76 77 78 79 80 81 82
    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 已提交
83 84 85 86 87 88 89
    std::string msg = opt.meta.path;
    try {
        zilliz::milvus::engine::DB::Open(opt, &db_);
    } catch(std::exception& ex) {
        msg = ex.what();
    }

G
groot 已提交
90
    if(db_ == nullptr) {
G
groot 已提交
91
        std::cout << "ERROR! Failed to open database: " << msg << std::endl;
G
groot 已提交
92
        kill(0, SIGUSR1);
G
groot 已提交
93 94 95 96 97 98 99 100 101 102
    }
}

DBWrapper::~DBWrapper() {
    delete db_;
}

}
}
}