test_db.cpp 46.6 KB
Newer Older
1
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
J
jinhai 已提交
2
//
3 4
// Licensed 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
J
jinhai 已提交
5
//
6 7 8 9 10
// 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.
J
jinhai 已提交
11

S
shengjh 已提交
12 13
#include <fiu-control.h>
#include <fiu-local.h>
14
#include <gtest/gtest.h>
Z
Zhiru Zhu 已提交
15 16 17 18 19

#include <boost/filesystem.hpp>
#include <random>
#include <thread>

Y
youny626 已提交
20 21
#include "cache/CpuCacheMgr.h"
#include "db/Constants.h"
G
groot 已提交
22
#include "db/DB.h"
Y
youny626 已提交
23
#include "db/DBFactory.h"
X
Xu Peng 已提交
24
#include "db/DBImpl.h"
25
#include "db/IDGenerator.h"
S
starlord 已提交
26
#include "db/meta/MetaConsts.h"
Y
youny626 已提交
27
#include "db/utils.h"
28
#include "server/Config.h"
Y
youny626 已提交
29
#include "utils/CommonUtil.h"
G
groot 已提交
30

G
groot 已提交
31 32
namespace {

Y
youny626 已提交
33
static const char* TABLE_NAME = "test_group";
S
starlord 已提交
34 35 36 37 38 39
static constexpr int64_t TABLE_DIM = 256;
static constexpr int64_t VECTOR_COUNT = 25000;
static constexpr int64_t INSERT_LOOP = 1000;
static constexpr int64_t SECONDS_EACH_HOUR = 3600;
static constexpr int64_t DAY_SECONDS = 24 * 60 * 60;

S
starlord 已提交
40
milvus::engine::meta::TableSchema
S
starlord 已提交
41
BuildTableSchema() {
S
starlord 已提交
42
    milvus::engine::meta::TableSchema table_info;
S
starlord 已提交
43 44 45 46
    table_info.dimension_ = TABLE_DIM;
    table_info.table_id_ = TABLE_NAME;
    return table_info;
}
G
groot 已提交
47

S
starlord 已提交
48
void
49
BuildVectors(uint64_t n, uint64_t batch_index, milvus::engine::VectorsData& vectors) {
G
groot 已提交
50 51 52 53 54 55
    vectors.vector_count_ = n;
    vectors.float_data_.clear();
    vectors.float_data_.resize(n * TABLE_DIM);
    float* data = vectors.float_data_.data();
    for (uint64_t i = 0; i < n; i++) {
        for (int64_t j = 0; j < TABLE_DIM; j++) data[TABLE_DIM * i + j] = drand48();
S
starlord 已提交
56
        data[TABLE_DIM * i] += i / 2000.;
57 58

        vectors.id_array_.push_back(n * batch_index + i);
G
groot 已提交
59
    }
S
starlord 已提交
60
}
G
groot 已提交
61

S
starlord 已提交
62 63 64 65 66 67 68 69
std::string
CurrentTmDate(int64_t offset_day = 0) {
    time_t tt;
    time(&tt);
    tt = tt + 8 * SECONDS_EACH_HOUR;
    tt = tt + 24 * SECONDS_EACH_HOUR * offset_day;
    tm t;
    gmtime_r(&tt, &t);
70

Y
youny626 已提交
71 72
    std::string str =
        std::to_string(t.tm_year + 1900) + "-" + std::to_string(t.tm_mon + 1) + "-" + std::to_string(t.tm_mday);
73

S
starlord 已提交
74 75
    return str;
}
76

S
starlord 已提交
77
void
Y
youny626 已提交
78 79
ConvertTimeRangeToDBDates(const std::string& start_value, const std::string& end_value,
                          std::vector<milvus::engine::meta::DateT>& dates) {
S
starlord 已提交
80
    dates.clear();
81

S
starlord 已提交
82 83
    time_t tt_start, tt_end;
    tm tm_start, tm_end;
S
starlord 已提交
84
    if (!milvus::server::CommonUtil::TimeStrToTime(start_value, tt_start, tm_start)) {
S
starlord 已提交
85 86
        return;
    }
87

S
starlord 已提交
88
    if (!milvus::server::CommonUtil::TimeStrToTime(end_value, tt_end, tm_end)) {
S
starlord 已提交
89 90
        return;
    }
91

Y
youny626 已提交
92
    int64_t days = (tt_end > tt_start) ? (tt_end - tt_start) / DAY_SECONDS : (tt_start - tt_end) / DAY_SECONDS;
S
starlord 已提交
93 94 95
    if (days == 0) {
        return;
    }
96

S
starlord 已提交
97 98 99
    for (int64_t i = 0; i < days; i++) {
        time_t tt_day = tt_start + DAY_SECONDS * i;
        tm tm_day;
S
starlord 已提交
100
        milvus::server::CommonUtil::ConvertTime(tt_day, tm_day);
101

Y
youny626 已提交
102
        int64_t date = tm_day.tm_year * 10000 + tm_day.tm_mon * 100 + tm_day.tm_mday;  // according to db logic
S
starlord 已提交
103
        dates.push_back(date);
104
    }
G
groot 已提交
105 106
}

Y
youny626 已提交
107
}  // namespace
S
starlord 已提交
108

X
Xu Peng 已提交
109 110
TEST_F(DBTest, CONFIG_TEST) {
    {
S
starlord 已提交
111
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf("wrong"));
112
        /* EXPECT_DEATH(engine::ArchiveConf conf("wrong"), ""); */
X
Xu Peng 已提交
113 114
    }
    {
S
starlord 已提交
115
        milvus::engine::ArchiveConf conf("delete");
X
Xu Peng 已提交
116 117
        ASSERT_EQ(conf.GetType(), "delete");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
118
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
119 120
    }
    {
S
starlord 已提交
121
        milvus::engine::ArchiveConf conf("swap");
X
Xu Peng 已提交
122 123
        ASSERT_EQ(conf.GetType(), "swap");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
124
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
125
    }
S
shengjh 已提交
126 127 128 129 130 131 132 133 134 135 136 137
    {
        fiu_init(0);
        fiu_enable("ArchiveConf.ParseCritirias.OptionsParseCritiriasOutOfRange", 1, NULL, 0);
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf("swap", "disk:"));
        fiu_disable("ArchiveConf.ParseCritirias.OptionsParseCritiriasOutOfRange");
    }
    {
        fiu_enable("ArchiveConf.ParseCritirias.empty_tokens", 1, NULL, 0);
        milvus::engine::ArchiveConf conf("swap", "");
        ASSERT_TRUE(conf.GetCriterias().empty());
        fiu_disable("ArchiveConf.ParseCritirias.empty_tokens");
    }
X
Xu Peng 已提交
138
    {
S
starlord 已提交
139 140 141
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf1("swap", "disk:"));
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf2("swap", "disk:a"));
        milvus::engine::ArchiveConf conf("swap", "disk:1024");
X
Xu Peng 已提交
142
        auto criterias = conf.GetCriterias();
S
starlord 已提交
143 144
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["disk"], 1024);
X
Xu Peng 已提交
145 146
    }
    {
S
starlord 已提交
147 148 149
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf1("swap", "days:"));
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf2("swap", "days:a"));
        milvus::engine::ArchiveConf conf("swap", "days:100");
X
Xu Peng 已提交
150
        auto criterias = conf.GetCriterias();
