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).
15 #include <unordered_map>
18 #include "db/write_callback.h"
19 #include "rocksdb/db.h"
20 #include "rocksdb/slice.h"
21 #include "rocksdb/snapshot.h"
22 #include "rocksdb/status.h"
23 #include "rocksdb/types.h"
24 #include "rocksdb/utilities/transaction.h"
25 #include "rocksdb/utilities/transaction_db.h"
26 #include "rocksdb/utilities/write_batch_with_index.h"
27 #include "util/autovector.h"
28 #include "utilities/transactions/pessimistic_transaction.h"
29 #include "utilities/transactions/pessimistic_transaction_db.h"
30 #include "utilities/transactions/transaction_base.h"
31 #include "utilities/transactions/transaction_util.h"
33 namespace ROCKSDB_NAMESPACE
{
35 class WritePreparedTxnDB
;
37 // This impl could write to DB also uncommitted data and then later tell apart
38 // committed data from uncommitted data. Uncommitted data could be after the
39 // Prepare phase in 2PC (WritePreparedTxn) or before that
40 // (WriteUnpreparedTxnImpl).
41 class WritePreparedTxn
: public PessimisticTransaction
{
43 WritePreparedTxn(WritePreparedTxnDB
* db
, const WriteOptions
& write_options
,
44 const TransactionOptions
& txn_options
);
46 WritePreparedTxn(const WritePreparedTxn
&) = delete;
47 void operator=(const WritePreparedTxn
&) = delete;
49 virtual ~WritePreparedTxn() {}
51 // To make WAL commit markers visible, the snapshot will be based on the last
52 // seq in the WAL that is also published, LastPublishedSequence, as opposed to
53 // the last seq in the memtable.
54 using Transaction::Get
;
55 virtual Status
Get(const ReadOptions
& options
,
56 ColumnFamilyHandle
* column_family
, const Slice
& key
,
57 PinnableSlice
* value
) override
;
59 using Transaction::MultiGet
;
60 virtual void MultiGet(const ReadOptions
& options
,
61 ColumnFamilyHandle
* column_family
,
62 const size_t num_keys
, const Slice
* keys
,
63 PinnableSlice
* values
, Status
* statuses
,
64 const bool sorted_input
= false) override
;
66 // Note: The behavior is undefined in presence of interleaved writes to the
68 // To make WAL commit markers visible, the snapshot will be
69 // based on the last seq in the WAL that is also published,
70 // LastPublishedSequence, as opposed to the last seq in the memtable.
71 using Transaction::GetIterator
;
72 virtual Iterator
* GetIterator(const ReadOptions
& options
) override
;
73 virtual Iterator
* GetIterator(const ReadOptions
& options
,
74 ColumnFamilyHandle
* column_family
) override
;
76 virtual void SetSnapshot() override
;
79 void Initialize(const TransactionOptions
& txn_options
) override
;
80 // Override the protected SetId to make it visible to the friend class
82 inline void SetId(uint64_t id
) override
{ Transaction::SetId(id
); }
85 friend class WritePreparedTransactionTest_BasicRecoveryTest_Test
;
86 friend class WritePreparedTxnDB
;
87 friend class WriteUnpreparedTxnDB
;
88 friend class WriteUnpreparedTxn
;
90 Status
PrepareInternal() override
;
92 Status
CommitWithoutPrepareInternal() override
;
94 Status
CommitBatchInternal(WriteBatch
* batch
, size_t batch_cnt
) override
;
96 // Since the data is already written to memtables at the Prepare phase, the
97 // commit entails writing only a commit marker in the WAL. The sequence number
98 // of the commit marker is then the commit timestamp of the transaction. To
99 // make WAL commit markers visible, the snapshot will be based on the last seq
100 // in the WAL that is also published, LastPublishedSequence, as opposed to the
101 // last seq in the memtable.
102 Status
CommitInternal() override
;
104 Status
RollbackInternal() override
;
106 virtual Status
ValidateSnapshot(ColumnFamilyHandle
* column_family
,
108 SequenceNumber
* tracked_at_seq
) override
;
110 virtual Status
RebuildFromWriteBatch(WriteBatch
* src_batch
) override
;
112 WritePreparedTxnDB
* wpt_db_
;
113 // Number of sub-batches in prepare
114 size_t prepare_batch_cnt_
= 0;
117 } // namespace ROCKSDB_NAMESPACE
119 #endif // ROCKSDB_LITE