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

S
starlord 已提交
18
#include "server/grpc_impl/GrpcServer.h"
S
starlord 已提交
19 20
#include "GrpcRequestHandler.h"
#include "grpc/gen-milvus/milvus.grpc.pb.h"
21
#include "server/Config.h"
S
starlord 已提交
22
#include "server/DBWrapper.h"
K
kun yu 已提交
23 24 25 26 27 28 29 30
#include "utils/Log.h"

#include <chrono>
#include <iostream>
#include <memory>
#include <random>
#include <string>
#include <thread>
S
starlord 已提交
31
#include <vector>
K
kun yu 已提交
32 33 34 35 36 37 38 39 40

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

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

S
starlord 已提交
43
constexpr int64_t MESSAGE_SIZE = -1;
K
kun yu 已提交
44

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

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

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

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

72
Status
Y
yudong.cai 已提交
73
GrpcServer::StartService() {
S
starlord 已提交
74
    Config& config = Config::GetInstance();
75 76 77 78 79 80 81 82 83 84 85
    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 已提交
86

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

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

Y
Yu Kun 已提交
94 95
    builder.SetCompressionAlgorithmSupportStatus(GRPC_COMPRESS_STREAM_GZIP, true);
    builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_STREAM_GZIP);
96
    builder.SetDefaultCompressionLevel(GRPC_COMPRESS_LEVEL_NONE);
K
kun yu 已提交
97

Y
Yu Kun 已提交
98
    GrpcRequestHandler service;
99

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

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

S
starlord 已提交
106
    return Status::OK();
K
kun yu 已提交
107 108
}

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

    return Status::OK();
K
kun yu 已提交
116 117
}

S
starlord 已提交
118 119 120
}  // namespace grpc
}  // namespace server
}  // namespace milvus