mysql_meta_test.cpp 16.0 KB
Newer Older
Z
update  
zhiru 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
////////////////////////////////////////////////////////////////////////////////
// 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"
#include "db/MySQLMetaImpl.h"
#include "db/Factories.h"
#include "db/Utils.h"
#include "db/MetaConsts.h"

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

#include <iostream>

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

24 25 26 27 28 29 30 31 32 33 34
//TEST_F(MySQLTest, InitializeTest) {
//    DBMetaOptions options;
//    //dialect+driver://username:password@host:port/database
//    options.backend_uri = "mysql://root:1234@:/test";
//    meta::MySQLMetaImpl impl(options);
//    auto status = impl.Initialize();
//    std::cout << status.ToString() << std::endl;
//    ASSERT_TRUE(status.ok());
//}

TEST_F(MySQLTest, core) {
G
groot 已提交
35
    DBMetaOptions options;
36 37 38
//    //dialect+driver://username:password@host:port/database
//    options.backend_uri = "mysql://root:1234@:/test";
//    options.path = "/tmp/vecwise_test";
G
groot 已提交
39 40 41 42 43 44 45
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

Z
update  
zhiru 已提交
46
    int mode = Options::MODE::SINGLE;
G
groot 已提交
47
    meta::MySQLMetaImpl impl(options, mode);
48 49
//    auto status = impl.Initialize();
//    ASSERT_TRUE(status.ok());
50

51 52 53
    meta::TableSchema schema1;
    schema1.table_id_ = "test1";
    schema1.dimension_ = 123;
54

55 56
    auto status = impl.CreateTable(schema1);
//    std::cout << status.ToString() << std::endl;
57 58
    ASSERT_TRUE(status.ok());

59 60 61 62 63
    meta::TableSchema schema2;
    schema2.table_id_ = "test2";
    schema2.dimension_ = 321;
    status = impl.CreateTable(schema2);
//    std::cout << status.ToString() << std::endl;
64 65
    ASSERT_TRUE(status.ok());

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    status = impl.CreateTable(schema2);
//    std::cout << status.ToString() << std::endl;
//    ASSERT_THROW(impl.CreateTable(schema), mysqlpp::BadQuery);
    ASSERT_TRUE(status.ok());

    status = impl.DeleteTable(schema2.table_id_);
//    std::cout << status.ToString() << std::endl;
    ASSERT_TRUE(status.ok());

    size_t id1 = schema1.id_;
    long created_on1 = schema1.created_on_;
    status = impl.DescribeTable(schema1);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(schema1.id_, id1);
    ASSERT_EQ(schema1.table_id_, "test1");
    ASSERT_EQ(schema1.created_on_, created_on1);
    ASSERT_EQ(schema1.files_cnt_, 0);
    ASSERT_EQ(schema1.engine_type_, 1);
    ASSERT_EQ(schema1.store_raw_data_, false);

    bool check;
    status = impl.HasTable("test1", check);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(check, true);

    std::vector<meta::TableSchema> table_schema_array;
    status = impl.AllTables(table_schema_array);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_schema_array.size(), 1);
    meta::TableSchema resultSchema = table_schema_array[0];
    ASSERT_EQ(resultSchema.id_, id1);
    ASSERT_EQ(resultSchema.table_id_, "test1");
    ASSERT_EQ(resultSchema.dimension_, 123);
    ASSERT_EQ(resultSchema.files_cnt_, 0);
    ASSERT_EQ(resultSchema.engine_type_, 1);
    ASSERT_EQ(resultSchema.store_raw_data_, false);

    meta::TableFileSchema tableFileSchema;
    tableFileSchema.table_id_ = "test1";

    status = impl.CreateTableFile(tableFileSchema);
//    std::cout << status.ToString() << std::endl;
    ASSERT_TRUE(status.ok());

    tableFileSchema.file_type_ = meta::TableFileSchema::TO_INDEX;
    status = impl.UpdateTableFile(tableFileSchema);
