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 #include "tensorflow/core/distributed_runtime/rpc/coordination/grpc_coordination_service_impl.h"
17
18 #include "tensorflow/core/platform/mutex.h"
19 #include "tensorflow/core/platform/threadpool.h"
20
21 namespace tensorflow {
22
GrpcCoordinationServiceImpl(thread::ThreadPool * compute_pool,::grpc::ServerBuilder * server_builder)23 GrpcCoordinationServiceImpl::GrpcCoordinationServiceImpl(
24 thread::ThreadPool* compute_pool, ::grpc::ServerBuilder* server_builder)
25 : compute_pool_(*compute_pool), shutdown_(false) {
26 server_builder->RegisterService(&service_);
27 cq_ = server_builder->AddCompletionQueue();
28 }
29
HandleRPCsLoop()30 void GrpcCoordinationServiceImpl::HandleRPCsLoop() {
31 #define ENQUEUE_REQUEST(method) \
32 do { \
33 tf_shared_lock l(shutdown_mu_); \
34 if (shutdown_) { \
35 continue; \
36 } \
37 Call<GrpcCoordinationServiceImpl, grpc::CoordinationService::AsyncService, \
38 method##Request, method##Response>:: \
39 EnqueueRequest( \
40 &service_, cq_.get(), \
41 &grpc::CoordinationService::AsyncService::Request##method, \
42 &GrpcCoordinationServiceImpl::method##Handler, false); \
43 } while (0)
44 ENQUEUE_REQUEST(RegisterTask);
45 ENQUEUE_REQUEST(WaitForAllTasks);
46 ENQUEUE_REQUEST(ShutdownTask);
47 ENQUEUE_REQUEST(ResetTask);
48 ENQUEUE_REQUEST(Heartbeat);
49 ENQUEUE_REQUEST(ReportErrorToTask);
50 ENQUEUE_REQUEST(ReportErrorToService);
51 ENQUEUE_REQUEST(InsertKeyValue);
52 ENQUEUE_REQUEST(GetKeyValue);
53 ENQUEUE_REQUEST(TryGetKeyValue);
54 ENQUEUE_REQUEST(GetKeyValueDir);
55 ENQUEUE_REQUEST(DeleteKeyValue);
56 ENQUEUE_REQUEST(Barrier);
57 ENQUEUE_REQUEST(CancelBarrier);
58 #undef ENQUEUE_REQUEST
59
60 void* tag; // Matches the operation started against this cq_.
61 bool ok;
62
63 while (true) {
64 if (!cq_->Next(&tag, &ok)) {
65 // The queue is shutting down.
66 break;
67 }
68 GrpcCallTag<GrpcCoordinationServiceImpl>* callback_tag =
69 static_cast<GrpcCallTag<GrpcCoordinationServiceImpl>*>(tag);
70
71 if (callback_tag) {
72 callback_tag->OnCompleted(this, ok);
73 } else {
74 cq_->Shutdown();
75 break;
76 }
77 }
78 }
79
Shutdown()80 void GrpcCoordinationServiceImpl::Shutdown() {
81 mutex_lock l(shutdown_mu_);
82 shutdown_ = true;
83 // This enqueues a special event (with a null tag) that causes the completion
84 // queue to be shut down on the polling thread.
85 shutdown_alarm_ = std::make_unique<::grpc::Alarm>(
86 cq_.get(), gpr_now(GPR_CLOCK_MONOTONIC), nullptr);
87 }
88
89 } // namespace tensorflow
90