mysql_meta_test.cpp 10.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>

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

Z
zhiru 已提交
24
TEST_F(DISABLED_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);
S
starlord 已提交
60
//    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
61

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

Z
zhiru 已提交
66
TEST_F(DISABLED_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

S
starlord 已提交
85

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

S
starlord 已提交
92 93 94 95 96
    meta::DatesT dates;
    dates.push_back(meta::Meta::GetDate());
    status = impl.DropPartitionsByDates(table_file.table_id_, dates);
    ASSERT_FALSE(status.ok());

Z
update  
zhiru 已提交
97 98
    uint64_t cnt = 0;
    status = impl.Count(table_id, cnt);
S
starlord 已提交
99 100
//    ASSERT_TRUE(status.ok());
//    ASSERT_EQ(cnt, 0UL);
Z
update  
zhiru 已提交
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
    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);

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

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

S
starlord 已提交
136 137
//    status = impl.NextTableId(table_id);

138 139 140 141
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

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

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

Z
update  
zhiru 已提交
159
    auto table_id = "meta_test_table";
160

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

    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
167
    table_file.table_id_ = table.table_id_;
168 169 170 171

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

    impl.Archive();
    int i = 0;

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

    for(auto& file : files_get) {
192 193 194 195 196 197 198 199
        if (days[i] < days_num) {
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
        } else {
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::TO_DELETE);
        }
        i++;
    }

200 201 202 203 204 205
    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 已提交
206 207 208 209

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

210 211 212 213
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

Z
zhiru 已提交
214
TEST_F(DISABLED_MySQLTest, ARCHIVE_TEST_DISK) {
G
groot 已提交
215 216 217 218 219 220 221 222
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }

223
    options.archive_conf = ArchiveConf("delete", "disk:11");
Z
update  
zhiru 已提交
224 225
    int mode = Options::MODE::SINGLE;
    auto impl = meta::MySQLMetaImpl(options, mode);
226 227
    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
228 229 230
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
231

S
starlord 已提交
232 233 234 235
    meta::TableSchema table_schema;
    table_schema.table_id_ = "";
    status = impl.CreateTable(table_schema);

236 237
    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
238
    table_file.table_id_ = table.table_id_;
239 240 241

    auto cnt = 10;
    auto each_size = 2UL;
Z
update  
zhiru 已提交
242
    std::vector<size_t> ids;
243 244 245
    for (auto i=0; i<cnt; ++i) {
        status = impl.CreateTableFile(table_file);
        table_file.file_type_ = meta::TableFileSchema::NEW;
246
        table_file.file_size_ = each_size * meta::G;
247 248
        status = impl.UpdateTableFile(table_file);
        files.push_back(table_file);
Z
update  
zhiru 已提交
249
        ids.push_back(table_file.id_);
250 251 252 253 254
    }

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
255 256 257 258 259
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
260 261 262 263 264 265 266 267 268 269 270 271
        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());
}

Z
zhiru 已提交
272
TEST_F(DISABLED_MySQLTest, TABLE_FILES_TEST) {
G
groot 已提交
273 274 275 276 277 278 279
    DBMetaOptions options;
    try {
        options = getDBMetaOptions();
    } catch(std::exception& ex) {
        ASSERT_TRUE(false);
        return;
    }
280

Z
update  
zhiru 已提交
281
    int mode = Options::MODE::SINGLE;
G
groot 已提交
282
    auto impl = meta::MySQLMetaImpl(options, mode);
283 284 285

    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
286 287 288
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
289 290 291 292 293 294 295

    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 已提交
296
    table_file.table_id_ = table.table_id_;
297 298 299 300 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

    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 已提交
329
    status = impl.FilesToMerge(table.table_id_, dated_files);
330 331 332 333 334 335 336 337
    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_};
338 339
    std::vector<size_t> ids;
    status = impl.FilesToSearch(table_id, ids, dates, dated_files);
340 341 342 343
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

344
    status = impl.FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
G
groot 已提交
345 346 347 348
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

X
xj.lin 已提交
349 350 351 352 353 354 355 356 357 358
    status = impl.FilesToSearch(table_id, ids, 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);

    ids.push_back(size_t(9999999999));
    status = impl.FilesToSearch(table_id, ids, dates, dated_files);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),0);

359 360 361
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}