From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id B50EC6EC40; Fri, 2 Jul 2021 01:10:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B50EC6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1625177410; bh=qBlbhj8tXaY5A9Pd1Zv2iY7rRrQ5Amp4/wlBLf5nHhQ=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=UL5ljq9pR3c7sgjLOCWDErMKIU9Hm528Q2tabZYdGifm8SYDrMuw9H7PeHAeEQH77 GsDCfnrHKvxy33yRC2ie6kDuYSeTmPyJcQXtl2eUv9uAbohDVQkE71w8jckgQzbjo9 B6iZgpAxZxLnmWeYo8GnvnUUt0XB9gxWQ56mJkKA= Received: from smtpng3.i.mail.ru (smtpng3.i.mail.ru [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 244A36EC40 for ; Fri, 2 Jul 2021 01:09:39 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 244A36EC40 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1lz4sM-00061t-9u; Fri, 02 Jul 2021 01:09:38 +0300 To: tarantool-patches@dev.tarantool.org, olegrok@tarantool.org, yaroslav.dynnikov@tarantool.org Date: Fri, 2 Jul 2021 00:09:31 +0200 Message-Id: <72bbbaad3488fd92a1ca193cba46161e81676326.1625177221.git.v.shpilevoy@tarantool.org> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD954DFF1DC42D673FB703477AD6D36A6E3513B6A65BE508F5F182A05F538085040A6D7FCC392926E3BD41543E994AEAB8D6FC41EB3C547331AE636ED7511524A5B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A3DED2DACB82E709C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE76042E2DB3E33BF2BEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BD6CF32B5F8F9D40445CFE6A74B2165890D650F29022C6D59CC7F00164DA146DAFE8445B8C89999728AA50765F790063740F9514592BAD022389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8A85A14DF5F041C99F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BAEB924C2B054B06E75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2AD77751E876CB595E8F7B195E1C97831B2FCC5B1B5B115E557D12FD7D753A721 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBCA6440478D4BA4609AF577D4A0A37EE45465ADEA6FA30869C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFB1D0AC14175A0720699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D341B5517184E88C1BD22EB7917479C7B6F664426E792EA10B788FBD96E462F0C0E3EC23911AB9DD7EB1D7E09C32AA3244C4EB79EE5BDFF5E7239F1998C3367A8238A6D4CC6FBFAC251FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXjw2yJDrod5MJp3KxYaDu8d X-Mailru-Sender: 689FA8AB762F73936BC43F508A0638220AABBEA80BE31EA1E6BF013586EF9CF73841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH vshard 1/6] replicaset: introduce netbox_wait_connected() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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)