From 2763a7f64a590aece865f9ffbdf287eec95c49fd Mon Sep 17 00:00:00 2001
From: AnastasMIPT <AtosBel@ya.ru>
Date: Fri, 17 Sep 2021 12:49:46 +0300
Subject: [PATCH] memtx: introduce box.txn_id() function

New function box.txn_id(), which returns the id of the current transaction if
called within a transaction, nil otherwise.

Closes #6396

@TarantoolBot document
Title: new function box.txn_id()
New function in module box:  box.txn_id(), which returns the id of the current
transaction if called within a transaction, nil otherwise.
---
 .../gh-6396-transaction-public-id.md          |  3 +
 src/box/lua/schema.lua                        |  8 +++
 test/box/misc.result                          |  1 +
 test/box/tx_man.result                        | 35 ++++++++++
 test/box/tx_man.test.lua                      | 11 +++
 test/engine/transaction.result                | 68 +++++++++++++++++++
 test/engine/transaction.test.lua              | 23 +++++++
 7 files changed, 149 insertions(+)
 create mode 100644 changelogs/unreleased/gh-6396-transaction-public-id.md

diff --git a/changelogs/unreleased/gh-6396-transaction-public-id.md b/changelogs/unreleased/gh-6396-transaction-public-id.md
new file mode 100644
index 0000000000..a39bc03e61
--- /dev/null
+++ b/changelogs/unreleased/gh-6396-transaction-public-id.md
@@ -0,0 +1,3 @@
+## feature/core
+
+* Added new function box.txn_id(), which returns the id of the current transaction if called within a transaction, nil otherwise.
\ No newline at end of file
diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index e60053a8a0..1fe2e1c5c0 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -336,6 +336,14 @@ end
 
 box.is_in_txn = builtin.box_txn
 
+box.txn_id = function()
+    local id = builtin.box_txn_id()
+    if -1 == id then
+        return nil
+    end
+    return tonumber(id)
+end
+
 box.savepoint = function()
     local csavepoint = builtin.box_txn_savepoint()
     if csavepoint == nil then
diff --git a/test/box/misc.result b/test/box/misc.result
index c86245914b..0a33509747 100644
--- a/test/box/misc.result
+++ b/test/box/misc.result
@@ -103,6 +103,7 @@ t
   - space
   - stat
   - tuple
+  - txn_id
   - unprepare
 ...
 t = nil
diff --git a/test/box/tx_man.result b/test/box/tx_man.result
index 1ed4eb403b..4a92614fc8 100644
--- a/test/box/tx_man.result
+++ b/test/box/tx_man.result
@@ -3784,6 +3784,41 @@ s:drop()
  | ---
  | ...
 
+-- https://github.com/tarantool/tarantool/issues/6396
+
+tx1:begin()
+ | ---
+ | - 
+ | ...
+tx1("tx1_id = box.txn_id()")
+ | ---
+ | - 
+ | ...
+tx2:begin()
+ | ---
+ | - 
+ | ...
+tx2("tx2_id = box.txn_id()")
+ | ---
+ | - 
+ | ...
+assert(tx1_id ~= tx2_id)
+ | ---
+ | - true
+ | ...
+tx2:commit()
+ | ---
+ | - 
+ | ...
+tx1("assert(tx1_id  == box.txn_id())")
+ | ---
+ | - - true
+ | ...
+tx1:commit()
+ | ---
+ | - 
+ | ...
+
 test_run:cmd("switch default")
  | ---
  | - true
diff --git a/test/box/tx_man.test.lua b/test/box/tx_man.test.lua
index a2b2061f32..f2c2ca6cad 100644
--- a/test/box/tx_man.test.lua
+++ b/test/box/tx_man.test.lua
@@ -1227,6 +1227,17 @@ s.index.sk:select()
 
 s:drop()
 
+-- https://github.com/tarantool/tarantool/issues/6396
+
+tx1:begin()
+tx1("tx1_id = box.txn_id()")
+tx2:begin()
+tx2("tx2_id = box.txn_id()")
+assert(tx1_id ~= tx2_id)
+tx2:commit()
+tx1("assert(tx1_id  == box.txn_id())")
+tx1:commit()
+
 test_run:cmd("switch default")
 test_run:cmd("stop server tx_man")
 test_run:cmd("cleanup server tx_man")
diff --git a/test/engine/transaction.result b/test/engine/transaction.result
index 910abb58ac..6650838528 100644
--- a/test/engine/transaction.result
+++ b/test/engine/transaction.result
@@ -527,3 +527,71 @@ stmts
 s:drop()
 ---
 ...
+-- https://github.com/tarantool/tarantool/issues/6396
+assert(not box.txn_id())
+---
+- true
+...
+box.begin()
+---
+...
+tx1_id = box.txn_id()
+---
+...
+assert(type(tx1_id) == "number")
+---
+- true
+...
+assert(tx1_id == box.txn_id())
+---
+- true
+...
+box.commit()
+---
+...
+box.begin()
+---
+...
+tx2_id = box.txn_id()
+---
+...
+assert(type(tx2_id) == "number")
+---
+- true
+...
+assert(tx1_id ~= tx2_id)
+---
+- true
+...
+box.commit()
+---
+...
+box.begin()
+---
+...
+tx3_id = box.txn_id()
+---
+...
+assert(type(tx2_id) == "number")
+---
+- true
+...
+assert(tx3_id ~= tx1_id)
+---
+- true
+...
+assert(tx3_id ~= tx2_id)
+---
+- true
+...
+assert(tx3_id == box.txn_id())
+---
+- true
+...
+box.commit()
+---
+...
+assert(not box.txn_id())
+---
+- true
+...
diff --git a/test/engine/transaction.test.lua b/test/engine/transaction.test.lua
index 51756f9333..8f331b2c48 100644
--- a/test/engine/transaction.test.lua
+++ b/test/engine/transaction.test.lua
@@ -228,3 +228,26 @@ box.begin() box.on_commit(on_commit) box.commit()
 stmts
 
 s:drop()
+
+-- https://github.com/tarantool/tarantool/issues/6396
+
+assert(not box.txn_id())
+box.begin()
+tx1_id = box.txn_id()
+assert(type(tx1_id) == "number")
+assert(tx1_id == box.txn_id())
+box.commit()
+box.begin()
+tx2_id = box.txn_id()
+assert(type(tx2_id) == "number")
+assert(tx1_id ~= tx2_id)
+box.commit()
+box.begin()
+tx3_id = box.txn_id()
+assert(type(tx2_id) == "number")
+assert(tx3_id ~= tx1_id)
+assert(tx3_id ~= tx2_id)
+assert(tx3_id == box.txn_id())
+box.commit()
+assert(not box.txn_id())
+
-- 
GitLab