Home | History | Annotate | Download | only in cloud
      1 /* Copyright 2017 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_PLATFORM_CLOUD_RETRYING_UTILS_H_
     17 #define TENSORFLOW_CORE_PLATFORM_CLOUD_RETRYING_UTILS_H_
     18 
     19 #include <functional>
     20 #include "tensorflow/core/lib/core/status.h"
     21 
     22 namespace tensorflow {
     23 
     24 // Default time before reporting failure: ~100 seconds.
     25 struct RetryConfig {
     26   RetryConfig(int64 init_delay_time_us = 100 * 1000,
     27               int64 max_delay_time_us = 32 * 1000 * 1000,
     28               int max_retries = 10) {
     29     this->init_delay_time_us = init_delay_time_us;
     30     this->max_delay_time_us = max_delay_time_us;
     31     this->max_retries = max_retries;
     32   }
     33 
     34   // In case of failure, every call will be retried max_retries times.
     35   int max_retries;
     36 
     37   // Initial backoff time
     38   int64 init_delay_time_us;
     39 
     40   // Maximum backoff time in microseconds.
     41   int64 max_delay_time_us;
     42 };
     43 
     44 class RetryingUtils {
     45  public:
     46   /// \brief Retries the function in case of failure with exponential backoff.
     47   ///
     48   /// The provided callback is retried with an exponential backoff until it
     49   /// returns OK or a non-retriable error status.
     50   /// If initial_delay_microseconds is zero, no delays will be made between
     51   /// retries.
     52   /// If all retries failed, returns the last error status.
     53   static Status CallWithRetries(const std::function<Status()>& f,
     54                                 const RetryConfig& config);
     55 
     56   /// sleep_usec is a function that sleeps for the given number of microseconds.
     57   static Status CallWithRetries(const std::function<Status()>& f,
     58                                 const std::function<void(int64)>& sleep_usec,
     59                                 const RetryConfig& config);
     60   /// \brief A retrying wrapper for a function that deletes a resource.
     61   ///
     62   /// The function takes care of the scenario when a delete operation
     63   /// returns a failure but succeeds under the hood: if a retry returns
     64   /// NOT_FOUND, the whole operation is considered a success.
     65   static Status DeleteWithRetries(const std::function<Status()>& delete_func,
     66                                   const RetryConfig& config);
     67 };
     68 
     69 }  // namespace tensorflow
     70 
     71 #endif  // TENSORFLOW_CORE_PLATFORM_CLOUD_RETRYING_UTILS_H_
     72