S
starlord 已提交
151 152
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["days"], 100);
X
Xu Peng 已提交
153 154
    }
    {
S
starlord 已提交
155 156 157
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf1("swap", "days:"));
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf2("swap", "days:a"));
        milvus::engine::ArchiveConf conf("swap", "days:100;disk:200");
X
Xu Peng 已提交
158
        auto criterias = conf.GetCriterias();
S
starlord 已提交
159 160 161
        ASSERT_EQ(criterias.size(), 2);
        ASSERT_EQ(criterias["days"], 100);
        ASSERT_EQ(criterias["disk"], 200);
X
Xu Peng 已提交
162 163 164
    }
}

165
TEST_F(DBTest, DB_TEST) {
S
starlord 已提交
166
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
167
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
168

S
starlord 已提交
169
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
170
    table_info_get.table_id_ = TABLE_NAME;
G
groot 已提交
171
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
172
    ASSERT_TRUE(stat.ok());
G
groot 已提交
173
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
G
groot 已提交
174

G
groot 已提交
175 176
    uint64_t qb = 5;
    milvus::engine::VectorsData qxb;
177
    BuildVectors(qb, 0, qxb);
X
Xu Peng 已提交
178

X
Xu Peng 已提交
179
    std::thread search([&]() {
G
groot 已提交
180 181
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
X
Xu Peng 已提交
182 183 184 185 186
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

        INIT_TIMER;
        std::stringstream ss;
G
groot 已提交
187 188
        uint64_t count = 0;
        uint64_t prev_count = 0;
189
        milvus::json json_params = {{"nprobe", 10}};
X
Xu Peng 已提交
190

S
starlord 已提交
191
        for (auto j = 0; j < 10; ++j) {
X
Xu Peng 已提交
192
            ss.str("");
X
Xu Peng 已提交
193
            db_->Size(count);
X
Xu Peng 已提交
194
            prev_count = count;
195 196 197
            if (count == 0) {
                continue;
            }
X
Xu Peng 已提交
198 199

            START_TIMER;
G
groot 已提交
200 201

            std::vector<std::string> tags;
202
            stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, qxb, result_ids, result_distances);
S
starlord 已提交
203
            ss << "Search " << j << " With Size " << count / milvus::engine::M << " M";
X
Xu Peng 已提交
204 205
            STOP_TIMER(ss.str());

S
starlord 已提交
206
            ASSERT_TRUE(stat.ok());
207
            ASSERT_EQ(result_ids.size(), qb * k);
G
groot 已提交
208
            for (auto i = 0; i < qb; ++i) {
X
Xu Peng 已提交
209
                ss.str("");
G
groot 已提交
210 211 212
                ss << "Result [" << i << "]:";
                for (auto t = 0; t < k; t++) {
                    ss << result_ids[i * k + t] << " ";
X
Xu Peng 已提交
213
                }
214
                /* LOG(DEBUG) << ss.str(); */
X
Xu Peng 已提交
215
            }
X
Xu Peng 已提交
216
            ASSERT_TRUE(count >= prev_count);
X
Xu Peng 已提交
217 218 219 220
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

G
groot 已提交
221
    int loop = INSERT_LOOP;
X
Xu Peng 已提交
222

S
starlord 已提交
223 224
    for (auto i = 0; i < loop; ++i) {
        if (i == 40) {
G
groot 已提交
225 226
            db_->InsertVectors(TABLE_NAME, "", qxb);
            ASSERT_EQ(qxb.id_array_.size(), qb);
X
Xu Peng 已提交
227
        } else {
228 229 230 231
            uint64_t nb = 50;
            milvus::engine::VectorsData xb;
            BuildVectors(nb, i, xb);

G
groot 已提交
232 233
            db_->InsertVectors(TABLE_NAME, "", xb);
            ASSERT_EQ(xb.id_array_.size(), nb);
X
Xu Peng 已提交
234
        }
235 236 237 238

        stat = db_->Flush();
        ASSERT_TRUE(stat.ok());

X
Xu Peng 已提交
239
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
240
    }
X
xj.lin 已提交
241

X
Xu Peng 已提交
242
    search.join();
S
starlord 已提交
243 244 245

    uint64_t count;
    stat = db_->GetTableRowCount(TABLE_NAME, count);
S
starlord 已提交
246
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
247
    ASSERT_GT(count, 0);
S
shengjh 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260

    // test invalid build db
    {
        auto options = GetOptions();
        options.meta_.backend_uri_ = "dummy";
        ASSERT_ANY_THROW(milvus::engine::DBFactory::Build(options));

        options.meta_.backend_uri_ = "mysql://root:123456@127.0.0.1:3306/test";
        ASSERT_ANY_THROW(milvus::engine::DBFactory::Build(options));

        options.meta_.backend_uri_ = "dummy://root:123456@127.0.0.1:3306/test";
        ASSERT_ANY_THROW(milvus::engine::DBFactory::Build(options));
    }
S
starlord 已提交
261
}
X
xj.lin 已提交
262

263
TEST_F(DBTest, SEARCH_TEST) {
264
    milvus::scheduler::OptimizerInst::GetInstance()->Init();
S
starlord 已提交
265 266
    std::string config_path(CONFIG_PATH);
    config_path += CONFIG_FILE;
Y
youny626 已提交
267
    milvus::server::Config& config = milvus::server::Config::GetInstance();
S
starlord 已提交
268
    milvus::Status s = config.LoadConfigFile(config_path);
269

S
starlord 已提交
270
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
271
    auto stat = db_->CreateTable(table_info);
X
xj.lin 已提交
272

S
starlord 已提交
273
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
274
    table_info_get.table_id_ = TABLE_NAME;
G
groot 已提交
275
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
276
    ASSERT_TRUE(stat.ok());
G
groot 已提交
277
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
X
xj.lin 已提交
278 279

    // prepare raw data
G
groot 已提交
280
    size_t nb = VECTOR_COUNT;
X
xj.lin 已提交
281 282
    size_t nq = 10;
    size_t k = 5;
G
groot 已提交
283 284 285 286 287 288
    milvus::engine::VectorsData xb, xq;
    xb.vector_count_ = nb;
    xb.float_data_.resize(nb * TABLE_DIM);
    xq.vector_count_ = nq;
    xq.float_data_.resize(nq * TABLE_DIM);
    xb.id_array_.resize(nb);
X
xj.lin 已提交
289 290 291 292

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis_xt(-1.0, 1.0);
S
starlord 已提交
293
    for (size_t i = 0; i < nb * TABLE_DIM; i++) {
G
groot 已提交
294
        xb.float_data_[i] = dis_xt(gen);
S
starlord 已提交
295
        if (i < nb) {
G
groot 已提交
296
            xb.id_array_[i] = i;
X
xj.lin 已提交
297 298
        }
    }
S
starlord 已提交
299
    for (size_t i = 0; i < nq * TABLE_DIM; i++) {
G
groot 已提交
300
        xq.float_data_[i] = dis_xt(gen);
X
xj.lin 已提交
301 302 303
    }

    // result data
Y
youny626 已提交
304
    // std::vector<long> nns_gt(k*nq);
S
starlord 已提交
305
    std::vector<int64_t> nns(k * nq);  // nns = nearst neg search
Y
youny626 已提交
306
    // std::vector<float> dis_gt(k*nq);
S
starlord 已提交
307
    std::vector<float> dis(k * nq);
X
xj.lin 已提交
308 309

    // insert data
G
groot 已提交
310 311
    stat = db_->InsertVectors(TABLE_NAME, "", xb);
    ASSERT_TRUE(stat.ok());
X
xj.lin 已提交
312

313
    milvus::json json_params = {{"nprobe", 10}};
S
starlord 已提交
314
    milvus::engine::TableIndex index;
Y
youny626 已提交
315
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IDMAP;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    index.extra_params_ = {{"nlist", 16384}};
//    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
//
//    {
//        std::vector<std::string> tags;
//        milvus::engine::ResultIds result_ids;
//        milvus::engine::ResultDistances result_distances;
//        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
//        ASSERT_TRUE(stat.ok());
//    }
//
//    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
//    index.extra_params_ = {{"nlist", 16384}};
//    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
//
//    {
//        std::vector<std::string> tags;
//        milvus::engine::ResultIds result_ids;
//        milvus::engine::ResultDistances result_distances;
//        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
//        ASSERT_TRUE(stat.ok());
//    }
338 339

    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8;
340
    index.extra_params_ = {{"nlist", 16384}};
341 342 343 344 345 346
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish

    {
        std::vector<std::string> tags;
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
347
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
348 349 350 351
        ASSERT_TRUE(stat.ok());
    }

#ifdef CUSTOMIZATION
352
#ifdef MILVUS_GPU_VERSION
353 354 355 356 357 358 359
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish

    {
        std::vector<std::string> tags;
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
360
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
361
        ASSERT_TRUE(stat.ok());
S
starlord 已提交
362
    }
363
#endif
364
#endif
S
starlord 已提交
365

Y
youny626 已提交
366
    {  // search by specify index file
367 368 369 370 371 372 373
        std::vector<std::string> file_ids;
        // sometimes this case run fast to merge file and build index, old file will be deleted immediately,
        // so the QueryByFileID cannot get files to search
        // input 100 files ids to avoid random failure of this case
        for (int i = 0; i < 100; i++) {
            file_ids.push_back(std::to_string(i));
        }
G
groot 已提交
374 375
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
376 377 378 379 380
        stat = db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k,
                                  json_params,
                                  xq,
                                  result_ids,
                                  result_distances);
S
starlord 已提交
381
        ASSERT_TRUE(stat.ok());
S
shengjh 已提交
382 383

        FIU_ENABLE_FIU("SqliteMetaImpl.FilesToSearch.throw_exception");
384 385
        stat =
            db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, json_params, xq, result_ids, result_distances);
S
shengjh 已提交
386 387 388 389
        ASSERT_FALSE(stat.ok());
        fiu_disable("SqliteMetaImpl.FilesToSearch.throw_exception");

        FIU_ENABLE_FIU("DBImpl.QueryByFileID.empty_files_array");
390 391
        stat =
            db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, json_params, xq, result_ids, result_distances);
S
shengjh 已提交
392 393
        ASSERT_FALSE(stat.ok());
        fiu_disable("DBImpl.QueryByFileID.empty_files_array");
X
xj.lin 已提交
394
    }
