rpc_test.cpp 16.8 KB
Newer Older
Y
Yu Kun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
////////////////////////////////////////////////////////////////////////////////
// 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 <boost/filesystem.hpp>

#include "server/Server.h"
#include "server/grpc_impl/GrpcRequestHandler.h"
#include "server/grpc_impl/GrpcRequestScheduler.h"
#include "server/grpc_impl/GrpcRequestTask.h"
#include "version.h"

#include "grpc/gen-milvus/milvus.grpc.pb.h"
#include "grpc/gen-status/status.pb.h"

#include "server/DBWrapper.h"
#include "server/ServerConfig.h"
#include "scheduler/SchedInst.h"
#include "scheduler/ResourceFactory.h"
#include "utils/CommonUtil.h"


namespace zilliz {
namespace milvus {
namespace server {
namespace grpc {

static const char *TABLE_NAME = "test_grpc";
static constexpr int64_t TABLE_DIM = 256;
static constexpr int64_t INDEX_FILE_SIZE = 1024;
Y
Yu Kun 已提交
34
static constexpr int64_t VECTOR_COUNT = 1000;
Y
Yu Kun 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static constexpr int64_t INSERT_LOOP = 10;
constexpr int64_t SECONDS_EACH_HOUR = 3600;

class RpcHandlerTest : public testing::Test {
 protected:
    void
    SetUp() override {

        auto res_mgr = engine::ResMgrInst::GetInstance();
        res_mgr->Clear();
        res_mgr->Add(engine::ResourceFactory::Create("disk", "DISK", 0, true, false));
        res_mgr->Add(engine::ResourceFactory::Create("cpu", "CPU", 0, true, true));
        res_mgr->Add(engine::ResourceFactory::Create("gtx1660", "GPU", 0, true, true));

        auto default_conn = engine::Connection("IO", 500.0);
        auto PCIE = engine::Connection("IO", 11000.0);
        res_mgr->Connect("disk", "cpu", default_conn);
        res_mgr->Connect("cpu", "gtx1660", PCIE);
        res_mgr->Start();
        engine::SchedInst::GetInstance()->Start();

        zilliz::milvus::engine::Options opt;

        ConfigNode &db_config = ServerConfig::GetInstance().GetConfig(CONFIG_DB);
        db_config.SetValue(CONFIG_DB_URL, "sqlite://:@:/");
        db_config.SetValue(CONFIG_DB_PATH, "/tmp/milvus_test");
        db_config.SetValue(CONFIG_DB_SLAVE_PATH, "");
        db_config.SetValue(CONFIG_DB_ARCHIVE_DISK, "");
        db_config.SetValue(CONFIG_DB_ARCHIVE_DAYS, "");

        ConfigNode &cache_config = ServerConfig::GetInstance().GetConfig(CONFIG_CACHE);
        cache_config.SetValue(CONFIG_INSERT_CACHE_IMMEDIATELY, "");

        ConfigNode &engine_config = ServerConfig::GetInstance().GetConfig(CONFIG_ENGINE);
        engine_config.SetValue(CONFIG_OMP_THREAD_NUM, "");

Y
Yu Kun 已提交
71 72 73 74 75 76 77 78
        ConfigNode &serverConfig = ServerConfig::GetInstance().GetConfig(CONFIG_SERVER);
//        serverConfig.SetValue(CONFIG_CLUSTER_MODE, "cluster");
//        DBWrapper::GetInstance().GetInstance().StartService();
//        DBWrapper::GetInstance().GetInstance().StopService();
//
//        serverConfig.SetValue(CONFIG_CLUSTER_MODE, "read_only");
//        DBWrapper::GetInstance().GetInstance().StartService();
//        DBWrapper::GetInstance().GetInstance().StopService();
Y
Yu Kun 已提交
79

Y
Yu Kun 已提交
80
        serverConfig.SetValue(CONFIG_CLUSTER_MODE, "single");
Y
Yu Kun 已提交
81
        DBWrapper::GetInstance().GetInstance().StartService();
Y
Yu Kun 已提交
82

Y
Yu Kun 已提交
83 84 85 86
        //initialize handler, create table
        handler = std::make_shared<GrpcRequestHandler>();
        ::grpc::ServerContext context;
        ::milvus::grpc::TableSchema request;
Y
Yu Kun 已提交
87
        ::milvus::grpc::Status status;
Y
Yu Kun 已提交
88 89 90 91 92 93 94 95 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        request.mutable_table_name()->set_table_name(TABLE_NAME);
        request.set_dimension(TABLE_DIM);
        request.set_index_file_size(INDEX_FILE_SIZE);
        request.set_metric_type(1);
        ::grpc::Status grpc_status = handler->CreateTable(&context, &request, &status);
    }