//    std::cout << status.ToString() << std::endl;
    ASSERT_TRUE(status.ok());

    meta::TableFilesSchema filesToIndex;
    status = impl.FilesToIndex(filesToIndex);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(filesToIndex.size(), 1);
    meta::TableFileSchema fileToIndex = filesToIndex[0];
    ASSERT_EQ(fileToIndex.table_id_, "test1");
    ASSERT_EQ(fileToIndex.dimension_, 123);

//    meta::TableFilesSchema filesToIndex;
//    status = impl.FilesToIndex(filesToIndex);
//    ASSERT_TRUE(status.ok());
//    ASSERT_EQ(filesToIndex.size(), 0);

    meta::DatesT partition;
    partition.push_back(tableFileSchema.date_);
    meta::DatePartionedTableFilesSchema filesToSearch;
    status = impl.FilesToSearch(tableFileSchema.table_id_, partition, filesToSearch);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(filesToSearch.size(), 1);
    ASSERT_EQ(filesToSearch[tableFileSchema.date_].size(), 1);
    meta::TableFileSchema fileToSearch = filesToSearch[tableFileSchema.date_][0];
    ASSERT_EQ(fileToSearch.table_id_, "test1");
    ASSERT_EQ(fileToSearch.dimension_, 123);

    tableFileSchema.file_type_ = meta::TableFileSchema::RAW;
    status = impl.UpdateTableFile(tableFileSchema);
    ASSERT_TRUE(status.ok());

    meta::DatePartionedTableFilesSchema filesToMerge;
    status = impl.FilesToMerge(tableFileSchema.table_id_, filesToMerge);
//    std::cout << status.ToString() << std::endl;
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(filesToMerge.size(), 1);
    ASSERT_EQ(filesToMerge[tableFileSchema.date_].size(), 1);
    meta::TableFileSchema fileToMerge = filesToMerge[tableFileSchema.date_][0];
    ASSERT_EQ(fileToMerge.table_id_, "test1");
    ASSERT_EQ(fileToMerge.dimension_, 123);

    meta::TableFilesSchema resultTableFilesSchema;
    std::vector<size_t> ids;
    ids.push_back(tableFileSchema.id_);
    status = impl.GetTableFiles(tableFileSchema.table_id_, ids, resultTableFilesSchema);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(resultTableFilesSchema.size(), 1);
    meta::TableFileSchema resultTableFileSchema = resultTableFilesSchema[0];
//    ASSERT_EQ(resultTableFileSchema.id_, tableFileSchema.id_);
    ASSERT_EQ(resultTableFileSchema.table_id_, tableFileSchema.table_id_);
    ASSERT_EQ(resultTableFileSchema.file_id_, tableFileSchema.file_id_);
    ASSERT_EQ(resultTableFileSchema.file_type_, tableFileSchema.file_type_);
    ASSERT_EQ(resultTableFileSchema.size_, tableFileSchema.size_);
    ASSERT_EQ(resultTableFileSchema.date_, tableFileSchema.date_);
    ASSERT_EQ(resultTableFileSchema.engine_type_, tableFileSchema.engine_type_);
    ASSERT_EQ(resultTableFileSchema.dimension_, tableFileSchema.dimension_);

    tableFileSchema.size_ = 234;
    meta::TableSchema schema3;
    schema3.table_id_ = "test3";
    schema3.dimension_ = 321;
    status = impl.CreateTable(schema3);
    ASSERT_TRUE(status.ok());
    meta::TableFileSchema tableFileSchema2;
    tableFileSchema2.table_id_ = "test3";
    tableFileSchema2.size_ = 345;
    status = impl.CreateTableFile(tableFileSchema2);
    ASSERT_TRUE(status.ok());
    meta::TableFilesSchema filesToUpdate;
    filesToUpdate.emplace_back(tableFileSchema);
    filesToUpdate.emplace_back(tableFileSchema2);
    status = impl.UpdateTableFile(tableFileSchema);
    ASSERT_TRUE(status.ok());

    uint64_t resultSize;
    status = impl.Size(resultSize);
//    std::cout << status.ToString() << std::endl;
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(resultSize, tableFileSchema.size_ + tableFileSchema2.size_);

    uint64_t countResult;
    status = impl.Count(tableFileSchema.table_id_, countResult);
    ASSERT_TRUE(status.ok());

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

}

