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

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

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

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

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

Z
update  
zhiru 已提交
43
    table.table_id_ = table_id;
S
starlord 已提交
44
    status = impl_->CreateTable(table);
S
starlord 已提交
45
    ASSERT_TRUE(status.IsAlreadyExist());
Z
update  
zhiru 已提交
46

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

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

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

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

S
starlord 已提交
63

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

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

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

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

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

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

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

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

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

    std::vector<size_t> ids = {table_file.id_};
    meta::TableFilesSchema files;
S
starlord 已提交
109
    status = impl_->GetTableFiles(table_file.table_id_, ids, files);
110
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
111 112
    ASSERT_EQ(files.size(), 1UL);
    ASSERT_TRUE(files[0].file_type_ == meta::TableFileSchema::TO_DELETE);
113 114
}

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

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

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

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

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

    auto cnt = 100;
    long ts = utils::GetMicroSecTimeStamp();
    std::vector<int> days;
Z
update  
zhiru 已提交
139
    std::vector<size_t> ids;
140 141 142 143 144 145 146 147
    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 已提交
148
        ids.push_back(table_file.id_);
149 150 151 152 153
    }

    impl.Archive();
    int i = 0;

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

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

167 168 169 170 171 172
    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 已提交
173 174 175 176

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

177 178 179 180
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

S
starlord 已提交
181 182
TEST_F(MySqlMetaTest, ARCHIVE_TEST_DISK) {
    DBMetaOptions options = GetOptions().meta;
G
groot 已提交
183

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

Z
update  
zhiru 已提交
189 190 191
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
192

S
starlord 已提交
193 194 195 196
    meta::TableSchema table_schema;
    table_schema.table_id_ = "";
    status = impl.CreateTable(table_schema);

197 198
    meta::TableFilesSchema files;
    meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
199
    table_file.table_id_ = table.table_id_;
200 201 202

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

    impl.Archive();
    int i = 0;

Z
update  
zhiru 已提交
216 217 218 219 220
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
221 222 223 224 225 226 227 228 229 230 231 232
        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());
}

S
starlord 已提交
233
TEST_F(MySqlMetaTest, TABLE_FILES_TEST) {
234 235
    auto table_id = "meta_test_group";

Z
update  
zhiru 已提交
236 237
    meta::TableSchema table;
    table.table_id_ = table_id;
S
starlord 已提交
238
    auto status = impl_->CreateTable(table);
239 240 241 242 243 244 245

    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 已提交
246
    table_file.table_id_ = table.table_id_;
247 248

    for (auto i=0; i<new_files_cnt; ++i) {
S
starlord 已提交
249
        status = impl_->CreateTableFile(table_file);
250
        table_file.file_type_ = meta::TableFileSchema::NEW;
S
starlord 已提交
251
        status = impl_->UpdateTableFile(table_file);
252 253 254
    }

    for (auto i=0; i<raw_files_cnt; ++i) {
S
starlord 已提交
255
        status = impl_->CreateTableFile(table_file);
256
        table_file.file_type_ = meta::TableFileSchema::RAW;
S
starlord 已提交
257
        status = impl_->UpdateTableFile(table_file);
258 259 260
    }

    for (auto i=0; i<to_index_files_cnt; ++i) {
S
starlord 已提交
261
        status = impl_->CreateTableFile(table_file);
262
        table_file.file_type_ = meta::TableFileSchema::TO_INDEX;
S
starlord 已提交
263
        status = impl_->UpdateTableFile(table_file);
264 265 266
    }

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

    meta::TableFilesSchema files;

S
starlord 已提交
274
    status = impl_->FilesToIndex(files);
275 276 277 278
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

    meta::DatePartionedTableFilesSchema dated_files;
S
starlord 已提交
279
    status = impl_->FilesToMerge(table.table_id_, dated_files);
280 281 282
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);

S
starlord 已提交
283
    status = impl_->FilesToIndex(files);
284 285 286 287
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

    meta::DatesT dates = {table_file.date_};
288
    std::vector<size_t> ids;
S
starlord 已提交
289
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
290 291 292 293
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

S
starlord 已提交
294
    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
G
groot 已提交
295 296 297 298
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),
              to_index_files_cnt+raw_files_cnt+index_files_cnt);

S
starlord 已提交
299
    status = impl_->FilesToSearch(table_id, ids, meta::DatesT(), dated_files);
X
xj.lin 已提交
300 301 302 303 304
    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));
S
starlord 已提交
305
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
X
xj.lin 已提交
306 307 308
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(dated_files[table_file.date_].size(),0);

S
starlord 已提交
309
    status = impl_->DropAll();
310 311
    ASSERT_TRUE(status.ok());
}