meta_tests.cpp 11.7 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());
S
starlord 已提交
104
    ASSERT_EQ(files.size(), 0UL);
X
Xu Peng 已提交
105
}
X
Xu Peng 已提交
106

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

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

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

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

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

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

145 146 147 148 149
    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 已提交
150
        if (days[i] < days_num) {
G
groot 已提交
151
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
152 153 154 155
        }
        i++;
    }

156
    impl.DropAll();
X
Xu Peng 已提交
157 158 159
}

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

S
starlord 已提交
164
    meta::SqliteMetaImpl impl(options);
165
    auto table_id = "meta_test_group";
X
Xu Peng 已提交
166

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

171
    meta::TableFilesSchema files;
X
Xu Peng 已提交
172
    meta::TableFileSchema table_file;
G
groot 已提交
173
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
174 175 176

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

X
Xu Peng 已提交
187
    impl.Archive();
X
Xu Peng 已提交
188 189
    int i = 0;

190 191 192 193 194
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
S
starlord 已提交
195
        if (i >= 5) {
G
groot 已提交
196
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
197 198 199 200
        }
        ++i;
    }

201
    impl.DropAll();
X
Xu Peng 已提交
202 203
}

X
Xu Peng 已提交
204
TEST_F(MetaTest, TABLE_FILES_TEST) {
205
    auto table_id = "meta_test_group";
X
Xu Peng 已提交
206

G
groot 已提交
207 208 209
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
210

S
starlord 已提交
211 212 213 214 215 216 217
    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 已提交
218

X
Xu Peng 已提交
219
    meta::TableFileSchema table_file;
G
groot 已提交
220
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
221

S
starlord 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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 已提交
241
    for (auto i=0; i<new_files_cnt; ++i) {
X
Xu Peng 已提交
242
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
243
        table_file.file_type_ = meta::TableFileSchema::NEW;
X
Xu Peng 已提交
244
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
245 246 247
    }

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

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

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

S
starlord 已提交
268 269 270 271
    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 已提交
272

S
starlord 已提交
273
    meta::TableFilesSchema files;
X
Xu Peng 已提交
274
    status = impl_->FilesToIndex(files);
X
Xu Peng 已提交
275 276
    ASSERT_EQ(files.size(), to_index_files_cnt);

277
    meta::DatePartionedTableFilesSchema dated_files;
G
groot 已提交
278
    status = impl_->FilesToMerge(table.table_id_, dated_files);
G
groot 已提交
279
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);
X
Xu Peng 已提交
280

X
Xu Peng 已提交
281
    status = impl_->FilesToIndex(files);
X
Xu Peng 已提交
282 283
    ASSERT_EQ(files.size(), to_index_files_cnt);

G
groot 已提交
284
    meta::DatesT dates = {table_file.date_};
285 286
    std::vector<size_t> ids;
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
G
groot 已提交
287
    ASSERT_EQ(dated_files[table_file.date_].size(),
X
Xu Peng 已提交
288
            to_index_files_cnt+raw_files_cnt+index_files_cnt);
G
groot 已提交
289

290
    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
G
groot 已提交
291 292
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);
X
xj.lin 已提交
293 294 295 296 297 298 299 300

    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
    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_EQ(dated_files[table_file.date_].size(),0);
S
starlord 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    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 已提交
332
}
S
starlord 已提交
333 334 335 336 337 338 339 340 341 342 343 344 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

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());
}