GrpcServer.cpp 3.1 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.

K
kun yu 已提交
18
#include "milvus.grpc.pb.h"
Y
yudong.cai 已提交
19
#include "GrpcServer.h"
20
#include "server/Config.h"
G
groot 已提交
21
#include "server/DBWrapper.h"
K
kun yu 已提交
22
#include "utils/Log.h"
Y
Yu Kun 已提交
23
#include "GrpcRequestHandler.h"
K
kun yu 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37

#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>

Y
Yu Kun 已提交
38

K
kun yu 已提交
39 40 41
namespace zilliz {
namespace milvus {
namespace server {
Y
Yu Kun 已提交
42
namespace grpc {
K
kun yu 已提交
43

K
kun yu 已提交
44

K
kun yu 已提交
45
constexpr long MESSAGE_SIZE = -1;
K
kun yu 已提交
46

G
groot 已提交
47
//this class is to check port occupation during server start
48 49
class NoReusePortOption : public ::grpc::ServerBuilderOption {
 public:
Y
Yu Kun 已提交
50
    void UpdateArguments(::grpc::ChannelArguments *args) override {
51 52 53
        args->SetInt(GRPC_ARG_ALLOW_REUSEPORT, 0);
    }

Y
yudong.cai 已提交
54 55 56
    void UpdatePlugins(std::vector<std::unique_ptr<::grpc::ServerBuilderPlugin>> *plugins) override {

    }
57 58 59
};


60
void
Y
yudong.cai 已提交
61 62
GrpcServer::Start() {
    thread_ptr_ = std::make_shared<std::thread>(&GrpcServer::StartService, this);
63 64 65
}

void
Y
yudong.cai 已提交
66
GrpcServer::Stop() {
67 68 69 70
    StopService();
    if (thread_ptr_) {
        thread_ptr_->join();
        thread_ptr_ = nullptr;
K
kun yu 已提交
71
    }
72
}
K
kun yu 已提交
73

74
Status
Y
yudong.cai 已提交
75
GrpcServer::StartService() {
76
    Config &config = Config::GetInstance();
77 78 79 80 81 82 83 84 85 86 87
    std::string address, port;
    Status s;

    s = config.GetServerConfigAddress(address);
    if (!s.ok()) {
        return s;
    }
    s = config.GetServerConfigPort(port);
    if (!s.ok()) {
        return s;
    }
K
kun yu 已提交
88

Y
yudong.cai 已提交
89
    std::string server_address(address + ":" + port);
K
kun yu 已提交
90

Y
Yu Kun 已提交
91 92 93 94
    ::grpc::ServerBuilder builder;
    builder.SetOption(std::unique_ptr<::grpc::ServerBuilderOption>(new NoReusePortOption));
    builder.SetMaxReceiveMessageSize(MESSAGE_SIZE); //default 4 * 1024 * 1024
    builder.SetMaxSendMessageSize(MESSAGE_SIZE);
K
kun yu 已提交
95

Y
Yu Kun 已提交
96 97 98
    builder.SetCompressionAlgorithmSupportStatus(GRPC_COMPRESS_STREAM_GZIP, true);
    builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_STREAM_GZIP);
    builder.SetDefaultCompressionLevel(GRPC_COMPRESS_LEVEL_HIGH);
K
kun yu 已提交
99

Y
Yu Kun 已提交
100
    GrpcRequestHandler service;
101

Y
Yu Kun 已提交
102 103
    builder.AddListeningPort(server_address, ::grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
104

105 106
    server_ptr_ = builder.BuildAndStart();
    server_ptr_->Wait();
K
kun yu 已提交
107

G
groot 已提交
108
    return Status::OK();
K
kun yu 已提交
109 110
}

G
groot 已提交
111
Status
Y
yudong.cai 已提交
112
GrpcServer::StopService() {
113 114
    if (server_ptr_ != nullptr) {
        server_ptr_->Shutdown();
K
kun yu 已提交
115
    }
G
groot 已提交
116 117

    return Status::OK();
K
kun yu 已提交
118 119
}

Y
Yu Kun 已提交
120
}
K
kun yu 已提交
121 122 123
}
}
}