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

G
groot 已提交
25
#include <faiss/utils.h>
G
groot 已提交
26 27
#include <omp.h>
#include <string>
28

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

G
groot 已提交
33 34
Status
DBWrapper::StartService() {
35
    Config& config = Config::GetInstance();
36
    Status s;
G
groot 已提交
37
    // db config
G
groot 已提交
38
    engine::DBOptions opt;
G
groot 已提交
39

40
    s = config.GetDBConfigBackendUrl(opt.meta_.backend_uri_);
G
groot 已提交
41 42 43
    if (!s.ok()) {
        return s;
    }
44 45

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

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

    std::string db_slave_path;
Y
yudong.cai 已提交
54
    s = config.GetDBConfigSecondaryPath(db_slave_path);
G
groot 已提交
55 56 57
    if (!s.ok()) {
        return s;
    }
58

G
groot 已提交
59
    StringHelpFunctions::SplitStringByDelimeter(db_slave_path, ";", opt.meta_.slave_paths_);
G
groot 已提交
60

61
    // cache config
62
    s = config.GetCacheConfigCacheInsertData(opt.insert_cache_immediately_);
G
groot 已提交
63 64 65
    if (!s.ok()) {
        return s;
    }
66 67

    std::string mode;
Y
yudong.cai 已提交
68
    s = config.GetServerConfigDeployMode(mode);
G
groot 已提交
69 70 71
    if (!s.ok()) {
        return s;
    }
G
groot 已提交
72

Z
update  
zhiru 已提交
73
    if (mode == "single") {
G
groot 已提交
74
        opt.mode_ = engine::DBOptions::MODE::SINGLE;
G
groot 已提交
75
    } else if (mode == "cluster_readonly") {
Y
yudong.cai 已提交
76
        opt.mode_ = engine::DBOptions::MODE::CLUSTER_READONLY;
G
groot 已提交
77
    } else if (mode == "cluster_writable") {
Y
yudong.cai 已提交
78
        opt.mode_ = engine::DBOptions::MODE::CLUSTER_WRITABLE;
G
groot 已提交
79
    } else {
G
groot 已提交
80 81
        std::cerr << "ERROR: mode specified in server_config must be ['single', 'cluster_readonly', 'cluster_writable']"
                  << std::endl;
Z
update  
zhiru 已提交
82 83
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
84

85
    // engine config
86 87
    int32_t omp_thread;
    s = config.GetEngineConfigOmpThreadNum(omp_thread);
G
groot 已提交
88 89 90 91
    if (!s.ok()) {
        return s;
    }

92
    if (omp_thread > 0) {
93 94
        omp_set_num_threads(omp_thread);
        SERVER_LOG_DEBUG << "Specify openmp thread number: " << omp_thread;
95 96
    } else {
        uint32_t sys_thread_cnt = 8;
G
groot 已提交
97
        if (CommonUtil::GetSystemAvailableThreads(sys_thread_cnt)) {
G
groot 已提交
98
            omp_thread = static_cast<int32_t>(ceil(sys_thread_cnt * 0.5));
99 100
            omp_set_num_threads(omp_thread);
        }
101
    }
102

G
groot 已提交
103
    // init faiss global variable
Y
yudong.cai 已提交
104 105
    int32_t use_blas_threshold;
    s = config.GetEngineConfigUseBlasThreshold(use_blas_threshold);
G
groot 已提交
106 107 108 109
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
110
    faiss::distance_compute_blas_threshold = use_blas_threshold;
G
groot 已提交
111

G
groot 已提交
112
    // set archive config
G
groot 已提交
113
    engine::ArchiveConf::CriteriaT criterial;
114 115
    int32_t disk, days;
    s = config.GetDBConfigArchiveDiskThreshold(disk);
G
groot 已提交
116 117 118 119
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
120
    if (disk > 0) {
G
groot 已提交
121 122
        criterial[engine::ARCHIVE_CONF_DISK] = disk;
    }
123 124

    s = config.GetDBConfigArchiveDaysThreshold(days);
G
groot 已提交
125 126 127 128
    if (!s.ok()) {
        return s;
    }

Y
yudong.cai 已提交
129
    if (days > 0) {
G
groot 已提交
130 131
        criterial[engine::ARCHIVE_CONF_DAYS] = days;
    }
G
groot 已提交
132
    opt.meta_.archive_conf_.SetCriterias(criterial);
G
groot 已提交
133

G
groot 已提交
134
    // create db root folder
G
groot 已提交
135
    Status status = CommonUtil::CreateDirectory(opt.meta_.path_);
G
groot 已提交
136
    if (!status.ok()) {
G
groot 已提交
137
        std::cerr << "ERROR! Failed to create database root path: " << opt.meta_.path_ << std::endl;
G
groot 已提交
138 139
        kill(0, SIGUSR1);
    }
G
groot 已提交
140

G
groot 已提交
141
    for (auto& path : opt.meta_.slave_paths_) {
G
groot 已提交
142
        status = CommonUtil::CreateDirectory(path);
G
groot 已提交
143
        if (!status.ok()) {
G
groot 已提交
144
            std::cerr << "ERROR! Failed to create database slave path: " << path << std::endl;
G
groot 已提交
145 146 147 148
            kill(0, SIGUSR1);
        }
    }

G
groot 已提交
149
    // create db instance
G
groot 已提交
150
    try {
G
groot 已提交
151
        db_ = engine::DBFactory::Build(opt);
G
groot 已提交
152
    } catch (std::exception& ex) {
153
        std::cerr << "ERROR! Failed to open database: " << ex.what() << std::endl;
G
groot 已提交
154
        kill(0, SIGUSR1);
G
groot 已提交
155
    }
G
groot 已提交
156 157 158

    db_->Start();

G
groot 已提交
159
    return Status::OK();
G
groot 已提交
160 161
}

G
groot 已提交
162 163
Status
DBWrapper::StopService() {
G
groot 已提交
164
    if (db_) {
G
groot 已提交
165 166 167
        db_->Stop();
    }

G
groot 已提交
168
    return Status::OK();
G
groot 已提交
169 170
}

G
groot 已提交
171 172 173
}  // namespace server
}  // namespace milvus
}  // namespace zilliz