mysql_meta_test.cpp 12.4 KB
Newer Older
Z
update  
zhiru 已提交
1 2 3 4 5 6 7 8 9 10 11 12
////////////////////////////////////////////////////////////////////////////////
// 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>
#include <stdlib.h>
#include <time.h>

#include "utils.h"
S
starlord 已提交
13
#include "db/meta/MySQLMetaImpl.h"
Z
update  
zhiru 已提交
14 15
#include "db/Factories.h"
#include "db/Utils.h"
S
starlord 已提交
16
#include "db/meta/MetaConsts.h"
Z
update  
zhiru 已提交
17

18 19 20 21
#include "mysql++/mysql++.h"

#include <iostream>

S
starlord 已提交
22
using namespace zilliz::milvus;
Z
update  
zhiru 已提交
23 24
using namespace zilliz::milvus::engine;

S
starlord 已提交
25
TEST_F(MySqlMetaTest, TABLE_TEST) {
Z
update  
zhiru 已提交
26
    auto table_id = "meta_test_table";
27

Z
update  
zhiru 已提交
28 29
    meta::TableSchema table;
    table.table_id_ = table_id;
S
starlord 已提交
30
    auto status = impl_->CreateTable(table);
31 32
    ASSERT_TRUE(status.ok());

Z
update  
zhiru 已提交
33 34
    auto gid = table.id_;
    table.id_ = -1;
S
starlord 已提交
35
    status = impl_->DescribeTable(table);
36
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
37 38
    ASSERT_EQ(table.id_, gid);
    ASSERT_EQ(table.table_id_, table_id);
39

Z
update  
zhiru 已提交
40
    table.table_id_ = "not_found";
S
starlord 已提交
41
    status = impl_->DescribeTable(table);
42 43
    ASSERT_TRUE(!status.ok());

Z
update  
zhiru 已提交
44
    table.table_id_ = table_id;
S
starlord 已提交
45
    status = impl_->CreateTable(table);
S
starlord 已提交
46
    ASSERT_EQ(status.code(), DB_ALREADY_EXIST);
Z
update  
zhiru 已提交
47

Z
update  
zhiru 已提交
48
    table.table_id_ = "";
S
starlord 已提交
49
    status = impl_->CreateTable(table);
S
starlord 已提交
50
//    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
51

S
starlord 已提交
52
    status = impl_->DropAll();
53 54 55
    ASSERT_TRUE(status.ok());
}

S
starlord 已提交
56
TEST_F(MySqlMetaTest, TABLE_FILE_TEST) {
Z
update  
zhiru 已提交
57
    auto table_id = "meta_test_table";
58

Z
update  
zhiru 已提交
59 60 61
    meta::TableSchema table;
    table.table_id_ = table_id;
    table.dimension_ = 256;
S
starlord 已提交
62
    auto status = impl_->CreateTable(table);
63

S
starlord 已提交
64

65
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
66
    table_file.table_id_ = table.table_id_;
S
starlord 已提交
67
    status = impl_->CreateTableFile(table_file);
68 69 70
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.file_type_, meta::TableFileSchema::NEW);

S
starlord 已提交
71
    meta::DatesT dates;
72
    dates.push_back(utils::GetDate());
S
starlord 已提交
73
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
S
starlord 已提交
74
    ASSERT_TRUE(status.ok());
S
starlord 已提交
75

Z
update  
zhiru 已提交
76
    uint64_t cnt = 0;
S
starlord 已提交
77
    status = impl_->Count(table_id, cnt);
S
starlord 已提交
78 79
//    ASSERT_TRUE(status.ok());
//    ASSERT_EQ(cnt, 0UL);
Z
update  
zhiru 已提交
80

81 82 83 84 85
    auto file_id = table_file.file_id_;

    auto new_file_type = meta::TableFileSchema::INDEX;
    table_file.file_type_ = new_file_type;

S
starlord 已提交
86
    status = impl_->UpdateTableFile(table_file);