X
xj.lin 已提交
395

396 397
    // TODO(zhiru): PQ build takes forever
#if 0
398 399 400 401
    {
        std::vector<std::string> tags;
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
402
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
403
        ASSERT_TRUE(stat.ok());
404
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
S
shengjh 已提交
405 406 407
        ASSERT_TRUE(stat.ok());

        FIU_ENABLE_FIU("SqliteMetaImpl.FilesToSearch.throw_exception");
408
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, json_params, xq, result_ids, result_distances);
S
shengjh 已提交
409 410
        ASSERT_FALSE(stat.ok());
        fiu_disable("SqliteMetaImpl.FilesToSearch.throw_exception");
411
    }
412
#endif
413

Z
update  
zhiru 已提交
414
#ifdef CUSTOMIZATION
415
#ifdef MILVUS_GPU_VERSION
Y
youny626 已提交
416
    // test FAISS_IVFSQ8H optimizer
417
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
Y
youny626 已提交
418
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
419 420 421
    std::vector<std::string> partition_tag;
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_dists;
422 423

    {
424 425
        result_ids.clear();
        result_dists.clear();
426
        stat = db_->Query(dummy_context_, TABLE_NAME, partition_tag, k, json_params, xq, result_ids, result_dists);
Y
Yu Kun 已提交
427 428 429
        ASSERT_TRUE(stat.ok());
    }

Y
youny626 已提交
430
    {  // search by specify index file
431 432 433 434 435 436 437
        std::vector<std::string> file_ids;
        // sometimes this case run fast to merge file and build index, old file will be deleted immediately,
        // so the QueryByFileID cannot get files to search
        // input 100 files ids to avoid random failure of this case
        for (int i = 0; i < 100; i++) {
            file_ids.push_back(std::to_string(i));
        }
438 439
        result_ids.clear();
        result_dists.clear();
440
        stat = db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, json_params, xq, result_ids, result_dists);
441 442
        ASSERT_TRUE(stat.ok());
    }
443
#endif
X
xiaojun.lin 已提交
444
#endif
S
starlord 已提交
445
}
Y
c  
yu yunfeng 已提交
446

Y
Yu Kun 已提交
447
TEST_F(DBTest, PRELOADTABLE_TEST) {
S
shengjh 已提交
448 449
    fiu_init(0);

S
starlord 已提交
450
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
451
    auto stat = db_->CreateTable(table_info);
Y
Yu Kun 已提交
452

S
starlord 已提交
453
    milvus::engine::meta::TableSchema table_info_get;
Y
Yu Kun 已提交
454 455
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
456
    ASSERT_TRUE(stat.ok());
Y
Yu Kun 已提交
457 458
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);

Y
Yu Kun 已提交
459
    int loop = 5;
S
starlord 已提交
460
    for (auto i = 0; i < loop; ++i) {
461 462 463 464
        uint64_t nb = VECTOR_COUNT;
        milvus::engine::VectorsData xb;
        BuildVectors(nb, i, xb);

G
groot 已提交
465 466
        db_->InsertVectors(TABLE_NAME, "", xb);
        ASSERT_EQ(xb.id_array_.size(), nb);
Y
Yu Kun 已提交
467
    }
468

S
starlord 已提交
469
    milvus::engine::TableIndex index;
Y
youny626 已提交
470 471
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IDMAP;
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
Y
Yu Kun 已提交
472

S
starlord 已提交
473
    int64_t prev_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
474
    stat = db_->PreloadTable(TABLE_NAME);
S
starlord 已提交
475
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
476
    int64_t cur_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
477
    ASSERT_TRUE(prev_cache_usage < cur_cache_usage);
S
shengjh 已提交
478 479 480 481 482 483

    FIU_ENABLE_FIU("SqliteMetaImpl.FilesToSearch.throw_exception");
    stat = db_->PreloadTable(TABLE_NAME);
    ASSERT_FALSE(stat.ok());
    fiu_disable("SqliteMetaImpl.FilesToSearch.throw_exception");

484
    // create a partition
S
shengjh 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    stat = db_->CreatePartition(TABLE_NAME, "part0", "0");
    ASSERT_TRUE(stat.ok());
    stat = db_->PreloadTable(TABLE_NAME);
    ASSERT_TRUE(stat.ok());

    FIU_ENABLE_FIU("DBImpl.PreloadTable.null_engine");
    stat = db_->PreloadTable(TABLE_NAME);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.PreloadTable.null_engine");

    FIU_ENABLE_FIU("DBImpl.PreloadTable.exceed_cache");
    stat = db_->PreloadTable(TABLE_NAME);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.PreloadTable.exceed_cache");

    FIU_ENABLE_FIU("DBImpl.PreloadTable.engine_throw_exception");
    stat = db_->PreloadTable(TABLE_NAME);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.PreloadTable.engine_throw_exception");
