]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | |
6 | #pragma once | |
7 | ||
8 | #ifndef ROCKSDB_LITE | |
9 | ||
10 | #include <stack> | |
11 | #include <string> | |
12 | #include <vector> | |
13 | ||
14 | #include "rocksdb/db.h" | |
15 | #include "rocksdb/slice.h" | |
16 | #include "rocksdb/snapshot.h" | |
17 | #include "rocksdb/status.h" | |
18 | #include "rocksdb/types.h" | |
19 | #include "rocksdb/utilities/transaction.h" | |
20 | #include "rocksdb/utilities/transaction_db.h" | |
21 | #include "rocksdb/utilities/write_batch_with_index.h" | |
22 | #include "utilities/transactions/transaction_util.h" | |
23 | ||
24 | namespace rocksdb { | |
25 | ||
26 | class TransactionBaseImpl : public Transaction { | |
27 | public: | |
28 | TransactionBaseImpl(DB* db, const WriteOptions& write_options); | |
29 | ||
30 | virtual ~TransactionBaseImpl(); | |
31 | ||
32 | // Remove pending operations queued in this transaction. | |
33 | virtual void Clear(); | |
34 | ||
35 | void Reinitialize(DB* db, const WriteOptions& write_options); | |
36 | ||
37 | // Called before executing Put, Merge, Delete, and GetForUpdate. If TryLock | |
38 | // returns non-OK, the Put/Merge/Delete/GetForUpdate will be failed. | |
11fdf7f2 | 39 | // skip_validate will be true if called from PutUntracked, DeleteUntracked, or |
7c673cae FG |
40 | // MergeUntracked. |
41 | virtual Status TryLock(ColumnFamilyHandle* column_family, const Slice& key, | |
42 | bool read_only, bool exclusive, | |
11fdf7f2 | 43 | bool skip_validate = false) = 0; |
7c673cae FG |
44 | |
45 | void SetSavePoint() override; | |
46 | ||
47 | Status RollbackToSavePoint() override; | |
11fdf7f2 TL |
48 | |
49 | Status PopSavePoint() override; | |
7c673cae | 50 | |
11fdf7f2 | 51 | using Transaction::Get; |
7c673cae FG |
52 | Status Get(const ReadOptions& options, ColumnFamilyHandle* column_family, |
53 | const Slice& key, std::string* value) override; | |
54 | ||
11fdf7f2 TL |
55 | Status Get(const ReadOptions& options, ColumnFamilyHandle* column_family, |
56 | const Slice& key, PinnableSlice* value) override; | |
57 | ||
7c673cae FG |
58 | Status Get(const ReadOptions& options, const Slice& key, |
59 | std::string* value) override { | |
60 | return Get(options, db_->DefaultColumnFamily(), key, value); | |
61 | } | |
62 | ||
11fdf7f2 | 63 | using Transaction::GetForUpdate; |
7c673cae FG |
64 | Status GetForUpdate(const ReadOptions& options, |
65 | ColumnFamilyHandle* column_family, const Slice& key, | |
66 | std::string* value, bool exclusive) override; | |
67 | ||
11fdf7f2 TL |
68 | Status GetForUpdate(const ReadOptions& options, |
69 | ColumnFamilyHandle* column_family, const Slice& key, | |
70 | PinnableSlice* pinnable_val, bool exclusive) override; | |
71 | ||
7c673cae FG |
72 | Status GetForUpdate(const ReadOptions& options, const Slice& key, |
73 | std::string* value, bool exclusive) override { | |
74 | return GetForUpdate(options, db_->DefaultColumnFamily(), key, value, | |
75 | exclusive); | |
76 | } | |
77 | ||
78 | std::vector<Status> MultiGet( | |
79 | const ReadOptions& options, | |
80 | const std::vector<ColumnFamilyHandle*>& column_family, | |
81 | const std::vector<Slice>& keys, | |
82 | std::vector<std::string>* values) override; | |
83 | ||
84 | std::vector<Status> MultiGet(const ReadOptions& options, | |
85 | const std::vector<Slice>& keys, | |
86 | std::vector<std::string>* values) override { | |
87 | return MultiGet(options, std::vector<ColumnFamilyHandle*>( | |
88 | keys.size(), db_->DefaultColumnFamily()), | |
89 | keys, values); | |
90 | } | |
91 | ||
92 | std::vector<Status> MultiGetForUpdate( | |
93 | const ReadOptions& options, | |
94 | const std::vector<ColumnFamilyHandle*>& column_family, | |
95 | const std::vector<Slice>& keys, | |
96 | std::vector<std::string>* values) override; | |
97 | ||
98 | std::vector<Status> MultiGetForUpdate( | |
99 | const ReadOptions& options, const std::vector<Slice>& keys, | |
100 | std::vector<std::string>* values) override { | |
101 | return MultiGetForUpdate(options, | |
102 | std::vector<ColumnFamilyHandle*>( | |
103 | keys.size(), db_->DefaultColumnFamily()), | |
104 | keys, values); | |
105 | } | |
106 | ||
107 | Iterator* GetIterator(const ReadOptions& read_options) override; | |
108 | Iterator* GetIterator(const ReadOptions& read_options, | |
109 | ColumnFamilyHandle* column_family) override; | |
110 | ||
111 | Status Put(ColumnFamilyHandle* column_family, const Slice& key, | |
112 | const Slice& value) override; | |
113 | Status Put(const Slice& key, const Slice& value) override { | |
114 | return Put(nullptr, key, value); | |
115 | } | |
116 | ||
117 | Status Put(ColumnFamilyHandle* column_family, const SliceParts& key, | |
118 | const SliceParts& value) override; | |
119 | Status Put(const SliceParts& key, const SliceParts& value) override { | |
120 | return Put(nullptr, key, value); | |
121 | } | |
122 | ||
123 | Status Merge(ColumnFamilyHandle* column_family, const Slice& key, | |
124 | const Slice& value) override; | |
125 | Status Merge(const Slice& key, const Slice& value) override { | |
126 | return Merge(nullptr, key, value); | |
127 | } | |
128 | ||
129 | Status Delete(ColumnFamilyHandle* column_family, const Slice& key) override; | |
130 | Status Delete(const Slice& key) override { return Delete(nullptr, key); } | |
131 | Status Delete(ColumnFamilyHandle* column_family, | |
132 | const SliceParts& key) override; | |
133 | Status Delete(const SliceParts& key) override { return Delete(nullptr, key); } | |
134 | ||
135 | Status SingleDelete(ColumnFamilyHandle* column_family, | |
136 | const Slice& key) override; | |
137 | Status SingleDelete(const Slice& key) override { | |
138 | return SingleDelete(nullptr, key); | |
139 | } | |
140 | Status SingleDelete(ColumnFamilyHandle* column_family, | |
141 | const SliceParts& key) override; | |
142 | Status SingleDelete(const SliceParts& key) override { | |
143 | return SingleDelete(nullptr, key); | |
144 | } | |
145 | ||
146 | Status PutUntracked(ColumnFamilyHandle* column_family, const Slice& key, | |
147 | const Slice& value) override; | |
148 | Status PutUntracked(const Slice& key, const Slice& value) override { | |
149 | return PutUntracked(nullptr, key, value); | |
150 | } | |
151 | ||
152 | Status PutUntracked(ColumnFamilyHandle* column_family, const SliceParts& key, | |
153 | const SliceParts& value) override; | |
154 | Status PutUntracked(const SliceParts& key, const SliceParts& value) override { | |
155 | return PutUntracked(nullptr, key, value); | |
156 | } | |
157 | ||
158 | Status MergeUntracked(ColumnFamilyHandle* column_family, const Slice& key, | |
159 | const Slice& value) override; | |
160 | Status MergeUntracked(const Slice& key, const Slice& value) override { | |
161 | return MergeUntracked(nullptr, key, value); | |
162 | } | |
163 | ||
164 | Status DeleteUntracked(ColumnFamilyHandle* column_family, | |
165 | const Slice& key) override; | |
166 | Status DeleteUntracked(const Slice& key) override { | |
167 | return DeleteUntracked(nullptr, key); | |
168 | } | |
169 | Status DeleteUntracked(ColumnFamilyHandle* column_family, | |
170 | const SliceParts& key) override; | |
171 | Status DeleteUntracked(const SliceParts& key) override { | |
172 | return DeleteUntracked(nullptr, key); | |
173 | } | |
174 | ||
11fdf7f2 TL |
175 | Status SingleDeleteUntracked(ColumnFamilyHandle* column_family, |
176 | const Slice& key) override; | |
177 | Status SingleDeleteUntracked(const Slice& key) override { | |
178 | return SingleDeleteUntracked(nullptr, key); | |
179 | } | |
180 | ||
7c673cae FG |
181 | void PutLogData(const Slice& blob) override; |
182 | ||
183 | WriteBatchWithIndex* GetWriteBatch() override; | |
184 | ||
11fdf7f2 | 185 | virtual void SetLockTimeout(int64_t /*timeout*/) override { /* Do nothing */ |
7c673cae FG |
186 | } |
187 | ||
188 | const Snapshot* GetSnapshot() const override { | |
189 | return snapshot_ ? snapshot_.get() : nullptr; | |
190 | } | |
191 | ||
11fdf7f2 | 192 | virtual void SetSnapshot() override; |
7c673cae FG |
193 | void SetSnapshotOnNextOperation( |
194 | std::shared_ptr<TransactionNotifier> notifier = nullptr) override; | |
195 | ||
196 | void ClearSnapshot() override { | |
197 | snapshot_.reset(); | |
198 | snapshot_needed_ = false; | |
199 | snapshot_notifier_ = nullptr; | |
200 | } | |
201 | ||
202 | void DisableIndexing() override { indexing_enabled_ = false; } | |
203 | ||
204 | void EnableIndexing() override { indexing_enabled_ = true; } | |
205 | ||
206 | uint64_t GetElapsedTime() const override; | |
207 | ||
208 | uint64_t GetNumPuts() const override; | |
209 | ||
210 | uint64_t GetNumDeletes() const override; | |
211 | ||
212 | uint64_t GetNumMerges() const override; | |
213 | ||
214 | uint64_t GetNumKeys() const override; | |
215 | ||
216 | void UndoGetForUpdate(ColumnFamilyHandle* column_family, | |
217 | const Slice& key) override; | |
218 | void UndoGetForUpdate(const Slice& key) override { | |
219 | return UndoGetForUpdate(nullptr, key); | |
220 | }; | |
221 | ||
222 | // Get list of keys in this transaction that must not have any conflicts | |
223 | // with writes in other transactions. | |
224 | const TransactionKeyMap& GetTrackedKeys() const { return tracked_keys_; } | |
225 | ||
226 | WriteOptions* GetWriteOptions() override { return &write_options_; } | |
227 | ||
228 | void SetWriteOptions(const WriteOptions& write_options) override { | |
229 | write_options_ = write_options; | |
230 | } | |
231 | ||
232 | // Used for memory management for snapshot_ | |
233 | void ReleaseSnapshot(const Snapshot* snapshot, DB* db); | |
234 | ||
235 | // iterates over the given batch and makes the appropriate inserts. | |
236 | // used for rebuilding prepared transactions after recovery. | |
11fdf7f2 | 237 | virtual Status RebuildFromWriteBatch(WriteBatch* src_batch) override; |
7c673cae FG |
238 | |
239 | WriteBatch* GetCommitTimeWriteBatch() override; | |
240 | ||
241 | protected: | |
242 | // Add a key to the list of tracked keys. | |
243 | // | |
244 | // seqno is the earliest seqno this key was involved with this transaction. | |
245 | // readonly should be set to true if no data was written for this key | |
246 | void TrackKey(uint32_t cfh_id, const std::string& key, SequenceNumber seqno, | |
247 | bool readonly, bool exclusive); | |
248 | ||
249 | // Helper function to add a key to the given TransactionKeyMap | |
250 | static void TrackKey(TransactionKeyMap* key_map, uint32_t cfh_id, | |
251 | const std::string& key, SequenceNumber seqno, | |
252 | bool readonly, bool exclusive); | |
253 | ||
254 | // Called when UndoGetForUpdate determines that this key can be unlocked. | |
255 | virtual void UnlockGetForUpdate(ColumnFamilyHandle* column_family, | |
256 | const Slice& key) = 0; | |
257 | ||
258 | std::unique_ptr<TransactionKeyMap> GetTrackedKeysSinceSavePoint(); | |
259 | ||
260 | // Sets a snapshot if SetSnapshotOnNextOperation() has been called. | |
261 | void SetSnapshotIfNeeded(); | |
262 | ||
263 | DB* db_; | |
264 | DBImpl* dbimpl_; | |
265 | ||
266 | WriteOptions write_options_; | |
267 | ||
268 | const Comparator* cmp_; | |
269 | ||
270 | // Stores that time the txn was constructed, in microseconds. | |
271 | uint64_t start_time_; | |
272 | ||
273 | // Stores the current snapshot that was set by SetSnapshot or null if | |
274 | // no snapshot is currently set. | |
275 | std::shared_ptr<const Snapshot> snapshot_; | |
276 | ||
277 | // Count of various operations pending in this transaction | |
278 | uint64_t num_puts_ = 0; | |
279 | uint64_t num_deletes_ = 0; | |
280 | uint64_t num_merges_ = 0; | |
281 | ||
282 | struct SavePoint { | |
283 | std::shared_ptr<const Snapshot> snapshot_; | |
284 | bool snapshot_needed_; | |
285 | std::shared_ptr<TransactionNotifier> snapshot_notifier_; | |
286 | uint64_t num_puts_; | |
287 | uint64_t num_deletes_; | |
288 | uint64_t num_merges_; | |
289 | ||
290 | // Record all keys tracked since the last savepoint | |
291 | TransactionKeyMap new_keys_; | |
292 | ||
293 | SavePoint(std::shared_ptr<const Snapshot> snapshot, bool snapshot_needed, | |
294 | std::shared_ptr<TransactionNotifier> snapshot_notifier, | |
295 | uint64_t num_puts, uint64_t num_deletes, uint64_t num_merges) | |
296 | : snapshot_(snapshot), | |
297 | snapshot_needed_(snapshot_needed), | |
298 | snapshot_notifier_(snapshot_notifier), | |
299 | num_puts_(num_puts), | |
300 | num_deletes_(num_deletes), | |
301 | num_merges_(num_merges) {} | |
302 | }; | |
303 | ||
304 | // Records writes pending in this transaction | |
305 | WriteBatchWithIndex write_batch_; | |
306 | ||
307 | private: | |
11fdf7f2 TL |
308 | friend class WritePreparedTxn; |
309 | // Extra data to be persisted with the commit. Note this is only used when | |
310 | // prepare phase is not skipped. | |
7c673cae FG |
311 | WriteBatch commit_time_batch_; |
312 | ||
313 | // Stack of the Snapshot saved at each save point. Saved snapshots may be | |
314 | // nullptr if there was no snapshot at the time SetSavePoint() was called. | |
315 | std::unique_ptr<std::stack<TransactionBaseImpl::SavePoint>> save_points_; | |
316 | ||
317 | // Map from column_family_id to map of keys that are involved in this | |
318 | // transaction. | |
11fdf7f2 | 319 | // For Pessimistic Transactions this is the list of locked keys. |
7c673cae FG |
320 | // Optimistic Transactions will wait till commit time to do conflict checking. |
321 | TransactionKeyMap tracked_keys_; | |
322 | ||
323 | // If true, future Put/Merge/Deletes will be indexed in the | |
324 | // WriteBatchWithIndex. | |
325 | // If false, future Put/Merge/Deletes will be inserted directly into the | |
326 | // underlying WriteBatch and not indexed in the WriteBatchWithIndex. | |
327 | bool indexing_enabled_; | |
328 | ||
329 | // SetSnapshotOnNextOperation() has been called and the snapshot has not yet | |
330 | // been reset. | |
331 | bool snapshot_needed_ = false; | |
332 | ||
333 | // SetSnapshotOnNextOperation() has been called and the caller would like | |
334 | // a notification through the TransactionNotifier interface | |
335 | std::shared_ptr<TransactionNotifier> snapshot_notifier_ = nullptr; | |
336 | ||
337 | Status TryLock(ColumnFamilyHandle* column_family, const SliceParts& key, | |
11fdf7f2 | 338 | bool read_only, bool exclusive, bool skip_validate = false); |
7c673cae FG |
339 | |
340 | WriteBatchBase* GetBatchForWrite(); | |
7c673cae FG |
341 | void SetSnapshotInternal(const Snapshot* snapshot); |
342 | }; | |
343 | ||
344 | } // namespace rocksdb | |
345 | ||
346 | #endif // ROCKSDB_LITE |