write_prepared_txn.h 4.4 KB

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