test_meta_mysql.cpp 13.7 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

Z
update  
zhiru 已提交
18
#include "db/Utils.h"
S
starlord 已提交
19
#include "db/meta/MetaConsts.h"
Y
youny626 已提交
20 21
#include "db/meta/MySQLMetaImpl.h"
#include "db/utils.h"
Z
update  
zhiru 已提交
22

23 24
#include <gtest/gtest.h>
#include <mysql++/mysql++.h>
Y
youny626 已提交
25 26 27 28
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <thread>
29

S
starlord 已提交
30
TEST_F(MySqlMetaTest, TABLE_TEST) {
Z
update  
zhiru 已提交
31
    auto table_id = "meta_test_table";
32

S
starlord 已提交
33
    milvus::engine::meta::TableSchema table;
Z
update  
zhiru 已提交
34
    table.table_id_ = table_id;
S
starlord 已提交
35
    auto status = impl_->CreateTable(table);
36 37
    ASSERT_TRUE(status.ok());

Z
update  
zhiru 已提交
38 39
    auto gid = table.id_;
    table.id_ = -1;
S
starlord 已提交
40
    status = impl_->DescribeTable(table);
41
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
42 43
    ASSERT_EQ(table.id_, gid);
    ASSERT_EQ(table.table_id_, table_id);
44

Z
update  
zhiru 已提交
45
    table.table_id_ = "not_found";
S
starlord 已提交
46
    status = impl_->DescribeTable(table);
47 48
    ASSERT_TRUE(!status.ok());

Z
update  
zhiru 已提交
49
    table.table_id_ = table_id;
S
starlord 已提交
50
    status = impl_->CreateTable(table);
S
starlord 已提交
51
    ASSERT_EQ(status.code(), milvus::DB_ALREADY_EXIST);
Z
update  
zhiru 已提交
52

Z
update  
zhiru 已提交
53
    table.table_id_ = "";
S
starlord 已提交
54
    status = impl_->CreateTable(table);
Y
youny626 已提交
55
    //    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
56

S
starlord 已提交
57
    status = impl_->DropAll();
58 59 60
    ASSERT_TRUE(status.ok());
}

S
starlord 已提交
61
TEST_F(MySqlMetaTest, TABLE_FILE_TEST) {
Z
update  
zhiru 已提交
62
    auto table_id = "meta_test_table";
63

S
starlord 已提交
64
    milvus::engine::meta::TableSchema table;
Z
update  
zhiru 已提交
65 66
    table.table_id_ = table_id;
    table.dimension_ = 256;
S
starlord 已提交
67
    auto status = impl_->CreateTable(table);
68

S
starlord 已提交
69
    milvus::engine::meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
70
    table_file.table_id_ = table.table_id_;
S
starlord 已提交
71
    status = impl_->CreateTableFile(table_file);
72
    ASSERT_TRUE(status.ok());
S
starlord 已提交
73
    ASSERT_EQ(table_file.file_type_, milvus::engine::meta::TableFileSchema::NEW);
74

S
starlord 已提交
75 76
    milvus::engine::meta::DatesT dates;
    dates.push_back(milvus::engine::utils::GetDate());
G
groot 已提交
77
    status = impl_->DropDataByDate(table_file.table_id_, dates);
S
starlord 已提交
78
    ASSERT_TRUE(status.ok());
S
starlord 已提交
79

Z
update  
zhiru 已提交
80
    uint64_t cnt = 0;
S
starlord 已提交
81
    status = impl_->Count(table_id, cnt);
Y
youny626 已提交
82 83
    //    ASSERT_TRUE(status.ok());
    //    ASSERT_EQ(cnt, 0UL);
Z
update  
zhiru 已提交
84

85 86
    auto file_id = table_file.file_id_;

S
starlord 已提交
87
    auto new_file_type = milvus::engine::meta::TableFileSchema::INDEX;
88 89
    table_file.file_type_ = new_file_type;

S
starlord 已提交
90
    status = impl_->UpdateTableFile(table_file);
91 92 93 94
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(table_file.file_type_, new_file_type);

    dates.clear();
S
starlord 已提交
95
    for (auto i = 2; i < 10; ++i) {
S
starlord 已提交
96
        dates.push_back(milvus::engine::utils::GetDateWithDelta(-1 * i));
97
    }
G
groot 已提交
98
    status = impl_->DropDataByDate(table_file.table_id_, dates);
99 100
    ASSERT_TRUE(status.ok());

S
starlord 已提交
101
    table_file.date_ = milvus::engine::utils::GetDateWithDelta(-2);
S
starlord 已提交
102
    status = impl_->UpdateTableFile(table_file);
103
    ASSERT_TRUE(status.ok());
S
starlord 已提交
104 105
    ASSERT_EQ(table_file.date_, milvus::engine::utils::GetDateWithDelta(-2));
    ASSERT_FALSE(table_file.file_type_ == milvus::engine::meta::TableFileSchema::TO_DELETE);
106 107 108

    dates.clear();
    dates.push_back(table_file.date_);
G
groot 已提交
109
    status = impl_->DropDataByDate(table_file.table_id_, dates);
110
    ASSERT_TRUE(status.ok());
Z
update  
zhiru 已提交
111 112

    std::vector<size_t> ids = {table_file.id_};
S
starlord 已提交
113
    milvus::engine::meta::TableFilesSchema files;
S
starlord 已提交
114
    status = impl_->GetTableFiles(table_file.table_id_, ids, files);
S
starlord 已提交
115
    ASSERT_EQ(files.size(), 0UL);
116 117
}

