brpc_server.cc 4.8 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed 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.

15 16
#include "paddle/fluid/operators/distributed/brpc_server.h"
#include "paddle/fluid/operators/distributed/request_handler.h"
G
gongweibao 已提交
17 18 19 20

namespace sendrecv {

typedef std::unordered_map<std::string,
21
                           paddle::operators::distributed::RequestHandler*>
G
gongweibao 已提交
22 23 24 25 26 27 28 29
    HandlerMap;

class BRPCServiceImpl : public SendRecvService {
 public:
  explicit BRPCServiceImpl(const HandlerMap& rpc_call_map)
      : request_send_h_(nullptr),
        request_get_h_(nullptr),
        request_prefetch_h_(nullptr) {
30
    auto it = rpc_call_map.find(paddle::operators::distributed::kRequestSend);
G
gongweibao 已提交
31 32 33 34
    if (it != rpc_call_map.end()) {
      request_send_h_ = it->second;
    }

35
    it = rpc_call_map.find(paddle::operators::distributed::kRequestSend);
G
gongweibao 已提交
36 37 38 39
    if (it != rpc_call_map.end()) {
      request_get_h_ = it->second;
    }

40
    it = rpc_call_map.find(paddle::operators::distributed::kRequestPrefetch);
G
gongweibao 已提交
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    if (it != rpc_call_map.end()) {
      request_prefetch_h_ = it->second;
    }
  }

  virtual ~BRPCServiceImpl() {}

  void SendVariable(google::protobuf::RpcController* cntl_butil,
                    const VariableMessage* request, VoidMessage* response,
                    google::protobuf::Closure* done) override {
    PADDLE_ENFORCE(request_send_h_ != nullptr,
                   "RequestSend handler should be registed first!");
    brpc::ClosureGuard done_guard(done);

    paddle::framework::Scope* local_scope = request_send_h_->scope();
    paddle::framework::Variable* outvar = nullptr;
    paddle::framework::Variable* invar = nullptr;

    std::string varname = request->varname();

    if (!request_send_h_->sync_mode()) {
      local_scope = &request_send_h_->scope()->NewScope();
      invar = local_scope->Var(varname);
    } else {
      invar = local_scope->FindVar(varname);
    }

    request_send_h_->Handle(varname, local_scope, invar, &outvar);

    if (!request_send_h_->sync_mode()) {
      request_send_h_->scope()->DeleteScope(local_scope);
    }
  }

  void GetVariable(google::protobuf::RpcController* cntl_butil,
                   const VariableMessage* request, VariableMessage* response,
                   google::protobuf::Closure* done) override {
    PADDLE_ENFORCE(request_get_h_ != nullptr,
                   "RequestGet handler should be registed first!");
  }

  void PrefetchVariable(google::protobuf::RpcController* cntl_butil,
                        const VariableMessage* request,
                        VariableMessage* response,
                        google::protobuf::Closure* done) override {
    PADDLE_ENFORCE(request_prefetch_h_ != nullptr,
                   "kRequestPrefetch handler should be registed first!");
  }

 private:
91 92 93
  paddle::operators::distributed::RequestHandler* request_send_h_;
  paddle::operators::distributed::RequestHandler* request_get_h_;
  paddle::operators::distributed::RequestHandler* request_prefetch_h_;
G
gongweibao 已提交
94 95 96 97 98
};
}  // namespace sendrecv

namespace paddle {
namespace operators {
99
namespace distributed {
G
gongweibao 已提交
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

void AsyncBRPCServer::StartServer() {
  // Instance of your service.
  sendrecv::BRPCServiceImpl service_impl(rpc_call_map_);

  // Add the service into server. Notice the second parameter, because the
  // service is put on stack, we don't want server to delete it, otherwise
  // use brpc::SERVER_OWNS_SERVICE.
  if (server_.AddService(&service_impl, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
    LOG(FATAL) << "Fail to add service";
    return;
  }

  brpc::ServerOptions options;
  options.idle_timeout_sec = idle_timeout_s_;
  options.max_concurrency = max_concurrency_;
  if (server_.Start(bind_address_.c_str(), &options) != 0) {
    LOG(FATAL) << "Fail to start EchoServer" << bind_address_;
    return;
  }

  butil::EndPoint ep = server_.listen_address();
  selected_port_ = ep.port;

  {
    std::lock_guard<std::mutex> lock(this->mutex_ready_);
    ready_ = 1;
  }
  condition_ready_.notify_all();

  server_.Join();
}

void AsyncBRPCServer::ShutDownImpl() { server_.Stop(1000); }

void AsyncBRPCServer::WaitServerReady() {
136
  VLOG(30) << "AsyncGRPCServer is wait server ready";
G
gongweibao 已提交
137 138
  std::unique_lock<std::mutex> lock(this->mutex_ready_);
  condition_ready_.wait(lock, [=] { return this->ready_ == 1; });
139
  VLOG(30) << "AsyncGRPCServer WaitSeverReady";
G
gongweibao 已提交
140 141
}

142
};  // namespace distributed
G
gongweibao 已提交
143 144
};  // namespace operators
};  // namespace paddle