test_db.cpp 34.1 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.

Z
Zhiru Zhu 已提交
18
#include <gtest/gtest.h>
S
shengjh 已提交
19 20
#include <fiu-control.h>
#include <fiu-local.h>
Z
Zhiru Zhu 已提交
21 22 23 24 25

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

Y
youny626 已提交
26 27
#include "cache/CpuCacheMgr.h"
#include "db/Constants.h"
G
groot 已提交
28
#include "db/DB.h"
Y
youny626 已提交
29
#include "db/DBFactory.h"
X
Xu Peng 已提交
30
#include "db/DBImpl.h"
S
starlord 已提交
31
#include "db/meta/MetaConsts.h"
Y
youny626 已提交
32
#include "db/utils.h"
33
#include "server/Config.h"
Y
youny626 已提交
34
#include "utils/CommonUtil.h"
G
groot 已提交
35

G
groot 已提交
36 37
namespace {

Y
youny626 已提交
38
static const char* TABLE_NAME = "test_group";
S
starlord 已提交
39 40 41 42 43 44
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 已提交
45
milvus::engine::meta::TableSchema
S
starlord 已提交
46
BuildTableSchema() {
S
starlord 已提交
47
    milvus::engine::meta::TableSchema table_info;
S
starlord 已提交
48 49 50 51
    table_info.dimension_ = TABLE_DIM;
    table_info.table_id_ = TABLE_NAME;
    return table_info;
}
G
groot 已提交
52

S
starlord 已提交
53
void
G
groot 已提交
54 55 56 57 58 59 60
BuildVectors(uint64_t n, milvus::engine::VectorsData& vectors) {
    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 已提交
61
        data[TABLE_DIM * i] += i / 2000.;
G
groot 已提交
62
    }
S
starlord 已提交
63
}
G
groot 已提交
64

S
starlord 已提交
65 66 67 68 69 70 71 72
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);
73

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

S
starlord 已提交
77 78
    return str;
}
79

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

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

S
starlord 已提交
91
    if (!milvus::server::CommonUtil::TimeStrToTime(end_value, tt_end, tm_end)) {
S
starlord 已提交
92 93
        return;
    }
94

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

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

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

Y
youny626 已提交
110
}  // namespace
S
starlord 已提交
111

X
Xu Peng 已提交
112 113
TEST_F(DBTest, CONFIG_TEST) {
    {
S
starlord 已提交
114
        ASSERT_ANY_THROW(milvus::engine::ArchiveConf conf("wrong"));
115
        /* EXPECT_DEATH(engine::ArchiveConf conf("wrong"), ""); */
X
Xu Peng 已提交
116 117
    }
    {
S
starlord 已提交
118
        milvus::engine::ArchiveConf conf("delete");
X
Xu Peng 已提交
119 120
        ASSERT_EQ(conf.GetType(), "delete");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
121
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
122 123
    }
    {
S
starlord 已提交
124
        milvus::engine::ArchiveConf conf("swap");
X
Xu Peng 已提交
125 126
        ASSERT_EQ(conf.GetType(), "swap");
        auto criterias = conf.GetCriterias();
S
starlord 已提交
127
        ASSERT_EQ(criterias.size(), 0);
X
Xu Peng 已提交
128
    }
S
shengjh 已提交
129 130 131 132 133 134 135 136 137 138 139 140
    {
        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 已提交
141
    {
S
starlord 已提交
142 143 144
        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 已提交
145
        auto criterias = conf.GetCriterias();
S
starlord 已提交
146 147
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["disk"], 1024);
X
Xu Peng 已提交
148 149
    }
    {
S
starlord 已提交
150 151 152
        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 已提交
153
        auto criterias = conf.GetCriterias();
S
starlord 已提交
154 155
        ASSERT_EQ(criterias.size(), 1);
        ASSERT_EQ(criterias["days"], 100);
X
Xu Peng 已提交
156 157
    }
    {
S
starlord 已提交
158 159 160
        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 已提交
161
        auto criterias = conf.GetCriterias();
S
starlord 已提交
162 163 164
        ASSERT_EQ(criterias.size(), 2);
        ASSERT_EQ(criterias["days"], 100);
        ASSERT_EQ(criterias["disk"], 200);
X
Xu Peng 已提交
165 166 167
    }
}

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

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

