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.

K
kun yu 已提交
18
#include "milvus.grpc.pb.h"
Y
yudong.cai 已提交
19
#include "GrpcServer.h"
S
starlord 已提交
20 21
#include "server/ServerConfig.h"
#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

S
starlord 已提交
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() {
Y
Yu Kun 已提交
76 77 78 79 80
    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");
    int32_t port = server_config.GetInt32Value(CONFIG_SERVER_PORT, 19530);
K
kun yu 已提交
81

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

Y
Yu Kun 已提交
84 85 86 87
    ::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 已提交
88

Y
Yu Kun 已提交
89 90 91
    builder.SetCompressionAlgorithmSupportStatus(GRPC_COMPRESS_STREAM_GZIP, true);
    builder.SetDefaultCompressionAlgorithm(GRPC_COMPRESS_STREAM_GZIP);
    builder.SetDefaultCompressionLevel(GRPC_COMPRESS_LEVEL_HIGH);
K
kun yu 已提交
92

Y
Yu Kun 已提交
93
    GrpcRequestHandler service;
94

Y
Yu Kun 已提交
95 96
    builder.AddListeningPort(server_address, ::grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
97

98 99
    server_ptr_ = builder.BuildAndStart();
    server_ptr_->Wait();
K
kun yu 已提交
100

S
starlord 已提交
101
    return Status::OK();
K
kun yu 已提交
102 103
}

S
starlord 已提交
104
Status
Y
yudong.cai 已提交
105
GrpcServer::StopService() {
106 107
    if (server_ptr_ != nullptr) {
        server_ptr_->Shutdown();
K
kun yu 已提交
108
    }
S
starlord 已提交
109 110

    return Status::OK();
K
kun yu 已提交
111 112
}

Y
Yu Kun 已提交
113
}
K
kun yu 已提交
114 115 116
}
}
}