1 /* Copyright 2021 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_COORDINATION_GRPC_COORDINATION_SERVICE_IMPL_H_
17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_COORDINATION_GRPC_COORDINATION_SERVICE_IMPL_H_
18 
19 #include "grpcpp/alarm.h"
20 #include "grpcpp/completion_queue.h"
21 #include "grpcpp/server_builder.h"
22 #include "tensorflow/core/distributed_runtime/coordination/coordination_service_rpc_handler.h"
23 #include "tensorflow/core/distributed_runtime/rpc/async_service_interface.h"
24 #include "tensorflow/core/distributed_runtime/rpc/grpc_call.h"
25 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h"
26 #include "tensorflow/core/distributed_runtime/worker_env.h"
27 #include "tensorflow/core/platform/mutex.h"
28 #include "tensorflow/core/platform/platform.h"
29 #include "tensorflow/core/platform/thread_annotations.h"
30 #include "tensorflow/core/platform/threadpool.h"
31 #include "tensorflow/core/protobuf/coordination_service.grpc.pb.h"
32 #include "tensorflow/core/protobuf/coordination_service.pb.h"
33 
34 namespace tensorflow {
35 
36 class GrpcCoordinationServiceImpl : public AsyncServiceInterface {
37  public:
38   template <class RequestMessage, class ResponseMessage>
39   using CoordCall =
40       Call<GrpcCoordinationServiceImpl, grpc::CoordinationService::AsyncService,
41            RequestMessage, ResponseMessage>;
42 
43   GrpcCoordinationServiceImpl(thread::ThreadPool* compute_pool,
44                               ::grpc::ServerBuilder* server_builder);
~GrpcCoordinationServiceImpl()45   ~GrpcCoordinationServiceImpl() override {}
46 
47   void HandleRPCsLoop() override;
48   void Shutdown() override;
SetCoordinationServiceAgentInstance(CoordinationServiceAgent * agent)49   void SetCoordinationServiceAgentInstance(CoordinationServiceAgent* agent) {
50     rpc_handler_.SetAgentInstance(agent);
51   }
52 
53  private:
54 #define HANDLER(method)                                                        \
55   void method##Handler(CoordCall<method##Request, method##Response>* call) {   \
56     tf_shared_lock l(shutdown_mu_);                                            \
57     if (shutdown_) {                                                           \
58       return;                                                                  \
59     }                                                                          \
60     compute_pool_.Schedule([this, call]() {                                    \
61       rpc_handler_.method##Async(&call->request, &call->response,              \
62                                  [call](const Status& s) {                     \
63                                    call->ClearCancelCallback();                \
64                                    call->SendResponse(ToGrpcStatus(s));        \
65                                  });                                           \
66     });                                                                        \
67     Call<GrpcCoordinationServiceImpl, grpc::CoordinationService::AsyncService, \
68          method##Request, method##Response>::                                  \
69         EnqueueRequest(                                                        \
70             &service_, cq_.get(),                                              \
71             &grpc::CoordinationService::AsyncService::Request##method,         \
72             &GrpcCoordinationServiceImpl::method##Handler,                     \
73             /*supports_cancel=*/false);                                        \
74   }
75   HANDLER(RegisterTask);
76   HANDLER(WaitForAllTasks);
77   HANDLER(ShutdownTask);
78   HANDLER(ResetTask);
79   HANDLER(Heartbeat);
80   HANDLER(ReportErrorToTask);
81   HANDLER(ReportErrorToService);
82   HANDLER(InsertKeyValue);
83   HANDLER(GetKeyValue);
84   HANDLER(TryGetKeyValue);
85   HANDLER(GetKeyValueDir);
86   HANDLER(DeleteKeyValue);
87   HANDLER(Barrier);
88   HANDLER(CancelBarrier);
89 #undef HANDLER
90 
91   thread::ThreadPool& compute_pool_;
92   CoordinationServiceRpcHandler rpc_handler_;
93 
94   mutex shutdown_mu_;
95   bool shutdown_ TF_GUARDED_BY(shutdown_mu_);
96   std::unique_ptr<::grpc::Alarm> shutdown_alarm_;
97 
98   std::unique_ptr<::grpc::ServerCompletionQueue> cq_;
99   grpc::CoordinationService::AsyncService service_;
100 
101   TF_DISALLOW_COPY_AND_ASSIGN(GrpcCoordinationServiceImpl);
102 };
103 
104 }  // namespace tensorflow
105 
106 #endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_COORDINATION_GRPC_COORDINATION_SERVICE_IMPL_H_
107