S
starlord 已提交
118
TEST_F(MySqlMetaTest, ARCHIVE_TEST_DAYS) {
119
    srand(time(0));
S
starlord 已提交
120
    milvus::engine::DBMetaOptions options = GetOptions().meta_;
G
groot 已提交
121

S
starlord 已提交
122 123
    unsigned int seed = 1;
    int days_num = rand_r(&seed) % 100;
124 125
    std::stringstream ss;
    ss << "days:" << days_num;
S
starlord 已提交
126 127 128
    options.archive_conf_ = milvus::engine::ArchiveConf("delete", ss.str());
    int mode = milvus::engine::DBOptions::MODE::SINGLE;
    milvus::engine::meta::MySQLMetaImpl impl(options, mode);
129

Z
update  
zhiru 已提交
130
    auto table_id = "meta_test_table";
131

S
starlord 已提交
132
    milvus::engine::meta::TableSchema table;
Z
update  
zhiru 已提交
133 134
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
135

S
starlord 已提交
136 137
    milvus::engine::meta::TableFilesSchema files;
    milvus::engine::meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
138
    table_file.table_id_ = table.table_id_;
139 140

    auto cnt = 100;
S
starlord 已提交
141
    int64_t ts = milvus::engine::utils::GetMicroSecTimeStamp();
142
    std::vector<int> days;
Z
update  
zhiru 已提交
143
    std::vector<size_t> ids;
S
starlord 已提交
144
    for (auto i = 0; i < cnt; ++i) {
145
        status = impl.CreateTableFile(table_file);
S
starlord 已提交
146
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::NEW;
S
starlord 已提交
147
        int day = rand_r(&seed) % (days_num * 2);
S
starlord 已提交
148
        table_file.created_on_ = ts - day * milvus::engine::meta::D_SEC * milvus::engine::meta::US_PS - 10000;
149 150 151
        status = impl.UpdateTableFile(table_file);
        files.push_back(table_file);
        days.push_back(day);
Z
update  
zhiru 已提交
152
        ids.push_back(table_file.id_);
153 154 155 156 157
    }

    impl.Archive();
    int i = 0;

S
starlord 已提交
158
    milvus::engine::meta::TableFilesSchema files_get;
Z
update  
zhiru 已提交
159 160 161
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

Y
youny626 已提交
162
    for (auto& file : files_get) {
163
        if (days[i] < days_num) {
S
starlord 已提交
164
            ASSERT_EQ(file.file_type_, milvus::engine::meta::TableFileSchema::NEW);
165 166 167 168
        }
        i++;
    }

169
    std::vector<int> file_types = {
Y
youny626 已提交
170
        (int)milvus::engine::meta::TableFileSchema::NEW,
171 172 173 174
    };
    std::vector<std::string> file_ids;
    status = impl.FilesByType(table_id, file_types, file_ids);
    ASSERT_FALSE(file_ids.empty());
S
starlord 已提交
175 176 177 178

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

179 180 181 182
    status = impl.DropAll();
    ASSERT_TRUE(status.ok());
}

