xref: /aosp_15_r20/external/tensorflow/tensorflow/core/data/service/grpc_util.cc (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 #include "tensorflow/core/data/service/grpc_util.h"
17 
18 #include <algorithm>
19 #include <functional>
20 #include <string>
21 
22 #include "tensorflow/core/data/service/common.h"
23 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h"
24 #include "tensorflow/core/lib/core/errors.h"
25 #include "tensorflow/core/platform/env.h"
26 #include "tensorflow/core/platform/env_time.h"
27 #include "tensorflow/core/platform/errors.h"
28 #include "tensorflow/core/platform/status.h"
29 
30 namespace tensorflow {
31 namespace data {
32 namespace grpc_util {
33 
WrapError(const std::string & message,const::grpc::Status & status)34 Status WrapError(const std::string& message, const ::grpc::Status& status) {
35   if (status.ok()) {
36     return errors::Internal("Expected a non-ok grpc status. Wrapping message: ",
37                             message);
38   } else {
39     Status s = FromGrpcStatus(status);
40     return Status(s.code(),
41                   absl::StrCat(message, ": ", status.error_message()));
42   }
43 }
44 
Retry(const std::function<Status ()> & f,const std::string & description,int64_t deadline_micros)45 Status Retry(const std::function<Status()>& f, const std::string& description,
46              int64_t deadline_micros) {
47   return Retry(
48       f, [] { return true; }, description, deadline_micros);
49 }
50 
Retry(const std::function<Status ()> & f,const std::function<bool ()> & should_retry,const std::string & description,int64_t deadline_micros)51 Status Retry(const std::function<Status()>& f,
52              const std::function<bool()>& should_retry,
53              const std::string& description, int64_t deadline_micros) {
54   Status s = f();
55   for (int num_retries = 0;; ++num_retries) {
56     if (!IsPreemptedError(s)) {
57       return s;
58     }
59     int64_t now_micros = EnvTime::NowMicros();
60     if (now_micros > deadline_micros || !should_retry()) {
61       return s;
62     }
63     int64_t deadline_with_backoff_micros =
64         now_micros + ::tensorflow::ComputeBackoffMicroseconds(num_retries);
65     // Wait for a short period of time before retrying. If our backoff would put
66     // us past the deadline, we truncate it to ensure our attempt starts before
67     // the deadline.
68     int64_t backoff_until =
69         std::min(deadline_with_backoff_micros, deadline_micros);
70     int64_t wait_time_micros = backoff_until - now_micros;
71     if (wait_time_micros > 100 * 1000) {
72       LOG(INFO) << "Failed to " << description << ": " << s
73                 << ". Will retry in " << wait_time_micros / 1000 << "ms.";
74     }
75     Env::Default()->SleepForMicroseconds(wait_time_micros);
76     s = f();
77   }
78   return s;
79 }
80 
81 }  // namespace grpc_util
82 }  // namespace data
83 }  // namespace tensorflow
84