1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_EAGER_GRPC_EAGER_SERVICE_IMPL_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_EAGER_GRPC_EAGER_SERVICE_IMPL_H_ 18 19 #include "grpcpp/alarm.h" 20 #include "grpcpp/completion_queue.h" 21 #include "grpcpp/server_builder.h" 22 #include "tensorflow/core/distributed_runtime/eager/eager_service_impl.h" 23 #include "tensorflow/core/distributed_runtime/rpc/async_service_interface.h" 24 #include "tensorflow/core/distributed_runtime/rpc/eager/grpc_eager_service.h" 25 #include "tensorflow/core/distributed_runtime/rpc/grpc_call.h" 26 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h" 27 28 namespace tensorflow { 29 namespace eager { 30 31 // This class is a wrapper that handles communication for gRPC. 32 class GrpcEagerServiceImpl : public AsyncServiceInterface { 33 public: 34 template <class RequestMessage, class ResponseMessage> 35 using EagerCall = Call<GrpcEagerServiceImpl, grpc::EagerService::AsyncService, 36 RequestMessage, ResponseMessage>; 37 38 GrpcEagerServiceImpl(const WorkerEnv* env, 39 ::grpc::ServerBuilder* server_builder); ~GrpcEagerServiceImpl()40 virtual ~GrpcEagerServiceImpl() {} 41 42 void HandleRPCsLoop() override; 43 void Shutdown() override; 44 45 private: 46 #define HANDLER(method) \ 47 void method##Handler(EagerCall<method##Request, method##Response>* call) { \ 48 env_->compute_pool->Schedule([this, call]() { \ 49 call->SendResponse( \ 50 ToGrpcStatus(local_impl_.method(&call->request, &call->response))); \ 51 }); \ 52 Call<GrpcEagerServiceImpl, \ 53 tensorflow::eager::grpc::EagerService::AsyncService, method##Request, \ 54 method##Response>:: \ 55 EnqueueRequest(&service_, cq_.get(), \ 56 &grpc::EagerService::AsyncService::Request##method, \ 57 &GrpcEagerServiceImpl::method##Handler, false); \ 58 } 59 HANDLER(CreateContext); 60 HANDLER(Enqueue); 61 HANDLER(WaitQueueDone); 62 HANDLER(KeepAlive); 63 HANDLER(CloseContext); 64 HANDLER(RegisterFunction); 65 HANDLER(SendTensor); 66 #undef HANDLER 67 68 const WorkerEnv* const env_; // Not owned. 69 EagerServiceImpl local_impl_; 70 71 std::unique_ptr<::grpc::Alarm> shutdown_alarm_; 72 73 std::unique_ptr<::grpc::ServerCompletionQueue> cq_; 74 tensorflow::eager::grpc::EagerService::AsyncService service_; 75 76 TF_DISALLOW_COPY_AND_ASSIGN(GrpcEagerServiceImpl); 77 }; 78 79 } // namespace eager 80 } // namespace tensorflow 81 82 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_EAGER_GRPC_EAGER_SERVICE_IMPL_H_ 83