Y
Yu Kun 已提交
504 505
}

S
starlord 已提交
506 507 508
TEST_F(DBTest, SHUTDOWN_TEST) {
    db_->Stop();

S
starlord 已提交
509
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
510
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
511 512 513 514 515
    ASSERT_FALSE(stat.ok());

    stat = db_->DescribeTable(table_info);
    ASSERT_FALSE(stat.ok());

S
shengjh 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    stat = db_->UpdateTableFlag(TABLE_NAME, 0);
    ASSERT_FALSE(stat.ok());

    stat = db_->CreatePartition(TABLE_NAME, "part0", "0");
    ASSERT_FALSE(stat.ok());

    stat = db_->DropPartition("part0");
    ASSERT_FALSE(stat.ok());

    stat = db_->DropPartitionByTag(TABLE_NAME, "0");
    ASSERT_FALSE(stat.ok());

    std::vector<milvus::engine::meta::TableSchema> partition_schema_array;
    stat = db_->ShowPartitions(TABLE_NAME, partition_schema_array);
    ASSERT_FALSE(stat.ok());

    std::vector<milvus::engine::meta::TableSchema> table_infos;
    stat = db_->AllTables(table_infos);
    ASSERT_EQ(stat.code(), milvus::DB_ERROR);

S
starlord 已提交
536 537 538 539
    bool has_table = false;
    stat = db_->HasTable(table_info.table_id_, has_table);
    ASSERT_FALSE(stat.ok());

G
groot 已提交
540 541
    milvus::engine::VectorsData xb;
    stat = db_->InsertVectors(table_info.table_id_, "", xb);
S
starlord 已提交
542 543
    ASSERT_FALSE(stat.ok());

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    stat = db_->Flush();
    ASSERT_FALSE(stat.ok());

    stat = db_->DeleteVector(table_info.table_id_, 0);
    ASSERT_FALSE(stat.ok());

    milvus::engine::IDNumbers ids_to_delete{0};
    stat = db_->DeleteVectors(table_info.table_id_, ids_to_delete);
    ASSERT_FALSE(stat.ok());

    stat = db_->Compact(table_info.table_id_);
    ASSERT_FALSE(stat.ok());

    milvus::engine::VectorsData vector;
    stat = db_->GetVectorByID(table_info.table_id_, 0, vector);
    ASSERT_FALSE(stat.ok());

S
starlord 已提交
561 562 563 564 565 566 567
    stat = db_->PreloadTable(table_info.table_id_);
    ASSERT_FALSE(stat.ok());

    uint64_t row_count = 0;
    stat = db_->GetTableRowCount(table_info.table_id_, row_count);
    ASSERT_FALSE(stat.ok());

S
starlord 已提交
568
    milvus::engine::TableIndex index;
S
starlord 已提交
569 570 571 572 573 574
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_FALSE(stat.ok());

    stat = db_->DescribeIndex(table_info.table_id_, index);
    ASSERT_FALSE(stat.ok());

S
shengjh 已提交
575 576 577
    stat = db_->DropIndex(TABLE_NAME);
    ASSERT_FALSE(stat.ok());

G
groot 已提交
578 579 580
    std::vector<std::string> tags;
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_distances;
581 582
    milvus::json json_params = {{"nprobe", 1}};
    stat = db_->Query(dummy_context_, table_info.table_id_, tags, 1, json_params, xb, result_ids, result_distances);
S
starlord 已提交
583 584
    ASSERT_FALSE(stat.ok());
    std::vector<std::string> file_ids;
585 586 587 588 589 590 591 592
    stat = db_->QueryByFileID(dummy_context_,
                              table_info.table_id_,
                              file_ids,
                              1,
                              json_params,
                              xb,
                              result_ids,
                              result_distances);
S
starlord 已提交
593 594
    ASSERT_FALSE(stat.ok());

595 596 597 598 599 600 601
    stat = db_->Query(dummy_context_,
                      table_info.table_id_,
                      tags,
                      1,
                      json_params,
                      milvus::engine::VectorsData(),
                      result_ids,
602
                      result_distances);
S
shengjh 已提交
603 604
    ASSERT_FALSE(stat.ok());

605
    stat = db_->DropTable(table_info.table_id_);
S
starlord 已提交
606 607 608
    ASSERT_FALSE(stat.ok());
}

S
shengjh 已提交
609 610 611 612
TEST_F(DBTest, BACK_TIMER_THREAD_1) {
    fiu_init(0);
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    milvus::Status stat;
613
    // test background timer thread
S
shengjh 已提交
614 615 616 617 618 619
    {
        FIU_ENABLE_FIU("DBImpl.StartMetricTask.InvalidTotalCache");
        FIU_ENABLE_FIU("SqliteMetaImpl.FilesToMerge.throw_exception");
        stat = db_->CreateTable(table_info);
        ASSERT_TRUE(stat.ok());

620
        // insert some vector to create some tablefiles
S
shengjh 已提交
621 622
        int loop = 10;
        for (auto i = 0; i < loop; ++i) {
623 624 625
            int64_t nb = VECTOR_COUNT;
            milvus::engine::VectorsData xb;
            BuildVectors(nb, i, xb);
S
shengjh 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
            db_->InsertVectors(TABLE_NAME, "", xb);
            ASSERT_EQ(xb.id_array_.size(), nb);
        }

        std::this_thread::sleep_for(std::chrono::seconds(2));
        db_->Stop();
        fiu_disable("DBImpl.StartMetricTask.InvalidTotalCache");
        fiu_disable("SqliteMetaImpl.FilesToMerge.throw_exception");
    }

    FIU_ENABLE_FIU("DBImpl.StartMetricTask.InvalidTotalCache");
    db_->Start();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    db_->Stop();
    fiu_disable("DBImpl.StartMetricTask.InvalidTotalCache");
}

TEST_F(DBTest, BACK_TIMER_THREAD_2) {
    fiu_init(0);
    milvus::Status stat;
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();

    stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

651
    // insert some vector to create some tablefiles
S
shengjh 已提交
652 653
    int loop = 10;
    for (auto i = 0; i < loop; ++i) {
654 655 656
        int64_t nb = VECTOR_COUNT;
        milvus::engine::VectorsData xb;
        BuildVectors(nb, i, xb);
S
shengjh 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
        db_->InsertVectors(TABLE_NAME, "", xb);
        ASSERT_EQ(xb.id_array_.size(), nb);
    }

    FIU_ENABLE_FIU("SqliteMetaImpl.CreateTableFile.throw_exception");
    std::this_thread::sleep_for(std::chrono::seconds(2));
    db_->Stop();
    fiu_disable("SqliteMetaImpl.CreateTableFile.throw_exception");
}