G
groot 已提交
178 179
    uint64_t nb = 50;
    milvus::engine::VectorsData xb;
G
groot 已提交
180
    BuildVectors(nb, xb);
G
groot 已提交
181

G
groot 已提交
182 183
    uint64_t qb = 5;
    milvus::engine::VectorsData qxb;
G
groot 已提交
184
    BuildVectors(qb, qxb);
X
Xu Peng 已提交
185

X
Xu Peng 已提交
186
    std::thread search([&]() {
G
groot 已提交
187 188
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
X
Xu Peng 已提交
189 190 191 192 193
        int k = 10;
        std::this_thread::sleep_for(std::chrono::seconds(2));

        INIT_TIMER;
        std::stringstream ss;
G
groot 已提交
194 195
        uint64_t count = 0;
        uint64_t prev_count = 0;
X
Xu Peng 已提交
196

S
starlord 已提交
197
        for (auto j = 0; j < 10; ++j) {
X
Xu Peng 已提交
198
            ss.str("");
X
Xu Peng 已提交
199
            db_->Size(count);
X
Xu Peng 已提交
200
            prev_count = count;
X
Xu Peng 已提交
201 202

            START_TIMER;
G
groot 已提交
203 204

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

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

G
groot 已提交
223
    int loop = INSERT_LOOP;
X
Xu Peng 已提交
224

S
starlord 已提交
225 226
    for (auto i = 0; i < loop; ++i) {
        if (i == 40) {
G
groot 已提交
227 228 229
            qxb.id_array_.clear();
            db_->InsertVectors(TABLE_NAME, "", qxb);
            ASSERT_EQ(qxb.id_array_.size(), qb);
X
Xu Peng 已提交
230
        } else {
G
groot 已提交
231 232 233
            xb.id_array_.clear();
            db_->InsertVectors(TABLE_NAME, "", xb);
            ASSERT_EQ(xb.id_array_.size(), nb);
X
Xu Peng 已提交
234
        }
X
Xu Peng 已提交
235
        std::this_thread::sleep_for(std::chrono::microseconds(1));
X
Xu Peng 已提交
236
    }
X
xj.lin 已提交
237

X
Xu Peng 已提交
238
    search.join();
S
starlord 已提交
239 240 241

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

    // 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 已提交
257
}
X
xj.lin 已提交
258

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

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

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

    // prepare raw data
G
groot 已提交
276
    size_t nb = VECTOR_COUNT;
X
xj.lin 已提交
277 278
    size_t nq = 10;
    size_t k = 5;
G
groot 已提交
279 280 281 282 283 284
    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 已提交
285 286 287 288

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

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

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

S
starlord 已提交
309
    milvus::engine::TableIndex index;
Y
youny626 已提交
310 311
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IDMAP;
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
X
xj.lin 已提交
312

S
starlord 已提交
313
    {
G
groot 已提交
314 315 316
        std::vector<std::string> tags;
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
G
groot 已提交
317
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
318 319 320 321 322 323 324 325 326 327
        ASSERT_TRUE(stat.ok());
    }

    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
    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;
G
groot 已提交
328
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
329 330 331 332 333 334 335 336 337 338
        ASSERT_TRUE(stat.ok());
    }

    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8;
    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;
G
groot 已提交
339
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
340 341 342 343 344 345 346 347 348 349 350
        ASSERT_TRUE(stat.ok());
    }

#ifdef CUSTOMIZATION
    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;
G
groot 已提交
351
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
352
        ASSERT_TRUE(stat.ok());
S
starlord 已提交
353
    }
354
#endif
S
starlord 已提交
355

Y
youny626 已提交
356
    {  // search by specify index file
S
starlord 已提交
357
        milvus::engine::meta::DatesT dates;
358 359 360 361 362 363 364
        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 已提交
365 366
        milvus::engine::ResultIds result_ids;
        milvus::engine::ResultDistances result_distances;
G
groot 已提交
367
        stat = db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, 10, xq, dates, result_ids,
Z
Zhiru Zhu 已提交
368
                                  result_distances);
