GrpcMilvusServer.cpp 2.3 KB
Newer Older
K
kun yu 已提交
1
/*******************************************************************************
Y
Yu Kun 已提交
2 3 4 5
* Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential.
******************************************************************************/
K
kun yu 已提交
6
#include "milvus.grpc.pb.h"
Y
Yu Kun 已提交
7
#include "GrpcMilvusServer.h"
K
kun yu 已提交
8 9
#include "../ServerConfig.h"
#include "../DBWrapper.h"
K
kun yu 已提交
10 11
#include "utils/Log.h"
#include "faiss/utils.h"
Y
Yu Kun 已提交
12
#include "GrpcRequestHandler.h"
K
kun yu 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

#include <chrono>
#include <iostream>
#include <memory>
#include <random>
#include <string>
#include <thread>

#include <grpc/grpc.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <grpcpp/grpcpp.h>

namespace zilliz {
namespace milvus {
namespace server {
Y
Yu Kun 已提交
31
namespace grpc {
K
kun yu 已提交
32

Y
Yu Kun 已提交
33
static std::unique_ptr<::grpc::Server> server;
K
kun yu 已提交
34

K
kun yu 已提交
35
constexpr long MESSAGE_SIZE = -1;
K
kun yu 已提交
36 37

void
Y
Yu Kun 已提交
38
GrpcMilvusServer::StartService() {
Y
Yu Kun 已提交
39
    if (server != nullptr) {
K
kun yu 已提交
40 41 42 43 44 45 46 47
        std::cout << "stopservice!\n";
        StopService();
    }

    ServerConfig &config = ServerConfig::GetInstance();
    ConfigNode server_config = config.GetConfig(CONFIG_SERVER);
    ConfigNode engine_config = config.GetConfig(CONFIG_ENGINE);
    std::string address = server_config.GetValue(CONFIG_SERVER_ADDRESS, "127.0.0.1");
Y
Yu Kun 已提交
48
    int32_t port = server_config.GetInt32Value(CONFIG_SERVER_PORT, 19530);
K
kun yu 已提交
49 50

    faiss::distance_compute_blas_threshold = engine_config.GetInt32Value(CONFIG_DCBT, 20);
K
kun yu 已提交
51

K
kun yu 已提交
52 53
    DBWrapper::DB();//initialize db

Y
Yu Kun 已提交
54
    std::string server_address(address + ":" + std::to_string(port + 1));
K
kun yu 已提交
55

Y
Yu Kun 已提交
56
    ::grpc::ServerBuilder builder;
K
kun yu 已提交
57 58
    builder.SetMaxReceiveMessageSize(MESSAGE_SIZE); //default 4 * 1024 * 1024
    builder.SetMaxSendMessageSize(MESSAGE_SIZE);
K
kun yu 已提交
59 60 61 62 63

    builder.SetCompressionAlgorithmSupportStatus(GRPC_COMPRESS_STREAM_GZIP, true);
    builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_STREAM_GZIP);
    builder.SetDefaultCompressionLevel(GRPC_COMPRESS_LEVEL_HIGH);

Y
Yu Kun 已提交
64
    GrpcRequestHandler service;
K
kun yu 已提交
65

Y
Yu Kun 已提交
66
    builder.AddListeningPort(server_address, ::grpc::InsecureServerCredentials());
K
kun yu 已提交
67 68 69 70 71 72 73 74
    builder.RegisterService(&service);

    server = builder.BuildAndStart();
    server->Wait();

}

void
Y
Yu Kun 已提交
75
GrpcMilvusServer::StopService() {
K
kun yu 已提交
76 77 78
    if (server != nullptr) {
        server->Shutdown();
    }
K
kun yu 已提交
79 80
}

Y
Yu Kun 已提交
81
}
K
kun yu 已提交
82 83 84
}
}
}