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

S
starlord 已提交
19
#include "server/DBWrapper.h"
20
#include "Config.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

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

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

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

S
starlord 已提交
37
Status DBWrapper::StartService() {
38
    Config& config = Config::GetInstance();
39
    Status s;
40
    //db config
S
starlord 已提交
41
    engine::DBOptions opt;
S
starlord 已提交
42

43
    s = config.GetDBConfigBackendUrl(opt.meta_.backend_uri_);
44 45 46
    if (!s.ok()) return s;

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

50
    opt.meta_.path_ = path + "/db";
51 52

    std::string db_slave_path;
Y
yudong.cai 已提交
53
    s = config.GetDBConfigSecondaryPath(db_slave_path);
54 55
    if (!s.ok()) return s;

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

58
    // cache config
59 60 61 62
    s = config.GetCacheConfigCacheInsertData(opt.insert_cache_immediately_);
    if (!s.ok()) return s;

    std::string mode;
Y
yudong.cai 已提交
63
    s = config.GetServerConfigDeployMode(mode);
64
    if (!s.ok()) return s;
S
starlord 已提交
65

Z
update  
zhiru 已提交
66
    if (mode == "single") {
S
starlord 已提交
67
        opt.mode_ = engine::DBOptions::MODE::SINGLE;
S
starlord 已提交
68
    } else if (mode == "cluster_readonly") {
Y
yudong.cai 已提交
69
        opt.mode_ = engine::DBOptions::MODE::CLUSTER_READONLY;
S
starlord 已提交
70
    } else if (mode == "cluster_writable") {
Y
yudong.cai 已提交
71
        opt.mode_ = engine::DBOptions::MODE::CLUSTER_WRITABLE;
S
starlord 已提交
72
    } else {
S
starlord 已提交
73 74 75
        std::cerr <<
        "ERROR: mode specified in server_config must be ['single', 'cluster_readonly', 'cluster_writable']"
        << std::endl;
Z
update  
zhiru 已提交
76 77
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
78

79
    // engine config
80 81 82 83
    int32_t omp_thread;
    s = config.GetEngineConfigOmpThreadNum(omp_thread);
    if (!s.ok()) return s;
    if (omp_thread > 0) {
84 85
        omp_set_num_threads(omp_thread);
        SERVER_LOG_DEBUG << "Specify openmp thread number: " << omp_thread;
86 87
    } else {
        uint32_t sys_thread_cnt = 8;
S
starlord 已提交
88
        if (CommonUtil::GetSystemAvailableThreads(sys_thread_cnt)) {
89 90 91
            omp_thread = (int32_t)ceil(sys_thread_cnt*0.5);
            omp_set_num_threads(omp_thread);
        }
92
    }
93

94
    //init faiss global variable
95 96 97 98
    int32_t blas_threshold;
    s = config.GetEngineConfigBlasThreshold(blas_threshold);
    if (!s.ok()) return s;
    faiss::distance_compute_blas_threshold = blas_threshold;
S
starlord 已提交
99

G
groot 已提交
100 101
    //set archive config
    engine::ArchiveConf::CriteriaT criterial;
102 103 104
    int32_t disk, days;
    s = config.GetDBConfigArchiveDiskThreshold(disk);
    if (!s.ok()) return s;
Y
yudong.cai 已提交
105
    if (disk > 0) {
G
groot 已提交
106 107
        criterial[engine::ARCHIVE_CONF_DISK] = disk;
    }
108 109 110

    s = config.GetDBConfigArchiveDaysThreshold(days);
    if (!s.ok()) return s;
Y
yudong.cai 已提交
111
    if (days > 0) {
G
groot 已提交
112 113
        criterial[engine::ARCHIVE_CONF_DAYS] = days;
    }
S
starlord 已提交
114
    opt.meta_.archive_conf_.SetCriterias(criterial);
G
groot 已提交
115 116

    //create db root folder
S
starlord 已提交
117
    Status status = CommonUtil::CreateDirectory(opt.meta_.path_);
S
starlord 已提交
118
    if (!status.ok()) {
S
starlord 已提交
119
        std::cerr << "ERROR! Failed to create database root path: " << opt.meta_.path_ << std::endl;
G
groot 已提交
120 121
        kill(0, SIGUSR1);
    }
G
groot 已提交
122

S
starlord 已提交
123
    for (auto& path : opt.meta_.slave_paths_) {
S
starlord 已提交
124
        status = CommonUtil::CreateDirectory(path);
S
starlord 已提交
125
        if (!status.ok()) {
S
starlord 已提交
126
            std::cerr << "ERROR! Failed to create database slave path: " << path << std::endl;
S
starlord 已提交
127 128 129 130
            kill(0, SIGUSR1);
        }
    }

131
    //create db instance
G
groot 已提交
132
    try {
S
starlord 已提交
133
        db_ = engine::DBFactory::Build(opt);
G
groot 已提交
134
    } catch(std::exception& ex) {
135
        std::cerr << "ERROR! Failed to open database: " << ex.what() << std::endl;
G
groot 已提交
136
        kill(0, SIGUSR1);
G
groot 已提交
137
    }
S
starlord 已提交
138 139 140

    db_->Start();

S
starlord 已提交
141
    return Status::OK();
G
groot 已提交
142 143
}

S
starlord 已提交
144
Status DBWrapper::StopService() {
S
starlord 已提交
145
    if (db_) {
S
starlord 已提交
146 147 148
        db_->Stop();
    }

S
starlord 已提交
149
    return Status::OK();
G
groot 已提交
150 151
}

S
starlord 已提交
152 153 154
} // namespace server
} // namespace milvus
} // namespace zilliz