write_prepared_txn.h 4.7 KB

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