MySQLMetaImpl_test.cpp 15.8 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) {
S
starlord 已提交
35
    DBMetaOptions options;
Z
update  
zhiru 已提交
36 37 38
//    //dialect+driver://username:password@host:port/database
//    options.backend_uri = "mysql://root:1234@:/test";
//    options.path = "/tmp/vecwise_test";
S
starlord 已提交
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;
S
starlord 已提交
47
    meta::MySQLMetaImpl impl(options, mode);
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
//    auto status = impl.Initialize();
//    ASSERT_TRUE(status.ok());

    meta::TableSchema schema1;
    schema1.table_id_ = "test1";
    schema1.dimension_ = 123;

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

    meta::TableSchema schema2;
    schema2.table_id_ = "test2";
    schema2.dimension_ = 321;
    status = impl.CreateTable(schema2);
//    std::cout << status.ToString() << std::endl;
    ASSERT_TRUE(status.ok());

    status = impl.CreateTable(schema2);
//    std::cout << status.ToString() << std::endl;
//    ASSERT_THROW(impl.CreateTable(schema), mysqlpp::BadQuery);
Z
update  
zhiru 已提交
69
    ASSERT_TRUE(status.ok());
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

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

Z
update  
zhiru 已提交
153 154 155 156
    meta::TableFilesSchema resultTableFilesSchema;
    std::vector<size_t> ids;
    ids.push_back(tableFileSchema.id_);
    status = impl.GetTableFiles(tableFileSchema.table_id_, ids, resultTableFilesSchema);
157
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
158 159 160
    ASSERT_EQ(resultTableFilesSchema.size(), 1);
    meta::TableFileSchema resultTableFileSchema = resultTableFilesSchema[0];
//    ASSERT_EQ(resultTableFileSchema.id_, tableFileSchema.id_);
161 162 163 164 165
    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_);
Z
update  
zhiru 已提交
166 167
    ASSERT_EQ(resultTableFileSchema.engine_type_, tableFileSchema.engine_type_);
    ASSERT_EQ(resultTableFileSchema.dimension_, tableFileSchema.dimension_);
168 169

    tableFileSchema.size_ = 234;
Z
update  
zhiru 已提交
170 171 172 173
    meta::TableSchema schema3;
    schema3.table_id_ = "test3";
    schema3.dimension_ = 321;
    status = impl.CreateTable(schema3);
174 175
    ASSERT_TRUE(status.ok());
    meta::TableFileSchema tableFileSchema2;
Z
update  
zhiru 已提交
176
    tableFileSchema2.table_id_ = "test3";
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
    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) {
S
starlord 已提交
202 203 204 205 206 207 208
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }
Z
update  
zhiru 已提交
209 210

    int mode = Options::MODE::SINGLE;
S
starlord 已提交
211
    meta::MySQLMetaImpl impl(options, mode);
Z
update  
zhiru 已提交
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    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);
    ASSERT_TRUE(!status.ok());

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

    group.table_id_ = "";
    status = impl.CreateTable(group);
    ASSERT_TRUE(status.ok());

239 240 241 242 243 244

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

TEST_F(MySQLTest, table_file_TEST) {
S
starlord 已提交
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;
S
starlord 已提交
254
    meta::MySQLMetaImpl impl(options, mode);
255 256 257 258 259

    auto table_id = "meta_test_group";

    meta::TableSchema group;
    group.table_id_ = table_id;
Z
update  
zhiru 已提交
260
    group.dimension_ = 256;
261 262 263 264 265
    auto status = impl.CreateTable(group);

    meta::TableFileSchema table_file;
    table_file.table_id_ = group.table_id_;
    status = impl.CreateTableFile(table_file);
Z
zhiru 已提交
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));
S
starlord 已提交
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 336 337 338 339 340 341 342 343 344 345 346 347

    auto table_id = "meta_test_group";

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

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
    table_file.table_id_ = group.table_id_;

    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) {
S
starlord 已提交
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 394 395 396 397 398 399 400 401 402 403
    auto table_id = "meta_test_group";

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

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
    table_file.table_id_ = group.table_id_;

    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) {
S
starlord 已提交
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;
S
starlord 已提交
444
    auto impl = meta::MySQLMetaImpl(options, mode);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

    auto table_id = "meta_test_group";

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

    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;
    table_file.table_id_ = group.table_id_;

    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;
    status = impl.FilesToMerge(group.table_id_, dated_files);
    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);

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