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