    void
    TearDown() override {
        DBWrapper::GetInstance().StopService();
        engine::ResMgrInst::GetInstance()->Stop();
        engine::SchedInst::GetInstance()->Stop();
        boost::filesystem::remove_all("/tmp/milvus_test");
    }
 protected:
    std::shared_ptr<GrpcRequestHandler> handler;
};

namespace {
void BuildVectors(int64_t from, int64_t to,
                  std::vector<std::vector<float >> &vector_record_array) {
    if (to <= from) {
        return;
    }

    vector_record_array.clear();
    for (int64_t k = from; k < to; k++) {
        std::vector<float> record;
        record.resize(TABLE_DIM);
        for (int64_t i = 0; i < TABLE_DIM; i++) {
            record[i] = (float) (k % (i + 1));
        }

        vector_record_array.emplace_back(record);
    }
}

std::string CurrentTmDate(int64_t offset_day = 0) {
    time_t tt;
    time(&tt);
    tt = tt + 8 * SECONDS_EACH_HOUR;
    tt = tt + 24 * SECONDS_EACH_HOUR * offset_day;
    tm *t = gmtime(&tt);

    std::string str = std::to_string(t->tm_year + 1900) + "-" + std::to_string(t->tm_mon + 1)
        + "-" + std::to_string(t->tm_mday);

    return str;
}
}

TEST_F(RpcHandlerTest, HasTableTest) {
    ::grpc::ServerContext context;
    ::milvus::grpc::TableName request;
    ::milvus::grpc::BoolReply reply;
    ::grpc::Status status = handler->HasTable(&context, &request, &reply);
Y
Yu Kun 已提交
144 145
    request.set_table_name(TABLE_NAME);
    status = handler->HasTable(&context, &request, &reply);
Y
Yu Kun 已提交
146 147 148 149 150 151 152 153 154
    ASSERT_TRUE(status.error_code() == ::grpc::Status::OK.error_code());
    int error_code = reply.status().error_code();
    ASSERT_EQ(error_code, ::milvus::grpc::ErrorCode::SUCCESS);
}

TEST_F(RpcHandlerTest, IndexTest) {
    ::grpc::ServerContext context;
    ::milvus::grpc::IndexParam request;
    ::milvus::grpc::Status response;
Y
Yu Kun 已提交
155 156 157 158
    ::grpc::Status grpc_status = handler->CreateIndex(&context, &request, &response);
    request.mutable_table_name()->set_table_name("test1");
    handler->CreateIndex(&context, &request, &response);

Y
Yu Kun 已提交
159
    request.mutable_table_name()->set_table_name(TABLE_NAME);
Y
Yu Kun 已提交
160 161
    handler->CreateIndex(&context, &request, &response);

Y
Yu Kun 已提交
162
    request.mutable_index()->set_index_type(1);
Y
Yu Kun 已提交
163 164
    handler->CreateIndex(&context, &request, &response);

Y
Yu Kun 已提交
165
    request.mutable_index()->set_nlist(16384);
Y
Yu Kun 已提交
166
    grpc_status = handler->CreateIndex(&context, &request, &response);
Y
Yu Kun 已提交
167 168
    ASSERT_EQ(grpc_status.error_code(), ::grpc::Status::OK.error_code());
    int error_code = response.error_code();
Y
Yu Kun 已提交
169
//    ASSERT_EQ(error_code, ::milvus::grpc::ErrorCode::SUCCESS);
Y
Yu Kun 已提交
170 171 172 173

    ::milvus::grpc::TableName table_name;
    ::milvus::grpc::IndexParam index_param;
    handler->DescribeIndex(&context, &table_name, &index_param);
Y
Yu Kun 已提交
174 175 176 177
    table_name.set_table_name("test4");
    handler->DescribeIndex(&context, &table_name, &index_param);
    table_name.set_table_name(TABLE_NAME);
    handler->DescribeIndex(&context, &table_name, &index_param);
Y
Yu Kun 已提交
178
    ::milvus::grpc::Status status;
Y
Yu Kun 已提交
179 180 181 182 183
    table_name.Clear();
    handler->DropIndex(&context, &table_name, &status);
    table_name.set_table_name("test5");
    handler->DropIndex(&context, &table_name, &status);
    table_name.set_table_name(TABLE_NAME);
Y
Yu Kun 已提交
184 185 186 187 188 189 190
    handler->DropIndex(&context, &table_name, &status);
}

TEST_F(RpcHandlerTest, InsertTest) {
    ::grpc::ServerContext context;
    ::milvus::grpc::InsertParam request;
    ::milvus::grpc::Status response;
Y
Yu Kun 已提交
191

Y
Yu Kun 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    request.set_table_name(TABLE_NAME);
    std::vector<std::vector<float>> record_array;
    BuildVectors(0, VECTOR_COUNT, record_array);
    ::milvus::grpc::VectorIds vector_ids;
    for (auto &record : record_array) {
        ::milvus::grpc::RowRecord *grpc_record = request.add_row_record_array();
        for (size_t i = 0; i < record.size(); i++) {
            grpc_record->add_vector_data(record[i]);
        }
    }
    handler->Insert(&context, &request, &vector_ids);
    ASSERT_EQ(vector_ids.vector_id_array_size(), VECTOR_COUNT);
}

TEST_F(RpcHandlerTest, SearchTest) {
    ::grpc::ServerContext context;
    ::milvus::grpc::SearchParam request;
    ::milvus::grpc::TopKQueryResultList response;
Y
Yu Kun 已提交
210 211 212 213 214 215 216 217 218 219 220
    //test null input
    handler->Search(&context, nullptr, &response);

    //test invalid table name
    handler->Search(&context, &request, &response);

    //test table not exist
    request.set_table_name("test3");
    handler->Search(&context, &request, &response);

    //test invalid topk
Y
Yu Kun 已提交
221
    request.set_table_name(TABLE_NAME);
Y
Yu Kun 已提交
222 223 224
    handler->Search(&context, &request, &response);

    //test invalid nprobe
Y
Yu Kun 已提交
225
    request.set_topk(10);
Y
Yu Kun 已提交
226 227 228
    handler->Search(&context, &request, &response);

    //test empty query record array
Y
Yu Kun 已提交
229
    request.set_nprobe(32);
Y
Yu Kun 已提交
230 231
    handler->Search(&context, &request, &response);

Y
Yu Kun 已提交
232
    std::vector<std::vector<float>> record_array;
Y
Yu Kun 已提交
233
    BuildVectors(0, VECTOR_COUNT, record_array);
Y
Yu Kun 已提交
234 235 236 237 238 239 240 241 242 243 244
    ::milvus::grpc::InsertParam insert_param;
    for (auto &record : record_array) {
        ::milvus::grpc::RowRecord *grpc_record = insert_param.add_row_record_array();
        for (size_t i = 0; i < record.size(); i++) {
            grpc_record->add_vector_data(record[i]);
        }
    }
    //insert vectors
    insert_param.set_table_name(TABLE_NAME);
    ::milvus::grpc::VectorIds vector_ids;
    handler->Insert(&context, &insert_param, &vector_ids);
Y
Yu Kun 已提交
245
    sleep(7);
Y
Yu Kun 已提交
246 247

    BuildVectors(0, 10, record_array);
Y
Yu Kun 已提交
248 249 250 251 252 253
    for (auto &record : record_array) {
        ::milvus::grpc::RowRecord *row_record = request.add_query_record_array();
        for (auto &rec : record) {
            row_record->add_vector_data(rec);
        }
    }
Y
Yu Kun 已提交
254 255
    handler->Search(&context, &request, &response);

Y
Yu Kun 已提交
256 257 258 259
    //test search with range
    ::milvus::grpc::Range *range = request.mutable_query_range_array()->Add();
    range->set_start_value(CurrentTmDate(-2));
    range->set_end_value(CurrentTmDate(-3));
Y
Yu Kun 已提交
260
    handler->Search(&context, &request, &response);
Y
Yu Kun 已提交
261
    request.mutable_query_range_array()->Clear();
Y
Yu Kun 已提交
262 263 264 265

    request.set_table_name("test2");
    handler->Search(&context, &request, &response);
    request.set_table_name(TABLE_NAME);
Y
Yu Kun 已提交
266 267
    handler->Search(&context, &request, &response);

Y
Yu Kun 已提交
268
    ::milvus::grpc::SearchInFilesParam search_in_files_param;
Y
Yu Kun 已提交
269 270
    std::string *file_id = search_in_files_param.add_file_id_array();
    *file_id = "test_tbl";
Y
Yu Kun 已提交
271 272 273 274 275
    handler->SearchInFiles(&context, &search_in_files_param, &response);
}

TEST_F(RpcHandlerTest, TablesTest) {
    ::grpc::ServerContext context;
Y
Yu Kun 已提交
276
    ::milvus::grpc::TableSchema tableschema;
Y
Yu Kun 已提交
277
    ::milvus::grpc::Status response;
Y
Yu Kun 已提交
278 279 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
    std::string tablename = "tbl";

    //create table test
    //test null input
    handler->CreateTable(&context, nullptr, &response);
    //test invalid table name
    handler->CreateTable(&context, &tableschema, &response);
    //test invalid table dimension
    tableschema.mutable_table_name()->set_table_name(tablename);
    handler->CreateTable(&context, &tableschema, &response);
    //test invalid index file size
    tableschema.set_dimension(TABLE_DIM);
//    handler->CreateTable(&context, &tableschema, &response);
    //test invalid index metric type
    tableschema.set_index_file_size(INDEX_FILE_SIZE);
    handler->CreateTable(&context, &tableschema, &response);
    //test table already exist
    tableschema.set_metric_type(1);
    handler->CreateTable(&context, &tableschema, &response);

    //describe table test
    //test invalid table name
    ::milvus::grpc::TableName table_name;
    ::milvus::grpc::TableSchema table_schema;
    handler->DescribeTable(&context, &table_name, &table_schema);

    table_name.set_table_name(TABLE_NAME);
    ::grpc::Status status = handler->DescribeTable(&context, &table_name, &table_schema);
    ASSERT_EQ(status.error_code(), ::grpc::Status::OK.error_code());


    ::milvus::grpc::InsertParam request;
Y
Yu Kun 已提交
310 311 312
    std::vector<std::vector<float>> record_array;
    BuildVectors(0, VECTOR_COUNT, record_array);
    ::milvus::grpc::VectorIds vector_ids;
Y
Yu Kun 已提交
313 314 315 316 317 318 319
    //Insert vectors
    //test invalid table name
    handler->Insert(&context, &request, &vector_ids);
    request.set_table_name(tablename);
    //test empty row record
    handler->Insert(&context, &request, &vector_ids);

Y
Yu Kun 已提交
320 321 322 323 324 325
    for (auto &record : record_array) {
        ::milvus::grpc::RowRecord *grpc_record = request.add_row_record_array();
        for (size_t i = 0; i < record.size(); i++) {
            grpc_record->add_vector_data(record[i]);
        }
    }
Y
Yu Kun 已提交
326
    //test vector_id size not equal to row record size
Y
Yu Kun 已提交
327 328
    vector_ids.clear_vector_id_array();
    vector_ids.add_vector_id_array(1);
Y
Yu Kun 已提交
329
    handler->Insert(&context, &request, &vector_ids);
Y
Yu Kun 已提交
330

Y
Yu Kun 已提交
331 332
    //normally test
    vector_ids.clear_vector_id_array();
Y
Yu Kun 已提交
333 334
    handler->Insert(&context, &request, &vector_ids);

Y
Yu Kun 已提交
335 336 337 338 339 340 341 342 343 344
    request.clear_row_record_array();
    vector_ids.clear_vector_id_array();
    for (uint64_t i = 0; i < 10; ++i) {
        ::milvus::grpc::RowRecord *grpc_record = request.add_row_record_array();
        for (size_t j = 0; j < 10; j++) {
            grpc_record->add_vector_data(record_array[i][j]);
        }
    }
    handler->Insert(&context, &request, &vector_ids);

Y
Yu Kun 已提交
345

Y
Yu Kun 已提交
346 347 348
//Show table
//    ::milvus::grpc::Command cmd;
//    ::grpc::ServerWriter<::milvus::grpc::TableName> *writer;
Y
Yu Kun 已提交
349 350 351
//    status = handler->ShowTables(&context, &cmd, writer);
//    ASSERT_EQ(status.error_code(), ::grpc::Status::OK.error_code());

Y
Yu Kun 已提交
352 353
    //Count Table
    ::milvus::grpc::TableRowCount count;
Y
Yu Kun 已提交
354 355
    table_name.Clear();
    status = handler->CountTable(&context, &table_name, &count);
Y
Yu Kun 已提交
356
    table_name.set_table_name(tablename);
Y
Yu Kun 已提交
357
    status = handler->CountTable(&context, &table_name, &count);
Y
Yu Kun 已提交
358
    ASSERT_EQ(status.error_code(), ::grpc::Status::OK.error_code());
Y
Yu Kun 已提交
359
//    ASSERT_EQ(count.table_row_count(), vector_ids.vector_id_array_size());
Y
Yu Kun 已提交
360 361 362


    //Preload Table
Y
Yu Kun 已提交
363 364 365
    table_name.Clear();
    status = handler->PreloadTable(&context, &table_name, &response);
    table_name.set_table_name(TABLE_NAME);
Y
Yu Kun 已提交
366 367 368 369
    status = handler->PreloadTable(&context, &table_name, &response);
    ASSERT_EQ(status.error_code(), ::grpc::Status::OK.error_code());

    //Drop table
Y
Yu Kun 已提交
370 371
    table_name.set_table_name("");
    //test invalid table name
Y
Yu Kun 已提交
372
    ::grpc::Status grpc_status = handler->DropTable(&context, &table_name, &response);
Y
Yu Kun 已提交
373 374
    table_name.set_table_name(tablename);
    grpc_status = handler->DropTable(&context, &table_name, &response);
Y
Yu Kun 已提交
375 376 377 378 379 380 381 382 383 384 385
    ASSERT_EQ(grpc_status.error_code(), ::grpc::Status::OK.error_code());
    int error_code = status.error_code();
    ASSERT_EQ(error_code, ::milvus::grpc::ErrorCode::SUCCESS);
}

TEST_F(RpcHandlerTest, CmdTest) {
    ::grpc::ServerContext context;
    ::milvus::grpc::Command command;
    command.set_cmd("version");
    ::milvus::grpc::StringReply reply;
    handler->Cmd(&context, &command, &reply);
Y
Yu Kun 已提交
386 387
    ASSERT_EQ(reply.string_reply(), MILVUS_VERSION);

Y
Yu Kun 已提交
388 389 390 391
    command.set_cmd("tasktable");
    handler->Cmd(&context, &command, &reply);
    command.set_cmd("test");
    handler->Cmd(&context, &command, &reply);
Y
Yu Kun 已提交
392 393 394 395 396 397
}

TEST_F(RpcHandlerTest, DeleteByRangeTest) {
    ::grpc::ServerContext context;
    ::milvus::grpc::DeleteByRangeParam request;
    ::milvus::grpc::Status status;
Y
Yu Kun 已提交
398 399 400
    handler->DeleteByRange(&context, nullptr, &status);
    handler->DeleteByRange(&context, &request, &status);

Y
Yu Kun 已提交
401 402 403 404 405 406 407
    request.set_table_name(TABLE_NAME);
    request.mutable_range()->set_start_value(CurrentTmDate(-2));
    request.mutable_range()->set_end_value(CurrentTmDate(-3));

    ::grpc::Status grpc_status = handler->DeleteByRange(&context, &request, &status);
    int error_code = status.error_code();
    ASSERT_EQ(error_code, ::milvus::grpc::ErrorCode::SUCCESS);
Y
Yu Kun 已提交
408

Y
Yu Kun 已提交
409
    request.mutable_range()->set_start_value("test6");
Y
Yu Kun 已提交
410 411
    grpc_status = handler->DeleteByRange(&context, &request, &status);
    request.mutable_range()->set_start_value(CurrentTmDate(-2));
Y
Yu Kun 已提交
412
    request.mutable_range()->set_end_value("test6");
Y
Yu Kun 已提交
413 414 415 416
    grpc_status = handler->DeleteByRange(&context, &request, &status);
    request.mutable_range()->set_end_value(CurrentTmDate(-2));
    grpc_status = handler->DeleteByRange(&context, &request, &status);

Y
Yu Kun 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
}

//////////////////////////////////////////////////////////////////////
class DummyTask : public GrpcBaseTask {
 public:
    ErrorCode
    OnExecute() override {
        return 0;
    }

