From b4e8eb0ab757a5d04c668da69056f98ef36073bf Mon Sep 17 00:00:00 2001 From: andreyaksenov Date: Fri, 21 Jul 2023 10:39:09 +0300 Subject: [PATCH] box.atomic tnx_isolation --- .../test/transactions/box_atomic_test.lua | 25 +++++++++++++------ .../test/transactions/box_commit_test.lua | 12 ++++++--- .../box_txn_management/atomic.rst | 15 ++++++++--- .../box_txn_management/begin.rst | 9 ++++--- .../box_txn_management/commit.rst | 2 +- 5 files changed, 45 insertions(+), 18 deletions(-) diff --git a/doc/code_snippets/test/transactions/box_atomic_test.lua b/doc/code_snippets/test/transactions/box_atomic_test.lua index 9cd85e4876..46c0c7167b 100644 --- a/doc/code_snippets/test/transactions/box_atomic_test.lua +++ b/doc/code_snippets/test/transactions/box_atomic_test.lua @@ -15,7 +15,6 @@ g.before_each(function(cg) { name = 'band_name', type = 'string' }, { name = 'year', type = 'unsigned' } }) - box.space.bands:create_index('primary', { parts = { 'id' } }) end) end) @@ -26,18 +25,28 @@ end) g.test_space_is_updated = function(cg) cg.server:exec(function() + -- Create an index with the specified sequence -- + box.schema.sequence.create('id_sequence', { min = 1 }) + box.space.bands:create_index('primary', { parts = { 'id' }, sequence = 'id_sequence' }) + -- Insert test data -- box.space.bands:insert { 1, 'Roxette', 1986 } box.space.bands:insert { 2, 'Scorpions', 1965 } box.space.bands:insert { 3, 'Ace of Base', 1987 } - -- Commit the transaction implicitly -- - box.atomic(function() - box.space.bands:insert { 4, 'The Beatles', 1960 } - box.space.bands:replace { 1, 'Pink Floyd', 1965 } - end) + -- Define a function -- + local function insert_band(band_name, year) + box.space.bands:insert { nil, band_name, year } + end + + -- Begin and commit the transaction implicitly -- + box.atomic(insert_band, 'The Beatles', 1960) + + -- Begin the transaction with the specified isolation level -- + box.atomic({ txn_isolation = 'read-committed' }, + insert_band, 'The Rolling Stones', 1962) - t.assert_equals(box.space.bands:count(), 4) - t.assert_equals(box.space.bands:select { 1 }[1], { 1, 'Pink Floyd', 1965 }) + t.assert_equals(box.space.bands:count(), 5) + t.assert_equals(box.space.bands:select { 5 }[1], { 5, 'The Rolling Stones', 1962 }) end) end \ No newline at end of file diff --git a/doc/code_snippets/test/transactions/box_commit_test.lua b/doc/code_snippets/test/transactions/box_commit_test.lua index 17abbd8970..122572a58f 100644 --- a/doc/code_snippets/test/transactions/box_commit_test.lua +++ b/doc/code_snippets/test/transactions/box_commit_test.lua @@ -31,13 +31,19 @@ g.test_space_is_updated = function(cg) box.space.bands:insert { 2, 'Scorpions', 1965 } box.space.bands:insert { 3, 'Ace of Base', 1987 } - -- Commit the transaction -- + -- Begin and commit the transaction explicitly -- box.begin() box.space.bands:insert { 4, 'The Beatles', 1960 } box.space.bands:replace { 1, 'Pink Floyd', 1965 } box.commit() - t.assert_equals(box.space.bands:count(), 4) - t.assert_equals(box.space.bands:select { 1 }[1], { 1, 'Pink Floyd', 1965 }) + -- Begin the transaction with the specified isolation level -- + box.begin({ txn_isolation = 'read-committed' }) + box.space.bands:insert { 5, 'The Rolling Stones', 1962 } + box.space.bands:replace { 1, 'The Doors', 1965 } + box.commit() + + t.assert_equals(box.space.bands:count(), 5) + t.assert_equals(box.space.bands:select { 1 }[1], { 1, 'The Doors', 1965 }) end) end \ No newline at end of file diff --git a/doc/reference/reference_lua/box_txn_management/atomic.rst b/doc/reference/reference_lua/box_txn_management/atomic.rst index 1d1ae39256..f46ca5f325 100644 --- a/doc/reference/reference_lua/box_txn_management/atomic.rst +++ b/doc/reference/reference_lua/box_txn_management/atomic.rst @@ -4,14 +4,23 @@ box.atomic() ================================================================================ -.. function:: box.atomic(tx-function [, function-arguments]) +.. function:: box.atomic([opts,] tx-function [, function-arguments]) Execute a function, acting as if the function starts with an implicit :doc:`/reference/reference_lua/box_txn_management/begin` and ends with an implicit :doc:`/reference/reference_lua/box_txn_management/commit` if successful, or ends with an implicit :doc:`/reference/reference_lua/box_txn_management/rollback` if there is an error. - :return: the result of the function passed to ``atomic()`` as an argument. + :param table opts: (optional) transaction options: + + * ``txn_isolation`` -- the :ref:`transaction isolation level ` + * ``timeout`` -- a timeout (in seconds), after which the transactions are rolled back + + :param string tx-function: the function name + + :param function-arguments: (optional) arguments passed to the function + + :return: the result of the function passed to ``atomic()`` as an argument **Possible errors:** @@ -23,5 +32,5 @@ box.atomic() .. literalinclude:: /code_snippets/test/transactions/box_atomic_test.lua :language: lua - :lines: 29-38 + :lines: 28-47 :dedent: diff --git a/doc/reference/reference_lua/box_txn_management/begin.rst b/doc/reference/reference_lua/box_txn_management/begin.rst index 17ea1bf0b8..593df04f38 100644 --- a/doc/reference/reference_lua/box_txn_management/begin.rst +++ b/doc/reference/reference_lua/box_txn_management/begin.rst @@ -3,7 +3,7 @@ box.begin() =========== -.. function:: box.begin([txn_isolation]) +.. function:: box.begin([opts]) Begin the transaction. Disable :ref:`implicit yields ` until the transaction ends. @@ -12,7 +12,10 @@ box.begin() In effect the fiber which executes ``box.begin()`` is starting an "active multi-request transaction", blocking all other fibers. - :param txn_isolation: :ref:`transaction isolation level ` + :param table opts: (optional) transaction options: + + * ``txn_isolation`` -- the :ref:`transaction isolation level ` + * ``timeout`` -- a timeout (in seconds), after which the transactions are rolled back **Possible errors:** @@ -23,5 +26,5 @@ box.begin() .. literalinclude:: /code_snippets/test/transactions/box_commit_test.lua :language: lua - :lines: 29-38 + :lines: 29-44 :dedent: diff --git a/doc/reference/reference_lua/box_txn_management/commit.rst b/doc/reference/reference_lua/box_txn_management/commit.rst index dfcb7d0ae4..08a12add15 100644 --- a/doc/reference/reference_lua/box_txn_management/commit.rst +++ b/doc/reference/reference_lua/box_txn_management/commit.rst @@ -18,5 +18,5 @@ box.commit() .. literalinclude:: /code_snippets/test/transactions/box_commit_test.lua :language: lua - :lines: 29-38 + :lines: 29-44 :dedent: