Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn Transaction into a pure interface class #13153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 10 additions & 23 deletions include/rocksdb/utilities/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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; }

Expand All @@ -729,16 +729,16 @@ 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
// might or might not be set depending on the implementation. Similarly the
// 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");
Expand All @@ -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<TransactionState> 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 */,
Expand All @@ -783,8 +772,6 @@ class Transaction {
return s;
}

virtual uint64_t GetLastLogNumber() const { return log_number_; }

private:
friend class PessimisticTransactionDB;
friend class WriteUnpreparedTxnDB;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
1 change: 0 additions & 1 deletion utilities/transactions/transaction_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ TransactionBaseImpl::TransactionBaseImpl(
0 /* default_cf_ts_sz */),
indexing_enabled_(true) {
assert(dynamic_cast<DBImpl*>(db_) != nullptr);
log_number_ = 0;
if (dbimpl_->allow_2pc()) {
InitWriteBatch();
}
Expand Down
29 changes: 29 additions & 0 deletions utilities/transactions/transaction_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename IterType, typename ImplType,
typename ErrorIteratorFuncType>
std::unique_ptr<IterType> NewMultiCfIterator(
Expand Down Expand Up @@ -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<TransactionState> txn_state_{STARTED};

uint64_t id_ = 0;

// Stores that time the txn was constructed, in microseconds.
uint64_t start_time_;

Expand Down
2 changes: 1 addition & 1 deletion utilities/transactions/write_prepared_txn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading