utils.cpp 8.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.

X
Xu Peng 已提交
18 19

#include <iostream>
X
Xu Peng 已提交
20
#include <thread>
S
starlord 已提交
21 22
#include <memory>
#include <string>
X
Xu Peng 已提交
23
#include <boost/filesystem.hpp>
X
Xu Peng 已提交
24

S
starlord 已提交
25
#include "db/utils.h"
S
starlord 已提交
26 27
#include "cache/GpuCacheMgr.h"
#include "cache/CpuCacheMgr.h"
S
starlord 已提交
28
#include "db/DBFactory.h"
Z
update  
zhiru 已提交
29
#include "db/Options.h"
X
xiaojun.lin 已提交
30
#include "knowhere/index/vector_index/helpers/FaissGpuResourceMgr.h"
S
starlord 已提交
31
#include "utils/CommonUtil.h"
X
Xu Peng 已提交
32

Z
zhiru 已提交
33 34
INITIALIZE_EASYLOGGINGPP

S
starlord 已提交
35
namespace {
X
Xu Peng 已提交
36

S
starlord 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static const char
    * CONFIG_STR = "# All the following configurations are default values.\n"
                   "\n"
                   "server_config:\n"
                   "  address: 0.0.0.0                  # milvus server ip address (IPv4)\n"
                   "  port: 19530                       # port range: 1025 ~ 65534\n"
                   "  deploy_mode: single               \n"
                   "  time_zone: UTC+8\n"
                   "\n"
                   "db_config:\n"
                   "  primary_path: /tmp/milvus         # path used to store data and meta\n"
                   "  secondary_path:                   # path used to store data only, split by semicolon\n"
                   "\n"
                   "  backend_url: sqlite://:@:/        \n"
                   "                                    \n"
                   "                                    # Replace 'dialect' with 'mysql' or 'sqlite'\n"
                   "\n"
                   "  insert_buffer_size: 4             # GB, maximum insert buffer size allowed\n"
                   "\n"
                   "metric_config:\n"
                   "  enable_monitor: false             # enable monitoring or not\n"
                   "  collector: prometheus             # prometheus\n"
                   "  prometheus_config:\n"
                   "    port: 8080                      # port prometheus used to fetch metrics\n"
                   "\n"
                   "cache_config:\n"
                   "  cpu_mem_capacity: 16              # GB, CPU memory used for cache\n"
                   "  cpu_mem_threshold: 0.85           # percentage of data kept when cache cleanup triggered\n"
                   "  cache_insert_data: false          # whether load inserted data into cache\n"
                   "\n"
                   "engine_config:\n"
                   "  blas_threshold: 20\n"
                   "\n"
                   "resource_config:\n"
71
                   "  search_resources:\n"
S
starlord 已提交
72 73 74 75 76 77 78 79 80 81 82 83
                   "    - gpu0\n"
                   "  index_build_device: gpu0          # GPU used for building index";

void
WriteToFile(const std::string& file_path, const char* content) {
    std::fstream fs(file_path.c_str(), std::ios_base::out);

    //write data to file
    fs << content;
    fs.close();
}

Z
zhiru 已提交
84
class DBTestEnvironment : public ::testing::Environment {
S
starlord 已提交
85 86 87 88
 public:
    explicit DBTestEnvironment(const std::string& uri)
        : uri_(uri) {
    }
Z
zhiru 已提交
89

S
starlord 已提交
90 91
    std::string getURI() const {
        return uri_;
Z
zhiru 已提交
92 93 94 95 96 97
    }

    void SetUp() override {
        getURI();
    }

S
starlord 已提交
98 99
 private:
    std::string uri_;
Z
zhiru 已提交
100 101
};

S
starlord 已提交
102 103 104 105 106 107
DBTestEnvironment* test_env = nullptr;

} // namespace



S
starlord 已提交
108
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
109 110
void
BaseTest::InitLog() {
X
Xu Peng 已提交
111 112 113
    el::Configurations defaultConf;
    defaultConf.setToDefault();
    defaultConf.set(el::Level::Debug,
S
starlord 已提交
114
                    el::ConfigurationType::Format, "[%thread-%datetime-%level]: %msg (%fbase:%line)");
X
Xu Peng 已提交
115 116
    el::Loggers::reconfigureLogger("default", defaultConf);
}
X
Xu Peng 已提交
117

