Remove redundant transaction from index creation

Reviewers: florijan

Reviewed By: florijan

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1011
This commit is contained in:
Dominik Gleich 2017-11-27 14:57:45 +01:00
parent 73c1206e81
commit a7f9255c17
3 changed files with 18 additions and 16 deletions

View File

@ -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

View File

@ -100,6 +100,15 @@ class Engine {
return snapshot_copy;
}
/**
* Returns active transactions.
*/
Snapshot ActiveTransactions() {
std::lock_guard<SpinLock> 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) {

View File

@ -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) {