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"
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 virtual ~WritePreparedTxn() {}
48 // To make WAL commit markers visible, the snapshot will be based on the last
49 // seq in the WAL that is also published, LastPublishedSequence, as opposed to
50 // the last seq in the memtable.
51 using Transaction::Get
;
52 virtual Status
Get(const ReadOptions
& options
,
53 ColumnFamilyHandle
* column_family
, const Slice
& key
,
54 PinnableSlice
* value
) override
;
56 // Note: The behavior is undefined in presence of interleaved writes to the
58 // To make WAL commit markers visible, the snapshot will be
59 // based on the last seq in the WAL that is also published,
60 // LastPublishedSequence, as opposed to the last seq in the memtable.
61 using Transaction::GetIterator
;
62 virtual Iterator
* GetIterator(const ReadOptions
& options
) override
;
63 virtual Iterator
* GetIterator(const ReadOptions
& options
,
64 ColumnFamilyHandle
* column_family
) override
;
66 virtual void SetSnapshot() override
;
69 void Initialize(const TransactionOptions
& txn_options
) override
;
70 // Override the protected SetId to make it visible to the friend class
72 inline void SetId(uint64_t id
) override
{ Transaction::SetId(id
); }
75 friend class WritePreparedTransactionTest_BasicRecoveryTest_Test
;
76 friend class WritePreparedTxnDB
;
77 friend class WriteUnpreparedTxnDB
;
78 friend class WriteUnpreparedTxn
;
80 Status
PrepareInternal() override
;
82 Status
CommitWithoutPrepareInternal() override
;
84 Status
CommitBatchInternal(WriteBatch
* batch
, size_t batch_cnt
) override
;
86 // Since the data is already written to memtables at the Prepare phase, the
87 // commit entails writing only a commit marker in the WAL. The sequence number
88 // of the commit marker is then the commit timestamp of the transaction. To
89 // make WAL commit markers visible, the snapshot will be based on the last seq
90 // in the WAL that is also published, LastPublishedSequence, as opposed to the
91 // last seq in the memtable.
92 Status
CommitInternal() override
;
94 Status
RollbackInternal() override
;
96 virtual Status
ValidateSnapshot(ColumnFamilyHandle
* column_family
,
98 SequenceNumber
* tracked_at_seq
) override
;
100 virtual Status
RebuildFromWriteBatch(WriteBatch
* src_batch
) override
;
102 // No copying allowed
103 WritePreparedTxn(const WritePreparedTxn
&);
104 void operator=(const WritePreparedTxn
&);
106 WritePreparedTxnDB
* wpt_db_
;
107 // Number of sub-batches in prepare
108 size_t prepare_batch_cnt_
= 0;
111 } // namespace rocksdb
113 #endif // ROCKSDB_LITE