S
starlord 已提交
118 119
void
BaseTest::SetUp() {
S
starlord 已提交
120
    InitLog();
S
starlord 已提交
121

S
starlord 已提交
122
    knowhere::FaissGpuResourceMgr::GetInstance().InitDevice(0, 1024 * 1024 * 200, 1024 * 1024 * 300, 2);
S
starlord 已提交
123 124
}

S
starlord 已提交
125 126
void
BaseTest::TearDown() {
S
starlord 已提交
127 128 129
    milvus::cache::CpuCacheMgr::GetInstance()->ClearCache();
    milvus::cache::GpuCacheMgr::GetInstance(0)->ClearCache();
    knowhere::FaissGpuResourceMgr::GetInstance().Free();
S
starlord 已提交
130 131
}

S
starlord 已提交
132
milvus::engine::DBOptions
S
starlord 已提交
133
BaseTest::GetOptions() {
S
starlord 已提交
134
    auto options = milvus::engine::DBFactory::BuildOption();
S
starlord 已提交
135
    options.meta_.path_ = CONFIG_PATH;
S
starlord 已提交
136
    options.meta_.backend_uri_ = "sqlite://:@:/";
X
Xu Peng 已提交
137 138 139
    return options;
}

S
starlord 已提交
140
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
141 142
void
DBTest::SetUp() {
S
starlord 已提交
143
    BaseTest::SetUp();
W
wxyu 已提交
144

S
starlord 已提交
145
    auto res_mgr = milvus::scheduler::ResMgrInst::GetInstance();
W
wxyu 已提交
146
    res_mgr->Clear();
S
starlord 已提交
147
    res_mgr->Add(milvus::scheduler::ResourceFactory::Create("disk", "DISK", 0, true, false));
148
    res_mgr->Add(milvus::scheduler::ResourceFactory::Create("cpu", "CPU", 0, true, true));
S
starlord 已提交
149
    res_mgr->Add(milvus::scheduler::ResourceFactory::Create("gtx1660", "GPU", 0, true, true));
W
wxyu 已提交
150

S
starlord 已提交
151 152
    auto default_conn = milvus::scheduler::Connection("IO", 500.0);
    auto PCIE = milvus::scheduler::Connection("IO", 11000.0);
W
wxyu 已提交
153
    res_mgr->Connect("disk", "cpu", default_conn);
Y
Yu Kun 已提交
154
    res_mgr->Connect("cpu", "gtx1660", PCIE);
W
wxyu 已提交
155
    res_mgr->Start();
S
starlord 已提交
156
    milvus::scheduler::SchedInst::GetInstance()->Start();
W
wxyu 已提交
157

S
starlord 已提交
158
    milvus::scheduler::JobMgrInst::GetInstance()->Start();
W
wxyu 已提交
159

X
Xu Peng 已提交
160
    auto options = GetOptions();
S
starlord 已提交
161
    db_ = milvus::engine::DBFactory::Build(options);
S
starlord 已提交
162 163 164

    std::string config_path(options.meta_.path_ + CONFIG_FILE);
    WriteToFile(config_path, CONFIG_STR);
X
Xu Peng 已提交
165 166
}

S
starlord 已提交
167 168
void
DBTest::TearDown() {
S
starlord 已提交
169
    db_->Stop();
S
starlord 已提交
170
    db_->DropAll();
W
wxyu 已提交
171

S
starlord 已提交
172 173 174 175
    milvus::scheduler::JobMgrInst::GetInstance()->Stop();
    milvus::scheduler::SchedInst::GetInstance()->Stop();
    milvus::scheduler::ResMgrInst::GetInstance()->Stop();
    milvus::scheduler::ResMgrInst::GetInstance()->Clear();
W
wxyu 已提交
176

S
starlord 已提交
177 178
    BaseTest::TearDown();

179
    auto options = GetOptions();
S
starlord 已提交
180
    boost::filesystem::remove_all(options.meta_.path_);
X
Xu Peng 已提交
181 182
}

