From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 501F5452566 for ; Tue, 5 Nov 2019 16:12:11 +0300 (MSK) Date: Tue, 5 Nov 2019 16:12:08 +0300 From: Alexander Turenko Message-ID: <20191105131207.3tyydx33ti45rgdu@tkn_work_nb> References: <0706877cdc0598bb77489636dd22e852b7a50682.1572385348.git.v.shpilevoy@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <0706877cdc0598bb77489636dd22e852b7a50682.1572385348.git.v.shpilevoy@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH 1/1] netbox: don't fire on_connect() at schema update List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org 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()