TEST_F(DBTest, BACK_TIMER_THREAD_3) {
    fiu_init(0);
    milvus::Status stat;
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();

    stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

675
    // insert some vector to create some tablefiles
S
shengjh 已提交
676 677
    int loop = 10;
    for (auto i = 0; i < loop; ++i) {
678 679 680
        int64_t nb = VECTOR_COUNT;
        milvus::engine::VectorsData xb;
        BuildVectors(nb, i, xb);
S
shengjh 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
        db_->InsertVectors(TABLE_NAME, "", xb);
        ASSERT_EQ(xb.id_array_.size(), nb);
    }

    FIU_ENABLE_FIU("DBImpl.MergeFiles.Serialize_ThrowException");
    db_->Start();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    db_->Stop();
    fiu_disable("DBImpl.MergeFiles.Serialize_ThrowException");
}

TEST_F(DBTest, BACK_TIMER_THREAD_4) {
    fiu_init(0);
    milvus::Status stat;
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();

    stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

700
    // insert some vector to create some tablefiles
S
shengjh 已提交
701 702
    int loop = 10;
    for (auto i = 0; i < loop; ++i) {
703 704 705
        int64_t nb = VECTOR_COUNT;
        milvus::engine::VectorsData xb;
        BuildVectors(nb, i, xb);
S
shengjh 已提交
706 707 708 709 710 711 712 713 714 715 716
        db_->InsertVectors(TABLE_NAME, "", xb);
        ASSERT_EQ(xb.id_array_.size(), nb);
    }

    FIU_ENABLE_FIU("DBImpl.MergeFiles.Serialize_ErrorStatus");
    db_->Start();
    std::this_thread::sleep_for(std::chrono::seconds(2));
    db_->Stop();
    fiu_disable("DBImpl.MergeFiles.Serialize_ErrorStatus");
}

S
starlord 已提交
717
TEST_F(DBTest, INDEX_TEST) {
S
starlord 已提交
718
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
719
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
720

G
groot 已提交
721 722
    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
723
    BuildVectors(nb, 0, xb);
S
starlord 已提交
724

G
groot 已提交
725 726
    db_->InsertVectors(TABLE_NAME, "", xb);
    ASSERT_EQ(xb.id_array_.size(), nb);
S
starlord 已提交
727

S
starlord 已提交
728
    milvus::engine::TableIndex index;
Y
youny626 已提交
729 730
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8;
    index.metric_type_ = (int)milvus::engine::MetricType::IP;
S
starlord 已提交
731 732 733
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

Y
youny626 已提交
734
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
Y
Yu Kun 已提交
735 736 737
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

S
shengjh 已提交
738 739 740 741 742 743 744 745 746 747 748 749
    fiu_init(0);
    FIU_ENABLE_FIU("SqliteMetaImpl.DescribeTableIndex.throw_exception");
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_FALSE(stat.ok());
    fiu_disable("SqliteMetaImpl.DescribeTableIndex.throw_exception");

    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_PQ;
    FIU_ENABLE_FIU("DBImpl.UpdateTableIndexRecursively.fail_update_table_index");
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.UpdateTableIndexRecursively.fail_update_table_index");

Y
Yu Kun 已提交
750
#ifdef CUSTOMIZATION
751
#ifdef MILVUS_GPU_VERSION
Y
Yu Kun 已提交
752 753 754
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());
755
#endif
Y
Yu Kun 已提交
756 757
#endif

S
starlord 已提交
758
    milvus::engine::TableIndex index_out;
S
starlord 已提交
759
    stat = db_->DescribeIndex(table_info.table_id_, index_out);
S
starlord 已提交
760 761
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(index.engine_type_, index_out.engine_type_);
762
    ASSERT_EQ(index.extra_params_, index_out.extra_params_);
S
starlord 已提交
763
    ASSERT_EQ(table_info.metric_type_, index_out.metric_type_);
S
starlord 已提交
764 765 766 767 768

    stat = db_->DropIndex(table_info.table_id_);
    ASSERT_TRUE(stat.ok());
}

G
groot 已提交
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
TEST_F(DBTest, PARTITION_TEST) {
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    auto stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

    // create partition and insert data
    const int64_t PARTITION_COUNT = 5;
    const int64_t INSERT_BATCH = 2000;
    std::string table_name = TABLE_NAME;
    for (int64_t i = 0; i < PARTITION_COUNT; i++) {
        std::string partition_tag = std::to_string(i);
        std::string partition_name = table_name + "_" + partition_tag;
        stat = db_->CreatePartition(table_name, partition_name, partition_tag);
        ASSERT_TRUE(stat.ok());

784 785 786 787 788 789 790
        // not allow nested partition
        stat = db_->CreatePartition(partition_name, "dumy", "dummy");
        ASSERT_FALSE(stat.ok());

        // not allow duplicated partition
        stat = db_->CreatePartition(table_name, partition_name, partition_tag);
        ASSERT_FALSE(stat.ok());
G
groot 已提交
791

G
groot 已提交
792
        milvus::engine::VectorsData xb;
793
        BuildVectors(INSERT_BATCH, i, xb);
G
groot 已提交
794 795 796 797

        milvus::engine::IDNumbers vector_ids;
        vector_ids.resize(INSERT_BATCH);
        for (int64_t k = 0; k < INSERT_BATCH; k++) {
Z
Zhiru Zhu 已提交
798
            vector_ids[k] = i * INSERT_BATCH + k;
G
groot 已提交
799 800
        }

G
groot 已提交
801
        db_->InsertVectors(table_name, partition_tag, xb);
G
groot 已提交
802
        ASSERT_EQ(vector_ids.size(), INSERT_BATCH);
S
shengjh 已提交
803

804
        // insert data into not existed partition
S
shengjh 已提交
805 806
        stat = db_->InsertVectors(TABLE_NAME, "notexist", xb);
        ASSERT_FALSE(stat.ok());
G
groot 已提交
807 808
    }

Z
Zhiru Zhu 已提交
809
    // duplicated partition is not allowed
G
groot 已提交
810 811 812
    stat = db_->CreatePartition(table_name, "", "0");
    ASSERT_FALSE(stat.ok());

G
groot 已提交
813 814
    std::vector<milvus::engine::meta::TableSchema> partition_schema_array;
    stat = db_->ShowPartitions(table_name, partition_schema_array);
G
groot 已提交
815
    ASSERT_TRUE(stat.ok());
G
groot 已提交
816
    ASSERT_EQ(partition_schema_array.size(), PARTITION_COUNT);
G
groot 已提交
817
    for (int64_t i = 0; i < PARTITION_COUNT; i++) {
G
groot 已提交
818
        ASSERT_EQ(partition_schema_array[i].table_id_, table_name + "_" + std::to_string(i));
G
groot 已提交
819 820
    }

821 822 823 824 825 826 827 828 829 830
    // check table existence
    std::string special_part = "special";
    stat = db_->CreatePartition(table_name, special_part, special_part);
    ASSERT_TRUE(stat.ok());
    bool has_table = false;
    stat = db_->HasNativeTable(special_part, has_table);
    ASSERT_FALSE(has_table);
    stat = db_->HasTable(special_part, has_table);
    ASSERT_TRUE(has_table);

Z
Zhiru Zhu 已提交
831
    {  // build index
G
groot 已提交
832
        milvus::engine::TableIndex index;
Z
Zhiru Zhu 已提交
833 834
        index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
        index.metric_type_ = (int)milvus::engine::MetricType::L2;
G
groot 已提交
835 836 837
        stat = db_->CreateIndex(table_info.table_id_, index);
        ASSERT_TRUE(stat.ok());

S
shengjh 已提交
838
        fiu_init(0);
839
        FIU_ENABLE_FIU("DBImpl.WaitTableIndexRecursively.fail_build_table_Index_for_partition");
S
shengjh 已提交
840 841
        stat = db_->CreateIndex(table_info.table_id_, index);
        ASSERT_FALSE(stat.ok());
842
        fiu_disable("DBImpl.WaitTableIndexRecursively.fail_build_table_Index_for_partition");
S
shengjh 已提交
843

844
        FIU_ENABLE_FIU("DBImpl.WaitTableIndexRecursively.not_empty_err_msg");
S
shengjh 已提交
845 846
        stat = db_->CreateIndex(table_info.table_id_, index);
        ASSERT_FALSE(stat.ok());
847
        fiu_disable("DBImpl.WaitTableIndexRecursively.not_empty_err_msg");
S
shengjh 已提交
848

G
groot 已提交
849 850 851
        uint64_t row_count = 0;
        stat = db_->GetTableRowCount(TABLE_NAME, row_count);
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
852
        ASSERT_EQ(row_count, INSERT_BATCH * PARTITION_COUNT);
S
shengjh 已提交
853 854 855 856 857 858 859 860 861 862

        FIU_ENABLE_FIU("SqliteMetaImpl.Count.throw_exception");
        stat = db_->GetTableRowCount(TABLE_NAME, row_count);
        ASSERT_FALSE(stat.ok());
        fiu_disable("SqliteMetaImpl.Count.throw_exception");

        FIU_ENABLE_FIU("DBImpl.GetTableRowCountRecursively.fail_get_table_rowcount_for_partition");
        stat = db_->GetTableRowCount(TABLE_NAME, row_count);
        ASSERT_FALSE(stat.ok());
        fiu_disable("DBImpl.GetTableRowCountRecursively.fail_get_table_rowcount_for_partition");
G
groot 已提交
863 864
    }

Z
Zhiru Zhu 已提交
865
    {  // search
G
groot 已提交
866 867 868
        const int64_t nq = 5;
        const int64_t topk = 10;
        const int64_t nprobe = 10;
G
groot 已提交
869
        milvus::engine::VectorsData xq;
870
        BuildVectors(nq, 0, xq);
G
groot 已提交
871 872 873 874 875

        // specify partition tags
        std::vector<std::string> tags = {"0", std::to_string(PARTITION_COUNT - 1)};
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
876 877 878
        milvus::json json_params = {{"nprobe", nprobe}};

        stat = db_->Query(dummy_context_, TABLE_NAME, tags, topk, json_params, xq, result_ids, result_distances);
G
groot 已提交
879
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
880
        ASSERT_EQ(result_ids.size() / topk, nq);
G
groot 已提交
881 882 883 884 885

        // search in whole table
        tags.clear();
        result_ids.clear();
        result_distances.clear();
886
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, topk, json_params, xq, result_ids, result_distances);
G
groot 已提交
887
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
888
        ASSERT_EQ(result_ids.size() / topk, nq);
G
groot 已提交
889 890 891 892 893

        // search in all partitions(tag regex match)
        tags.push_back("\\d");
        result_ids.clear();
        result_distances.clear();
894
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, topk, json_params, xq, result_ids, result_distances);
G
groot 已提交
895
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
896
        ASSERT_EQ(result_ids.size() / topk, nq);
G
groot 已提交
897 898 899 900 901 902 903 904
    }

    stat = db_->DropPartition(table_name + "_0");
    ASSERT_TRUE(stat.ok());

    stat = db_->DropPartitionByTag(table_name, "1");
    ASSERT_TRUE(stat.ok());

S
shengjh 已提交
905 906 907 908 909 910 911 912 913 914
    FIU_ENABLE_FIU("DBImpl.DropTableIndexRecursively.fail_drop_table_Index_for_partition");
    stat = db_->DropIndex(table_info.table_id_);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.DropTableIndexRecursively.fail_drop_table_Index_for_partition");

    FIU_ENABLE_FIU("DBImpl.DropTableIndexRecursively.fail_drop_table_Index_for_partition");
    stat = db_->DropIndex(table_info.table_id_);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.DropTableIndexRecursively.fail_drop_table_Index_for_partition");

G
groot 已提交
915 916 917
    stat = db_->DropIndex(table_name);
    ASSERT_TRUE(stat.ok());

918
    stat = db_->DropTable(table_name);
G
groot 已提交
919 920 921
    ASSERT_TRUE(stat.ok());
}

G
groot 已提交
922
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
S
starlord 已提交
923
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
924
    auto stat = db_->CreateTable(table_info);
Z
zhiru 已提交
925

S
starlord 已提交
926
    std::vector<milvus::engine::meta::TableSchema> table_schema_array;
G
groot 已提交
927
    stat = db_->AllTables(table_schema_array);
S
starlord 已提交
928
    ASSERT_TRUE(stat.ok());
G
groot 已提交
929
    bool bfound = false;
Y
youny626 已提交
930
    for (auto& schema : table_schema_array) {
S
starlord 已提交
931
        if (schema.table_id_ == TABLE_NAME) {
G
groot 已提交
932 933 934 935 936 937
            bfound = true;
            break;
        }
    }
    ASSERT_TRUE(bfound);

S
starlord 已提交
938
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
939 940
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
941
    ASSERT_TRUE(stat.ok());
G
groot 已提交
942
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
Z
zhiru 已提交
943

G
groot 已提交
944
    uint64_t size;
Z
zhiru 已提交
945 946
    db_->Size(size);

G
groot 已提交
947
    int loop = INSERT_LOOP;
S
starlord 已提交
948
    for (auto i = 0; i < loop; ++i) {
949 950 951 952
        uint64_t nb = 10;
        milvus::engine::VectorsData xb;
        BuildVectors(nb, i, xb);

G
groot 已提交
953
        db_->InsertVectors(TABLE_NAME, "", xb);
Z
zhiru 已提交
954 955 956
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

G
groot 已提交
957
    std::this_thread::sleep_for(std::chrono::seconds(1));
Z
zhiru 已提交
958 959 960

    db_->Size(size);
    LOG(DEBUG) << "size=" << size;
S
starlord 已提交
961
    ASSERT_LE(size, 1 * milvus::engine::G);
S
starlord 已提交
962
}
Z
zhiru 已提交
963

G
groot 已提交
964
TEST_F(DBTest2, DELETE_TEST) {
S
starlord 已提交
965
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
966
    auto stat = db_->CreateTable(table_info);
G
groot 已提交
967

S
starlord 已提交
968
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
969 970
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
971
    ASSERT_TRUE(stat.ok());
G
groot 已提交
972

S
starlord 已提交
973 974 975
    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);
G
groot 已提交
976 977 978 979

    uint64_t size;
    db_->Size(size);

G
groot 已提交
980 981
    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
982
    BuildVectors(nb, 0, xb);
G
groot 已提交
983

S
starlord 已提交
984
    milvus::engine::IDNumbers vector_ids;
G
groot 已提交
985
    stat = db_->InsertVectors(TABLE_NAME, "", xb);
S
starlord 已提交
986
    milvus::engine::TableIndex index;
987
    stat = db_->CreateIndex(TABLE_NAME, index);
G
groot 已提交
988

989
    // create partition, drop table will drop partition recursively
S
shengjh 已提交
990 991 992
    stat = db_->CreatePartition(TABLE_NAME, "part0", "0");
    ASSERT_TRUE(stat.ok());

993
    // fail drop table
S
shengjh 已提交
994 995
    fiu_init(0);
    FIU_ENABLE_FIU("DBImpl.DropTableRecursively.failed");
996
    stat = db_->DropTable(TABLE_NAME);
S
shengjh 已提交
997 998 999
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.DropTableRecursively.failed");

1000 1001
    stat = db_->DropTable(TABLE_NAME);

G
groot 已提交
1002 1003
    std::this_thread::sleep_for(std::chrono::seconds(2));
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
1004 1005 1006

    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_FALSE(has_table);
S
starlord 已提交
1007
}
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
TEST_F(DBTest2, SHOW_TABLE_INFO_TEST) {
    std::string table_name = TABLE_NAME;
    milvus::engine::meta::TableSchema table_schema = BuildTableSchema();
    auto stat = db_->CreateTable(table_schema);

    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
    BuildVectors(nb, 0, xb);

    milvus::engine::IDNumbers vector_ids;
    stat = db_->InsertVectors(table_name, "", xb);

    // create partition and insert data
    const int64_t PARTITION_COUNT = 2;
    const int64_t INSERT_BATCH = 2000;
    for (int64_t i = 0; i < PARTITION_COUNT; i++) {
        std::string partition_tag = std::to_string(i);
        std::string partition_name = table_name + "_" + partition_tag;
        stat = db_->CreatePartition(table_name, partition_name, partition_tag);
        ASSERT_TRUE(stat.ok());

        milvus::engine::VectorsData xb;
        BuildVectors(INSERT_BATCH, i, xb);

        db_->InsertVectors(table_name, partition_tag, xb);
    }

    stat = db_->Flush();
    ASSERT_TRUE(stat.ok());

    {
        milvus::engine::TableInfo table_info;
        stat = db_->GetTableInfo(table_name, table_info);
        ASSERT_TRUE(stat.ok());
        int64_t row_count = 0;
        for (auto& part : table_info.partitions_stat_) {
            row_count = 0;
            for (auto& stat : part.segments_stat_) {
                row_count += stat.row_count_;
                ASSERT_EQ(stat.index_name_, "IDMAP");
                ASSERT_GT(stat.data_size_, 0);
            }
            if (part.tag_ == milvus::engine::DEFAULT_PARTITON_TAG) {
                ASSERT_EQ(row_count, VECTOR_COUNT);
            } else {
                ASSERT_EQ(row_count, INSERT_BATCH);
            }
        }
    }
}

TEST_F(DBTestWAL, DB_INSERT_TEST) {
S
starlord 已提交
1061
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
1062
    auto stat = db_->CreateTable(table_info);
1063
    ASSERT_TRUE(stat.ok());
1064

1065 1066 1067 1068 1069 1070 1071
    uint64_t qb = 100;
    milvus::engine::VectorsData qxb;
    BuildVectors(qb, 0, qxb);

    std::string partition_name = "part_name";
    std::string partition_tag = "part_tag";
    stat = db_->CreatePartition(table_info.table_id_, partition_name, partition_tag);
S
starlord 已提交
1072
    ASSERT_TRUE(stat.ok());
1073

1074 1075
    stat = db_->InsertVectors(table_info.table_id_, partition_tag, qxb);
    ASSERT_TRUE(stat.ok());
1076

1077 1078
    stat = db_->InsertVectors(table_info.table_id_, "", qxb);
    ASSERT_TRUE(stat.ok());
1079

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    stat = db_->InsertVectors(table_info.table_id_, "not exist", qxb);
    ASSERT_FALSE(stat.ok());

    db_->Flush(table_info.table_id_);

    stat = db_->DropTable(table_info.table_id_);
    ASSERT_TRUE(stat.ok());
}

TEST_F(DBTestWAL, DB_STOP_TEST) {
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    auto stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

    uint64_t qb = 100;
    for (int i = 0; i < 5; i++) {
        milvus::engine::VectorsData qxb;
        BuildVectors(qb, i, qxb);
        stat = db_->InsertVectors(table_info.table_id_, "", qxb);
        ASSERT_TRUE(stat.ok());
    }

    db_->Stop();
    db_->Start();

    const int64_t topk = 10;
    const int64_t nprobe = 10;
1107
    milvus::json json_params = {{"nprobe", nprobe}};
1108 1109 1110 1111
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_distances;
    milvus::engine::VectorsData qxb;
    BuildVectors(qb, 0, qxb);
1112
    stat = db_->Query(dummy_context_, table_info.table_id_, {}, topk, json_params, qxb, result_ids, result_distances);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(result_ids.size() / topk, qb);

    stat = db_->DropTable(table_info.table_id_);
    ASSERT_TRUE(stat.ok());
}

TEST_F(DBTestWALRecovery, RECOVERY_WITH_NO_ERROR) {
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    auto stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

    uint64_t qb = 100;

    for (int i = 0; i < 5; i++) {
        milvus::engine::VectorsData qxb;
        BuildVectors(qb, i, qxb);
        stat = db_->InsertVectors(table_info.table_id_, "", qxb);
        ASSERT_TRUE(stat.ok());
    }

    const int64_t topk = 10;
    const int64_t nprobe = 10;
1136
    milvus::json json_params = {{"nprobe", nprobe}};
1137 1138 1139 1140
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_distances;
    milvus::engine::VectorsData qxb;
    BuildVectors(qb, 0, qxb);
1141
    stat = db_->Query(dummy_context_, table_info.table_id_, {}, topk, json_params, qxb, result_ids, result_distances);
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
    ASSERT_TRUE(stat.ok());
    ASSERT_NE(result_ids.size() / topk, qb);

    fiu_init(0);
    fiu_enable("DBImpl.ExexWalRecord.return", 1, nullptr, 0);
    db_ = nullptr;
    fiu_disable("DBImpl.ExexWalRecord.return");
    auto options = GetOptions();
    db_ = milvus::engine::DBFactory::Build(options);

    result_ids.clear();
    result_distances.clear();
1154
    stat = db_->Query(dummy_context_, table_info.table_id_, {}, topk, json_params, qxb, result_ids, result_distances);
1155 1156 1157 1158 1159 1160
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(result_ids.size(), 0);

    db_->Flush();
    result_ids.clear();
    result_distances.clear();
1161
    stat = db_->Query(dummy_context_, table_info.table_id_, {}, topk, json_params, qxb, result_ids, result_distances);
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(result_ids.size() / topk, qb);
}

TEST_F(DBTestWALRecovery_Error, RECOVERY_WITH_INVALID_LOG_FILE) {
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    auto stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

    uint64_t qb = 100;
    milvus::engine::VectorsData qxb;
    BuildVectors(qb, 0, qxb);

    stat = db_->InsertVectors(table_info.table_id_, "", qxb);
    ASSERT_TRUE(stat.ok());

    fiu_init(0);
    fiu_enable("DBImpl.ExexWalRecord.return", 1, nullptr, 0);
    db_ = nullptr;
    fiu_disable("DBImpl.ExexWalRecord.return");

    auto options = GetOptions();
    // delete wal log file so that recovery will failed when start db next time.
    boost::filesystem::remove(options.mxlog_path_ + "0.wal");
    ASSERT_ANY_THROW(db_ = milvus::engine::DBFactory::Build(options));
}

TEST_F(DBTest2, FLUSH_NON_EXISTING_TABLE) {
    auto status = db_->Flush("non_existing_table");
    ASSERT_FALSE(status.ok());
}

