/Users/deen/code/yugabyte-db/src/yb/tablet/transaction_participant.h
Line | Count | Source (jump to first uncovered line) |
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_TABLET_TRANSACTION_PARTICIPANT_H |
17 | | #define YB_TABLET_TRANSACTION_PARTICIPANT_H |
18 | | |
19 | | #include <stdint.h> |
20 | | |
21 | | #include <cstdint> |
22 | | #include <functional> |
23 | | #include <future> |
24 | | #include <memory> |
25 | | #include <type_traits> |
26 | | |
27 | | #include <boost/optional/optional.hpp> |
28 | | |
29 | | #include "yb/common/doc_hybrid_time.h" |
30 | | #include "yb/common/transaction.h" |
31 | | |
32 | | #include "yb/docdb/docdb_fwd.h" |
33 | | |
34 | | #include "yb/rpc/rpc_fwd.h" |
35 | | |
36 | | #include "yb/server/server_fwd.h" |
37 | | |
38 | | #include "yb/tablet/tablet_fwd.h" |
39 | | |
40 | | #include "yb/util/enums.h" |
41 | | #include "yb/util/math_util.h" |
42 | | #include "yb/util/opid.h" |
43 | | #include "yb/util/opid.pb.h" |
44 | | |
45 | | namespace rocksdb { |
46 | | |
47 | | class DB; |
48 | | class WriteBatch; |
49 | | |
50 | | } |
51 | | |
52 | | namespace yb { |
53 | | |
54 | | class MetricEntity; |
55 | | class HybridTime; |
56 | | class OneWayBitmap; |
57 | | class RWOperationCounter; |
58 | | class TransactionMetadataPB; |
59 | | |
60 | | namespace tserver { |
61 | | |
62 | | class GetTransactionStatusAtParticipantResponsePB; |
63 | | class TransactionStatePB; |
64 | | |
65 | | } |
66 | | |
67 | | namespace tablet { |
68 | | |
69 | | struct TransactionApplyData { |
70 | | int64_t leader_term = -1; |
71 | | TransactionId transaction_id = TransactionId::Nil(); |
72 | | AbortedSubTransactionSet aborted; |
73 | | OpId op_id; |
74 | | HybridTime commit_ht; |
75 | | HybridTime log_ht; |
76 | | bool sealed = false; |
77 | | TabletId status_tablet; |
78 | | // Owned by running transaction if non-null. |
79 | | const docdb::ApplyTransactionState* apply_state = nullptr; |
80 | | |
81 | | std::string ToString() const; |
82 | | }; |
83 | | |
84 | | struct RemoveIntentsData { |
85 | | OpId op_id; |
86 | | HybridTime log_ht; |
87 | | }; |
88 | | |
89 | | struct GetIntentsData { |
90 | | OpIdPB op_id; |
91 | | HybridTime log_ht; |
92 | | }; |
93 | | |
94 | | struct TransactionalBatchData { |
95 | | // Write id of last strong write intent in transaction. |
96 | | IntraTxnWriteId next_write_id = 0; |
97 | | |
98 | | // Hybrid time of last replicated write in transaction. |
99 | | HybridTime hybrid_time; |
100 | | |
101 | 0 | std::string ToString() const { |
102 | 0 | return YB_STRUCT_TO_STRING(next_write_id, hybrid_time); |
103 | 0 | } Unexecuted instantiation: _ZNK2yb6tablet22TransactionalBatchData8ToStringEv Unexecuted instantiation: _ZNK2yb6tablet22TransactionalBatchData8ToStringEv |
104 | | }; |
105 | | |
106 | | // TransactionParticipant manages running transactions, i.e. transactions that have intents in |
107 | | // appropriate tablet. Since this class manages transactions of tablet there is separate class |
108 | | // instance per tablet. |
109 | | class TransactionParticipant : public TransactionStatusManager { |
110 | | public: |
111 | | TransactionParticipant( |
112 | | TransactionParticipantContext* context, TransactionIntentApplier* applier, |
113 | | const scoped_refptr<MetricEntity>& entity); |
114 | | virtual ~TransactionParticipant(); |
115 | | |
116 | | // Notify participant that this context is ready and it could start performing its requests. |
117 | | void Start(); |
118 | | |
119 | | // Adds new running transaction. |
120 | | // Returns true if transaction was added, false if transaction already present. |
121 | | Result<bool> Add(const TransactionMetadata& metadata); |
122 | | |
123 | | Result<TransactionMetadata> PrepareMetadata(const TransactionMetadataPB& id) override; |
124 | | |
125 | | // Prepares batch data for specified transaction id. |
126 | | // I.e. adds specified batch idx to set of replicated batches and fills encoded_replicated_batches |
127 | | // with new state of replicated batch indexes. Encoding does not matter for user of this function, |
128 | | // he should just append it to appropriate value. |
129 | | // |
130 | | // Returns boost::none when transaction is unknown. |
131 | | boost::optional<std::pair<IsolationLevel, TransactionalBatchData>> PrepareBatchData( |
132 | | const TransactionId& id, size_t batch_idx, |
133 | | boost::container::small_vector_base<uint8_t>* encoded_replicated_batches); |
134 | | |
135 | | void BatchReplicated(const TransactionId& id, const TransactionalBatchData& data); |
136 | | |
137 | | HybridTime LocalCommitTime(const TransactionId& id) override; |
138 | | |
139 | | boost::optional<CommitMetadata> LocalCommitData(const TransactionId& id) override; |
140 | | |
141 | | void RequestStatusAt(const StatusRequest& request) override; |
142 | | |
143 | | void Abort(const TransactionId& id, TransactionStatusCallback callback) override; |
144 | | |
145 | | void Handle(std::unique_ptr<tablet::UpdateTxnOperation> request, int64_t term); |
146 | | |
147 | | void Cleanup(TransactionIdSet&& set) override; |
148 | | |
149 | | // Used to pass arguments to ProcessReplicated. |
150 | | struct ReplicatedData { |
151 | | int64_t leader_term = -1; |
152 | | const TransactionStatePB& state; |
153 | | const OpId& op_id; |
154 | | HybridTime hybrid_time; |
155 | | bool sealed = false; |
156 | | AlreadyAppliedToRegularDB already_applied_to_regular_db; |
157 | | |
158 | | std::string ToString() const; |
159 | | }; |
160 | | |
161 | | CHECKED_STATUS ProcessReplicated(const ReplicatedData& data); |
162 | | |
163 | | void SetDB( |
164 | | const docdb::DocDB& db, const docdb::KeyBounds* key_bounds, |
165 | | RWOperationCounter* pending_op_counter); |
166 | | |
167 | | CHECKED_STATUS CheckAborted(const TransactionId& id); |
168 | | |
169 | | void FillPriorities( |
170 | | boost::container::small_vector_base<std::pair<TransactionId, uint64_t>>* inout) override; |
171 | | |
172 | | void GetStatus(const TransactionId& transaction_id, |
173 | | size_t required_num_replicated_batches, |
174 | | int64_t term, |
175 | | tserver::GetTransactionStatusAtParticipantResponsePB* response, |
176 | | rpc::RpcContext* context); |
177 | | |
178 | | TransactionParticipantContext* context() const; |
179 | | |
180 | | HybridTime MinRunningHybridTime() const override; |
181 | | |
182 | | Result<HybridTime> WaitForSafeTime(HybridTime safe_time, CoarseTimePoint deadline) override; |
183 | | |
184 | | // When minimal start hybrid time of running transaction will be at least `ht` applier |
185 | | // method `MinRunningHybridTimeSatisfied` will be invoked. |
186 | | void WaitMinRunningHybridTime(HybridTime ht); |
187 | | |
188 | | void StartShutdown(); |
189 | | |
190 | | void CompleteShutdown(); |
191 | | |
192 | | // Resolve all transactions that were committed or aborted at resolve_at. |
193 | | // After this function returns with success: |
194 | | // - All intents of committed transactions will have been applied. |
195 | | // - No transactions can be committed with commit time <= resolve_at from that point on.. |
196 | | CHECKED_STATUS ResolveIntents(HybridTime resolve_at, CoarseTimePoint deadline); |
197 | | |
198 | | // Attempts to abort all transactions that started prior to cutoff time. |
199 | | // Waits until deadline, for txns to abort. If not, it returns a TimedOut. |
200 | | // After this call, there should be no active (non-aborted/committed) txn that |
201 | | // started before cutoff which is active on this tablet. |
202 | | CHECKED_STATUS StopActiveTxnsPriorTo( |
203 | | HybridTime cutoff, CoarseTimePoint deadline, TransactionId* exclude_txn_id = nullptr); |
204 | | |
205 | | void IgnoreAllTransactionsStartedBefore(HybridTime limit); |
206 | | |
207 | | std::string DumpTransactions() const; |
208 | | |
209 | | const TabletId& tablet_id() const override; |
210 | | |
211 | | size_t TEST_GetNumRunningTransactions() const; |
212 | | |
213 | | // Returns pair of number of intents and number of transactions. |
214 | | std::pair<size_t, size_t> TEST_CountIntents() const; |
215 | | |
216 | | OneWayBitmap TEST_TransactionReplicatedBatches(const TransactionId& id) const; |
217 | | |
218 | | private: |
219 | | int64_t RegisterRequest() override; |
220 | | void UnregisterRequest(int64_t request) override; |
221 | | |
222 | | class Impl; |
223 | | std::unique_ptr<Impl> impl_; |
224 | | }; |
225 | | |
226 | | } // namespace tablet |
227 | | } // namespace yb |
228 | | |
229 | | #endif // YB_TABLET_TRANSACTION_PARTICIPANT_H |