aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/client/transaction.h
blob: a363020b246ce59c676d5eb2473b24a772053fcd (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
#pragma once

#include "abortable_registry.h"

#include <yt/cpp/mapreduce/http/requests.h>
#include <yt/cpp/mapreduce/http/retry_request.h>

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

namespace NYT {

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

class TPingableTransaction
{
public:
    //
    // Start a new transaction.
    TPingableTransaction(
        const IClientRetryPolicyPtr& retryPolicy,
        const TClientContext& context,
        const TTransactionId& parentId,
        ITransactionPingerPtr transactionPinger,
        const TStartTransactionOptions& options);

    //
    // Attach to an existing transaction.
    TPingableTransaction(
        const IClientRetryPolicyPtr& retryPolicy,
        const TClientContext& context,
        const TTransactionId& transactionId,
        ITransactionPingerPtr transactionPinger,
        const TAttachTransactionOptions& options);

    ~TPingableTransaction();

    const TTransactionId GetId() const;

    const std::pair<TDuration, TDuration> GetPingInterval() const;
    const TClientContext GetContext() const;

    void Commit();
    void Abort();
    void Detach();


private:
    enum class EStopAction
    {
        Detach,
        Abort,
        Commit,
    };

private:
    IClientRetryPolicyPtr ClientRetryPolicy_;
    TClientContext Context_;
    TTransactionId TransactionId_;
    TDuration MinPingInterval_;
    TDuration MaxPingInterval_;

    // We have to own an IntrusivePtr to registry to prevent use-after-free.
    ::TIntrusivePtr<NDetail::TAbortableRegistry> AbortableRegistry_;

    bool AbortOnTermination_;

    bool AutoPingable_;
    bool Finalized_ = false;
    ITransactionPingerPtr Pinger_;

private:
    void Init(
        const TClientContext& context,
        const TTransactionId& transactionId,
        TDuration timeout);

    void Stop(EStopAction action);
};

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

TYPath Snapshot(
    const IClientRetryPolicyPtr& clientRetryPolicy,
    const TClientContext& context,
    const TTransactionId& transactionId,
    const TYPath& path);

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

} // namespace NYT