TEST_F(DBTest2, GET_VECTOR_NON_EXISTING_TABLE) {
    milvus::engine::VectorsData vector;
    auto status = db_->GetVectorByID("non_existing_table", 0, vector);
    ASSERT_FALSE(status.ok());
}

TEST_F(DBTest2, GET_VECTOR_BY_ID_TEST) {
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    auto stat = db_->CreateTable(table_info);
    ASSERT_TRUE(stat.ok());

    uint64_t qb = 1000;
    milvus::engine::VectorsData qxb;
    BuildVectors(qb, 0, qxb);

    std::string partition_name = "part_name";
    std::string partition_tag = "part_tag";
    stat = db_->CreatePartition(table_info.table_id_, partition_name, partition_tag);
    ASSERT_TRUE(stat.ok());

    stat = db_->InsertVectors(table_info.table_id_, partition_tag, qxb);
    ASSERT_TRUE(stat.ok());

    db_->Flush(table_info.table_id_);

    milvus::engine::VectorsData vector_data;
    stat = db_->GetVectorByID(TABLE_NAME, qxb.id_array_[0], vector_data);
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(vector_data.vector_count_, 1);
    ASSERT_EQ(vector_data.float_data_.size(), TABLE_DIM);

    for (int64_t i = 0; i < TABLE_DIM; i++) {
        ASSERT_FLOAT_EQ(vector_data.float_data_[i], qxb.float_data_[i]);
    }
}

TEST_F(DBTest2, GET_VECTOR_IDS_TEST) {
    milvus::engine::meta::TableSchema table_schema = BuildTableSchema();
    auto stat = db_->CreateTable(table_schema);
    ASSERT_TRUE(stat.ok());

    uint64_t BATCH_COUNT = 1000;
    milvus::engine::VectorsData vector_1;
    BuildVectors(BATCH_COUNT, 0, vector_1);

    stat = db_->InsertVectors(TABLE_NAME, "", vector_1);
    ASSERT_TRUE(stat.ok());

    std::string partition_tag = "part_tag";
    stat = db_->CreatePartition(TABLE_NAME, "", partition_tag);
    ASSERT_TRUE(stat.ok());

    milvus::engine::VectorsData vector_2;
    BuildVectors(BATCH_COUNT, 1, vector_2);
    stat = db_->InsertVectors(TABLE_NAME, partition_tag, vector_2);
    ASSERT_TRUE(stat.ok());

    db_->Flush();

    milvus::engine::TableInfo table_info;
    stat = db_->GetTableInfo(TABLE_NAME, table_info);
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(table_info.partitions_stat_.size(), 2UL);

    std::string default_segment = table_info.partitions_stat_[0].segments_stat_[0].name_;
    std::string partition_segment = table_info.partitions_stat_[1].segments_stat_[0].name_;
1260

S
starlord 已提交
1261
    milvus::engine::IDNumbers vector_ids;
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    stat = db_->GetVectorIDs(TABLE_NAME, default_segment, vector_ids);
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(vector_ids.size(), BATCH_COUNT);

    stat = db_->GetVectorIDs(TABLE_NAME, partition_segment, vector_ids);
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(vector_ids.size(), BATCH_COUNT);

    milvus::engine::IDNumbers ids_to_delete{0, 100, 999, 1000, 1500, 1888, 1999};
    stat = db_->DeleteVectors(TABLE_NAME, ids_to_delete);
    ASSERT_TRUE(stat.ok());

    db_->Flush();

    stat = db_->GetVectorIDs(TABLE_NAME, default_segment, vector_ids);
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(vector_ids.size(), BATCH_COUNT - 3);

    stat = db_->GetVectorIDs(TABLE_NAME, partition_segment, vector_ids);
    ASSERT_TRUE(stat.ok());
    //    ASSERT_EQ(vector_ids.size(), BATCH_COUNT - 4);
}

TEST_F(DBTest2, INSERT_DUPLICATE_ID) {
    auto options = GetOptions();
    options.wal_enable_ = false;
    db_ = milvus::engine::DBFactory::Build(options);

    milvus::engine::meta::TableSchema table_schema = BuildTableSchema();
    auto stat = db_->CreateTable(table_schema);
    ASSERT_TRUE(stat.ok());

    uint64_t size = 20;
    milvus::engine::VectorsData vector;
    BuildVectors(size, 0, vector);
    vector.id_array_.clear();
    for (int i = 0; i < size; ++i) {
        vector.id_array_.emplace_back(0);
    }

    stat = db_->InsertVectors(TABLE_NAME, "", vector);
    ASSERT_TRUE(stat.ok());

    stat = db_->Flush(TABLE_NAME);
    ASSERT_TRUE(stat.ok());
}

/*
TEST_F(DBTest2, SEARCH_WITH_DIFFERENT_INDEX) {
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    // table_info.index_file_size_ = 1 * milvus::engine::M;
    auto stat = db_->CreateTable(table_info);

    int loop = 10;
    uint64_t nb = 100000;
    for (auto i = 0; i < loop; ++i) {
        milvus::engine::VectorsData xb;
        BuildVectors(nb, i, xb);

        db_->InsertVectors(TABLE_NAME, "", xb);
        stat = db_->Flush();
        ASSERT_TRUE(stat.ok());
    }

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int64_t> dis(0, nb * loop - 1);

    int64_t num_query = 10;
    std::vector<int64_t> ids_to_search;
    for (int64_t i = 0; i < num_query; ++i) {
        int64_t index = dis(gen);
        ids_to_search.emplace_back(index);
    }

S
starlord 已提交
1337
    milvus::engine::TableIndex index;
1338 1339 1340 1341
    // index.metric_type_ = (int)milvus::engine::MetricType::IP;
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());
1342

1343 1344 1345 1346
    stat = db_->PreloadTable(table_info.table_id_);
    ASSERT_TRUE(stat.ok());

    int topk = 10, nprobe = 10;
1347
    milvus::json json_params = {{"nprobe", nprobe}};
1348 1349 1350 1351 1352 1353 1354

    for (auto id : ids_to_search) {
        //        std::cout << "xxxxxxxxxxxxxxxxxxxx " << i << std::endl;
        std::vector<std::string> tags;
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;

1355
        stat = db_->QueryByID(dummy_context_, table_info.table_id_, tags, topk, json_params, id, result_ids,
1356 1357 1358 1359 1360
result_distances);
        ASSERT_TRUE(stat.ok());
        ASSERT_EQ(result_ids[0], id);
        ASSERT_LT(result_distances[0], 1e-4);
    }
1361

1362
    db_->DropIndex(table_info.table_id_);
1363

1364 1365
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8;
    stat = db_->CreateIndex(table_info.table_id_, index);
S
starlord 已提交
1366
    ASSERT_TRUE(stat.ok());
1367

1368 1369 1370 1371 1372 1373 1374 1375 1376
    stat = db_->PreloadTable(table_info.table_id_);
    ASSERT_TRUE(stat.ok());

    for (auto id : ids_to_search) {
        //        std::cout << "xxxxxxxxxxxxxxxxxxxx " << i << std::endl;
        std::vector<std::string> tags;
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;

1377
        stat = db_->QueryByID(dummy_context_, table_info.table_id_, tags, topk, json_params, id, result_ids,
1378 1379 1380 1381 1382
result_distances);
        ASSERT_TRUE(stat.ok());
        ASSERT_EQ(result_ids[0], id);
        ASSERT_LT(result_distances[0], 1e-4);
    }
S
starlord 已提交
1383
}
1384
 */