1 /* Copyright 2019 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_DATA_SERVICE_GRPC_WORKER_IMPL_H_ 17 #define TENSORFLOW_CORE_DATA_SERVICE_GRPC_WORKER_IMPL_H_ 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 23 #include "grpcpp/server_builder.h" 24 #include "tensorflow/core/data/service/export.pb.h" 25 #include "tensorflow/core/data/service/worker.grpc.pb.h" 26 #include "tensorflow/core/data/service/worker.pb.h" 27 #include "tensorflow/core/data/service/worker_impl.h" 28 #include "tensorflow/core/platform/status.h" 29 #include "tensorflow/core/protobuf/service_config.pb.h" 30 31 namespace tensorflow { 32 namespace data { 33 34 // This class is a wrapper that handles communication for gRPC. 35 class GrpcWorkerImpl : public WorkerService::Service { 36 public: 37 // Constructs a GrpcWorkerImpl with the given config, and registers it with 38 // `server_builder`. 39 explicit GrpcWorkerImpl(const experimental::WorkerConfig& config, 40 ::grpc::ServerBuilder& server_builder); ~GrpcWorkerImpl()41 ~GrpcWorkerImpl() override { Stop(); } 42 43 Status Start(const std::string& worker_address, 44 const std::string& transfer_address); 45 void Stop(); 46 47 std::function<Status(const GetElementRequest*, GetElementResult*)> get_element_getter()48 get_element_getter() { 49 return [this](const GetElementRequest* request, GetElementResult* result) { 50 return impl_->GetElementResult(request, result); 51 }; 52 } 53 54 WorkerStateExport ExportState() const; 55 56 #define HANDLER(method) \ 57 ::grpc::Status method(::grpc::ServerContext* context, \ 58 const method##Request* request, \ 59 method##Response* response) override; 60 HANDLER(ProcessTask); 61 HANDLER(GetElement); 62 HANDLER(GetWorkerTasks); 63 #undef HANDLER 64 65 private: 66 std::string worker_address_; 67 // A std::shared_ptr allows clients to access local servers and directly call 68 // the servers' methods to avoid RPC calls and data copy. 69 std::shared_ptr<DataServiceWorkerImpl> impl_; 70 71 TF_DISALLOW_COPY_AND_ASSIGN(GrpcWorkerImpl); 72 }; 73 74 } // namespace data 75 } // namespace tensorflow 76 77 #endif // TENSORFLOW_CORE_DATA_SERVICE_GRPC_WORKER_IMPL_H_ 78