[Tarantool-patches] [PATCH v3 8/8] net.box: add interactive transaction support in net.box
Vladimir Davydov
vdavydov at tarantool.org
Wed Aug 11 15:47:07 MSK 2021
On Wed, Aug 11, 2021 at 11:56:58AM +0300, mechanik20051988 wrote:
> From: mechanik20051988 <mechanik20.05.1988 at gmail.com>
>
> Implement `begin`, `commit` and `rollback` methods for stream object
> in `net.box`, which allows to begin, commit and rollback transaction
> accordingly.
>
> Closes #5860
>
> @TarantoolBot document
> Title: add interactive transaction support in net.box
> Implement `begin`, `commit` and `rollback` methods for stream object
> in `net.box`, which allows to begin, commit and rollback transaction
> accordingly. Now there are multiple ways to begin, commit and rollback
> transaction from `net.box`: using appropriate stream methods, using 'call`
> or 'eval' methods or using `execute` method with sql transaction syntax.
> User can mix these methods, for example, start transaction using
> `stream:begin()`, and commit transaction using `stream:call('box.commit')`
> or stream:execute('COMMIT').
> Simple example of using interactive transactions via iproto from net.box:
> ```lua
> stream = conn:new_stream()
> space = stream.space.test
> space_not_from_stream = conn.space.test
>
> stream:begin()
> space:replace({1})
> -- return previously inserted tuple, because request
> -- belongs to transaction.
> space:select({})
> -- empty select, because select doesn't belongs to
> -- transaction
> space_not_from_stream:select({})
> stream:call('box.commit')
> -- now transaction was commited, so all requests
> -- returns tuple.
> ```
> Different examples of using streams you can find in
> gh-5860-implement-streams-in-iproto.test.lua
> ---
> .../gh-5860-implement-streams-in-iproto.md | 26 +
> src/box/lua/net_box.c | 49 +-
> src/box/lua/net_box.lua | 35 +-
> test/box/stream.result | 3558 +++++++++++++++--
> test/box/stream.test.lua | 1202 ++++++
Please add a separate test file for this (net.box.transactions or
something like this).
> 5 files changed, 4554 insertions(+), 316 deletions(-)
> create mode 100644 changelogs/unreleased/gh-5860-implement-streams-in-iproto.md
>
> diff --git a/src/box/lua/net_box.lua b/src/box/lua/net_box.lua
> index 3dffc245f..745a8c0f5 100644
> --- a/src/box/lua/net_box.lua
> +++ b/src/box/lua/net_box.lua
> @@ -51,8 +51,11 @@ local M_GET = 13
> local M_MIN = 14
> local M_MAX = 15
> local M_COUNT = 16
> +local M_BEGIN = 17
> +local M_COMMIT = 18
> +local M_ROLLBACK = 19
> -- Injects raw data into connection. Used by console and tests.
> -local M_INJECT = 17
> +local M_INJECT = 20
>
> -- utility tables
> local is_final_state = {closed = 1, error = 1}
> @@ -754,11 +757,38 @@ local function new_stream(stream)
> return stream._conn:new_stream()
> end
>
> +local function begin(stream, opts)
stream_begin
> + check_remote_arg(stream, 'begin')
> + local res = stream:_request(M_BEGIN, opts, nil, stream._stream_id)
> + if opts and opts.is_async then
> + return res
> + end
> +end
> +
> +local function commit(stream, opts)
stream_commit
> + check_remote_arg(stream, 'commit')
> + local res = stream:_request(M_COMMIT, opts, nil, stream._stream_id)
> + if opts and opts.is_async then
> + return res
> + end
> +end
> +
> +local function rollback(stream, opts)
stream_rollback
> + check_remote_arg(stream, 'rollback')
> + local res = stream:_request(M_ROLLBACK, opts, nil, stream._stream_id)
> + if opts and opts.is_async then
> + return res
> + end
> +end
> +
> function remote_methods:new_stream()
> check_remote_arg(self, 'new_stream')
> self._last_stream_id = self._last_stream_id + 1
> local stream = setmetatable({
> new_stream = new_stream,
> + begin = begin,
> + commit = commit,
> + rollback = rollback,
> _stream_id = self._last_stream_id,
> space = setmetatable({
> _stream_space_cache = {},
More information about the Tarantool-patches
mailing list