S
starlord 已提交
369
        ASSERT_TRUE(stat.ok());
S
shengjh 已提交
370 371 372 373 374 375 376 377 378 379 380 381

        FIU_ENABLE_FIU("SqliteMetaImpl.FilesToSearch.throw_exception");
        stat = db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, 10, xq, dates, result_ids,
                                  result_distances);
        ASSERT_FALSE(stat.ok());
        fiu_disable("SqliteMetaImpl.FilesToSearch.throw_exception");

        FIU_ENABLE_FIU("DBImpl.QueryByFileID.empty_files_array");
        stat = db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, 10, xq, dates, result_ids,
                                  result_distances);
        ASSERT_FALSE(stat.ok());
        fiu_disable("DBImpl.QueryByFileID.empty_files_array");
X
xj.lin 已提交
382
    }
X
xj.lin 已提交
383

384 385 386 387 388 389 390
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_PQ;
    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;
G
groot 已提交
391
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
392
        ASSERT_TRUE(stat.ok());
S
shengjh 已提交
393 394 395 396 397 398 399
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
        ASSERT_TRUE(stat.ok());

        FIU_ENABLE_FIU("SqliteMetaImpl.FilesToSearch.throw_exception");
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, k, 10, xq, result_ids, result_distances);
        ASSERT_FALSE(stat.ok());
        fiu_disable("SqliteMetaImpl.FilesToSearch.throw_exception");
400 401
    }

Z
update  
zhiru 已提交
402
#ifdef CUSTOMIZATION
Y
youny626 已提交
403
    // test FAISS_IVFSQ8H optimizer
404
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
Y
youny626 已提交
405
    db_->CreateIndex(TABLE_NAME, index);  // wait until build index finish
406 407 408
    std::vector<std::string> partition_tag;
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_dists;
409 410

    {
411 412
        result_ids.clear();
        result_dists.clear();
G
groot 已提交
413
        stat = db_->Query(dummy_context_, TABLE_NAME, partition_tag, k, 10, xq, result_ids, result_dists);
Y
Yu Kun 已提交
414 415 416
        ASSERT_TRUE(stat.ok());
    }

Y
youny626 已提交
417
    {  // search by specify index file
418
        milvus::engine::meta::DatesT dates;
419 420 421 422 423 424 425
        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));
        }
426 427
        result_ids.clear();
        result_dists.clear();
G
groot 已提交
428
        stat = db_->QueryByFileID(dummy_context_, TABLE_NAME, file_ids, k, 10, xq, dates, result_ids,
Z
Zhiru Zhu 已提交
429
                                  result_dists);
430 431
        ASSERT_TRUE(stat.ok());
    }
Y
Yu Kun 已提交
432

X
xiaojun.lin 已提交
433
#endif
S
starlord 已提交
434
}
Y
c  
yu yunfeng 已提交
435

Y
Yu Kun 已提交
436
TEST_F(DBTest, PRELOADTABLE_TEST) {
S
shengjh 已提交
437 438
    fiu_init(0);

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

S
starlord 已提交
442
    milvus::engine::meta::TableSchema table_info_get;
Y
Yu Kun 已提交
443 444
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
445
    ASSERT_TRUE(stat.ok());
Y
Yu Kun 已提交
446 447
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);

G
groot 已提交
448 449
    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
Y
Yu Kun 已提交
450 451
    BuildVectors(nb, xb);

Y
Yu Kun 已提交
452
    int loop = 5;
S
starlord 已提交
453
    for (auto i = 0; i < loop; ++i) {
G
groot 已提交
454 455 456
        xb.id_array_.clear();
        db_->InsertVectors(TABLE_NAME, "", xb);
        ASSERT_EQ(xb.id_array_.size(), nb);
Y
Yu Kun 已提交
457
    }
458

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

S
starlord 已提交
463
    int64_t prev_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
464
    stat = db_->PreloadTable(TABLE_NAME);
S
starlord 已提交
465
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
466
    int64_t cur_cache_usage = milvus::cache::CpuCacheMgr::GetInstance()->CacheUsage();
Y
Yu Kun 已提交
467
    ASSERT_TRUE(prev_cache_usage < cur_cache_usage);
