meta_tests.cpp 12.2 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5 6 7 8
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include <thread>
#include <easylogging++.h>
X
Xu Peng 已提交
9 10
#include <stdlib.h>
#include <time.h>
X
Xu Peng 已提交
11 12

#include "utils.h"
S
starlord 已提交
13
#include "db/meta/SqliteMetaImpl.h"
X
Xu Peng 已提交
14
#include "db/Factories.h"
X
Xu Peng 已提交
15
#include "db/Utils.h"
S
starlord 已提交
16
#include "db/meta/MetaConsts.h"
X
Xu Peng 已提交
17

S
starlord 已提交
18
using namespace zilliz::milvus;
J
jinhai 已提交
19
using namespace zilliz::milvus::engine;
X
Xu Peng 已提交
20

G
groot 已提交
21 22
TEST_F(MetaTest, TABLE_TEST) {
    auto table_id = "meta_test_table";
X
Xu Peng 已提交
23

G
groot 已提交
24 25 26
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
27 28
    ASSERT_TRUE(status.ok());

G
groot 已提交
29 30 31
    auto gid = table.id_;
    table.id_ = -1;
    status = impl_->DescribeTable(table);
X
Xu Peng 已提交
32
    ASSERT_TRUE(status.ok());
G
groot 已提交
33 34
    ASSERT_EQ(table.id_, gid);
    ASSERT_EQ(table.table_id_, table_id);
X
Xu Peng 已提交
35

G
groot 已提交
36 37
    table.table_id_ = "not_found";
    status = impl_->DescribeTable(table);
X
Xu Peng 已提交
38 39
    ASSERT_TRUE(!status.ok());

G
groot 已提交
40 41
    table.table_id_ = table_id;
    status = impl_->CreateTable(table);
S
starlord 已提交
42
    ASSERT_EQ(status.code(), DB_ALREADY_EXIST);
G
groot 已提交
43 44 45 46

    table.table_id_ = "";
    status = impl_->CreateTable(table);
    ASSERT_TRUE(status.ok());
X
Xu Peng 已提交
47
}
X
Xu Peng 已提交
48

