meta_tests.cpp 8.2 KB
Newer Older
X
Xu Peng 已提交
1 2 3 4 5 6 7 8
////////////////////////////////////////////////////////////////////////////////
// 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>
X
Xu Peng 已提交
9 10
#include <stdlib.h>
#include <time.h>
X
Xu Peng 已提交
11 12 13 14

#include "utils.h"
#include "db/DBMetaImpl.h"
#include "db/Factories.h"
X
Xu Peng 已提交
15
#include "db/Utils.h"
X
Xu Peng 已提交
16
#include "db/MetaConsts.h"
X
Xu Peng 已提交
17

J
jinhai 已提交
18
using namespace zilliz::milvus::engine;
X
Xu Peng 已提交
19

S
starlord 已提交
20 21
TEST_F(MetaTest, TABLE_TEST) {
    auto table_id = "meta_test_table";
X
Xu Peng 已提交
22

S
starlord 已提交
23 24 25
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
26 27
    ASSERT_TRUE(status.ok());

S
starlord 已提交
28 29 30
    auto gid = table.id_;
    table.id_ = -1;
    status = impl_->DescribeTable(table);
X
Xu Peng 已提交
31
    ASSERT_TRUE(status.ok());
S
starlord 已提交
32 33
    ASSERT_EQ(table.id_, gid);
    ASSERT_EQ(table.table_id_, table_id);
X
Xu Peng 已提交
34

S
starlord 已提交
35 36
    table.table_id_ = "not_found";
    status = impl_->DescribeTable(table);
X
Xu Peng 已提交
37 38
    ASSERT_TRUE(!status.ok());

S
starlord 已提交
39 40 41
    table.table_id_ = table_id;
    status = impl_->CreateTable(table);
    ASSERT_TRUE(status.ok());
S
starlord 已提交
42 43 44 45

    table.table_id_ = "";
    status = impl_->CreateTable(table);
    ASSERT_TRUE(status.ok());
X
Xu Peng 已提交
46
}
X
Xu Peng 已提交
47