S
shengjh 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493

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

    //create a partition
    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 已提交
494 495
}

S
starlord 已提交
496 497 498
TEST_F(DBTest, SHUTDOWN_TEST) {
    db_->Stop();

S
starlord 已提交
499
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
500
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
501 502 503 504 505
    ASSERT_FALSE(stat.ok());

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

S
shengjh 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    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 已提交
526 527 528 529
    bool has_table = false;
    stat = db_->HasTable(table_info.table_id_, has_table);
    ASSERT_FALSE(stat.ok());

G
groot 已提交
530 531
    milvus::engine::VectorsData xb;
    stat = db_->InsertVectors(table_info.table_id_, "", xb);
S
starlord 已提交
532 533 534 535 536 537 538 539 540
    ASSERT_FALSE(stat.ok());

    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 已提交
541
    milvus::engine::TableIndex index;
S
starlord 已提交
542 543 544 545 546 547
    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 已提交
548 549 550
    stat = db_->DropIndex(TABLE_NAME);
    ASSERT_FALSE(stat.ok());

G
groot 已提交
551
    std::vector<std::string> tags;
S
starlord 已提交
552
    milvus::engine::meta::DatesT dates;
G
groot 已提交
553 554
    milvus::engine::ResultIds result_ids;
    milvus::engine::ResultDistances result_distances;
Z
Zhiru Zhu 已提交
555
    stat =
G
groot 已提交
556
        db_->Query(dummy_context_, table_info.table_id_, tags, 1, 1, xb, dates, result_ids, result_distances);
S
starlord 已提交
557 558
    ASSERT_FALSE(stat.ok());
    std::vector<std::string> file_ids;
G
groot 已提交
559
    stat = db_->QueryByFileID(dummy_context_, table_info.table_id_, file_ids, 1, 1, xb, dates, result_ids,
Z
Zhiru Zhu 已提交
560
                              result_distances);
S
starlord 已提交
561 562
    ASSERT_FALSE(stat.ok());

S
shengjh 已提交
563 564 565 566
    stat = db_->Query(dummy_context_, table_info.table_id_, tags, 1, 1,
                      milvus::engine::VectorsData(), result_ids, result_distances);
    ASSERT_FALSE(stat.ok());

G
groot 已提交
567
    stat = db_->DropTable(table_info.table_id_, dates);
S
starlord 已提交
568 569 570
    ASSERT_FALSE(stat.ok());
}

