]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/transactions/write_prepared_txn.h
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / utilities / transactions / write_prepared_txn.h
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 #ifndef ROCKSDB_LITE
9
10 #include <algorithm>
11 #include <atomic>
12 #include <mutex>
13 #include <stack>
14 #include <string>
15 #include <unordered_map>
16 #include <vector>
17
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"
32
33 namespace rocksdb {
34
35 class WritePreparedTxnDB;
36
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 {
42 public:
43 WritePreparedTxn(WritePreparedTxnDB* db, const WriteOptions& write_options,
44 const TransactionOptions& txn_options);
45
46 virtual ~WritePreparedTxn() {}
47
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;
55
56 // Note: The behavior is undefined in presence of interleaved writes to the
57 // same transaction.
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;
65
66 virtual void SetSnapshot() override;
67
68 protected:
69 void Initialize(const TransactionOptions& txn_options) override;
70 // Override the protected SetId to make it visible to the friend class
71 // WritePreparedTxnDB
72 inline void SetId(uint64_t id) override { Transaction::SetId(id); }
73
74 private:
75 friend class WritePreparedTransactionTest_BasicRecoveryTest_Test;
76 friend class WritePreparedTxnDB;
77 friend class WriteUnpreparedTxnDB;
78 friend class WriteUnpreparedTxn;
79
80 Status PrepareInternal() override;
81
82 Status CommitWithoutPrepareInternal() override;
83
84 Status CommitBatchInternal(WriteBatch* batch, size_t batch_cnt) override;
85
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;
93
94 Status RollbackInternal() override;
95
96 virtual Status ValidateSnapshot(ColumnFamilyHandle* column_family,
97 const Slice& key,
98 SequenceNumber* tracked_at_seq) override;
99
100 virtual Status RebuildFromWriteBatch(WriteBatch* src_batch) override;
101
102 // No copying allowed
103 WritePreparedTxn(const WritePreparedTxn&);
104 void operator=(const WritePreparedTxn&);
105
106 WritePreparedTxnDB* wpt_db_;
107 // Number of sub-batches in prepare
108 size_t prepare_batch_cnt_ = 0;
109 };
110
111 } // namespace rocksdb
112
113 #endif // ROCKSDB_LITE