G
groot 已提交
49 50
TEST_F(MetaTest, TABLE_FILE_TEST) {
    auto table_id = "meta_test_table";
X
Xu Peng 已提交
51

G
groot 已提交
52 53
    meta::TableSchema table;
    table.table_id_ = table_id;
G
groot 已提交
54
    table.dimension_ = 256;
G
groot 已提交
55
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
56

X
Xu Peng 已提交
57
    meta::TableFileSchema table_file;
G
groot 已提交
58
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
59
    status = impl_->CreateTableFile(table_file);
X
Xu Peng 已提交
60
    ASSERT_TRUE(status.ok());
G
groot 已提交
61
    ASSERT_EQ(table_file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
62

G
groot 已提交
63 64 65 66 67
    uint64_t cnt = 0;
    status = impl_->Count(table_id, cnt);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(cnt, 0UL);

G
groot 已提交
68
    auto file_id = table_file.file_id_;
X
Xu Peng 已提交
69

70
    auto new_file_type = meta::TableFileSchema::INDEX;
G
groot 已提交
71
    table_file.file_type_ = new_file_type;
X
Xu Peng 已提交
72

X
Xu Peng 已提交
73
    status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
74
    ASSERT_TRUE(status.ok());
G
groot 已提交
75
    ASSERT_EQ(table_file.file_type_, new_file_type);
X
Xu Peng 已提交
76

X
Xu Peng 已提交
77
    meta::DatesT dates;
78
    dates.push_back(utils::GetDate());
G
groot 已提交
79
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
80
    ASSERT_TRUE(status.ok());
X
Xu Peng 已提交
81

X
Xu Peng 已提交
82 83
    dates.clear();
    for (auto i=2; i < 10; ++i) {
84
        dates.push_back(utils::GetDateWithDelta(-1*i));
X
Xu Peng 已提交
85
    }
G
groot 已提交
86
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
X
Xu Peng 已提交
87 88
    ASSERT_TRUE(status.ok());

89
    table_file.date_ = utils::GetDateWithDelta(-2);
X
Xu Peng 已提交
90
    status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
91
    ASSERT_TRUE(status.ok());
92
    ASSERT_EQ(table_file.date_, utils::GetDateWithDelta(-2));
G
groot 已提交
93
    ASSERT_FALSE(table_file.file_type_ == meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
94 95

    dates.clear();
G
groot 已提交
96 97
    dates.push_back(table_file.date_);
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
X
Xu Peng 已提交
98
    ASSERT_TRUE(status.ok());
99 100 101 102

    std::vector<size_t> ids = {table_file.id_};
    meta::TableFilesSchema files;
    status = impl_->GetTableFiles(table_file.table_id_, ids, files);
X
Xu Peng 已提交
103
    ASSERT_TRUE(status.ok());
104 105
    ASSERT_EQ(files.size(), 1UL);
    ASSERT_TRUE(files[0].file_type_ == meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
106
}
X
Xu Peng 已提交
107

X
Xu Peng 已提交
108 109 110
TEST_F(MetaTest, ARCHIVE_TEST_DAYS) {
    srand(time(0));
    DBMetaOptions options;
G
groot 已提交
111
    options.path = "/tmp/milvus_test";
X
Xu Peng 已提交
112 113 114 115 116
    int days_num = rand() % 100;
    std::stringstream ss;
    ss << "days:" << days_num;
    options.archive_conf = ArchiveConf("delete", ss.str());

S
starlord 已提交
117
    meta::SqliteMetaImpl impl(options);
G
groot 已提交
118
    auto table_id = "meta_test_table";
X
Xu Peng 已提交
119

G
groot 已提交
120 121 122
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
X
Xu Peng 已提交
123

124
    meta::TableFilesSchema files;
X
Xu Peng 已提交
125
    meta::TableFileSchema table_file;
G
groot 已提交
126
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
127 128 129 130

    auto cnt = 100;
    long ts = utils::GetMicroSecTimeStamp();
    std::vector<int> days;
131
    std::vector<size_t> ids;
X
Xu Peng 已提交
132
    for (auto i=0; i<cnt; ++i) {
X
Xu Peng 已提交
133
        status = impl.CreateTableFile(table_file);
G
groot 已提交
134
        table_file.file_type_ = meta::TableFileSchema::NEW;
X
Xu Peng 已提交
135
        int day = rand() % (days_num*2);
G
groot 已提交
136
        table_file.created_on_ = ts - day*meta::D_SEC*meta::US_PS - 10000;
X
Xu Peng 已提交
137
        status = impl.UpdateTableFile(table_file);
X
Xu Peng 已提交
138
        files.push_back(table_file);
X
Xu Peng 已提交
139
        days.push_back(day);
140
        ids.push_back(table_file.id_);
X
Xu Peng 已提交
141 142
    }

X
Xu Peng 已提交
143
    impl.Archive();
X
Xu Peng 已提交
144 145
    int i = 0;

146 147 148 149 150
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
X
Xu Peng 已提交
151
        if (days[i] < days_num) {
G
groot 已提交
152
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
153
        } else {
G
groot 已提交
154
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
155 156 157 158
        }
        i++;
    }

159
    impl.DropAll();
X
Xu Peng 已提交
160 161 162
}

TEST_F(MetaTest, ARCHIVE_TEST_DISK) {
X
Xu Peng 已提交
163
    DBMetaOptions options;
G
groot 已提交
164
    options.path = "/tmp/milvus_test";
X
Xu Peng 已提交
165
    options.archive_conf = ArchiveConf("delete", "disk:11");
X
Xu Peng 已提交
166

S
starlord 已提交
167
    meta::SqliteMetaImpl impl(options);
168
    auto table_id = "meta_test_group";
X
Xu Peng 已提交
169

G
groot 已提交
170 171 172
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
X
Xu Peng 已提交
173

174
    meta::TableFilesSchema files;
X
Xu Peng 已提交
175
    meta::TableFileSchema table_file;
G
groot 已提交
176
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
177 178 179

    auto cnt = 10;
    auto each_size = 2UL;
180
    std::vector<size_t> ids;
X
Xu Peng 已提交
181
    for (auto i=0; i<cnt; ++i) {
X
Xu Peng 已提交
182
        status = impl.CreateTableFile(table_file);
G
groot 已提交
183
        table_file.file_type_ = meta::TableFileSchema::NEW;
184
        table_file.file_size_ = each_size * meta::G;
X
Xu Peng 已提交
185
        status = impl.UpdateTableFile(table_file);
X
Xu Peng 已提交
186
        files.push_back(table_file);
187
        ids.push_back(table_file.id_);
X
Xu Peng 已提交
188 189
    }

X
Xu Peng 已提交
190
    impl.Archive();
X
Xu Peng 已提交
191 192
    int i = 0;

193 194 195 196 197
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
X
Xu Peng 已提交
198
        if (i < 5) {
G
groot 已提交
199
            ASSERT_TRUE(file.file_type_ == meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
200
        } else {
G
groot 已提交
201
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
202 203 204 205
        }
        ++i;
    }

206
    impl.DropAll();
X
Xu Peng 已提交
207 208
}

X
Xu Peng 已提交
209
TEST_F(MetaTest, TABLE_FILES_TEST) {
210
    auto table_id = "meta_test_group";
X
Xu Peng 已提交
211

G
groot 已提交
212 213 214
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
215

S
starlord 已提交
216 217 218 219 220 221 222
    uint64_t new_merge_files_cnt = 1;
    uint64_t new_index_files_cnt = 2;
    uint64_t backup_files_cnt = 3;
    uint64_t new_files_cnt = 4;
    uint64_t raw_files_cnt = 5;
    uint64_t to_index_files_cnt = 6;
    uint64_t index_files_cnt = 7;
X
Xu Peng 已提交
223

X
Xu Peng 已提交
224
    meta::TableFileSchema table_file;
G
groot 已提交
225
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
226

S
starlord 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    for (auto i=0; i<new_merge_files_cnt; ++i) {
        status = impl_->CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW_MERGE;
        status = impl_->UpdateTableFile(table_file);
    }

    for (auto i=0; i<new_index_files_cnt; ++i) {
        status = impl_->CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW_INDEX;
        status = impl_->UpdateTableFile(table_file);
    }

    for (auto i=0; i<backup_files_cnt; ++i) {
        status = impl_->CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::BACKUP;
        table_file.row_count_ = 1;
        status = impl_->UpdateTableFile(table_file);
    }

X
Xu Peng 已提交
246
    for (auto i=0; i<new_files_cnt; ++i) {
X
Xu Peng 已提交
247
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
248
        table_file.file_type_ = meta::TableFileSchema::NEW;
X
Xu Peng 已提交
249
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
250 251 252
    }

    for (auto i=0; i<raw_files_cnt; ++i) {
X
Xu Peng 已提交
253
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
254
        table_file.file_type_ = meta::TableFileSchema::RAW;
S
starlord 已提交
255
        table_file.row_count_ = 1;
X
Xu Peng 已提交
256
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
257 258 259
    }

    for (auto i=0; i<to_index_files_cnt; ++i) {
X
Xu Peng 已提交
260
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
261
        table_file.file_type_ = meta::TableFileSchema::TO_INDEX;
S
starlord 已提交
262
        table_file.row_count_ = 1;
X
Xu Peng 已提交
263
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
264 265 266
    }

    for (auto i=0; i<index_files_cnt; ++i) {
X
Xu Peng 已提交
267
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
268
        table_file.file_type_ = meta::TableFileSchema::INDEX;
S
starlord 已提交
269
        table_file.row_count_ = 1;
X
Xu Peng 已提交
270
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
271 272
    }

S
starlord 已提交
273 274 275 276
    uint64_t total_row_count = 0;
    status = impl_->Count(table_id, total_row_count);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(total_row_count, raw_files_cnt+to_index_files_cnt+index_files_cnt);
X
Xu Peng 已提交
277

S
starlord 已提交
278
    meta::TableFilesSchema files;
X
Xu Peng 已提交
279
    status = impl_->FilesToIndex(files);
X
Xu Peng 已提交
280 281 282
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

283
    meta::DatePartionedTableFilesSchema dated_files;
G
groot 已提交
284
    status = impl_->FilesToMerge(table.table_id_, dated_files);
X
Xu Peng 已提交
285
    ASSERT_TRUE(status.ok());
G
groot 已提交
286
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);
X
Xu Peng 已提交
287

X
Xu Peng 已提交
288
    status = impl_->FilesToIndex(files);
X
Xu Peng 已提交
289 290 291
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

G
groot 已提交
292
    meta::DatesT dates = {table_file.date_};
293 294
    std::vector<size_t> ids;
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
X
Xu Peng 已提交
295
    ASSERT_TRUE(status.ok());
G
groot 已提交
296
    ASSERT_EQ(dated_files[table_file.date_].size(),
X
Xu Peng 已提交
297
            to_index_files_cnt+raw_files_cnt+index_files_cnt);
G
groot 已提交
298

299
    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
G
groot 已提交
300 301 302
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);
X
xj.lin 已提交
303 304 305 306 307 308 309 310 311 312

    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

    ids.push_back(size_t(9999999999));
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),0);
S
starlord 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

    std::vector<int> file_types;
    std::vector<std::string> file_ids;
    status = impl_->FilesByType(table.table_id_, file_types, file_ids);
    ASSERT_TRUE(file_ids.empty());
    ASSERT_FALSE(status.ok());

    file_types = {
            meta::TableFileSchema::NEW,
            meta::TableFileSchema::NEW_MERGE,
            meta::TableFileSchema::NEW_INDEX,
            meta::TableFileSchema::TO_INDEX,
            meta::TableFileSchema::INDEX,
            meta::TableFileSchema::RAW,
            meta::TableFileSchema::BACKUP,
    };
    status = impl_->FilesByType(table.table_id_, file_types, file_ids);
    ASSERT_TRUE(status.ok());
    uint64_t total_cnt = new_index_files_cnt + new_merge_files_cnt +
                         backup_files_cnt + new_files_cnt + raw_files_cnt +
                         to_index_files_cnt + index_files_cnt;
    ASSERT_EQ(file_ids.size(), total_cnt);

    status = impl_->DeleteTableFiles(table_id);
    ASSERT_TRUE(status.ok());

    status = impl_->DeleteTable(table_id);
    ASSERT_TRUE(status.ok());

    status = impl_->CleanUpFilesWithTTL(1UL);
    ASSERT_TRUE(status.ok());
X
Xu Peng 已提交
344
}
S
starlord 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

TEST_F(MetaTest, INDEX_TEST) {
    auto table_id = "index_test";

    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);

    TableIndex index;
    index.metric_type_ = 2;
    index.nlist_ = 1234;
    index.engine_type_ = 3;
    status = impl_->UpdateTableIndex(table_id, index);
    ASSERT_TRUE(status.ok());

    int64_t flag = 65536;
    status = impl_->UpdateTableFlag(table_id, flag);
    ASSERT_TRUE(status.ok());

    engine::meta::TableSchema table_info;
    table_info.table_id_ = table_id;
    status = impl_->DescribeTable(table_info);
    ASSERT_EQ(table_info.flag_, flag);

    TableIndex index_out;
    status = impl_->DescribeTableIndex(table_id, index_out);
    ASSERT_EQ(index_out.metric_type_, index.metric_type_);
    ASSERT_EQ(index_out.nlist_, index.nlist_);
    ASSERT_EQ(index_out.engine_type_, index.engine_type_);

    status = impl_->DropTableIndex(table_id);
    ASSERT_TRUE(status.ok());
    status = impl_->DescribeTableIndex(table_id, index_out);
    ASSERT_NE(index_out.metric_type_, index.metric_type_);
    ASSERT_NE(index_out.nlist_, index.nlist_);
    ASSERT_NE(index_out.engine_type_, index.engine_type_);

    status = impl_->UpdateTableFilesToIndex(table_id);
    ASSERT_TRUE(status.ok());
}