S
shengjh 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
TEST_F(DBTest, BACK_TIMER_THREAD_1) {
    fiu_init(0);
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
    milvus::Status stat;
    //test background timer thread
    {
        FIU_ENABLE_FIU("DBImpl.StartMetricTask.InvalidTotalCache");
        FIU_ENABLE_FIU("SqliteMetaImpl.FilesToMerge.throw_exception");
        stat = db_->CreateTable(table_info);
        ASSERT_TRUE(stat.ok());

        //insert some vector to create some tablefiles
        int64_t nb = VECTOR_COUNT;
        milvus::engine::VectorsData xb;
        BuildVectors(nb, xb);

        int loop = 10;
        for (auto i = 0; i < loop; ++i) {
            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());

    //insert some vector to create some tablefiles
    int64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
    BuildVectors(nb, xb);

    int loop = 10;
    for (auto i = 0; i < loop; ++i) {
        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());

    //insert some vector to create some tablefiles
    int64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
    BuildVectors(nb, xb);

    int loop = 10;
    for (auto i = 0; i < loop; ++i) {
        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());

    //insert some vector to create some tablefiles
    int64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
    BuildVectors(nb, xb);

    int loop = 10;
    for (auto i = 0; i < loop; ++i) {
        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 已提交
684
TEST_F(DBTest, INDEX_TEST) {
S
starlord 已提交
685
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
686
    auto stat = db_->CreateTable(table_info);
S
starlord 已提交
687

G
groot 已提交
688 689
    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
S
starlord 已提交
690 691
    BuildVectors(nb, xb);

G
groot 已提交
692 693
    db_->InsertVectors(TABLE_NAME, "", xb);
    ASSERT_EQ(xb.id_array_.size(), nb);
S
starlord 已提交
694

S
starlord 已提交
695
    milvus::engine::TableIndex index;
Y
youny626 已提交
696 697
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8;
    index.metric_type_ = (int)milvus::engine::MetricType::IP;
S
starlord 已提交
698 699 700
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

Y
youny626 已提交
701
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
Y
Yu Kun 已提交
702 703 704
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());

S
shengjh 已提交
705 706 707 708 709 710 711 712 713 714 715 716
    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 已提交
717
#ifdef CUSTOMIZATION
718
#ifdef MILVUS_GPU_VERSION
Y
Yu Kun 已提交
719 720 721
    index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFSQ8H;
    stat = db_->CreateIndex(table_info.table_id_, index);
    ASSERT_TRUE(stat.ok());
722
#endif
Y
Yu Kun 已提交
723 724
#endif

S
starlord 已提交
725
    milvus::engine::TableIndex index_out;
S
starlord 已提交
726
    stat = db_->DescribeIndex(table_info.table_id_, index_out);
S
starlord 已提交
727 728 729
    ASSERT_TRUE(stat.ok());
    ASSERT_EQ(index.engine_type_, index_out.engine_type_);
    ASSERT_EQ(index.nlist_, index_out.nlist_);
S
starlord 已提交
730
    ASSERT_EQ(table_info.metric_type_, index_out.metric_type_);
S
starlord 已提交
731 732 733 734 735

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

G
groot 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
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());

751 752 753 754 755 756 757
        // 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 已提交
758

G
groot 已提交
759
        milvus::engine::VectorsData xb;
G
groot 已提交
760 761 762 763 764
        BuildVectors(INSERT_BATCH, xb);

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

G
groot 已提交
768
        db_->InsertVectors(table_name, partition_tag, xb);
G
groot 已提交
769
        ASSERT_EQ(vector_ids.size(), INSERT_BATCH);
S
shengjh 已提交
770 771 772 773

        //insert data into not existed partition
        stat = db_->InsertVectors(TABLE_NAME, "notexist", xb);
        ASSERT_FALSE(stat.ok());
G
groot 已提交
774 775
    }

Z
Zhiru Zhu 已提交
776
    // duplicated partition is not allowed
G
groot 已提交
777 778 779
    stat = db_->CreatePartition(table_name, "", "0");
    ASSERT_FALSE(stat.ok());

G
groot 已提交
780 781
    std::vector<milvus::engine::meta::TableSchema> partition_schema_array;
    stat = db_->ShowPartitions(table_name, partition_schema_array);
G
groot 已提交
782
    ASSERT_TRUE(stat.ok());
G
groot 已提交
783
    ASSERT_EQ(partition_schema_array.size(), PARTITION_COUNT);
G
groot 已提交
784
    for (int64_t i = 0; i < PARTITION_COUNT; i++) {
G
groot 已提交
785
        ASSERT_EQ(partition_schema_array[i].table_id_, table_name + "_" + std::to_string(i));
G
groot 已提交
786 787
    }

Z
Zhiru Zhu 已提交
788
    {  // build index
G
groot 已提交
789
        milvus::engine::TableIndex index;
Z
Zhiru Zhu 已提交
790 791
        index.engine_type_ = (int)milvus::engine::EngineType::FAISS_IVFFLAT;
        index.metric_type_ = (int)milvus::engine::MetricType::L2;
G
groot 已提交
792 793 794
        stat = db_->CreateIndex(table_info.table_id_, index);
        ASSERT_TRUE(stat.ok());

S
shengjh 已提交
795 796 797 798 799 800 801 802 803 804 805
        fiu_init(0);
        FIU_ENABLE_FIU("DBImpl.BuildTableIndexRecursively.fail_build_table_Index_for_partition");
        stat = db_->CreateIndex(table_info.table_id_, index);
        ASSERT_FALSE(stat.ok());
        fiu_disable("DBImpl.BuildTableIndexRecursively.fail_build_table_Index_for_partition");

	FIU_ENABLE_FIU("DBImpl.BuildTableIndexRecursively.not_empty_err_msg");
        stat = db_->CreateIndex(table_info.table_id_, index);
        ASSERT_FALSE(stat.ok());
	fiu_disable("DBImpl.BuildTableIndexRecursively.not_empty_err_msg");

G
groot 已提交
806 807 808
        uint64_t row_count = 0;
        stat = db_->GetTableRowCount(TABLE_NAME, row_count);
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
809
        ASSERT_EQ(row_count, INSERT_BATCH * PARTITION_COUNT);
S
shengjh 已提交
810 811 812 813 814 815 816 817 818 819

        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 已提交
820 821
    }

Z
Zhiru Zhu 已提交
822
    {  // search
G
groot 已提交
823 824 825
        const int64_t nq = 5;
        const int64_t topk = 10;
        const int64_t nprobe = 10;
G
groot 已提交
826
        milvus::engine::VectorsData xq;
G
groot 已提交
827 828 829 830 831 832
        BuildVectors(nq, xq);

        // 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;
G
groot 已提交
833
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, topk, nprobe, xq, result_ids, result_distances);
G
groot 已提交
834
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
835
        ASSERT_EQ(result_ids.size() / topk, nq);
G
groot 已提交
836 837 838 839 840

        // search in whole table
        tags.clear();
        result_ids.clear();
        result_distances.clear();
G
groot 已提交
841
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, topk, nprobe, xq, result_ids, result_distances);
G
groot 已提交
842
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
843
        ASSERT_EQ(result_ids.size() / topk, nq);
G
groot 已提交
844 845 846 847 848

        // search in all partitions(tag regex match)
        tags.push_back("\\d");
        result_ids.clear();
        result_distances.clear();
G
groot 已提交
849
        stat = db_->Query(dummy_context_, TABLE_NAME, tags, topk, nprobe, xq, result_ids, result_distances);
G
groot 已提交
850
        ASSERT_TRUE(stat.ok());
Z
Zhiru Zhu 已提交
851
        ASSERT_EQ(result_ids.size() / topk, nq);
G
groot 已提交
852 853 854 855 856 857 858 859
    }

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

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

S
shengjh 已提交
860 861 862 863 864 865 866 867 868 869
    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 已提交
870 871 872 873 874 875 876 877
    stat = db_->DropIndex(table_name);
    ASSERT_TRUE(stat.ok());

    milvus::engine::meta::DatesT dates;
    stat = db_->DropTable(table_name, dates);
    ASSERT_TRUE(stat.ok());
}

G
groot 已提交
878
TEST_F(DBTest2, ARHIVE_DISK_CHECK) {
S
starlord 已提交
879
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
880
    auto stat = db_->CreateTable(table_info);
Z
zhiru 已提交
881

S
starlord 已提交
882
    std::vector<milvus::engine::meta::TableSchema> table_schema_array;
G
groot 已提交
883
    stat = db_->AllTables(table_schema_array);
S
starlord 已提交
884
    ASSERT_TRUE(stat.ok());
G
groot 已提交
885
    bool bfound = false;
Y
youny626 已提交
886
    for (auto& schema : table_schema_array) {
S
starlord 已提交
887
        if (schema.table_id_ == TABLE_NAME) {
G
groot 已提交
888 889 890 891 892 893
            bfound = true;
            break;
        }
    }
    ASSERT_TRUE(bfound);

S
starlord 已提交
894
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
895 896
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
897
    ASSERT_TRUE(stat.ok());
G
groot 已提交
898
    ASSERT_EQ(table_info_get.dimension_, TABLE_DIM);
Z
zhiru 已提交
899

G
groot 已提交
900
    uint64_t size;
Z
zhiru 已提交
901 902
    db_->Size(size);

G
groot 已提交
903 904
    uint64_t nb = 10;
    milvus::engine::VectorsData xb;
G
groot 已提交
905
    BuildVectors(nb, xb);
Z
zhiru 已提交
906

G
groot 已提交
907
    int loop = INSERT_LOOP;
S
starlord 已提交
908
    for (auto i = 0; i < loop; ++i) {
S
starlord 已提交
909
        milvus::engine::IDNumbers vector_ids;
G
groot 已提交
910
        db_->InsertVectors(TABLE_NAME, "", xb);
Z
zhiru 已提交
911 912 913
        std::this_thread::sleep_for(std::chrono::microseconds(1));
    }

G
groot 已提交
914
    std::this_thread::sleep_for(std::chrono::seconds(1));
Z
zhiru 已提交
915 916 917

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

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

S
starlord 已提交
925
    milvus::engine::meta::TableSchema table_info_get;
G
groot 已提交
926 927
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
928
    ASSERT_TRUE(stat.ok());
G
groot 已提交
929

S
starlord 已提交
930 931 932
    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);
