aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/common/retry_lib.h
blob: c6c061f614b9b0485fbd2be1a62c53882c35a54f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once

#include "fwd.h"

#include <yt/cpp/mapreduce/interface/fwd.h>

#include <util/datetime/base.h>
#include <util/generic/maybe.h>
#include <util/generic/ptr.h>
#include <util/generic/string.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

// IRequestRetryPolicy class controls retries of single request.
class IRequestRetryPolicy
    : public virtual TThrRefBase
{
public:
    // Helper function that returns text description of current attempt, e.g.
    //   "attempt 3 / 10"
    // used in logs.
    virtual TString GetAttemptDescription() const = 0;

    // Library code calls this function before any request attempt.
    virtual void NotifyNewAttempt() = 0;

    // OnRetriableError is called whenever client gets YT error that can be retried (e.g. operation limit exceeded).
    // OnGenericError is called whenever request failed due to generic error like network error.
    //
    // Both methods must return nothing if policy doesn't want to retry this error.
    // Otherwise method should return backoff time.
    virtual TMaybe<TDuration> OnRetriableError(const TErrorResponse& e) = 0;
    virtual TMaybe<TDuration> OnGenericError(const std::exception& e) = 0;

    // OnIgnoredError is called whenever client gets an error but is going to ignore it.
    virtual void OnIgnoredError(const TErrorResponse& /*e*/) = 0;
};
using IRequestRetryPolicyPtr = ::TIntrusivePtr<IRequestRetryPolicy>;

////////////////////////////////////////////////////////////////////////////////

// IClientRetryPolicy controls creation of policies for individual requests.
class IClientRetryPolicy
    : public virtual TThrRefBase
{
public:
    virtual IRequestRetryPolicyPtr CreatePolicyForGenericRequest() = 0;
    virtual IRequestRetryPolicyPtr CreatePolicyForStartOperationRequest() = 0;
};


////////////////////////////////////////////////////////////////////////////////

class TAttemptLimitedRetryPolicy
    : public IRequestRetryPolicy
{
public:
    explicit TAttemptLimitedRetryPolicy(ui32 attemptLimit, const TConfigPtr& config);

    void NotifyNewAttempt() override;

    TMaybe<TDuration> OnGenericError(const std::exception& e) override;
    TMaybe<TDuration> OnRetriableError(const TErrorResponse& e) override;
    void OnIgnoredError(const TErrorResponse& e) override;
    TString GetAttemptDescription() const override;

    bool IsAttemptLimitExceeded() const;

protected:
    const TConfigPtr Config_;

private:
    const ui32 AttemptLimit_;
    ui32 Attempt_ = 0;
};

////////////////////////////////////////////////////////////////////////////////

IRequestRetryPolicyPtr CreateDefaultRequestRetryPolicy(const TConfigPtr& config);
IClientRetryPolicyPtr CreateDefaultClientRetryPolicy(IRetryConfigProviderPtr retryConfigProvider, const TConfigPtr& config);
IRetryConfigProviderPtr CreateDefaultRetryConfigProvider();

////////////////////////////////////////////////////////////////////////////////

// Check if error returned by YT can be retried
bool IsRetriable(const TErrorResponse& errorResponse);
bool IsRetriable(const std::exception& ex);

// Get backoff duration for errors returned by YT.
TDuration GetBackoffDuration(const TErrorResponse& errorResponse, const TConfigPtr& config);

// Get backoff duration for errors that are not TErrorResponse.
TDuration GetBackoffDuration(const std::exception& error, const TConfigPtr& config);
TDuration GetBackoffDuration(const TConfigPtr& config);

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT