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_CHANNEL_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_CHANNEL_H_ 18 19 #include <map> 20 #include <memory> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "grpcpp/grpcpp.h" 26 27 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h" 28 #include "tensorflow/core/protobuf/config.pb.h" 29 30 namespace tensorflow { 31 32 // Consolidated parameter structure to ease use of generic interfaces. 33 // 34 // Each job_id requires: 35 // - a list of host:port (or sparse list of index:host:port) 36 // - the number of tasks per replica 37 class GrpcChannelSpec { 38 public: 39 struct HostPortsJob { HostPortsJobHostPortsJob40 HostPortsJob(const string& job_id, const std::map<int, string>& host_ports) 41 : job_id(job_id), host_ports(host_ports) {} 42 const string job_id; 43 const std::map<int, string> host_ports; 44 }; 45 46 Status AddHostPortsJob(const string& job_id, 47 const std::vector<string>& host_ports); 48 49 Status AddHostPortsJob(const string& job_id, 50 const std::map<int, string>& host_ports); 51 host_ports_jobs()52 const std::vector<HostPortsJob>& host_ports_jobs() const { 53 return host_ports_jobs_; 54 } 55 56 private: 57 std::vector<HostPortsJob> host_ports_jobs_; 58 std::set<string> job_ids_; 59 }; 60 61 class GrpcChannelCache { 62 public: ~GrpcChannelCache()63 virtual ~GrpcChannelCache() {} 64 65 // Populates *workers with names of all workers which this object 66 // was created to handle. Worker names are in the format 67 // /job:<job identifier>/task:<task id> 68 // e.g. /job:mnist/task:2 69 virtual void ListWorkers(std::vector<string>* workers) = 0; 70 virtual void ListWorkersInJob(const string& job_name, 71 std::vector<string>* workers) = 0; 72 73 // If found, returns a gRPC channel that is connected to the remote 74 // worker named by 'target'. 'target' is of the following 75 // format: /job:<job identifier>/task:<task id> 76 // E.g., /job:mnist/task:2 77 virtual SharedGrpcChannelPtr FindWorkerChannel(const string& target) = 0; 78 79 // Translates a string in the form `/job:X/task:Z` into a host_port. 80 virtual string TranslateTask(const string& task) = 0; 81 }; 82 83 typedef std::function<SharedGrpcChannelPtr(string)> ChannelCreationFunction; 84 85 GrpcChannelCache* NewGrpcChannelCache(const GrpcChannelSpec& channel_spec, 86 ChannelCreationFunction channel_func); 87 88 // Below here are internal-only functions. 89 90 ::grpc::ChannelArguments GetChannelArguments(const RPCOptions* rpc_options); 91 92 ChannelCreationFunction ConvertToChannelCreationFunction( 93 const std::function<Status(string, const RPCOptions*, 94 SharedGrpcChannelPtr*)>& new_channel_func_ptr); 95 96 Status NewHostPortGrpcChannel(const string& target, 97 const RPCOptions* rpc_options, 98 SharedGrpcChannelPtr* channel_pointer); 99 100 } // namespace tensorflow 101 102 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_CHANNEL_H_ 103