G
groot 已提交
933 934 935 936

    uint64_t size;
    db_->Size(size);

G
groot 已提交
937 938
    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
G
groot 已提交
939 940
    BuildVectors(nb, xb);

S
starlord 已提交
941
    milvus::engine::IDNumbers vector_ids;
G
groot 已提交
942
    stat = db_->InsertVectors(TABLE_NAME, "", xb);
S
starlord 已提交
943
    milvus::engine::TableIndex index;
944
    stat = db_->CreateIndex(TABLE_NAME, index);
G
groot 已提交
945

S
starlord 已提交
946
    std::vector<milvus::engine::meta::DateT> dates;
S
shengjh 已提交
947 948 949 950 951 952 953 954 955 956 957 958

    //create partition, drop table will drop partition recursively
    stat = db_->CreatePartition(TABLE_NAME, "part0", "0");
    ASSERT_TRUE(stat.ok());

    //fail drop table
    fiu_init(0);
    FIU_ENABLE_FIU("DBImpl.DropTableRecursively.failed");
    stat = db_->DropTable(TABLE_NAME, dates);
    ASSERT_FALSE(stat.ok());
    fiu_disable("DBImpl.DropTableRecursively.failed");

G
groot 已提交
959
    stat = db_->DropTable(TABLE_NAME, dates);
G
groot 已提交
960 961
    std::this_thread::sleep_for(std::chrono::seconds(2));
    ASSERT_TRUE(stat.ok());
S
starlord 已提交
962 963 964

    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_FALSE(has_table);
S
starlord 已提交
965
}
966 967

TEST_F(DBTest2, DELETE_BY_RANGE_TEST) {
S
starlord 已提交
968
    milvus::engine::meta::TableSchema table_info = BuildTableSchema();
S
starlord 已提交
969
    auto stat = db_->CreateTable(table_info);
970

S
starlord 已提交
971
    milvus::engine::meta::TableSchema table_info_get;
972 973
    table_info_get.table_id_ = TABLE_NAME;
    stat = db_->DescribeTable(table_info_get);
S
starlord 已提交
974
    ASSERT_TRUE(stat.ok());
975 976 977 978 979 980 981

    bool has_table = false;
    db_->HasTable(TABLE_NAME, has_table);
    ASSERT_TRUE(has_table);

    uint64_t size;
    db_->Size(size);
982
    ASSERT_EQ(size, 0UL);
983

G
groot 已提交
984 985
    uint64_t nb = VECTOR_COUNT;
    milvus::engine::VectorsData xb;
986 987
    BuildVectors(nb, xb);

S
starlord 已提交
988
    milvus::engine::IDNumbers vector_ids;
G
groot 已提交
989
    stat = db_->InsertVectors(TABLE_NAME, "", xb);
S
starlord 已提交
990
    milvus::engine::TableIndex index;
991 992 993 994
    stat = db_->CreateIndex(TABLE_NAME, index);

    db_->Size(size);
    ASSERT_NE(size, 0UL);
995

S
starlord 已提交
996
    std::vector<milvus::engine::meta::DateT> dates;
G
groot 已提交
997 998
    std::string start_value = CurrentTmDate(-5);
    std::string end_value = CurrentTmDate(5);
999 1000
    ConvertTimeRangeToDBDates(start_value, end_value, dates);

G
groot 已提交
1001
    stat = db_->DropTable(TABLE_NAME, dates);
S
starlord 已提交
1002
    ASSERT_TRUE(stat.ok());
1003 1004 1005 1006

    uint64_t row_count = 0;
    db_->GetTableRowCount(TABLE_NAME, row_count);
    ASSERT_EQ(row_count, 0UL);
S
starlord 已提交
1007
}