/src/rocksdb/utilities/transactions/write_prepared_txn.h
Line | Count | Source |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
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). |
5 | | |
6 | | #pragma once |
7 | | |
8 | | #include <algorithm> |
9 | | #include <atomic> |
10 | | #include <mutex> |
11 | | #include <stack> |
12 | | #include <string> |
13 | | #include <unordered_map> |
14 | | #include <vector> |
15 | | |
16 | | #include "db/write_callback.h" |
17 | | #include "rocksdb/db.h" |
18 | | #include "rocksdb/slice.h" |
19 | | #include "rocksdb/snapshot.h" |
20 | | #include "rocksdb/status.h" |
21 | | #include "rocksdb/types.h" |
22 | | #include "rocksdb/utilities/transaction.h" |
23 | | #include "rocksdb/utilities/transaction_db.h" |
24 | | #include "rocksdb/utilities/write_batch_with_index.h" |
25 | | #include "util/autovector.h" |
26 | | #include "utilities/transactions/pessimistic_transaction.h" |
27 | | #include "utilities/transactions/pessimistic_transaction_db.h" |
28 | | #include "utilities/transactions/transaction_base.h" |
29 | | #include "utilities/transactions/transaction_util.h" |
30 | | |
31 | | namespace ROCKSDB_NAMESPACE { |
32 | | |
33 | | class WritePreparedTxnDB; |
34 | | |
35 | | // This impl could write to DB also uncommitted data and then later tell apart |
36 | | // committed data from uncommitted data. Uncommitted data could be after the |
37 | | // Prepare phase in 2PC (WritePreparedTxn) or before that |
38 | | // (WriteUnpreparedTxnImpl). |
39 | | class WritePreparedTxn : public PessimisticTransaction { |
40 | | public: |
41 | | WritePreparedTxn(WritePreparedTxnDB* db, const WriteOptions& write_options, |
42 | | const TransactionOptions& txn_options); |
43 | | // No copying allowed |
44 | | WritePreparedTxn(const WritePreparedTxn&) = delete; |
45 | | void operator=(const WritePreparedTxn&) = delete; |
46 | | |
47 | 0 | virtual ~WritePreparedTxn() {} |
48 | | |
49 | | // To make WAL commit markers visible, the snapshot will be based on the last |
50 | | // seq in the WAL that is also published, LastPublishedSequence, as opposed to |
51 | | // the last seq in the memtable. |
52 | | using Transaction::Get; |
53 | | Status Get(const ReadOptions& _read_options, |
54 | | ColumnFamilyHandle* column_family, const Slice& key, |
55 | | PinnableSlice* value) override; |
56 | | |
57 | | using Transaction::MultiGet; |
58 | | void MultiGet(const ReadOptions& _read_options, |
59 | | ColumnFamilyHandle* column_family, const size_t num_keys, |
60 | | const Slice* keys, PinnableSlice* values, Status* statuses, |
61 | | const bool sorted_input = false) override; |
62 | | |
63 | | // Note: The behavior is undefined in presence of interleaved writes to the |
64 | | // same transaction. |
65 | | // To make WAL commit markers visible, the snapshot will be |
66 | | // based on the last seq in the WAL that is also published, |
67 | | // LastPublishedSequence, as opposed to the last seq in the memtable. |
68 | | using Transaction::GetIterator; |
69 | | Iterator* GetIterator(const ReadOptions& options) override; |
70 | | Iterator* GetIterator(const ReadOptions& options, |
71 | | ColumnFamilyHandle* column_family) override; |
72 | | |
73 | | std::unique_ptr<Iterator> GetCoalescingIterator( |
74 | | const ReadOptions& read_options, |
75 | | const std::vector<ColumnFamilyHandle*>& column_families) override; |
76 | | |
77 | | std::unique_ptr<AttributeGroupIterator> GetAttributeGroupIterator( |
78 | | const ReadOptions& read_options, |
79 | | const std::vector<ColumnFamilyHandle*>& column_families) override; |
80 | | |
81 | | void SetSnapshot() override; |
82 | | |
83 | | protected: |
84 | | void Initialize(const TransactionOptions& txn_options) override; |
85 | | // Override the protected SetId to make it visible to the friend class |
86 | | // WritePreparedTxnDB |
87 | 0 | inline void SetId(uint64_t id) override { Transaction::SetId(id); } |
88 | | |
89 | | private: |
90 | | friend class WritePreparedTransactionTest_BasicRecoveryTest_Test; |
91 | | friend class WritePreparedTxnDB; |
92 | | friend class WriteUnpreparedTxnDB; |
93 | | friend class WriteUnpreparedTxn; |
94 | | |
95 | | using Transaction::GetImpl; |
96 | | Status GetImpl(const ReadOptions& options, ColumnFamilyHandle* column_family, |
97 | | const Slice& key, PinnableSlice* value) override; |
98 | | |
99 | | Status PrepareInternal() override; |
100 | | |
101 | | Status CommitWithoutPrepareInternal() override; |
102 | | |
103 | | Status CommitBatchInternal(WriteBatch* batch, size_t batch_cnt) override; |
104 | | |
105 | | // Since the data is already written to memtables at the Prepare phase, the |
106 | | // commit entails writing only a commit marker in the WAL. The sequence number |
107 | | // of the commit marker is then the commit timestamp of the transaction. To |
108 | | // make WAL commit markers visible, the snapshot will be based on the last seq |
109 | | // in the WAL that is also published, LastPublishedSequence, as opposed to the |
110 | | // last seq in the memtable. |
111 | | Status CommitInternal() override; |
112 | | |
113 | | Status RollbackInternal() override; |
114 | | |
115 | | Status ValidateSnapshot(ColumnFamilyHandle* column_family, const Slice& key, |
116 | | SequenceNumber* tracked_at_seq) override; |
117 | | |
118 | | Status RebuildFromWriteBatch(WriteBatch* src_batch) override; |
119 | | |
120 | | WritePreparedTxnDB* wpt_db_; |
121 | | // Number of sub-batches in prepare |
122 | | size_t prepare_batch_cnt_ = 0; |
123 | | }; |
124 | | |
125 | | } // namespace ROCKSDB_NAMESPACE |