Skip to content
This repository has been archived by the owner on Sep 27, 2019. It is now read-only.

Read-Only TxnContext Interface; Read-Only (single-statement select, txn) optimizations #1402

Open
wants to merge 19 commits into
base: master
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
8 changes: 8 additions & 0 deletions src/concurrency/timestamp_ordering_transaction_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ ResultType TimestampOrderingTransactionManager::CommitTransaction(
gc_object_set->emplace_back(database_oid, table_oid, index_oid);
}

// see if we can end early
if (!current_txn->IsWritten()) {
LOG_TRACE("Transaction not yet written, ending transaction.");
log_manager.StopLogging();
EndTransaction(current_txn);
return ResultType::SUCCESS;
}

// install everything.
// 1. install a new version for update operations;
// 2. install an empty version for delete operations;
Expand Down
12 changes: 7 additions & 5 deletions src/concurrency/transaction_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ namespace concurrency {
* i : insert
*/

TransactionContext::TransactionContext(const size_t thread_id,
TransactionContext::TransactionContext(bool read_only, const size_t thread_id,
const IsolationLevelType isolation,
const cid_t &read_id) {
Init(thread_id, isolation, read_id);
Init(read_only, thread_id, isolation, read_id);
}

TransactionContext::TransactionContext(const size_t thread_id,
TransactionContext::TransactionContext(bool read_only, const size_t thread_id,
const IsolationLevelType isolation,
const cid_t &read_id,
const cid_t &commit_id) {
Init(thread_id, isolation, read_id, commit_id);
Init(read_only, thread_id, isolation, read_id, commit_id);
}

void TransactionContext::Init(const size_t thread_id,
void TransactionContext::Init(bool read_only, const size_t thread_id,
const IsolationLevelType isolation,
const cid_t &read_id, const cid_t &commit_id) {
read_only_ = read_only;

read_id_ = read_id;

// commit id can be set at a transaction's commit phase.
Expand Down
13 changes: 5 additions & 8 deletions src/concurrency/transaction_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ConflictAvoidanceType TransactionManager::conflict_avoidance_ =
ConflictAvoidanceType::ABORT;

TransactionContext *TransactionManager::BeginTransaction(
const size_t thread_id, const IsolationLevelType type, bool read_only) {
bool read_only, const size_t thread_id, const IsolationLevelType type) {
TransactionContext *txn = nullptr;

if (type == IsolationLevelType::SNAPSHOT) {
Expand All @@ -45,9 +45,10 @@ TransactionContext *TransactionManager::BeginTransaction(
cid_t commit_id = EpochManagerFactory::GetInstance().EnterEpoch(
thread_id, TimestampType::COMMIT);

txn = new TransactionContext(thread_id, type, read_id, commit_id);
txn = new TransactionContext(read_only, thread_id, type, read_id,
commit_id);
} else {
txn = new TransactionContext(thread_id, type, read_id);
txn = new TransactionContext(read_only, thread_id, type, read_id);
}

} else {
Expand All @@ -58,11 +59,7 @@ TransactionContext *TransactionManager::BeginTransaction(
// transaction processing with decentralized epoch manager
cid_t read_id = EpochManagerFactory::GetInstance().EnterEpoch(
thread_id, TimestampType::READ);
txn = new TransactionContext(thread_id, type, read_id);
}

if (read_only) {
txn->SetReadOnly();
txn = new TransactionContext(read_only, thread_id, type, read_id);
}

txn->SetTimestamp(function::DateFunctions::Now());
Expand Down
27 changes: 14 additions & 13 deletions src/include/concurrency/transaction_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,27 @@ class TransactionContext : public Printable {
TransactionContext(TransactionContext const &) = delete;

public:
TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
const cid_t &read_id);
TransactionContext(bool read_only, const size_t thread_id,
const IsolationLevelType isolation, const cid_t &read_id);

TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
const cid_t &read_id, const cid_t &commit_id);
TransactionContext(bool read_only, const size_t thread_id,
const IsolationLevelType isolation, const cid_t &read_id,
const cid_t &commit_id);

/**
* @brief Destroys the object.
*/
~TransactionContext() = default;

private:
void Init(const size_t thread_id, const IsolationLevelType isolation,
const cid_t &read_id) {
Init(thread_id, isolation, read_id, read_id);
void Init(bool read_only, const size_t thread_id,
const IsolationLevelType isolation, const cid_t &read_id) {
Init(read_only, thread_id, isolation, read_id, read_id);
}

void Init(const size_t thread_id, const IsolationLevelType isolation,
const cid_t &read_id, const cid_t &commit_id);
void Init(bool read_only, const size_t thread_id,
const IsolationLevelType isolation, const cid_t &read_id,
const cid_t &commit_id);

public:
//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -264,12 +266,11 @@ class TransactionContext : public Printable {
}

/**
* @brief mark this context as read only
* @brief Determines if already written.
*
* @return True if already written, False otherwise.
*/
void SetReadOnly() {
read_only_ = true;
}
bool IsWritten() const { return is_written_; }

/**
* @brief Gets the isolation level.
Expand Down
14 changes: 10 additions & 4 deletions src/include/concurrency/transaction_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,19 @@ class TransactionManager {
current_txn->SetResult(result);
}

TransactionContext *BeginTransaction() { return BeginTransaction(false); }

TransactionContext *BeginTransaction(const size_t thread_id) {
return BeginTransaction(false, thread_id, isolation_level_);
}

TransactionContext *BeginTransaction(const IsolationLevelType type) {
return BeginTransaction(0, type, false);
return BeginTransaction(false, 0, type);
}

TransactionContext *BeginTransaction(const size_t thread_id = 0,
const IsolationLevelType type = isolation_level_,
bool read_only = false);
TransactionContext *BeginTransaction(
bool read_only, const size_t thread_id = 0,
const IsolationLevelType type = isolation_level_);

/**
* @brief Ends a transaction.
Expand Down
1 change: 1 addition & 0 deletions test/executor/copy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ TEST_F(CopyTests, Copying) {
std::vector<ResultValue> result;

TestingSQLUtil::counter_.store(1);
traffic_cop.SetStatement(statement);
executor::ExecutionResult status = traffic_cop.ExecuteHelper(
statement->GetPlanTree(), params, result, result_format);

Expand Down
3 changes: 2 additions & 1 deletion test/include/concurrency/testing_transaction_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ class TransactionThread {
if (cur_seq == 0) {
if (schedule->declared_ro == true) {
/** starts a read only transaction*/
txn = txn_manager->BeginTransaction(0, IsolationLevelType::SNAPSHOT, true);
txn = txn_manager->BeginTransaction(true, 0,
IsolationLevelType::SNAPSHOT);
} else {
txn = txn_manager->BeginTransaction();
}
Expand Down