87 88 89 90 91
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.file_type_, new_file_type);

    dates.clear();
    for (auto i=2; i < 10; ++i) {
92
        dates.push_back(utils::GetDateWithDelta(-1*i));
93
    }
S
starlord 已提交
94
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
95 96
    ASSERT_TRUE(status.ok());

97
    table_file.date_ = utils::GetDateWithDelta(-2);
S
starlord 已提交
98
    status = impl_->UpdateTableFile(table_file);
99
    ASSERT_TRUE(status.ok());
100
    ASSERT_EQ(table_file.date_, utils::GetDateWithDelta(-2));
101 102 103 104
    ASSERT_FALSE(table_file.file_type_ == meta::TableFileSchema::TO_DELETE);

    dates.clear();
    dates.push_back(table_file.date_);
S
starlord 已提交
105
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
106
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
107 108 109

    std::vector<size_t> ids = {table_file.id_};
    meta::TableFilesSchema files;
S
starlord 已提交
110
    status = impl_->GetTableFiles(table_file.table_id_, ids, files);
S
starlord 已提交
111
    ASSERT_EQ(files.size(), 0UL);
112 113
}

S
starlord 已提交
114
TEST_F(MySqlMetaTest, ARCHIVE_TEST_DAYS) {
115
    srand(time(0));
S
starlord 已提交
116
    DBMetaOptions options = GetOptions().meta;
G
groot 已提交
117

118 119 120 121
    int days_num = rand() % 100;
    std::stringstream ss;
    ss << "days:" << days_num;
    options.archive_conf = ArchiveConf("delete", ss.str());
Z
update  
zhiru 已提交
122 123
    int mode = Options::MODE::SINGLE;
    meta::MySQLMetaImpl impl(options, mode);
124

Z
update  
zhiru 已提交
125
    auto table_id = "meta_test_table";
126

Z
update  
zhiru 已提交
127 128 129
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
130 131 132

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
133
    table_file.table_id_ = table.table_id_;
134 135 136 137

    auto cnt = 100;
    long ts = utils::GetMicroSecTimeStamp();
    std::vector<int> days;
Z
update  
zhiru 已提交
138
    std::vector<size_t> ids;
139 140 141 142 143 144 145 146
    for (auto i=0; i<cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW;
        int day = rand() % (days_num*2);
        table_file.created_on_ = ts - day*meta::D_SEC*meta::US_PS - 10000;
        status = impl.UpdateTableFile(table_file);
        files.push_back(table_file);
        days.push_back(day);
Z
update  
zhiru 已提交
147
        ids.push_back(table_file.id_);
148 149 150 151 152
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
153 154 155 156 157
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
158 159 160 161 162 163
        if (days[i] < days_num) {
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
        }
        i++;
    }

164 165 166 167 168 169
    std::vector<int> file_types = {
        (int) meta::TableFileSchema::NEW,
    };
    std::vector<std::string> file_ids;
    status = impl.FilesByType(table_id, file_types, file_ids);
    ASSERT_FALSE(file_ids.empty());
S
starlord 已提交
170 171 172 173

    status = impl.UpdateTableFilesToIndex(table_id);
    ASSERT_TRUE(status.ok());

174 175 176 177
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

S
starlord 已提交
178 179
TEST_F(MySqlMetaTest, ARCHIVE_TEST_DISK) {
    DBMetaOptions options = GetOptions().meta;
G
groot 已提交
180

181
    options.archive_conf = ArchiveConf("delete", "disk:11");
Z
update  
zhiru 已提交
182 183
    int mode = Options::MODE::SINGLE;
    auto impl = meta::MySQLMetaImpl(options, mode);
184 185
    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
186 187 188
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
189

S
starlord 已提交
190 191 192 193
    meta::TableSchema table_schema;
    table_schema.table_id_ = "";
    status = impl.CreateTable(table_schema);

194 195
    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
196
    table_file.table_id_ = table.table_id_;
197 198 199

    auto cnt = 10;
    auto each_size = 2UL;
Z
update  
zhiru 已提交
200
    std::vector<size_t> ids;
201 202 203
    for (auto i=0; i<cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW;
204
        table_file.file_size_ = each_size * meta::G;
205 206
        status = impl.UpdateTableFile(table_file);
        files.push_back(table_file);
Z
update  
zhiru 已提交
207
        ids.push_back(table_file.id_);
208 209 210 211 212
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
213 214 215 216 217
    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 已提交
218
        if (i >= 5) {
219 220 221 222 223 224 225 226 227
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
        }
        ++i;
    }

    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

S
starlord 已提交
228
TEST_F(MySqlMetaTest, TABLE_FILES_TEST) {
229 230
    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
231 232
    meta::TableSchema table;
    table.table_id_ = table_id;
S
starlord 已提交
233
    auto status = impl_->CreateTable(table);
234

S
starlord 已提交
235 236 237 238 239 240 241
    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;
242 243

    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
244
    table_file.table_id_ = table.table_id_;
245

S
starlord 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    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);
    }

265
    for (auto i=0; i<new_files_cnt; ++i) {
S
starlord 已提交
266
        status = impl_->CreateTableFile(table_file);
267
        table_file.file_type_ = meta::TableFileSchema::NEW;
S
starlord 已提交
268
        status = impl_->UpdateTableFile(table_file);
269 270 271
    }

    for (auto i=0; i<raw_files_cnt; ++i) {
S
starlord 已提交
272
        status = impl_->CreateTableFile(table_file);
273
        table_file.file_type_ = meta::TableFileSchema::RAW;
S
starlord 已提交
274
        table_file.row_count_ = 1;
S
starlord 已提交
275
        status = impl_->UpdateTableFile(table_file);
276 277 278
    }

    for (auto i=0; i<to_index_files_cnt; ++i) {
S
starlord 已提交
279
        status = impl_->CreateTableFile(table_file);
280
        table_file.file_type_ = meta::TableFileSchema::TO_INDEX;
S
starlord 已提交
281
        table_file.row_count_ = 1;
S
starlord 已提交
282
        status = impl_->UpdateTableFile(table_file);
283 284 285
    }

    for (auto i=0; i<index_files_cnt; ++i) {
S
starlord 已提交
286
        status = impl_->CreateTableFile(table_file);
287
        table_file.file_type_ = meta::TableFileSchema::INDEX;
S
starlord 已提交
288
        table_file.row_count_ = 1;
S
starlord 已提交
289
        status = impl_->UpdateTableFile(table_file);
290 291
    }

S
starlord 已提交
292 293 294 295
    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);
296

S
starlord 已提交
297
    meta::TableFilesSchema files;
S
starlord 已提交
298
    status = impl_->FilesToIndex(files);
299 300 301
    ASSERT_EQ(files.size(), to_index_files_cnt);

    meta::DatePartionedTableFilesSchema dated_files;
S
starlord 已提交
302
    status = impl_->FilesToMerge(table.table_id_, dated_files);
303 304
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);

S
starlord 已提交
305
    status = impl_->FilesToIndex(files);
306 307 308
    ASSERT_EQ(files.size(), to_index_files_cnt);

    meta::DatesT dates = {table_file.date_};
309
    std::vector<size_t> ids;
S
starlord 已提交
310
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
311 312 313
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

S
starlord 已提交
314
    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
G
groot 已提交
315 316 317
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

S
starlord 已提交
318
    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
X
xj.lin 已提交
319 320 321 322
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

    ids.push_back(size_t(9999999999));
S
starlord 已提交
323
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
X
xj.lin 已提交
324 325
    ASSERT_EQ(dated_files[table_file.date_].size(),0);

S
starlord 已提交
326 327 328 329 330 331 332 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    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());
}

TEST_F(MySqlMetaTest, 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);
395 396
    ASSERT_TRUE(status.ok());
}