TEST_F(MySQLTest, GROUP_TEST) {
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

    int mode = Options::MODE::SINGLE;
    meta::MySQLMetaImpl impl(options, mode);

    auto table_id = "meta_test_group";

    meta::TableSchema group;
    group.table_id_ = table_id;
    auto status = impl.CreateTable(group);
    ASSERT_TRUE(status.ok());

    auto gid = group.id_;
    group.id_ = -1;
    status = impl.DescribeTable(group);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(group.id_, gid);
    ASSERT_EQ(group.table_id_, table_id);

    group.table_id_ = "not_found";
    status = impl.DescribeTable(group);
229 230
    ASSERT_TRUE(!status.ok());

231 232
    group.table_id_ = table_id;
    status = impl.CreateTable(group);
Z
update  
zhiru 已提交
233 234
    ASSERT_TRUE(status.ok());

235 236
    group.table_id_ = "";
    status = impl.CreateTable(group);
Z
update  
zhiru 已提交
237 238
    ASSERT_TRUE(status.ok());

239

240 241 242 243
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

244
TEST_F(MySQLTest, table_file_TEST) {
G
groot 已提交
245 246 247 248 249 250 251
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }
252

Z
update  
zhiru 已提交
253
    int mode = Options::MODE::SINGLE;
G
groot 已提交
254
    meta::MySQLMetaImpl impl(options, mode);
255

256
    auto table_id = "meta_test_group";
257

258 259 260 261
    meta::TableSchema group;
    group.table_id_ = table_id;
    group.dimension_ = 256;
    auto status = impl.CreateTable(group);
262 263

    meta::TableFileSchema table_file;
264
    table_file.table_id_ = group.table_id_;
265
    status = impl.CreateTableFile(table_file);
266
//    std::cout << status.ToString() << std::endl;
267 268 269
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.file_type_, meta::TableFileSchema::NEW);

Z
update  
zhiru 已提交
270 271 272 273 274
    uint64_t cnt = 0;
    status = impl.Count(table_id, cnt);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(cnt, 0UL);

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    auto file_id = table_file.file_id_;

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

    status = impl.UpdateTableFile(table_file);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.file_type_, new_file_type);

    meta::DatesT dates;
    dates.push_back(meta::Meta::GetDate());
    status = impl.DropPartitionsByDates(table_file.table_id_, dates);
    ASSERT_FALSE(status.ok());

    dates.clear();
    for (auto i=2; i < 10; ++i) {
        dates.push_back(meta::Meta::GetDateWithDelta(-1*i));
    }
    status = impl.DropPartitionsByDates(table_file.table_id_, dates);
    ASSERT_TRUE(status.ok());

    table_file.date_ = meta::Meta::GetDateWithDelta(-2);
    status = impl.UpdateTableFile(table_file);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.date_, meta::Meta::GetDateWithDelta(-2));
    ASSERT_FALSE(table_file.file_type_ == meta::TableFileSchema::TO_DELETE);

    dates.clear();
    dates.push_back(table_file.date_);
    status = impl.DropPartitionsByDates(table_file.table_id_, dates);
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
306 307 308 309

    std::vector<size_t> ids = {table_file.id_};
    meta::TableFilesSchema files;
    status = impl.GetTableFiles(table_file.table_id_, ids, files);
310
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
311 312
    ASSERT_EQ(files.size(), 1UL);
    ASSERT_TRUE(files[0].file_type_ == meta::TableFileSchema::TO_DELETE);
313 314 315 316 317 318 319

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

