/Users/deen/code/yugabyte-db/src/yb/client/transaction.h
Line | Count | Source |
1 | | // |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. 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 distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | |
16 | | #ifndef YB_CLIENT_TRANSACTION_H |
17 | | #define YB_CLIENT_TRANSACTION_H |
18 | | |
19 | | #include <future> |
20 | | #include <memory> |
21 | | #include <unordered_map> |
22 | | #include <unordered_set> |
23 | | |
24 | | #include "yb/common/consistent_read_point.h" |
25 | | #include "yb/common/read_hybrid_time.h" |
26 | | #include "yb/common/transaction.h" |
27 | | |
28 | | #include "yb/client/client_fwd.h" |
29 | | #include "yb/client/in_flight_op.h" |
30 | | |
31 | | #include "yb/util/status_fwd.h" |
32 | | |
33 | | namespace yb { |
34 | | |
35 | | class HybridTime; |
36 | | |
37 | | class Trace; |
38 | | |
39 | | enum TxnPriorityRequirement { |
40 | | kLowerPriorityRange, |
41 | | kHigherPriorityRange, |
42 | | kHighestPriority |
43 | | }; |
44 | | |
45 | | namespace client { |
46 | | |
47 | | using Waiter = boost::function<void(const Status&)>; |
48 | | using PrepareChildCallback = std::function<void(const Result<ChildTransactionDataPB>&)>; |
49 | | |
50 | | struct ChildTransactionData { |
51 | | TransactionMetadata metadata; |
52 | | ReadHybridTime read_time; |
53 | | ConsistentReadPoint::HybridTimeMap local_limits; |
54 | | |
55 | | static Result<ChildTransactionData> FromPB(const ChildTransactionDataPB& data); |
56 | | }; |
57 | | |
58 | | // SealOnly is a special commit mode. |
59 | | // I.e. sealed transaction will be committed after seal record and all write batches are replicated. |
60 | | YB_STRONGLY_TYPED_BOOL(SealOnly); |
61 | | |
62 | | // YBTransaction is a representation of a single transaction. |
63 | | // After YBTransaction is created, it could be used during construction of YBSession, |
64 | | // to indicate that this session will send commands related to this transaction. |
65 | | class YBTransaction : public std::enable_shared_from_this<YBTransaction> { |
66 | | private: |
67 | | class PrivateOnlyTag {}; |
68 | | |
69 | | public: |
70 | | explicit YBTransaction(TransactionManager* manager, |
71 | | TransactionLocality locality = TransactionLocality::GLOBAL); |
72 | | |
73 | | // Trick to allow std::make_shared with this ctor only from methods of this class. |
74 | | YBTransaction(TransactionManager* manager, const TransactionMetadata& metadata, PrivateOnlyTag); |
75 | | |
76 | | // Creates "child" transaction. |
77 | | // Child transaction shares same metadata as parent transaction, so all writes are done |
78 | | // as part of parent transaction. |
79 | | // But lifetime is controlled by parent transaction. |
80 | | // I.e. only parent transaction could be committed or aborted, also only parent transaction |
81 | | // sends heartbeats. |
82 | | YBTransaction(TransactionManager* manager, ChildTransactionData data); |
83 | | |
84 | | ~YBTransaction(); |
85 | | |
86 | | Trace *trace(); |
87 | | void SetPriority(uint64_t priority); |
88 | | |
89 | | uint64_t GetPriority() const; |
90 | | |
91 | | // Should be invoked to complete transaction creation. |
92 | | // Transaction is unusable before Init is called. |
93 | | CHECKED_STATUS Init( |
94 | | IsolationLevel isolation, const ReadHybridTime& read_time = ReadHybridTime()); |
95 | | |
96 | | // Allows starting a transaction that reuses an existing read point. |
97 | | void InitWithReadPoint(IsolationLevel isolation, ConsistentReadPoint&& read_point); |
98 | | |
99 | | internal::TxnBatcherIf& batcher_if(); |
100 | | |
101 | | // Commits this transaction. |
102 | | void Commit(CoarseTimePoint deadline, SealOnly seal_only, CommitCallback callback); |
103 | | |
104 | | void Commit(CoarseTimePoint deadline, CommitCallback callback); |
105 | | |
106 | | void Commit(CommitCallback callback); |
107 | | |
108 | | // Utility function for Commit. |
109 | | std::future<Status> CommitFuture( |
110 | | CoarseTimePoint deadline = CoarseTimePoint(), SealOnly seal_only = SealOnly::kFalse); |
111 | | |
112 | | // Aborts this transaction. |
113 | | void Abort(CoarseTimePoint deadline = CoarseTimePoint()); |
114 | | |
115 | | // Returns transaction ID. |
116 | | const TransactionId& id() const; |
117 | | |
118 | | const ConsistentReadPoint& read_point() const; |
119 | | ConsistentReadPoint& read_point(); |
120 | | |
121 | | bool IsRestartRequired() const; |
122 | | |
123 | | // Creates restarted transaction, this transaction should be in the "restart required" state. |
124 | | Result<YBTransactionPtr> CreateRestartedTransaction(); |
125 | | |
126 | | // Setup precreated transaction to be restarted version of this transaction. |
127 | | CHECKED_STATUS FillRestartedTransaction(const YBTransactionPtr& dest); |
128 | | |
129 | | // Prepares child data, so child transaction could be started in another server. |
130 | | // Should be async because status tablet could be not ready yet. |
131 | | void PrepareChild( |
132 | | ForceConsistentRead force_consistent_read, CoarseTimePoint deadline, |
133 | | PrepareChildCallback callback); |
134 | | |
135 | | std::future<Result<ChildTransactionDataPB>> PrepareChildFuture( |
136 | | ForceConsistentRead force_consistent_read, CoarseTimePoint deadline = CoarseTimePoint()); |
137 | | |
138 | | // After we finish all child operations, we should finish child and send result to parent. |
139 | | Result<ChildTransactionResultPB> FinishChild(); |
140 | | |
141 | | // Apply results from child to this parent transaction. |
142 | | // `result` should be prepared with FinishChild of child transaction. |
143 | | CHECKED_STATUS ApplyChildResult(const ChildTransactionResultPB& result); |
144 | | |
145 | | std::shared_future<Result<TransactionMetadata>> GetMetadata() const; |
146 | | |
147 | | std::string ToString() const; |
148 | | |
149 | | IsolationLevel isolation() const; |
150 | | |
151 | | // Releases this transaction object returning its metadata. |
152 | | // So this transaction could be used by some other application instance. |
153 | | Result<TransactionMetadata> Release(); |
154 | | |
155 | | // Creates transaction by metadata, could be used in pair with release to transfer transaction |
156 | | // between application instances. |
157 | | static YBTransactionPtr Take(TransactionManager* manager, const TransactionMetadata& metadata); |
158 | | |
159 | | void SetActiveSubTransaction(SubTransactionId id); |
160 | | |
161 | | CHECKED_STATUS RollbackSubTransaction(SubTransactionId id); |
162 | | |
163 | | bool HasSubTransactionState(); |
164 | | |
165 | | private: |
166 | | class Impl; |
167 | | std::unique_ptr<Impl> impl_; |
168 | | }; |
169 | | |
170 | | class YBSubTransaction { |
171 | | public: |
172 | 669k | bool active() const { |
173 | 669k | return highest_subtransaction_id_ >= kMinSubTransactionId; |
174 | 669k | } |
175 | | |
176 | | void SetActiveSubTransaction(SubTransactionId id); |
177 | | |
178 | | CHECKED_STATUS RollbackSubTransaction(SubTransactionId id); |
179 | | |
180 | | const SubTransactionMetadata& get(); |
181 | | |
182 | | private: |
183 | | SubTransactionMetadata sub_txn_; |
184 | | |
185 | | // Tracks the highest observed subtransaction_id. Used during "ROLLBACK TO s" to abort from s to |
186 | | // the highest live subtransaction_id. |
187 | | SubTransactionId highest_subtransaction_id_ = 0; |
188 | | }; |
189 | | |
190 | | } // namespace client |
191 | | } // namespace yb |
192 | | |
193 | | #endif // YB_CLIENT_TRANSACTION_H |