mysql_meta_test.cpp 9.5 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;

Z
update  
zhiru 已提交
24
TEST_F(MySQLTest, TABLE_TEST) {
G
groot 已提交
25 26 27 28 29 30 31 32
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

Z
update  
zhiru 已提交
33
    int mode = Options::MODE::SINGLE;
G
groot 已提交
34
    meta::MySQLMetaImpl impl(options, mode);
35

Z
update  
zhiru 已提交
36
    auto table_id = "meta_test_table";
37

Z
update  
zhiru 已提交
38 39 40
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
41 42
    ASSERT_TRUE(status.ok());

Z
update  
zhiru 已提交
43 44 45
    auto gid = table.id_;
    table.id_ = -1;
    status = impl.DescribeTable(table);
46
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
47 48
    ASSERT_EQ(table.id_, gid);
    ASSERT_EQ(table.table_id_, table_id);
49

Z
update  
zhiru 已提交
50 51
    table.table_id_ = "not_found";
    status = impl.DescribeTable(table);
52 53
    ASSERT_TRUE(!status.ok());

Z
update  
zhiru 已提交
54 55
    table.table_id_ = table_id;
    status = impl.CreateTable(table);
Z
update  
zhiru 已提交
56 57
    ASSERT_TRUE(status.ok());

Z
update  
zhiru 已提交
58 59
    table.table_id_ = "";
    status = impl.CreateTable(table);
Z
update  
zhiru 已提交
60 61
    ASSERT_TRUE(status.ok());

62 63 64 65
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

Z
update  
zhiru 已提交
66
TEST_F(MySQLTest, TABLE_FILE_TEST) {
G
groot 已提交
67 68 69 70 71 72 73
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }
74

Z
update  
zhiru 已提交
75
    int mode = Options::MODE::SINGLE;
G
groot 已提交
76
    meta::MySQLMetaImpl impl(options, mode);
77

Z
update  
zhiru 已提交
78
    auto table_id = "meta_test_table";
79

Z
update  
zhiru 已提交
80 81 82 83
    meta::TableSchema table;
    table.table_id_ = table_id;
    table.dimension_ = 256;
    auto status = impl.CreateTable(table);
84 85

    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
86
    table_file.table_id_ = table.table_id_;
87 88 89 90
    status = impl.CreateTableFile(table_file);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.file_type_, meta::TableFileSchema::NEW);

Z
update  
zhiru 已提交
91 92 93 94 95
    uint64_t cnt = 0;
    status = impl.Count(table_id, cnt);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(cnt, 0UL);

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
    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 已提交
127 128 129 130

    std::vector<size_t> ids = {table_file.id_};
    meta::TableFilesSchema files;
    status = impl.GetTableFiles(table_file.table_id_, ids, files);
131
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
132 133
    ASSERT_EQ(files.size(), 1UL);
    ASSERT_TRUE(files[0].file_type_ == meta::TableFileSchema::TO_DELETE);
134 135 136 137 138 139 140

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

TEST_F(MySQLTest, ARCHIVE_TEST_DAYS) {
    srand(time(0));
G
groot 已提交
141 142 143 144 145 146 147 148
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

149 150 151 152
    int days_num = rand() % 100;
    std::stringstream ss;
    ss << "days:" << days_num;
    options.archive_conf = ArchiveConf("delete", ss.str());
Z
update  
zhiru 已提交
153 154
    int mode = Options::MODE::SINGLE;
    meta::MySQLMetaImpl impl(options, mode);
155

Z
update  
zhiru 已提交
156
    auto table_id = "meta_test_table";
157

Z
update  
zhiru 已提交
158 159 160
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
161 162 163

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
164
    table_file.table_id_ = table.table_id_;
165 166 167 168

    auto cnt = 100;
    long ts = utils::GetMicroSecTimeStamp();
    std::vector<int> days;
Z
update  
zhiru 已提交
169
    std::vector<size_t> ids;
170 171 172 173 174 175 176 177
    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 已提交
178
        ids.push_back(table_file.id_);
179 180 181 182 183
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
184 185 186 187 188
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
189 190 191 192 193 194 195 196 197 198 199 200 201
        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 已提交
202 203 204 205 206 207 208 209
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

210
    options.archive_conf = ArchiveConf("delete", "disk:11");
Z
update  
zhiru 已提交
211 212
    int mode = Options::MODE::SINGLE;
    auto impl = meta::MySQLMetaImpl(options, mode);
213 214
    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
215 216 217
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
218 219 220

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
221
    table_file.table_id_ = table.table_id_;
222 223 224

    auto cnt = 10;
    auto each_size = 2UL;
Z
update  
zhiru 已提交
225
    std::vector<size_t> ids;
226 227 228 229 230 231
    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 已提交
232
        ids.push_back(table_file.id_);
233 234 235 236 237
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
238 239 240 241 242
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
243 244 245 246 247 248 249 250 251 252 253 254 255
        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 已提交
256 257 258 259 260 261 262
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }
263

Z
update  
zhiru 已提交
264
    int mode = Options::MODE::SINGLE;
G
groot 已提交
265
    auto impl = meta::MySQLMetaImpl(options, mode);
266 267 268

    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
269 270 271
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
272 273 274 275 276 277 278

    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;
Z
update  
zhiru 已提交
279
    table_file.table_id_ = table.table_id_;
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 306 307 308 309 310 311

    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;
Z
update  
zhiru 已提交
312
    status = impl.FilesToMerge(table.table_id_, dated_files);
313 314 315 316 317 318 319 320 321 322 323 324 325
    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 已提交
326 327 328 329 330
    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);

331 332 333
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}