S
starlord 已提交
183
TEST_F(MySqlMetaTest, ARCHIVE_TEST_DISK) {
S
starlord 已提交
184
    milvus::engine::DBMetaOptions options = GetOptions().meta_;
G
groot 已提交
185

S
starlord 已提交
186 187
    options.archive_conf_ = milvus::engine::ArchiveConf("delete", "disk:11");
    int mode = milvus::engine::DBOptions::MODE::SINGLE;
G
groot 已提交
188
    milvus::engine::meta::MySQLMetaImpl impl(options, mode);
189 190
    auto table_id = "meta_test_group";

S
starlord 已提交
191
    milvus::engine::meta::TableSchema table;
Z
update  
zhiru 已提交
192 193
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
194

S
starlord 已提交
195
    milvus::engine::meta::TableSchema table_schema;
S
starlord 已提交
196 197 198
    table_schema.table_id_ = "";
    status = impl.CreateTable(table_schema);

S
starlord 已提交
199 200
    milvus::engine::meta::TableFilesSchema files;
    milvus::engine::meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
201
    table_file.table_id_ = table.table_id_;
202 203 204

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

    impl.Archive();
    int i = 0;

S
starlord 已提交
218
    milvus::engine::meta::TableFilesSchema files_get;
Z
update  
zhiru 已提交
219 220 221
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

Y
youny626 已提交
222
    for (auto& file : files_get) {
S
starlord 已提交
223
        if (i >= 5) {
S
starlord 已提交
224
            ASSERT_EQ(file.file_type_, milvus::engine::meta::TableFileSchema::NEW);
225 226 227 228 229 230 231 232
        }
        ++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";

S
starlord 已提交
236
    milvus::engine::meta::TableSchema table;
Z
update  
zhiru 已提交
237
    table.table_id_ = table_id;
S
starlord 已提交
238
    auto status = impl_->CreateTable(table);
239

S
starlord 已提交
240 241 242 243 244 245 246
    uint64_t new_merge_files_cnt = 1;
    uint64_t new_index_files_cnt = 2;
    uint64_t backup_files_cnt = 3;
    uint64_t new_files_cnt = 4;
    uint64_t raw_files_cnt = 5;
    uint64_t to_index_files_cnt = 6;
    uint64_t index_files_cnt = 7;
247

S
starlord 已提交
248
    milvus::engine::meta::TableFileSchema table_file;
Z
update  
zhiru 已提交
249
    table_file.table_id_ = table.table_id_;
250

S
starlord 已提交
251
    for (auto i = 0; i < new_merge_files_cnt; ++i) {
S
starlord 已提交
252
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
253
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::NEW_MERGE;
S
starlord 已提交
254 255 256
        status = impl_->UpdateTableFile(table_file);
    }

S
starlord 已提交
257
    for (auto i = 0; i < new_index_files_cnt; ++i) {
S
starlord 已提交
258
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
259
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::NEW_INDEX;
S
starlord 已提交
260 261 262
        status = impl_->UpdateTableFile(table_file);
    }

S
starlord 已提交
263
    for (auto i = 0; i < backup_files_cnt; ++i) {
S
starlord 已提交
264
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
265
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::BACKUP;
S
starlord 已提交
266 267 268 269
        table_file.row_count_ = 1;
        status = impl_->UpdateTableFile(table_file);
    }

S
starlord 已提交
270
    for (auto i = 0; i < new_files_cnt; ++i) {
S
starlord 已提交
271
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
272
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::NEW;
S
starlord 已提交
273
        status = impl_->UpdateTableFile(table_file);
274 275
    }

S
starlord 已提交
276
    for (auto i = 0; i < raw_files_cnt; ++i) {
S
starlord 已提交
277
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
278
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::RAW;
S
starlord 已提交
279
        table_file.row_count_ = 1;
S
starlord 已提交
280
        status = impl_->UpdateTableFile(table_file);
281 282
    }

S
starlord 已提交
283
    for (auto i = 0; i < to_index_files_cnt; ++i) {
S
starlord 已提交
284
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
285
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::TO_INDEX;
S
starlord 已提交
286
        table_file.row_count_ = 1;
S
starlord 已提交
287
        status = impl_->UpdateTableFile(table_file);
288 289
    }

S
starlord 已提交
290
    for (auto i = 0; i < index_files_cnt; ++i) {
S
starlord 已提交
291
        status = impl_->CreateTableFile(table_file);
S
starlord 已提交
292
        table_file.file_type_ = milvus::engine::meta::TableFileSchema::INDEX;
S
starlord 已提交
293
        table_file.row_count_ = 1;
S
starlord 已提交
294
        status = impl_->UpdateTableFile(table_file);
295 296
    }

S
starlord 已提交
297 298 299
    uint64_t total_row_count = 0;
    status = impl_->Count(table_id, total_row_count);
    ASSERT_TRUE(status.ok());
S
starlord 已提交
300
    ASSERT_EQ(total_row_count, raw_files_cnt + to_index_files_cnt + index_files_cnt);
301

S
starlord 已提交
302
    milvus::engine::meta::TableFilesSchema files;
S
starlord 已提交
303
    status = impl_->FilesToIndex(files);
304 305
    ASSERT_EQ(files.size(), to_index_files_cnt);

S
starlord 已提交
306
    milvus::engine::meta::DatePartionedTableFilesSchema dated_files;
S
starlord 已提交
307
    status = impl_->FilesToMerge(table.table_id_, dated_files);
308 309
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);

S
starlord 已提交
310
    status = impl_->FilesToIndex(files);
311 312
    ASSERT_EQ(files.size(), to_index_files_cnt);

S
starlord 已提交
313
    milvus::engine::meta::DatesT dates = {table_file.date_};
314
    std::vector<size_t> ids;
S
starlord 已提交
315
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
Y
youny626 已提交
316
    ASSERT_EQ(dated_files[table_file.date_].size(), to_index_files_cnt + raw_files_cnt + index_files_cnt);
317

S
starlord 已提交
318
    status = impl_->FilesToSearch(table_id, ids, milvus::engine::meta::DatesT(), dated_files);
Y
youny626 已提交
319
    ASSERT_EQ(dated_files[table_file.date_].size(), to_index_files_cnt + raw_files_cnt + index_files_cnt);
G
groot 已提交
320

S
starlord 已提交
321
    status = impl_->FilesToSearch(table_id, ids, milvus::engine::meta::DatesT(), dated_files);
Y
youny626 已提交
322
    ASSERT_EQ(dated_files[table_file.date_].size(), to_index_files_cnt + raw_files_cnt + index_files_cnt);
X
xj.lin 已提交
323 324

    ids.push_back(size_t(9999999999));
S
starlord 已提交
325
    status = impl_->FilesToSearch(table_id, ids, dates, dated_files);
S
starlord 已提交
326
    ASSERT_EQ(dated_files[table_file.date_].size(), 0);
X
xj.lin 已提交
327

S
starlord 已提交
328 329 330 331 332 333 334
    std::vector<int> file_types;
    std::vector<std::string> file_ids;
    status = impl_->FilesByType(table.table_id_, file_types, file_ids);
    ASSERT_TRUE(file_ids.empty());
    ASSERT_FALSE(status.ok());

    file_types = {
Y
youny626 已提交
335 336 337
        milvus::engine::meta::TableFileSchema::NEW,       milvus::engine::meta::TableFileSchema::NEW_MERGE,
        milvus::engine::meta::TableFileSchema::NEW_INDEX, milvus::engine::meta::TableFileSchema::TO_INDEX,
        milvus::engine::meta::TableFileSchema::INDEX,     milvus::engine::meta::TableFileSchema::RAW,
S
starlord 已提交
338
        milvus::engine::meta::TableFileSchema::BACKUP,
S
starlord 已提交
339 340 341
    };
    status = impl_->FilesByType(table.table_id_, file_types, file_ids);
    ASSERT_TRUE(status.ok());
Y
youny626 已提交
342 343
    uint64_t total_cnt = new_index_files_cnt + new_merge_files_cnt + backup_files_cnt + new_files_cnt + raw_files_cnt +
                         to_index_files_cnt + index_files_cnt;
S
starlord 已提交
344 345 346 347 348
    ASSERT_EQ(file_ids.size(), total_cnt);

    status = impl_->DeleteTableFiles(table_id);
    ASSERT_TRUE(status.ok());

G
groot 已提交
349
    status = impl_->DropTable(table_id);
S
starlord 已提交
350 351
    ASSERT_TRUE(status.ok());

Z
zhiru 已提交
352
    status = impl_->CleanUpFilesWithTTL(0UL);
S
starlord 已提交
353 354 355 356 357 358
    ASSERT_TRUE(status.ok());
}

