diff --git a/src/box/sophia_engine.cc b/src/box/sophia_engine.cc index 9124597012361f0648c080836ae4c9eff57c3810..f65f50ade8fb95a1c144c0b40bbdffd5e7c8a9b0 100644 --- a/src/box/sophia_engine.cc +++ b/src/box/sophia_engine.cc @@ -113,7 +113,6 @@ SophiaEngine::SophiaEngine() { flags = ENGINE_TRANSACTIONAL; env = NULL; - tx = NULL; recovery.state = READY_NO_KEYS; recovery.recover = sophia_recovery_begin_snapshot; recovery.replace = sophia_replace_recover; @@ -258,22 +257,22 @@ SophiaEngine::begin(struct txn *txn, struct space *space) { assert(space->handler->engine == this); if (txn->n_stmts == 1) { - assert(tx == NULL); + assert(txn->engine_tx == NULL); SophiaIndex *index = (SophiaIndex *)index_find(space, 0); (void) index; assert(index->db != NULL); - tx = sp_begin(env); - if (tx == NULL) + txn->engine_tx = sp_begin(env); + if (txn->engine_tx == NULL) sophia_raise(env); return; } - assert(tx != NULL); + assert(txn->engine_tx != NULL); } void SophiaEngine::commit(struct txn *txn) { - if (tx == NULL) + if (txn->engine_tx == NULL) return; /* free involved tuples */ struct txn_stmt *stmt; @@ -284,28 +283,28 @@ SophiaEngine::commit(struct txn *txn) tuple_unref(stmt->new_tuple); } auto scoped_guard = make_scoped_guard([=] { - tx = NULL; + txn->engine_tx = NULL; }); /* commit transaction using transaction * commit signature */ assert(txn->signature >= 0); - int rc = sp_prepare(tx, txn->signature); + int rc = sp_prepare(txn->engine_tx, txn->signature); assert(rc == 0); if (rc == -1) sophia_raise(env); - rc = sp_commit(tx); + rc = sp_commit(txn->engine_tx); if (rc == -1) sophia_raise(env); assert(rc == 0); } void -SophiaEngine::rollback(struct txn *) +SophiaEngine::rollback(struct txn *txn) { - if (tx == NULL) + if (txn->engine_tx == NULL) return; - sp_rollback(tx); - tx = NULL; + sp_rollback(txn->engine_tx); + txn->engine_tx = NULL; } static inline void diff --git a/src/box/sophia_engine.h b/src/box/sophia_engine.h index 5dbca59607f7af75d7fc60d8e2a948d28378d4d9..33db324e68263c5c85e8ab4ec02ff043ba986aba 100644 --- a/src/box/sophia_engine.h +++ b/src/box/sophia_engine.h @@ -48,7 +48,6 @@ struct SophiaEngine: public Engine { virtual void commit_checkpoint(); virtual void abort_checkpoint(); void *env; - void *tx; private: int64_t m_prev_checkpoint_lsn; int64_t m_checkpoint_lsn; diff --git a/src/box/sophia_index.cc b/src/box/sophia_index.cc index 169a9a23ab89ae2b37c4020abc1832b7be5f5090..6d5ae29ae3ef9761921a43c48f64f273f842525e 100644 --- a/src/box/sophia_index.cc +++ b/src/box/sophia_index.cc @@ -33,6 +33,7 @@ #include "errinj.h" #include "schema.h" #include "space.h" +#include "txn.h" #include "cfg.h" #include "sophia_engine.h" #include <sys/stat.h> @@ -83,15 +84,15 @@ sophia_replace_recover(struct space *space, enum dup_replace_mode) { SophiaIndex *index = (SophiaIndex*)index_find(space, 0); - SophiaEngine *engine = (SophiaEngine *)space->handler->engine; - assert(engine->tx != NULL); + struct txn *txn = in_txn(); + assert(txn != NULL && txn->engine_tx != NULL); int rc; if (old_tuple) { - rc = sophia_index_stmt(engine->tx, index->db, 1, + rc = sophia_index_stmt(txn->engine_tx, index->db, 1, index->key_def, old_tuple); } else { - rc = sophia_index_stmt(engine->tx, index->db, 0, + rc = sophia_index_stmt(txn->engine_tx, index->db, 0, index->key_def, new_tuple); } @@ -268,9 +269,8 @@ SophiaIndex::replace(struct tuple *old_tuple, struct tuple *new_tuple, enum dup_replace_mode mode) { struct space *space = space_cache_find(key_def->space_id); - SophiaEngine *engine = - (SophiaEngine *)space->handler->engine; - assert(engine->tx != NULL); + struct txn *txn = in_txn(); + assert(txn != NULL && txn->engine_tx != NULL); /* do not involve in tarantool transaction regarding old_tuple, * always return NULL. @@ -280,7 +280,8 @@ SophiaIndex::replace(struct tuple *old_tuple, struct tuple *new_tuple, int rc; if (old_tuple && new_tuple == NULL) { assert(old_tuple != NULL); - rc = sophia_index_stmt(engine->tx, db, 1, key_def, old_tuple); + rc = sophia_index_stmt(txn->engine_tx, db, 1, key_def, + old_tuple); if (rc == -1) sophia_raise(env); return NULL; @@ -289,7 +290,8 @@ SophiaIndex::replace(struct tuple *old_tuple, struct tuple *new_tuple, /* update */ if (old_tuple && new_tuple) { /* assume no primary key update is supported */ - rc = sophia_index_stmt(engine->tx, db, 0, key_def, new_tuple); + rc = sophia_index_stmt(txn->engine_tx, db, 0, key_def, + new_tuple); if (rc == -1) sophia_raise(env); return NULL; @@ -303,7 +305,8 @@ SophiaIndex::replace(struct tuple *old_tuple, struct tuple *new_tuple, mp_next(&keyptr); size_t keysize = keyptr - key; struct tuple *dup_tuple = - sophia_index_get(env, db, engine->tx, key, keysize, space->format); + sophia_index_get(env, db, txn->engine_tx, key, + keysize, space->format); if (dup_tuple) { tuple_ref(dup_tuple); int error = 0; @@ -316,7 +319,8 @@ SophiaIndex::replace(struct tuple *old_tuple, struct tuple *new_tuple, } } case DUP_REPLACE_OR_INSERT: - rc = sophia_index_stmt(engine->tx, db, 0, key_def, new_tuple); + rc = sophia_index_stmt(txn->engine_tx, db, 0, key_def, + new_tuple); if (rc == -1) sophia_raise(env); break; @@ -438,16 +442,11 @@ SophiaIndex::initIterator(struct iterator *ptr, it->db = db; it->space = space_cache_find(key_def->space_id); it->tx = NULL; - SophiaEngine *engine = - (SophiaEngine *)it->space->handler->engine; - /* - it->tx = engine->tx; - */ const char *compare; switch (type) { case ITER_EQ: it->base.next = sophia_iterator_eq; - it->tx = engine->tx; + it->tx = in_txn() ? in_txn()->engine_tx : NULL; return; case ITER_ALL: case ITER_GE: compare = ">="; diff --git a/src/box/txn.h b/src/box/txn.h index 1f2122c7677370a4c7dd9fb62d0bbc2890016e5c..f2158262e07e61cdebbd88da534d993b62067033 100644 --- a/src/box/txn.h +++ b/src/box/txn.h @@ -69,6 +69,8 @@ struct txn { bool autocommit; /** Engine involved in multi-statement transaction. */ Engine *engine; + /** Engine-specific transaction data */ + void *engine_tx; /** Triggers on fiber yield and stop to abort transaction for in-memory engine */ struct trigger fiber_on_yield, fiber_on_stop; };