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