[Tarantool-patches] [PATCH 1/1] netbox: don't fire on_connect() at schema update
Alexander Turenko
alexander.turenko at tarantool.org
Tue Nov 5 16:12:08 MSK 2019
The patch LGTM in the sense that it should work as expected as far as I
see.
However I would discuss points I described below: I think we can write
the patch in a bit more clean way. Vlad, please, let me know what do you
think about this.
WBR, Alexander Turenko.
> diff --git a/src/box/lua/net_box.lua b/src/box/lua/net_box.lua
> index 31a8c16b7..696b30fd9 100644
> --- a/src/box/lua/net_box.lua
> +++ b/src/box/lua/net_box.lua
> @@ -927,14 +927,17 @@ local function new_sm(host, port, opts, connection, greeting)
> local function callback(what, ...)
> if what == 'state_changed' then
> local state, errno, err = ...
> - if (remote.state == 'active' or remote.state == 'fetch_schema') and
> - (state == 'error' or state == 'closed' or
> - state == 'error_reconnect') then
> - remote._on_disconnect:run(remote)
> - end
> - if remote.state ~= 'error' and remote.state ~= 'error_reconnect' and
> - state == 'active' then
> - remote._on_connect:run(remote)
> + local was_connected = remote._is_connected
> + if state == 'active' then
> + if not was_connected then
> + remote._is_connected = true
> + remote._on_connect:run(remote)
> + end
We splitted states into 'connected', 'neural' and 'disconnected' (a kind
of tags or properties). We fire the trigger when a connection step into
one of 'connected' states from one of 'disconnected' ones ('neural' are
not counted). This looks okay.
> + elseif errno ~= nil then
> + if was_connected then
> + remote._is_connected = false
> + remote._on_disconnect:run(remote)
> + end
Here we use `errno ~= nil` condition to determine whether a state is
'disconnected' one. The condition is true for 'error', 'error_reconnect'
and 'closed' states. This way should give a correct behaviour.
When I saw the patch my question was whether a connection step into
'fetch_schema' state with `errno ~= nil`. It was not obvious for me what
list of states are always set with some 'errno' value (however it is
easy to deduce from set_state() calls). That is the first point.
The second is that I cannot prove (at least after brief look into the
code) that 'errno' is newer `nil` / `box.NULL` for 'disconnected'
states, because that are places where 'errno' is passed through a
function.
I think we should at least give a comment that by using `errno ~= nil`
we lean on assumption that we always step into 'error',
'error_reconnect' and 'closed' states with non-null 'errno' and that
there is no other states that set 'errno'; but better don't assume this.
Let's consider unix errno: it should not be used as a primary source of
information **whether** an error occurs. You always check a return code
and only if it says that an error occurs we can consider 'errno' as a
source of information **which kind** of error occurs.
That is why I generally against using of errno / diagnostic area as
sources of information whether an error occurs: in context of Unix APIs
this would be an improper usage.
I would mark states as 'connected' and 'disconnected' explicitly:
| -- XXX: Give a comment why, say, 'fetch_schema' is not here.
| local function is_state_connected(state)
| return state == 'active'
| end
|
| local disconnected_states = {
| initial = true,
| error = true,
| error_reconnect = true,
| closed = true,
| }
|
| local function is_state_disconnected(state)
| return disconnected_states[state]
| end
And use this markers instead of `state == 'active'` and `errno ~= nil`
conditions.
What do you think about this way?
> end
> remote.state, remote.error = state, err
> if state == 'error_reconnect' then
> @@ -989,6 +992,7 @@ local function new_sm(host, port, opts, connection, greeting)
> remote._on_schema_reload = trigger.new("on_schema_reload")
> remote._on_disconnect = trigger.new("on_disconnect")
> remote._on_connect = trigger.new("on_connect")
> + remote._is_connected = false
> remote._transport = create_transport(host, port, user, password, callback,
> connection, greeting)
> remote._transport.start()
More information about the Tarantool-patches
mailing list