From a7f9255c17bc443ac6e573b911536dfceca853b1 Mon Sep 17 00:00:00 2001 From: Dominik Gleich Date: Mon, 27 Nov 2017 14:57:45 +0100 Subject: [PATCH] Remove redundant transaction from index creation Reviewers: florijan Reviewed By: florijan Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D1011 --- src/database/graph_db_accessor.cpp | 11 +++-------- src/transactions/engine.hpp | 9 +++++++++ tests/unit/durability.cpp | 14 ++++++-------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/database/graph_db_accessor.cpp b/src/database/graph_db_accessor.cpp index ebc374573..1789c7777 100644 --- a/src/database/graph_db_accessor.cpp +++ b/src/database/graph_db_accessor.cpp @@ -124,19 +124,14 @@ void GraphDbAccessor::BuildIndex(const GraphDbTypes::Label &label, // happened earlier. We have to first wait for every transaction that // happend before, or a bit later than CreateIndex to end. { - auto wait_transaction = db_.tx_engine_.Begin(); - for (auto id : wait_transaction->snapshot()) { + auto wait_transactions = db_.tx_engine_.ActiveTransactions(); + for (auto id : wait_transactions) { if (id == transaction_->id_) continue; - while (wait_transaction->engine_.IsActive(id)) { + while (db_.tx_engine_.IsActive(id)) { // TODO reconsider this constant, currently rule-of-thumb chosen std::this_thread::sleep_for(std::chrono::microseconds(100)); } } - // We must notify the WAL about this transaction manually since it's not - // handled by a GraphDbAccessor. - db_.wal_.TxBegin(wait_transaction->id_); - db_.wal_.TxCommit(wait_transaction->id_); - wait_transaction->Commit(); } // This accessor's transaction surely sees everything that happened before diff --git a/src/transactions/engine.hpp b/src/transactions/engine.hpp index 33565cf07..082de47b9 100644 --- a/src/transactions/engine.hpp +++ b/src/transactions/engine.hpp @@ -100,6 +100,15 @@ class Engine { return snapshot_copy; } + /** + * Returns active transactions. + */ + Snapshot ActiveTransactions() { + std::lock_guard guard(lock_); + Snapshot active_transactions = active_; + return active_transactions; + } + /** Comits the given transaction. Deletes the transaction object, it's not * valid after this function executes. */ void Commit(const Transaction &t) { diff --git a/tests/unit/durability.cpp b/tests/unit/durability.cpp index 8e23a022d..d8c033794 100644 --- a/tests/unit/durability.cpp +++ b/tests/unit/durability.cpp @@ -338,7 +338,7 @@ TEST_F(Durability, WalEncoding) { } } reader.Close(); - ASSERT_EQ(ops.size(), 13); + ASSERT_EQ(ops.size(), 11); using Type = durability::WriteAheadLog::Op::Type; EXPECT_EQ(ops[0].type_, Type::TRANSACTION_BEGIN); @@ -369,16 +369,14 @@ TEST_F(Durability, WalEncoding) { EXPECT_EQ(ops[6].edge_id_, 0); EXPECT_EQ(ops[6].property_, "p0"); EXPECT_EQ(ops[6].value_.type(), PropertyValue::Type::List); - // The next four ops are the BuildIndex internal transactions. + // The next two ops are the BuildIndex internal transactions. EXPECT_EQ(ops[7].type_, Type::TRANSACTION_BEGIN); EXPECT_EQ(ops[8].type_, Type::TRANSACTION_COMMIT); - EXPECT_EQ(ops[9].type_, Type::TRANSACTION_BEGIN); + EXPECT_EQ(ops[9].type_, Type::BUILD_INDEX); + EXPECT_EQ(ops[9].label_, "l1"); + EXPECT_EQ(ops[9].property_, "p1"); EXPECT_EQ(ops[10].type_, Type::TRANSACTION_COMMIT); - EXPECT_EQ(ops[11].type_, Type::BUILD_INDEX); - EXPECT_EQ(ops[11].label_, "l1"); - EXPECT_EQ(ops[11].property_, "p1"); - EXPECT_EQ(ops[12].type_, Type::TRANSACTION_COMMIT); - EXPECT_EQ(ops[12].transaction_id_, 1); + EXPECT_EQ(ops[10].transaction_id_, 1); } TEST_F(Durability, SnapshotEncoding) {