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

#include "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

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

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

DBWrapper::DBWrapper() {
S
starlord 已提交
34 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 44 45 46 47 48 49 50 51 52 53 54 55
    s = config.GetDBConfigBackendUrl(opt.meta.backend_uri);
    if (!s.ok()) return s;

    std::string path;
    s = config.GetDBConfigPath(path);
    if (!s.ok()) return s;

    opt.meta.path = path + "/db";

    std::string db_slave_path;
    s = config.GetDBConfigSlavePath(db_slave_path);
    if (!s.ok()) return s;

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

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

    std::string mode;
    s = config.GetServerConfigMode(mode);
    if (!s.ok()) return s;
S
starlord 已提交
65

Z
update  
zhiru 已提交
66
    if (mode == "single") {
S
starlord 已提交
67
        opt.mode = engine::DBOptions::MODE::SINGLE;
Z
update  
zhiru 已提交
68 69
    }
    else if (mode == "cluster") {
S
starlord 已提交
70
        opt.mode = engine::DBOptions::MODE::CLUSTER;
Z
update  
zhiru 已提交
71 72
    }
    else if (mode == "read_only") {
S
starlord 已提交
73
        opt.mode = engine::DBOptions::MODE::READ_ONLY;
Z
update  
zhiru 已提交
74 75
    }
    else {
S
starlord 已提交
76
        std::cerr << "ERROR: mode specified in server_config is not one of ['single', 'cluster', 'read_only']" << std::endl;
Z
update  
zhiru 已提交
77 78
        kill(0, SIGUSR1);
    }
Z
update  
zhiru 已提交
79

80
    // engine config
81 82 83 84
    int32_t omp_thread;
    s = config.GetEngineConfigOmpThreadNum(omp_thread);
    if (!s.ok()) return s;
    if (omp_thread > 0) {
85 86
        omp_set_num_threads(omp_thread);
        SERVER_LOG_DEBUG << "Specify openmp thread number: " << omp_thread;
87 88 89 90 91 92
    } 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);
        }
93
    }
94

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

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

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

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

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

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

    db_->Start();

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

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

S
starlord 已提交
150
    return Status::OK();
G
groot 已提交
151 152 153 154 155
}

}
}
}