1 /* Copyright 2016 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_GRPC_WORKER_SERVICE_IMPL_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_SERVICE_IMPL_H_ 18 19 #include "grpcpp/impl/codegen/async_stream.h" 20 #include "grpcpp/impl/codegen/async_unary_call.h" 21 #include "grpcpp/impl/codegen/proto_utils.h" 22 #include "grpcpp/impl/codegen/rpc_method.h" 23 #include "grpcpp/impl/codegen/service_type.h" 24 #include "grpcpp/impl/codegen/status.h" 25 #include "grpcpp/impl/codegen/stub_options.h" 26 #include "grpcpp/impl/codegen/sync_stream.h" 27 #include "grpcpp/support/byte_buffer.h" 28 29 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h" 30 #include "tensorflow/core/distributed_runtime/tensor_coding.h" 31 #include "tensorflow/core/protobuf/worker.pb.h" 32 33 namespace grpc { 34 35 // Support parsing/unparsing of tensorflow::TensorResponse. 36 // Wire-format is identical to RecvTensorResponse. 37 // This is specializing an existing template, so it's okay to do this in a 38 // namespace that we don't own. 39 template <> 40 class SerializationTraits<tensorflow::TensorResponse> { 41 public: Serialize(const tensorflow::TensorResponse & msg,ByteBuffer * bp,bool * own_buffer)42 static Status Serialize(const tensorflow::TensorResponse& msg, ByteBuffer* bp, 43 bool* own_buffer) { 44 LOG(FATAL) << "TODO(sanjay,jeff): Implement"; 45 return Status(); 46 } Deserialize(ByteBuffer * buffer,tensorflow::TensorResponse * msg)47 static Status Deserialize(ByteBuffer* buffer, 48 tensorflow::TensorResponse* msg) { 49 if (buffer == nullptr) { 50 return Status(StatusCode::INTERNAL, "No payload"); 51 } 52 Status result = Status::OK; 53 if (result.ok()) { 54 ::tensorflow::GrpcByteSource source(buffer); 55 auto s = msg->ParseFrom(&source); 56 if (!s.ok()) { 57 result = Status(StatusCode::INTERNAL, 58 ::tensorflow::strings::StrCat( 59 "TensorResponse parse error", s.ToString())); 60 } 61 } 62 buffer->Clear(); 63 return result; 64 } 65 }; 66 67 } // namespace grpc 68 69 namespace tensorflow { 70 71 // Names of worker methods. 72 enum class GrpcWorkerMethod { 73 kGetStatus, 74 kCreateWorkerSession, 75 kDeleteWorkerSession, 76 kRegisterGraph, 77 kDeregisterGraph, 78 kRunGraph, 79 kCleanupGraph, 80 kCleanupAll, 81 kRecvTensor, 82 kRecvBuf, 83 kLogging, 84 kTracing, 85 kCompleteGroup, 86 kCompleteInstance, 87 kGetStepSequence, 88 kMarkRecvFinished, 89 }; 90 91 static const int kGrpcNumWorkerMethods = 92 static_cast<int>(GrpcWorkerMethod::kMarkRecvFinished) + 1; 93 94 const char* GrpcWorkerMethodName(GrpcWorkerMethod id); 95 96 namespace grpc { 97 98 // Implementation of `tensorflow.WorkerService`, based on the 99 // definition in "//tensorflow/core/protobuf/worker_service.proto", 100 // and the gRPC generated stub and service classes. 101 // See the proto file for the definition of methods and messages. 102 class WorkerService final { 103 public: 104 class AsyncService : public ::grpc::Service { 105 public: 106 AsyncService(); 107 virtual ~AsyncService(); 108 109 // Make RequestAsyncUnary public for grpc_call.h 110 using ::grpc::Service::RequestAsyncUnary; 111 }; 112 }; 113 114 } // namespace grpc 115 116 } // namespace tensorflow 117 118 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_SERVICE_IMPL_H_ 119