xref: /aosp_15_r20/external/tensorflow/tensorflow/core/data/service/grpc_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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_DATA_SERVICE_GRPC_UTIL_H_
17 #define TENSORFLOW_CORE_DATA_SERVICE_GRPC_UTIL_H_
18 
19 #include <functional>
20 #include <string>
21 
22 #include "grpcpp/grpcpp.h"
23 #include "tensorflow/core/platform/status.h"
24 
25 namespace tensorflow {
26 namespace data {
27 namespace grpc_util {
28 
29 // Wraps a grpc::Status in a tensorflow::Status with the given message.
30 Status WrapError(const std::string& message, const ::grpc::Status& status);
31 
32 // Retries the given function if the function produces UNAVAILABLE, ABORTED, or
33 // CANCELLED status codes. We retry these codes because they can all indicate
34 // preemption of a server. The retries continue until the deadline is exceeded
35 // or the `should_retry` callback returns false. `description` may be used to
36 // log that retries are happening. It should contain a description of the action
37 // being retried, e.g. "register dataset" The retry loop uses exponential
38 // backoff between retries. `deadline_micros` is interpreted as microseconds
39 // since the epoch.
40 Status Retry(const std::function<Status()>& f,
41              const std::function<bool()>& should_retry,
42              const std::string& description, int64_t deadline_micros);
43 
44 // Same as `Retry` above, but with a `should_retry` callback that always returns
45 // `true`.
46 Status Retry(const std::function<Status()>& f, const std::string& description,
47              int64_t deadline_micros);
48 
49 }  // namespace grpc_util
50 }  // namespace data
51 }  // namespace tensorflow
52 
53 #endif  // TENSORFLOW_CORE_DATA_SERVICE_GRPC_UTIL_H_
54