]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/transactions/write_prepared_txn.h
import quincy beta 17.1.0
[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_NAMESPACE {
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 // No copying allowed
46 WritePreparedTxn(const WritePreparedTxn&) = delete;
47 void operator=(const WritePreparedTxn&) = delete;
48
49 virtual ~WritePreparedTxn() {}
50
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;
58
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;
65
66 // Note: The behavior is undefined in presence of interleaved writes to the
67 // same transaction.
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;
75
76 virtual void SetSnapshot() override;
77
78 protected:
79 void Initialize(const TransactionOptions& txn_options) override;
80 // Override the protected SetId to make it visible to the friend class
81 // WritePreparedTxnDB
82 inline void SetId(uint64_t id) override { Transaction::SetId(id); }
83
84 private:
85 friend class WritePreparedTransactionTest_BasicRecoveryTest_Test;
86 friend class WritePreparedTxnDB;
87 friend class WriteUnpreparedTxnDB;
88 friend class WriteUnpreparedTxn;
89
90 Status PrepareInternal() override;
91
92 Status CommitWithoutPrepareInternal() override;
93
94 Status CommitBatchInternal(WriteBatch* batch, size_t batch_cnt) override;
95
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;
103
104 Status RollbackInternal() override;
105
106 virtual Status ValidateSnapshot(ColumnFamilyHandle* column_family,
107 const Slice& key,
108 SequenceNumber* tracked_at_seq) override;
109
110 virtual Status RebuildFromWriteBatch(WriteBatch* src_batch) override;
111
112 WritePreparedTxnDB* wpt_db_;
113 // Number of sub-batches in prepare
114 size_t prepare_batch_cnt_ = 0;
115 };
116
117 } // namespace ROCKSDB_NAMESPACE
118
119 #endif // ROCKSDB_LITE