    static BaseTaskPtr
    Create(std::string& dummy) {
        return std::shared_ptr<GrpcBaseTask>(new DummyTask(dummy));
    }

    ErrorCode
    DummySetError(ErrorCode error_code, const std::string &msg) {
        return SetError(error_code, msg);
    }

 public:
    explicit DummyTask(std::string &dummy) : GrpcBaseTask(dummy) {

    }
};

class RpcSchedulerTest : public testing::Test {
 protected:
    void
    SetUp() override {
        std::string dummy = "dql";
        task_ptr = std::make_shared<DummyTask>(dummy);
    }

    std::shared_ptr<DummyTask> task_ptr;
};

TEST_F(RpcSchedulerTest, BaseTaskTest){
    ErrorCode error_code = task_ptr->Execute();
    ASSERT_EQ(error_code, 0);

    error_code = task_ptr->DummySetError(0, "test error");
    ASSERT_EQ(error_code, 0);

    GrpcRequestScheduler::GetInstance().Start();
    ::milvus::grpc::Status grpc_status;
    std::string dummy = "dql";
    BaseTaskPtr base_task_ptr = DummyTask::Create(dummy);
    GrpcRequestScheduler::GetInstance().ExecTask(base_task_ptr, &grpc_status);

    GrpcRequestScheduler::GetInstance().ExecuteTask(task_ptr);
    task_ptr = nullptr;
    GrpcRequestScheduler::GetInstance().ExecuteTask(task_ptr);

    GrpcRequestScheduler::GetInstance().Stop();
}

}
}
}
}