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 7EE8B6EC40; Fri, 13 Aug 2021 12:59:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7EE8B6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628848772; bh=elz+8BABPa1A22UQ9uHn0z5EHyk7+c7xJeLxGpKxsgc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=s1j2bHF9otQSqPxesg4OtMUiTwofyJY76b6x1QeXmjXu2T8GGy46tHGdquApPAzLs 7bf2/Ht1w0abQ69Rw/+CRknSGN6fZb3rf5kIM/88ehAYh83JPtkXIBK0XgO9yLlQe9 d0p9PFMM85RfHBOKnKuaYQO8ASjOFaDrphE4CV5M= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (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 3F2B96EC40 for ; Fri, 13 Aug 2021 12:59:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3F2B96EC40 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mETyM-0006tm-2Y; Fri, 13 Aug 2021 12:59:30 +0300 To: tarantool-patches@dev.tarantool.org Date: Fri, 13 Aug 2021 12:59:29 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210813095700.7vumduzrevgmcuyi@esperanza> References: <20210813095700.7vumduzrevgmcuyi@esperanza> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9F9A2272A1D086A28553D1D5C4B4124EF182A05F538085040E9062C8223A33DB73189520DDAB31D7B696799CA1788DFCA16EA09120FCF6DCE X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE74B51810D54EC17F0C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE78FB0D5613E12B2E0EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F23B57663089BBDC107FE0FA259569AF7ACC7F00164DA146DAFE8445B8C89999728AA50765F7900637D0FEED2715E18529389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8062BEEFFB5F8EA3EF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDCE939D40DBB93CA75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86CA8C2CB3CB445ACA444C6A51F067C83779C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF3A74CE74F45F2162699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D340E23B77EA180781771391E5638C08FCD0E5F50225AEDF77D6DF9B291B156EAFC3A080B13D18576631D7E09C32AA3244CC5E5520E9D5235C0422ED1633BA35C929CA7333006C390A083B48618A63566E0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkydayEL73wPeQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D0CFE91A3D23BDF6696AAE99A5BB7DC9A274CEFED1673C562683ABF942079399BFB559BB5D741EB966A65DFF43FF7BE03240331F90058701C67EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3] net.box: allow to store user-defined fields in future object 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: Vladimir Davydov via Tarantool-patches Reply-To: Vladimir Davydov Cc: yaroslav.dynnikov@tarantool.org, v.shpilevoy@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Before commit 954194a1ca5c ("net.box: rewrite request implementation in C"), net.box future was a plain Lua table so that the caller could attach extra information to it. Now it isn't true anymore - a future is a userdata object, and it doesn't have indexing methods. For backward compatibility, let's add __index and __newindex fields and store user-defined fields in a Lua table, which is created lazily on the first __newindex invocation. __index falls back on the metatable methods if a field isn't found in the table. Follow-up #6241 Closes #6306 --- https://github.com/tarantool/tarantool/issues/6306 https://github.com/tarantool/tarantool/tree/vdavydov/netbox-allow-to-store-user-data-in-future-using-lua-table Changes in v3: - Use Lua table instead of mhash for storing user data, as suggested by imun@. v2: https://lists.tarantool.org/pipermail/tarantool-patches/2021-August/025402.html src/box/lua/net_box.c | 51 ++++++++ test/box/net.box_fiber-async_gh-3107.result | 109 +++++++++++++++++- test/box/net.box_fiber-async_gh-3107.test.lua | 39 ++++++- 3 files changed, 191 insertions(+), 8 deletions(-) diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c index 229dec590cf9..078bbfb07b18 100644 --- a/src/box/lua/net_box.c +++ b/src/box/lua/net_box.c @@ -124,6 +124,15 @@ struct netbox_request { /** Lua references to on_push trigger and its context. */ int on_push_ref; int on_push_ctx_ref; + /** + * Lua reference to a table with user-defined fields. + * We allow the user to attach extra information to a future object, + * e.g. a reference to a connection or the invoked method name/args. + * All the information is stored in this table, which is created + * lazily, on the first __newindex invocation. Until then, it's + * LUA_NOREF. + */ + int index_ref; /** * Lua reference to the request result or LUA_NOREF if the response * hasn't been received yet. If the response was decoded to a @@ -167,6 +176,7 @@ netbox_request_destroy(struct netbox_request *request) luaL_unref(tarantool_L, LUA_REGISTRYINDEX, request->on_push_ref); luaL_unref(tarantool_L, LUA_REGISTRYINDEX, request->on_push_ctx_ref); luaL_unref(tarantool_L, LUA_REGISTRYINDEX, request->result_ref); + luaL_unref(tarantool_L, LUA_REGISTRYINDEX, request->index_ref); if (request->error != NULL) error_unref(request->error); } @@ -1420,6 +1430,44 @@ luaT_netbox_request_gc(struct lua_State *L) return 0; } +static int +luaT_netbox_request_index(struct lua_State *L) +{ + struct netbox_request *request = luaT_check_netbox_request(L, 1); + if (request->index_ref != LUA_NOREF) { + lua_rawgeti(L, LUA_REGISTRYINDEX, request->index_ref); + /* Push the key (2nd argument) to the top. */ + lua_pushvalue(L, 2); + lua_rawget(L, -2); + if (lua_type(L, -1) != LUA_TNIL) + return 1; + /* Pop nil and the index table. */ + lua_pop(L, 2); + } + /* Fall back on metatable methods. */ + lua_getmetatable(L, 1); + /* Move the metatable before the key (2nd argument). */ + lua_insert(L, 2); + lua_rawget(L, 2); + return 1; +} + +static int +luaT_netbox_request_newindex(struct lua_State *L) +{ + struct netbox_request *request = luaT_check_netbox_request(L, 1); + if (request->index_ref == LUA_NOREF) { + /* Lazily create the index table on the first invocation. */ + lua_newtable(L); + request->index_ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + lua_rawgeti(L, LUA_REGISTRYINDEX, request->index_ref); + /* Move the index table before the key (2nd argument). */ + lua_insert(L, 2); + lua_rawset(L, 2); + return 0; +} + /** * Returns true if the response was received for the given request. */ @@ -1664,6 +1712,7 @@ netbox_make_request(struct lua_State *L, int idx, request->format = tuple_format_runtime; tuple_format_ref(request->format); fiber_cond_create(&request->cond); + request->index_ref = LUA_NOREF; request->result_ref = LUA_NOREF; request->error = NULL; if (netbox_request_register(request, registry) != 0) { @@ -2052,6 +2101,8 @@ luaopen_net_box(struct lua_State *L) static const struct luaL_Reg netbox_request_meta[] = { { "__gc", luaT_netbox_request_gc }, + { "__index", luaT_netbox_request_index }, + { "__newindex", luaT_netbox_request_newindex }, { "is_ready", luaT_netbox_request_is_ready }, { "result", luaT_netbox_request_result }, { "wait_result", luaT_netbox_request_wait_result }, diff --git a/test/box/net.box_fiber-async_gh-3107.result b/test/box/net.box_fiber-async_gh-3107.result index aaaca351a579..60033059335b 100644 --- a/test/box/net.box_fiber-async_gh-3107.result +++ b/test/box/net.box_fiber-async_gh-3107.result @@ -10,10 +10,7 @@ net = require('net.box') cond = nil --- ... -box.schema.func.create('long_function') ---- -... -box.schema.user.grant('guest', 'execute', 'function', 'long_function') +box.schema.user.grant('guest', 'execute', 'universe') --- ... function long_function(...) cond = fiber.cond() cond:wait() return ... end @@ -104,7 +101,109 @@ err:find('Usage') ~= nil --- - true ... -box.schema.func.drop('long_function') +-- Storing user data in future object. +future = c:eval('return 123', {}, {is_async = true}) +--- +... +future.abc -- nil +--- +- null +... +future.abc = nil +--- +... +future.abc -- nil +--- +- null +... +future.abc = 'abc' +--- +... +future.abc -- abc +--- +- abc +... +future.abc = nil +--- +... +future.abc -- nil +--- +- null +... +future.abc = nil +--- +... +future.abc -- nil +--- +- null +... +future:wait_result() -- 123 +--- +- [123] +... +-- Garbage collection of stored user data. +future = c:eval('return 123', {}, {is_async = true}) +--- +... +future.data1 = {1} +--- +... +future.data2 = {2} +--- +... +future.data3 = {3} +--- +... +gc = setmetatable({}, {__mode = 'v'}) +--- +... +gc.data1 = future.data1 +--- +... +gc.data2 = future.data2 +--- +... +gc.data3 = future.data3 +--- +... +future.data1 = nil +--- +... +_ = collectgarbage('collect') +--- +... +gc.data1 == nil +--- +- true +... +future.data2 = 123 +--- +... +_ = collectgarbage('collect') +--- +... +gc.data2 == nil +--- +- true +... +future:wait_result() -- 123 +--- +- [123] +... +future = nil +--- +... +_ = collectgarbage('collect') +--- +... +_ = collectgarbage('collect') +--- +... +gc.data3 == nil +--- +- true +... +box.schema.user.revoke('guest', 'execute', 'universe') --- ... c:close() diff --git a/test/box/net.box_fiber-async_gh-3107.test.lua b/test/box/net.box_fiber-async_gh-3107.test.lua index d23f368cbce4..474dfaa4e35b 100644 --- a/test/box/net.box_fiber-async_gh-3107.test.lua +++ b/test/box/net.box_fiber-async_gh-3107.test.lua @@ -5,8 +5,7 @@ net = require('net.box') -- gh-3107: fiber-async netbox. -- cond = nil -box.schema.func.create('long_function') -box.schema.user.grant('guest', 'execute', 'function', 'long_function') +box.schema.user.grant('guest', 'execute', 'universe') function long_function(...) cond = fiber.cond() cond:wait() return ... end function finalize_long() while not cond do fiber.sleep(0.01) end cond:signal() cond = nil end s = box.schema.create_space('test') @@ -36,7 +35,41 @@ err:find('Usage') ~= nil _, err = pcall(future.wait_result, future, '100') err:find('Usage') ~= nil -box.schema.func.drop('long_function') +-- Storing user data in future object. +future = c:eval('return 123', {}, {is_async = true}) +future.abc -- nil +future.abc = nil +future.abc -- nil +future.abc = 'abc' +future.abc -- abc +future.abc = nil +future.abc -- nil +future.abc = nil +future.abc -- nil +future:wait_result() -- 123 + +-- Garbage collection of stored user data. +future = c:eval('return 123', {}, {is_async = true}) +future.data1 = {1} +future.data2 = {2} +future.data3 = {3} +gc = setmetatable({}, {__mode = 'v'}) +gc.data1 = future.data1 +gc.data2 = future.data2 +gc.data3 = future.data3 +future.data1 = nil +_ = collectgarbage('collect') +gc.data1 == nil +future.data2 = 123 +_ = collectgarbage('collect') +gc.data2 == nil +future:wait_result() -- 123 +future = nil +_ = collectgarbage('collect') +_ = collectgarbage('collect') +gc.data3 == nil + +box.schema.user.revoke('guest', 'execute', 'universe') c:close() s:drop() -- 2.25.1