TEST_F(MySqlMetaTest, INDEX_TEST) {
    auto table_id = "index_test";

S
starlord 已提交
359
    milvus::engine::meta::TableSchema table;
S
starlord 已提交
360 361 362
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);

S
starlord 已提交
363
    milvus::engine::TableIndex index;
S
starlord 已提交
364 365 366 367 368 369 370 371 372 373
    index.metric_type_ = 2;
    index.nlist_ = 1234;
    index.engine_type_ = 3;
    status = impl_->UpdateTableIndex(table_id, index);
    ASSERT_TRUE(status.ok());

    int64_t flag = 65536;
    status = impl_->UpdateTableFlag(table_id, flag);
    ASSERT_TRUE(status.ok());

S
starlord 已提交
374
    milvus::engine::meta::TableSchema table_info;
S
starlord 已提交
375 376 377 378
    table_info.table_id_ = table_id;
    status = impl_->DescribeTable(table_info);
    ASSERT_EQ(table_info.flag_, flag);

S
starlord 已提交
379
    milvus::engine::TableIndex index_out;
S
starlord 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392
    status = impl_->DescribeTableIndex(table_id, index_out);
    ASSERT_EQ(index_out.metric_type_, index.metric_type_);
    ASSERT_EQ(index_out.nlist_, index.nlist_);
    ASSERT_EQ(index_out.engine_type_, index.engine_type_);

    status = impl_->DropTableIndex(table_id);
    ASSERT_TRUE(status.ok());
    status = impl_->DescribeTableIndex(table_id, index_out);
    ASSERT_NE(index_out.metric_type_, index.metric_type_);
    ASSERT_NE(index_out.nlist_, index.nlist_);
    ASSERT_NE(index_out.engine_type_, index.engine_type_);

    status = impl_->UpdateTableFilesToIndex(table_id);
393 394
    ASSERT_TRUE(status.ok());
}