• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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/platform/cloud/retrying_utils.h"
17 #include "tensorflow/core/lib/core/errors.h"
18 #include "tensorflow/core/lib/random/random.h"
19 #include "tensorflow/core/platform/env.h"
20 #include "tensorflow/core/platform/file_system.h"
21 
22 namespace tensorflow {
23 
24 namespace {
25 
IsRetriable(error::Code code)26 bool IsRetriable(error::Code code) {
27   switch (code) {
28     case error::UNAVAILABLE:
29     case error::DEADLINE_EXCEEDED:
30     case error::UNKNOWN:
31       return true;
32     default:
33       // OK also falls here.
34       return false;
35   }
36 }
37 
38 }  // namespace
39 
CallWithRetries(const std::function<Status ()> & f,const RetryConfig & config)40 Status RetryingUtils::CallWithRetries(const std::function<Status()>& f,
41                                       const RetryConfig& config) {
42   return CallWithRetries(
43       f,
44       [](int64 micros) { return Env::Default()->SleepForMicroseconds(micros); },
45       config);
46 }
47 
CallWithRetries(const std::function<Status ()> & f,const std::function<void (int64)> & sleep_usec,const RetryConfig & config)48 Status RetryingUtils::CallWithRetries(
49     const std::function<Status()>& f,
50     const std::function<void(int64)>& sleep_usec, const RetryConfig& config) {
51   int retries = 0;
52   while (true) {
53     auto status = f();
54     if (!IsRetriable(status.code())) {
55       return status;
56     }
57     if (retries >= config.max_retries) {
58       // Return AbortedError, so that it doesn't get retried again somewhere
59       // at a higher level.
60       return Status(
61           error::ABORTED,
62           strings::StrCat(
63               "All ", config.max_retries,
64               " retry attempts failed. The last failure: ", status.ToString()));
65     }
66     int64 delay_micros = 0;
67     if (config.init_delay_time_us > 0) {
68       const int64 random_micros = random::New64() % 1000000;
69       delay_micros = std::min(config.init_delay_time_us << retries,
70                               config.max_delay_time_us) +
71                      random_micros;
72     }
73     LOG(INFO) << "The operation failed and will be automatically retried in "
74               << (delay_micros / 1000000.0) << " seconds (attempt "
75               << (retries + 1) << " out of " << config.max_retries
76               << "), caused by: " << status.ToString();
77     sleep_usec(delay_micros);
78     retries++;
79   }
80 }
81 
DeleteWithRetries(const std::function<Status ()> & delete_func,const RetryConfig & config)82 Status RetryingUtils::DeleteWithRetries(
83     const std::function<Status()>& delete_func, const RetryConfig& config) {
84   bool is_retried = false;
85   return RetryingUtils::CallWithRetries(
86       [delete_func, &is_retried]() {
87         const Status status = delete_func();
88         if (is_retried && status.code() == error::NOT_FOUND) {
89           return Status::OK();
90         }
91         is_retried = true;
92         return status;
93       },
94       config);
95 }
96 
97 }  // namespace tensorflow
98