S
starlord 已提交
183
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
184
milvus::engine::DBOptions
S
starlord 已提交
185
DBTest2::GetOptions() {
S
starlord 已提交
186
    auto options = milvus::engine::DBFactory::BuildOption();
S
starlord 已提交
187
    options.meta_.path_ = "/tmp/milvus_test";
S
starlord 已提交
188
    options.meta_.archive_conf_ = milvus::engine::ArchiveConf("delete", "disk:1");
S
starlord 已提交
189
    options.meta_.backend_uri_ = "sqlite://:@:/";
X
Xu Peng 已提交
190 191 192
    return options;
}

S
starlord 已提交
193
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
194 195
void
MetaTest::SetUp() {
S
starlord 已提交
196 197
    BaseTest::SetUp();

198
    auto options = GetOptions();
S
starlord 已提交
199
    impl_ = std::make_shared<milvus::engine::meta::SqliteMetaImpl>(options.meta_);
X
Xu Peng 已提交
200 201
}

S
starlord 已提交
202 203
void
MetaTest::TearDown() {
204
    impl_->DropAll();
205

S
starlord 已提交
206 207
    BaseTest::TearDown();

208
    auto options = GetOptions();
S
starlord 已提交
209
    boost::filesystem::remove_all(options.meta_.path_);
X
Xu Peng 已提交
210
}
Z
update  
zhiru 已提交
211

S
starlord 已提交
212
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
213
milvus::engine::DBOptions
S
starlord 已提交
214
MySqlDBTest::GetOptions() {
S
starlord 已提交
215
    auto options = milvus::engine::DBFactory::BuildOption();
S
starlord 已提交
216
    options.meta_.path_ = "/tmp/milvus_test";
S
starlord 已提交
217
    options.meta_.backend_uri_ = test_env->getURI();
S
starlord 已提交
218

Z
update  
zhiru 已提交
219 220
    return options;
}
Z
zhiru 已提交
221

S
starlord 已提交
222
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
223 224
void
MySqlMetaTest::SetUp() {
S
starlord 已提交
225
    BaseTest::SetUp();
S
starlord 已提交
226

227
    auto options = GetOptions();
S
starlord 已提交
228
    impl_ = std::make_shared<milvus::engine::meta::MySQLMetaImpl>(options.meta_, options.mode_);
S
starlord 已提交
229 230
}

S
starlord 已提交
231 232
void
MySqlMetaTest::TearDown() {
S
starlord 已提交
233
    impl_->DropAll();
234

S
starlord 已提交
235 236
    BaseTest::TearDown();

237
    auto options = GetOptions();
S
starlord 已提交
238
    boost::filesystem::remove_all(options.meta_.path_);
S
starlord 已提交
239 240
}

S
starlord 已提交
241
milvus::engine::DBOptions
S
starlord 已提交
242
MySqlMetaTest::GetOptions() {
S
starlord 已提交
243
    auto options = milvus::engine::DBFactory::BuildOption();
S
starlord 已提交
244
    options.meta_.path_ = "/tmp/milvus_test";
S
starlord 已提交
245
    options.meta_.backend_uri_ = test_env->getURI();
Z
zhiru 已提交
246

Z
zhiru 已提交
247 248
    return options;
}
Z
zhiru 已提交
249

S
starlord 已提交
250
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
S
starlord 已提交
251 252
int
main(int argc, char **argv) {
Z
zhiru 已提交
253
    ::testing::InitGoogleTest(&argc, argv);
S
starlord 已提交
254 255

    std::string uri;
Z
zhiru 已提交
256 257 258
    if (argc > 1) {
        uri = argv[1];
    }
S
starlord 已提交
259

S
starlord 已提交
260 261
    test_env = new DBTestEnvironment(uri);
    ::testing::AddGlobalTestEnvironment(test_env);
Z
zhiru 已提交
262 263
    return RUN_ALL_TESTS();
}