• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #include "tensorflow/core/distributed_runtime/rpc/eager/grpc_eager_service_impl.h"
17 
18 #include "tensorflow/core/distributed_runtime/rpc/eager/grpc_eager_service.h"
19 #include "tensorflow/core/distributed_runtime/rpc/grpc_call.h"
20 #include "tensorflow/core/distributed_runtime/rpc/grpc_channel.h"
21 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h"
22 #include "tensorflow/core/distributed_runtime/rpc/grpc_worker_cache.h"
23 #include "tensorflow/core/util/ptr_util.h"
24 
25 namespace tensorflow {
26 namespace eager {
27 
GrpcEagerServiceImpl(const WorkerEnv * env,::grpc::ServerBuilder * server_builder)28 GrpcEagerServiceImpl::GrpcEagerServiceImpl(
29     const WorkerEnv* env, ::grpc::ServerBuilder* server_builder)
30     : env_(env),
31       local_impl_(env),
32       enqueue_streaming_thread_(env_->env, "enqueue_streaming_thread", 1) {
33   server_builder->RegisterService(&service_);
34   cq_ = server_builder->AddCompletionQueue();
35 }
36 
CreateMasterContext(const tensorflow::uint64 context_id,EagerContext * context)37 Status GrpcEagerServiceImpl::CreateMasterContext(
38     const tensorflow::uint64 context_id, EagerContext* context) {
39   return local_impl_.CreateMasterContext(context_id, context);
40 }
41 
HandleRPCsLoop()42 void GrpcEagerServiceImpl::HandleRPCsLoop() {
43 #define ENQUEUE_REQUEST(method)                                            \
44   do {                                                                     \
45     Call<GrpcEagerServiceImpl, grpc::EagerService::AsyncService,           \
46          method##Request, method##Response>::                              \
47         EnqueueRequest(&service_, cq_.get(),                               \
48                        &grpc::EagerService::AsyncService::Request##method, \
49                        &GrpcEagerServiceImpl::method##Handler, false);     \
50   } while (0)
51   ENQUEUE_REQUEST(CreateContext);
52   ENQUEUE_REQUEST(UpdateContext);
53   ENQUEUE_REQUEST(Enqueue);
54   ENQUEUE_REQUEST(WaitQueueDone);
55   ENQUEUE_REQUEST(RunComponentFunction);
56   ENQUEUE_REQUEST(KeepAlive);
57   ENQUEUE_REQUEST(CloseContext);
58 #undef ENQUEUE_REQUEST
59 
60   // Request a StreamingEnqueue call.
61   ServerBidirectionalStreamingCall<GrpcEagerServiceImpl,
62                                    grpc::EagerService::AsyncService,
63                                    EnqueueRequest, EnqueueResponse>::
64       EnqueueRequest(&service_, cq_.get(),
65                      &grpc::EagerService::AsyncService::RequestStreamingEnqueue,
66                      &GrpcEagerServiceImpl::StreamingEnqueueHandler);
67 
68   void* tag;  // Matches the operation started against this cq_.
69   bool ok;
70 
71   while (true) {
72     if (!cq_->Next(&tag, &ok)) {
73       // The queue is shutting down.
74       break;
75     }
76     GrpcCallTag<GrpcEagerServiceImpl>* callback_tag =
77         static_cast<GrpcCallTag<GrpcEagerServiceImpl>*>(tag);
78 
79     if (callback_tag) {
80       callback_tag->OnCompleted(this, ok);
81     } else {
82       cq_->Shutdown();
83       break;
84     }
85   }
86 }
87 
Shutdown()88 void GrpcEagerServiceImpl::Shutdown() {
89   // This enqueues a special event (with a null tag)
90   // that causes the completion queue to be shut down on the
91   // polling thread.
92   shutdown_alarm_ = MakeUnique<::grpc::Alarm>(
93       cq_.get(), gpr_now(GPR_CLOCK_MONOTONIC), nullptr);
94 }
95 
96 }  // namespace eager
97 }  // namespace tensorflow
98