S
starlord 已提交
48 49
TEST_F(MetaTest, TABLE_FILE_TEST) {
    auto table_id = "meta_test_table";
X
Xu Peng 已提交
50

S
starlord 已提交
51 52
    meta::TableSchema table;
    table.table_id_ = table_id;
S
starlord 已提交
53
    table.dimension_ = 256;
S
starlord 已提交
54
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
55

X
Xu Peng 已提交
56
    meta::TableFileSchema table_file;
S
starlord 已提交
57
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
58
    status = impl_->CreateTableFile(table_file);
X
Xu Peng 已提交
59
    ASSERT_TRUE(status.ok());
G
groot 已提交
60
    ASSERT_EQ(table_file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
61

S
starlord 已提交
62 63 64 65 66
    uint64_t cnt = 0;
    status = impl_->Count(table_id, cnt);
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(cnt, 0UL);

G
groot 已提交
67
    auto file_id = table_file.file_id_;
X
Xu Peng 已提交
68

69
    auto new_file_type = meta::TableFileSchema::INDEX;
G
groot 已提交
70
    table_file.file_type_ = new_file_type;
X
Xu Peng 已提交
71

X
Xu Peng 已提交
72
    status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
73
    ASSERT_TRUE(status.ok());
G
groot 已提交
74
    ASSERT_EQ(table_file.file_type_, new_file_type);
X
Xu Peng 已提交
75

X
Xu Peng 已提交
76 77
    meta::DatesT dates;
    dates.push_back(meta::Meta::GetDate());
G
groot 已提交
78
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
X
Xu Peng 已提交
79 80
    ASSERT_FALSE(status.ok());

X
Xu Peng 已提交
81 82 83 84
    dates.clear();
    for (auto i=2; i < 10; ++i) {
        dates.push_back(meta::Meta::GetDateWithDelta(-1*i));
    }
G
groot 已提交
85
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
X
Xu Peng 已提交
86 87
    ASSERT_TRUE(status.ok());

G
groot 已提交
88
    table_file.date_ = meta::Meta::GetDateWithDelta(-2);
X
Xu Peng 已提交
89
    status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
90
    ASSERT_TRUE(status.ok());
G
groot 已提交
91 92
    ASSERT_EQ(table_file.date_, meta::Meta::GetDateWithDelta(-2));
    ASSERT_FALSE(table_file.file_type_ == meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
93 94

    dates.clear();
G
groot 已提交
95 96
    dates.push_back(table_file.date_);
    status = impl_->DropPartitionsByDates(table_file.table_id_, dates);
X
Xu Peng 已提交
97
    ASSERT_TRUE(status.ok());
98 99 100 101

    std::vector<size_t> ids = {table_file.id_};
    meta::TableFilesSchema files;
    status = impl_->GetTableFiles(table_file.table_id_, ids, files);
X
Xu Peng 已提交
102
    ASSERT_TRUE(status.ok());
103 104
    ASSERT_EQ(files.size(), 1UL);
    ASSERT_TRUE(files[0].file_type_ == meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
105
}
X
Xu Peng 已提交
106

X
Xu Peng 已提交
107 108 109
TEST_F(MetaTest, ARCHIVE_TEST_DAYS) {
    srand(time(0));
    DBMetaOptions options;
G
groot 已提交
110
    options.path = "/tmp/milvus_test";
X
Xu Peng 已提交
111 112 113 114 115 116
    int days_num = rand() % 100;
    std::stringstream ss;
    ss << "days:" << days_num;
    options.archive_conf = ArchiveConf("delete", ss.str());

    auto impl = meta::DBMetaImpl(options);
S
starlord 已提交
117
    auto table_id = "meta_test_table";
X
Xu Peng 已提交
118

S
starlord 已提交
119 120 121
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
X
Xu Peng 已提交
122

123
    meta::TableFilesSchema files;
X
Xu Peng 已提交
124
    meta::TableFileSchema table_file;
S
starlord 已提交
125
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
126 127 128 129

    auto cnt = 100;
    long ts = utils::GetMicroSecTimeStamp();
    std::vector<int> days;
130
    std::vector<size_t> ids;
X
Xu Peng 已提交
131
    for (auto i=0; i<cnt; ++i) {
X
Xu Peng 已提交
132
        status = impl.CreateTableFile(table_file);
G
groot 已提交
133
        table_file.file_type_ = meta::TableFileSchema::NEW;
X
Xu Peng 已提交
134
        int day = rand() % (days_num*2);
G
groot 已提交
135
        table_file.created_on_ = ts - day*meta::D_SEC*meta::US_PS - 10000;
X
Xu Peng 已提交
136
        status = impl.UpdateTableFile(table_file);
X
Xu Peng 已提交
137
        files.push_back(table_file);
X
Xu Peng 已提交
138
        days.push_back(day);
139
        ids.push_back(table_file.id_);
X
Xu Peng 已提交
140 141
    }

X
Xu Peng 已提交
142
    impl.Archive();
X
Xu Peng 已提交
143 144
    int i = 0;

145 146 147 148 149
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
X
Xu Peng 已提交
150
        if (days[i] < days_num) {
G
groot 已提交
151
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
152
        } else {
G
groot 已提交
153
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
154 155 156 157
        }
        i++;
    }

158
    impl.DropAll();
X
Xu Peng 已提交
159 160 161
}

TEST_F(MetaTest, ARCHIVE_TEST_DISK) {
X
Xu Peng 已提交
162
    DBMetaOptions options;
G
groot 已提交
163
    options.path = "/tmp/milvus_test";
X
Xu Peng 已提交
164
    options.archive_conf = ArchiveConf("delete", "disk:11");
X
Xu Peng 已提交
165 166

    auto impl = meta::DBMetaImpl(options);
167
    auto table_id = "meta_test_group";
X
Xu Peng 已提交
168

S
starlord 已提交
169 170 171
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl.CreateTable(table);
X
Xu Peng 已提交
172

173
    meta::TableFilesSchema files;
X
Xu Peng 已提交
174
    meta::TableFileSchema table_file;
S
starlord 已提交
175
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
176 177 178

    auto cnt = 10;
    auto each_size = 2UL;
179
    std::vector<size_t> ids;
X
Xu Peng 已提交
180
    for (auto i=0; i<cnt; ++i) {
X
Xu Peng 已提交
181
        status = impl.CreateTableFile(table_file);
G
groot 已提交
182 183
        table_file.file_type_ = meta::TableFileSchema::NEW;
        table_file.size_ = each_size * meta::G;
X
Xu Peng 已提交
184
        status = impl.UpdateTableFile(table_file);
X
Xu Peng 已提交
185
        files.push_back(table_file);
186
        ids.push_back(table_file.id_);
X
Xu Peng 已提交
187 188
    }

X
Xu Peng 已提交
189
    impl.Archive();
X
Xu Peng 已提交
190 191
    int i = 0;

192 193 194 195 196
    meta::TableFilesSchema files_get;
    status = impl.GetTableFiles(table_file.table_id_, ids, files_get);
    ASSERT_TRUE(status.ok());

    for(auto& file : files_get) {
X
Xu Peng 已提交
197
        if (i < 5) {
G
groot 已提交
198
            ASSERT_TRUE(file.file_type_ == meta::TableFileSchema::TO_DELETE);
X
Xu Peng 已提交
199
        } else {
G
groot 已提交
200
            ASSERT_EQ(file.file_type_, meta::TableFileSchema::NEW);
X
Xu Peng 已提交
201 202 203 204
        }
        ++i;
    }

205
    impl.DropAll();
X
Xu Peng 已提交
206 207
}

X
Xu Peng 已提交
208
TEST_F(MetaTest, TABLE_FILES_TEST) {
209
    auto table_id = "meta_test_group";
X
Xu Peng 已提交
210

S
starlord 已提交
211 212 213
    meta::TableSchema table;
    table.table_id_ = table_id;
    auto status = impl_->CreateTable(table);
X
Xu Peng 已提交
214 215 216 217 218 219

    int new_files_cnt = 4;
    int raw_files_cnt = 5;
    int to_index_files_cnt = 6;
    int index_files_cnt = 7;

X
Xu Peng 已提交
220
    meta::TableFileSchema table_file;
S
starlord 已提交
221
    table_file.table_id_ = table.table_id_;
X
Xu Peng 已提交
222 223

    for (auto i=0; i<new_files_cnt; ++i) {
X
Xu Peng 已提交
224
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
225
        table_file.file_type_ = meta::TableFileSchema::NEW;
X
Xu Peng 已提交
226
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
227 228 229
    }

    for (auto i=0; i<raw_files_cnt; ++i) {
X
Xu Peng 已提交
230
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
231
        table_file.file_type_ = meta::TableFileSchema::RAW;
X
Xu Peng 已提交
232
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
233 234 235
    }

    for (auto i=0; i<to_index_files_cnt; ++i) {
X
Xu Peng 已提交
236
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
237
        table_file.file_type_ = meta::TableFileSchema::TO_INDEX;
X
Xu Peng 已提交
238
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
239 240 241
    }

    for (auto i=0; i<index_files_cnt; ++i) {
X
Xu Peng 已提交
242
        status = impl_->CreateTableFile(table_file);
G
groot 已提交
243
        table_file.file_type_ = meta::TableFileSchema::INDEX;
X
Xu Peng 已提交
244
        status = impl_->UpdateTableFile(table_file);
X
Xu Peng 已提交
245 246
    }

247
    meta::TableFilesSchema files;
X
Xu Peng 已提交
248

X
Xu Peng 已提交
249
    status = impl_->FilesToIndex(files);
X
Xu Peng 已提交
250 251 252
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

253
    meta::DatePartionedTableFilesSchema dated_files;
S
starlord 已提交
254
    status = impl_->FilesToMerge(table.table_id_, dated_files);
X
Xu Peng 已提交
255
    ASSERT_TRUE(status.ok());
G
groot 已提交
256
    ASSERT_EQ(dated_files[table_file.date_].size(), raw_files_cnt);
X
Xu Peng 已提交
257

X
Xu Peng 已提交
258
    status = impl_->FilesToIndex(files);
X
Xu Peng 已提交
259 260 261
    ASSERT_TRUE(status.ok());
    ASSERT_EQ(files.size(), to_index_files_cnt);

G
groot 已提交
262
    meta::DatesT dates = {table_file.date_};
X
Xu Peng 已提交
263
    status = impl_->FilesToSearch(table_id, dates, dated_files);
X
Xu Peng 已提交
264
    ASSERT_TRUE(status.ok());
G
groot 已提交
265
    ASSERT_EQ(dated_files[table_file.date_].size(),
X
Xu Peng 已提交
266
            to_index_files_cnt+raw_files_cnt+index_files_cnt);
Z
zhiru 已提交
267

S
starlord 已提交
268 269 270 271
    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);
X
Xu Peng 已提交
272
}