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 17 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
6 changes: 6 additions & 0 deletions src/concurrency/timestamp_ordering_transaction_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ ResultType TimestampOrderingTransactionManager::CommitTransaction(
//// handle other isolation levels
//////////////////////////////////////////////////////////

if (!current_txn->IsWritten()) {
LOG_TRACE("Transaction not yet written, ending transaction.");
EndTransaction(current_txn);
Copy link
Member

@apavlo apavlo Jun 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just add a LOG_TRACE statement here to record what is happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

return ResultType::SUCCESS;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the transaction just drops an object, like an index? IsWritten will still return false since that doesn't touch the RWSet yet we'll directly commit the transaction without adding the objects to the gc_object_set. I'm not sure we support drop index yet, but I foresee problems with this.


auto storage_manager = storage::StorageManager::GetInstance();
auto &log_manager = logging::LogManager::GetInstance();

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
4 changes: 3 additions & 1 deletion src/traffic_cop/traffic_cop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ executor::ExecutionResult TrafficCop::ExecuteHelper(
// new txn, reset result status
curr_state.second = ResultType::SUCCESS;
single_statement_txn_ = true;
txn = txn_manager.BeginTransaction(thread_id);
// txn is read-only for single-statement select
bool read_only = statement_->GetQueryType() == QueryType::QUERY_SELECT;
txn = txn_manager.BeginTransaction(read_only, thread_id);
tcop_txn_state_.emplace(txn, ResultType::SUCCESS);
}

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