TEST_F(MySQLTest, ARCHIVE_TEST_DAYS) {
    srand(time(0));
G
groot 已提交
320 321 322 323 324 325 326 327
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

328 329 330 331
    int days_num = rand() % 100;
    std::stringstream ss;
    ss << "days:" << days_num;
    options.archive_conf = ArchiveConf("delete", ss.str());
Z
update  
zhiru 已提交
332 333
    int mode = Options::MODE::SINGLE;
    meta::MySQLMetaImpl impl(options, mode);
334

335
    auto table_id = "meta_test_group";
336

337 338 339
    meta::TableSchema group;
    group.table_id_ = table_id;
    auto status = impl.CreateTable(group);
340 341 342

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
343
    table_file.table_id_ = group.table_id_;
344 345 346 347

    auto cnt = 100;
    long ts = utils::GetMicroSecTimeStamp();
    std::vector<int> days;
Z
update  
zhiru 已提交
348
    std::vector<size_t> ids;
349 350 351 352 353 354 355 356
    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 已提交
357
        ids.push_back(table_file.id_);
358 359 360 361 362
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
363 364 365 366 367
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
368 369 370 371 372 373 374 375 376 377 378 379 380
        if (days[i] < days_num) {
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
        } else {
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::TO_DELETE);
        }
        i++;
    }

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

TEST_F(MySQLTest, ARCHIVE_TEST_DISK) {
G
groot 已提交
381 382 383 384 385 386 387 388
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

389
    options.archive_conf = ArchiveConf("delete", "disk:11");
Z
update  
zhiru 已提交
390 391
    int mode = Options::MODE::SINGLE;
    auto impl = meta::MySQLMetaImpl(options, mode);
392 393
    auto table_id = "meta_test_group";

394 395 396
    meta::TableSchema group;
    group.table_id_ = table_id;
    auto status = impl.CreateTable(group);
397 398 399

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
400
    table_file.table_id_ = group.table_id_;
401 402 403

    auto cnt = 10;
    auto each_size = 2UL;
Z
update  
zhiru 已提交
404
    std::vector<size_t> ids;
405 406 407 408 409 410
    for (auto i=0; i<cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW;
        table_file.size_ = each_size * meta::G;
        status = impl.UpdateTableFile(table_file);
        files.push_back(table_file);
Z
update  
zhiru 已提交
411
        ids.push_back(table_file.id_);
412 413 414 415 416
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
417 418 419 420 421
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
422 423 424 425 426 427 428 429 430 431 432 433 434
        if (i < 5) {
            ASSERT_TRUE(file.file_type_ == meta::TableFileSchema::TO_DELETE);
        } else {
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
        }
        ++i;
    }

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

TEST_F(MySQLTest, TABLE_FILES_TEST) {
G
groot 已提交
435 436 437 438 439 440 441
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }
442

Z
update  
zhiru 已提交
443
    int mode = Options::MODE::SINGLE;
G
groot 已提交
444
    auto impl = meta::MySQLMetaImpl(options, mode);
445 446 447

    auto table_id = "meta_test_group";

448 449 450
    meta::TableSchema group;
    group.table_id_ = table_id;
    auto status = impl.CreateTable(group);
451 452 453 454 455 456 457

    int new_files_cnt = 4;
    int raw_files_cnt = 5;
    int to_index_files_cnt = 6;
    int index_files_cnt = 7;

    meta::TableFileSchema table_file;
458
    table_file.table_id_ = group.table_id_;
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

    for (auto i=0; i<new_files_cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW;
        status = impl.UpdateTableFile(table_file);
    }

    for (auto i=0; i<raw_files_cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::RAW;
        status = impl.UpdateTableFile(table_file);
    }

    for (auto i=0; i<to_index_files_cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::TO_INDEX;
        status = impl.UpdateTableFile(table_file);
    }

    for (auto i=0; i<index_files_cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::INDEX;
        status = impl.UpdateTableFile(table_file);
    }

    meta::TableFilesSchema files;

    status = impl.FilesToIndex(files);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

    meta::DatePartionedTableFilesSchema dated_files;
491
    status = impl.FilesToMerge(group.table_id_, dated_files);
492 493 494 495 496 497 498 499 500 501 502 503 504
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);

    status = impl.FilesToIndex(files);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

    meta::DatesT dates = {table_file.date_};
    status = impl.FilesToSearch(table_id, dates, dated_files);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

G
groot 已提交
505 506 507 508 509
    status = impl.FilesToSearch(table_id, 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);

510 511 512
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}