[Tarantool-patches] [PATCH vshard 1/6] replicaset: introduce netbox_wait_connected()

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Fri Jul 2 01:09:31 MSK 2021


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)



More information about the Tarantool-patches mailing list