diff --git a/include/rocksdb/utilities/transaction.h b/include/rocksdb/utilities/transaction.h index 6c444ac26df..f272ed07753 100644 --- a/include/rocksdb/utilities/transaction.h +++ b/include/rocksdb/utilities/transaction.h @@ -139,7 +139,7 @@ class Transaction { Transaction(const Transaction&) = delete; void operator=(const Transaction&) = delete; - virtual ~Transaction() {} + virtual ~Transaction() = default; // If a transaction has a snapshot set, the transaction will ensure that // any keys successfully written(or fetched via GetForUpdate()) have not @@ -699,13 +699,13 @@ class Transaction { // output SST file to have two identical internal keys. virtual WriteBatch* GetCommitTimeWriteBatch() = 0; - virtual void SetLogNumber(uint64_t log) { log_number_ = log; } + virtual void SetLogNumber(uint64_t log) = 0; - virtual uint64_t GetLogNumber() const { return log_number_; } + virtual uint64_t GetLogNumber() const = 0; virtual Status SetName(const TransactionName& name) = 0; - virtual TransactionName GetName() const { return name_; } + virtual TransactionName GetName() const = 0; virtual TransactionID GetID() const { return 0; } @@ -729,8 +729,8 @@ class Transaction { LOCKS_STOLEN = 7, }; - TransactionState GetState() const { return txn_state_; } - void SetState(TransactionState state) { txn_state_ = state; } + virtual TransactionState GetState() const = 0; + virtual void SetState(TransactionState state) = 0; // NOTE: Experimental feature // The globally unique id with which the transaction is identified. This id @@ -738,7 +738,7 @@ class Transaction { // implementation decides the point in lifetime of a transaction at which it // assigns the id. Although currently it is the case, the id is not guaranteed // to remain the same across restarts. - uint64_t GetId() { return id_; } + virtual uint64_t GetId() = 0; virtual Status SetReadTimestampForValidation(TxnTimestamp /*ts*/) { return Status::NotSupported("timestamp not supported"); @@ -751,22 +751,11 @@ class Transaction { virtual TxnTimestamp GetCommitTimestamp() const { return kMaxTxnTimestamp; } protected: - explicit Transaction(const TransactionDB* /*db*/) {} - Transaction() : log_number_(0), txn_state_(STARTED) {} + Transaction() = default; - // the log in which the prepared section for this txn resides - // (for two phase commit) - uint64_t log_number_; - TransactionName name_; + virtual void SetId(uint64_t id) = 0; - // Execution status of the transaction. - std::atomic txn_state_; - - uint64_t id_ = 0; - virtual void SetId(uint64_t id) { - assert(id_ == 0); - id_ = id; - } + virtual uint64_t GetLastLogNumber() const = 0; virtual Status GetImpl(const ReadOptions& /* options */, ColumnFamilyHandle* /* column_family */, @@ -783,8 +772,6 @@ class Transaction { return s; } - virtual uint64_t GetLastLogNumber() const { return log_number_; } - private: friend class PessimisticTransactionDB; friend class WriteUnpreparedTxnDB; diff --git a/unreleased_history/public_api_changes/transaction_interface.md b/unreleased_history/public_api_changes/transaction_interface.md new file mode 100644 index 00000000000..e769dd1b468 --- /dev/null +++ b/unreleased_history/public_api_changes/transaction_interface.md @@ -0,0 +1 @@ +The `Transaction` class is now an abstract interface class with no data members, and the methods `Transaction::{SetLogNumber, GetLogNumber, GetName, GetState, SetState, GetId, SetId, GetLastLogNumber}` are now accordingly pure virtuals implemented further down in the class hierarchy. diff --git a/utilities/transactions/transaction_base.cc b/utilities/transactions/transaction_base.cc index 063cc02b44e..f41cafd7bff 100644 --- a/utilities/transactions/transaction_base.cc +++ b/utilities/transactions/transaction_base.cc @@ -74,7 +74,6 @@ TransactionBaseImpl::TransactionBaseImpl( 0 /* default_cf_ts_sz */), indexing_enabled_(true) { assert(dynamic_cast(db_) != nullptr); - log_number_ = 0; if (dbimpl_->allow_2pc()) { InitWriteBatch(); } diff --git a/utilities/transactions/transaction_base.h b/utilities/transactions/transaction_base.h index 888ea174463..d36d7cfbde2 100644 --- a/utilities/transactions/transaction_base.h +++ b/utilities/transactions/transaction_base.h @@ -309,9 +309,27 @@ class TransactionBaseImpl : public Transaction { WriteBatch* GetCommitTimeWriteBatch() override; + void SetLogNumber(uint64_t log) override { log_number_ = log; } + + uint64_t GetLogNumber() const override { return log_number_; } + + TransactionName GetName() const override { return name_; } + + TransactionState GetState() const override { return txn_state_; } + void SetState(TransactionState state) override { txn_state_ = state; } + + uint64_t GetId() override { return id_; } + LockTracker& GetTrackedLocks() { return *tracked_locks_; } protected: + void SetId(uint64_t id) override { + assert(id_ == 0); + id_ = id; + } + + uint64_t GetLastLogNumber() const override { return log_number_; } + template std::unique_ptr NewMultiCfIterator( @@ -380,6 +398,17 @@ class TransactionBaseImpl : public Transaction { const LockTrackerFactory& lock_tracker_factory_; + // the log in which the prepared section for this txn resides + // (for two phase commit) + uint64_t log_number_ = 0; + + TransactionName name_; + + // Execution status of the transaction. + std::atomic txn_state_{STARTED}; + + uint64_t id_ = 0; + // Stores that time the txn was constructed, in microseconds. uint64_t start_time_; diff --git a/utilities/transactions/write_prepared_txn.h b/utilities/transactions/write_prepared_txn.h index aca6a19ea08..262dff8fd6d 100644 --- a/utilities/transactions/write_prepared_txn.h +++ b/utilities/transactions/write_prepared_txn.h @@ -84,7 +84,7 @@ class WritePreparedTxn : public PessimisticTransaction { void Initialize(const TransactionOptions& txn_options) override; // Override the protected SetId to make it visible to the friend class // WritePreparedTxnDB - inline void SetId(uint64_t id) override { Transaction::SetId(id); } + void SetId(uint64_t id) override { TransactionBaseImpl::SetId(id); } private: friend class WritePreparedTransactionTest_BasicRecoveryTest_Test;