Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org, olegrok@tarantool.org,
	yaroslav.dynnikov@tarantool.org
Subject: [Tarantool-patches] [PATCH vshard 1/6] replicaset: introduce netbox_wait_connected()
Date: Fri,  2 Jul 2021 00:09:31 +0200	[thread overview]
Message-ID: <72bbbaad3488fd92a1ca193cba46161e81676326.1625177221.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1625177221.git.v.shpilevoy@tarantool.org>

It is extracted from replicaset_wait_connected(). The new
function is going to be used in the future patch with master
discovery. There it will be needed to send async requests to
several replicas at once, but before that need to ensure they are
connected. Otherwise the async request fails right away.

Needed for #75
---
 vshard/replicaset.lua | 62 ++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/vshard/replicaset.lua b/vshard/replicaset.lua
index 56ea165..fa048c9 100644
--- a/vshard/replicaset.lua
+++ b/vshard/replicaset.lua
@@ -104,6 +104,38 @@ local function netbox_on_disconnect(conn)
     conn.replica.down_ts = fiber_clock()
 end
 
+--
+-- Wait until the connection is established. This is necessary at least for
+-- async requests because they fail immediately if the connection is not done.
+-- Returns the remaining timeout because is expected to be used to connect to
+-- many instances in a loop, where such return saves one clock get in the caller
+-- code and is just cleaner code.
+--
+local function netbox_wait_connected(conn, timeout)
+    -- Fast path. Usually everything is connected.
+    if conn:is_connected() then
+        return timeout
+    end
+    local deadline = fiber_clock() + timeout
+    -- Loop against spurious wakeups.
+    repeat
+        -- Netbox uses fiber_cond inside, which throws an irrelevant usage error
+        -- at negative timeout. Need to check the case manually.
+        if timeout < 0 then
+            return nil, lerror.timeout()
+        end
+        local ok, res = pcall(conn.wait_connected, conn, timeout)
+        if not ok then
+            return nil, lerror.make(res)
+        end
+        if not res then
+            return nil, lerror.timeout()
+        end
+        timeout = deadline - fiber_clock()
+    until conn:is_connected()
+    return timeout
+end
+
 --
 -- Connect to a specified replica and remember a new connection
 -- in the replica object. Note, that the function does not wait
@@ -140,36 +172,10 @@ local function replicaset_connect_master(replicaset)
 end
 
 --
--- Wait until the master instance is connected. This is necessary at least for
--- async requests because they fail immediately if the connection is not
--- established.
--- Returns the remaining timeout because is expected to be used to connect to
--- many replicasets in a loop, where such return saves one clock get in the
--- caller code and is just cleaner code.
+-- Wait until the master instance is connected.
 --
 local function replicaset_wait_connected(replicaset, timeout)
-    local deadline = fiber_clock() + timeout
-    local ok, res
-    while true do
-        local conn = replicaset_connect_master(replicaset)
-        if conn.state == 'active' then
-            return timeout
-        end
-        -- Netbox uses fiber_cond inside, which throws an irrelevant usage error
-        -- at negative timeout. Need to check the case manually.
-        if timeout < 0 then
-            return nil, lerror.timeout()
-        end
-        ok, res = pcall(conn.wait_connected, conn, timeout)
-        if not ok then
-            return nil, lerror.make(res)
-        end
-        if not res then
-            return nil, lerror.timeout()
-        end
-        timeout = deadline - fiber_clock()
-    end
-    assert(false)
+    return netbox_wait_connected(replicaset_connect_master(replicaset), timeout)
 end
 
 --
-- 
2.24.3 (Apple Git-128)


  reply	other threads:[~2021-07-01 22:10 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-01 22:09 [Tarantool-patches] [PATCH vshard 0/6] Master discovery Vladislav Shpilevoy via Tarantool-patches
2021-07-01 22:09 ` Vladislav Shpilevoy via Tarantool-patches [this message]
2021-07-02 11:46   ` [Tarantool-patches] [PATCH vshard 1/6] replicaset: introduce netbox_wait_connected() Oleg Babin via Tarantool-patches
2021-07-01 22:09 ` [Tarantool-patches] [PATCH vshard 2/6] test: sort some table prints Vladislav Shpilevoy via Tarantool-patches
2021-07-02 11:46   ` Oleg Babin via Tarantool-patches
2021-07-01 22:09 ` [Tarantool-patches] [PATCH vshard 3/6] storage: introduce vshard.storage._call('info') Vladislav Shpilevoy via Tarantool-patches
2021-07-02 11:46   ` Oleg Babin via Tarantool-patches
2021-07-01 22:09 ` [Tarantool-patches] [PATCH vshard 4/6] config: introduce master 'auto' replicaset option Vladislav Shpilevoy via Tarantool-patches
2021-07-02 11:47   ` Oleg Babin via Tarantool-patches
2021-07-02 21:32     ` Vladislav Shpilevoy via Tarantool-patches
2021-07-05  9:23       ` Oleg Babin via Tarantool-patches
2021-07-01 22:09 ` [Tarantool-patches] [PATCH vshard 5/6] router: introduce automatic master discovery Vladislav Shpilevoy via Tarantool-patches
2021-07-02 11:48   ` Oleg Babin via Tarantool-patches
2021-07-02 21:35     ` Vladislav Shpilevoy via Tarantool-patches
2021-07-05  9:24       ` Oleg Babin via Tarantool-patches
2021-07-05 20:53         ` Vladislav Shpilevoy via Tarantool-patches
2021-07-06  8:54           ` Oleg Babin via Tarantool-patches
2021-07-06 21:19             ` Vladislav Shpilevoy via Tarantool-patches
2021-07-01 22:09 ` [Tarantool-patches] [PATCH vshard 6/6] router: update master using a hint from storage Vladislav Shpilevoy via Tarantool-patches
2021-07-02 11:49   ` Oleg Babin via Tarantool-patches
2021-07-02 21:36     ` Vladislav Shpilevoy via Tarantool-patches
2021-07-05  9:24       ` Oleg Babin via Tarantool-patches
2021-07-05 20:53         ` Vladislav Shpilevoy via Tarantool-patches
2021-07-06  8:55           ` Oleg Babin via Tarantool-patches
2021-07-02 21:36 ` [Tarantool-patches] [PATCH vshard 7/6] util: truncate too long fiber name Vladislav Shpilevoy via Tarantool-patches
2021-07-05  9:23   ` Oleg Babin via Tarantool-patches
2021-08-03 21:55 ` [Tarantool-patches] [PATCH vshard 0/6] Master discovery Vladislav Shpilevoy via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=72bbbaad3488fd92a1ca193cba46161e81676326.1625177221.git.v.shpilevoy@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=olegrok@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --cc=yaroslav.dynnikov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH vshard 1/6] replicaset: introduce netbox_wait_connected()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox