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
#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 33
namespace zilliz {
namespace milvus {
namespace server {

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

G
groot 已提交
36 37
Status
DBWrapper::StartService() {
38
    Config& config = Config::GetInstance();
39
    Status s;
G
groot 已提交
40
    // db config
G
groot 已提交
41
    engine::DBOptions opt;
G
groot 已提交
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;

G
groot 已提交
56
    StringHelpFunctions::SplitStringByDelimeter(db_slave_path, ";", opt.meta_.slave_paths_);
G
groot 已提交
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;
G
groot 已提交
65

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

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

G
groot 已提交
93
    // init faiss global variable
Y
yudong.cai 已提交
94 95
    int32_t use_blas_threshold;
    s = config.GetEngineConfigUseBlasThreshold(use_blas_threshold);
96
    if (!s.ok()) return s;
Y
yudong.cai 已提交
97
    faiss::distance_compute_blas_threshold = use_blas_threshold;
G
groot 已提交
98

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

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

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

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

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

    db_->Start();

G
groot 已提交
140
    return Status::OK();
G
groot 已提交
141 142
}

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

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

G
groot 已提交
152 153 154
}  // namespace server
}  // namespace milvus
}  // namespace zilliz