1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include "tensorflow/core/distributed_runtime/rpc/grpc_remote_master.h" 17 18#include <utility> 19 20#include "tensorflow/core/distributed_runtime/call_options.h" 21#include "tensorflow/core/distributed_runtime/master_interface.h" 22#include "tensorflow/core/distributed_runtime/rpc/grpc_master_service_impl.h" 23#include "tensorflow/core/distributed_runtime/rpc/grpc_util.h" 24#include "tensorflow/core/lib/core/errors.h" 25#include "tensorflow/core/lib/core/status.h" 26#include "tensorflow/core/lib/strings/strcat.h" 27#include "tensorflow/core/platform/tracing.h" 28#include "tensorflow/core/protobuf/master.pb.h" 29 30namespace tensorflow { 31 32// GrpcRemoteMaster is an implementation of the MasterInterface 33// that uses gRPC to talk to the Master service. 34class GrpcRemoteMaster : public MasterInterface { 35 using MasterServiceStub = grpc::MasterService::Stub; 36 37 public: 38 explicit GrpcRemoteMaster(const SharedGrpcChannelPtr& client_channel) 39 : stub_(grpc::MasterService::NewStub(client_channel)) {} 40 41 ~GrpcRemoteMaster() override {} 42 43 Status CreateSession(CallOptions* call_options, 44 const CreateSessionRequest* request, 45 CreateSessionResponse* response) override { 46 ::grpc::ClientContext ctx; 47 return Call(&ctx, call_options, request, response, 48 &MasterServiceStub::CreateSession); 49 } 50 51 Status ExtendSession(CallOptions* call_options, 52 const ExtendSessionRequest* request, 53 ExtendSessionResponse* response) override { 54 ::grpc::ClientContext ctx; 55 return Call(&ctx, call_options, request, response, 56 &MasterServiceStub::ExtendSession); 57 } 58 59 Status PartialRunSetup(CallOptions* call_options, 60 const PartialRunSetupRequest* request, 61 PartialRunSetupResponse* response) override { 62 ::grpc::ClientContext ctx; 63 return Call(&ctx, call_options, request, response, 64 &MasterServiceStub::PartialRunSetup); 65 } 66 67 Status RunStep(CallOptions* call_options, RunStepRequestWrapper* request, 68 MutableRunStepResponseWrapper* response) override { 69 ::grpc::ClientContext ctx; 70 auto trace = TraceRpc("RunStep/Client", &ctx); 71 return Call(&ctx, call_options, &request->ToProto(), 72 get_proto_from_wrapper(response), &MasterServiceStub::RunStep); 73 } 74 75 Status CloseSession(CallOptions* call_options, 76 const CloseSessionRequest* request, 77 CloseSessionResponse* response) override { 78 ::grpc::ClientContext ctx; 79 return Call(&ctx, call_options, request, response, 80 &MasterServiceStub::CloseSession); 81 } 82 83 Status ListDevices(CallOptions* call_options, 84 const ListDevicesRequest* request, 85 ListDevicesResponse* response) override { 86 ::grpc::ClientContext ctx; 87 return Call(&ctx, call_options, request, response, 88 &MasterServiceStub::ListDevices); 89 } 90 91 Status Reset(CallOptions* call_options, const ResetRequest* request, 92 ResetResponse* response) override { 93 ::grpc::ClientContext ctx; 94 return Call(&ctx, call_options, request, response, 95 &MasterServiceStub::Reset); 96 } 97 98 private: 99 // Start tracing, attaching a unique ID to both the trace and the RPC. 100 port::Tracing::TraceMe TraceRpc(StringPiece name, 101 ::grpc::ClientContext* ctx) { 102 string trace_id = strings::StrCat(port::Tracing::UniqueId()); 103 ctx->AddMetadata(GrpcIdKey(), trace_id); 104 return port::Tracing::TraceMe(name, trace_id); 105 } 106 107 void SetDeadline(::grpc::ClientContext* ctx, int64 time_in_ms) { 108 if (time_in_ms > 0) { 109 ctx->set_deadline(gpr_time_from_millis(time_in_ms, GPR_TIMESPAN)); 110 } 111 } 112 113 template <typename Request, typename Response> 114 Status Call(::grpc::ClientContext* ctx, CallOptions* call_options, 115 const Request* request, Response* response, 116 ::grpc::Status (MasterServiceStub::*pfunc)(::grpc::ClientContext*, 117 const Request&, 118 Response*)) { 119 ctx->set_fail_fast(false); 120 SetDeadline(ctx, call_options->GetTimeout()); 121 return FromGrpcStatus((stub_.get()->*pfunc)(ctx, *request, response)); 122 } 123 124 std::unique_ptr<MasterServiceStub> stub_; 125}; 126 127MasterInterface* NewGrpcMaster(const SharedGrpcChannelPtr& channel) { 128 return new GrpcRemoteMaster(channel); 129} 130 131} // namespace tensorflow 132