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 D125F6EC40; Fri, 13 Aug 2021 15:58:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D125F6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628859503; bh=lOY6QqNn5QicxTEIwmv9rttUJCQ4gvhK5Ie2PkPGwEI=; h=To:Cc:Date:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=ENJzbw+mDUMk7Sz6jUnq0LQbw/38+96QRmShj2INEhoxKYMLSh1/GfzZFAfhex7hE AsBAqSSPGRUZzT/TrndEQRGpgOjbn5Qb1bOQaNHzaOyCJUhTEt+7gb+lBkwFZF0fIF cwu7qQVH2HKzlc35jcz2jkPfMlrAK9k6GM0XELME= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 422E96EC40 for ; Fri, 13 Aug 2021 15:58:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 422E96EC40 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mEWlQ-0004gY-5P; Fri, 13 Aug 2021 15:58:20 +0300 To: tarantool-patches@dev.tarantool.org Cc: v.shpilevoy@tarantool.org, yaroslav.dynnikov@tarantool.org Date: Fri, 13 Aug 2021 15:58:19 +0300 Message-Id: <02d9fe40f3b007f351d873e9cc880e41d8cfacad.1628859463.git.vdavydov@tarantool.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9736CF3E71F18CE0C3E1D5927724F4AAA182A05F538085040DFCD30678488E8842777BFD665CCC83609C39C8A3ECDACACCD030759D2BFBF11 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE74C265300876DF183C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE723628CE20AC63995EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F205255DE68D2447B6F0E1C029004F4BD5CC7F00164DA146DAFE8445B8C89999728AA50765F790063783E00425F71A4181389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8062BEEFFB5F8EA3EF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDCE939D40DBB93CA75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86C193157245D22FAF6597CDF875CCFA48C9C2B6934AE262D3EE7EAB7254005DCEDAAE47F5F63E737721E0A4E2319210D9B64D260DF9561598F01A9E91200F654B065218196CFC280F58E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D4140D813EC137C6197A8CC333C97C85E35FF8ED5B8837383CF7E5E325150E598887CAE10FA6B8C41D7E09C32AA3244C203140225CD0BE22A6408CF74E31B88C51E887DA02A9F7BF927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkxer45hjC3/QA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D7B12F91E569BA7885195119EA62B9C83274CEFED1673C562683ABF942079399BFB559BB5D741EB966A65DFF43FF7BE03240331F90058701C67EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH] net.box: add __serialize and __tostring methods for future objects 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 Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Example output (for more examples see the test output): tarantool> f = c:eval('return 123', {}, {is_async = true}) --- ... tarantool> tostring(f) --- - 'net.box.request: 7' ... tarantool> f --- - method: eval on_push_ctx: [] result: [123] on_push: 'function: builtin#91' sync: 7 ... Closes #6314 --- https://github.com/tarantool/tarantool/issues/6314 https://github.com/tarantool/tarantool/tree/vdavydov/netbox-future-serialize src/box/lua/net_box.c | 68 +++++++++++++ test/box/net.box_fiber-async_gh-3107.result | 95 ++++++++++++++++++- test/box/net.box_fiber-async_gh-3107.test.lua | 28 +++++- 3 files changed, 183 insertions(+), 8 deletions(-) diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c index 229dec590cf9..19ceec9544b6 100644 --- a/src/box/lua/net_box.c +++ b/src/box/lua/net_box.c @@ -82,6 +82,30 @@ enum netbox_method { netbox_method_MAX }; +static const char *netbox_method_str[] = { + [NETBOX_PING] = "ping", + [NETBOX_CALL_16] = "call_16", + [NETBOX_CALL_17] = "call", + [NETBOX_EVAL] = "eval", + [NETBOX_INSERT] = "insert", + [NETBOX_REPLACE] = "replace", + [NETBOX_DELETE] = "delete", + [NETBOX_UPDATE] = "update", + [NETBOX_UPSERT] = "upsert", + [NETBOX_SELECT] = "select", + [NETBOX_EXECUTE] = "execute", + [NETBOX_PREPARE] = "prepare", + [NETBOX_UNPREPARE] = "unprepare", + [NETBOX_GET] = "get", + [NETBOX_MIN] = "min", + [NETBOX_MAX] = "max", + [NETBOX_COUNT] = "count", + [NETBOX_BEGIN] = "begin", + [NETBOX_COMMIT] = "commit", + [NETBOX_ROLLBACK] = "rollback", + [NETBOX_INJECT] = "inject", +}; + struct netbox_registry { /** Next request id. */ uint64_t next_sync; @@ -1420,6 +1444,48 @@ luaT_netbox_request_gc(struct lua_State *L) return 0; } +static int +luaT_netbox_request_serialize(struct lua_State *L) +{ + struct netbox_request *request = luaT_check_netbox_request(L, 1); + lua_newtable(L); + luaL_pushuint64(L, request->sync); + lua_setfield(L, -2, "sync"); + lua_pushstring(L, netbox_method_str[request->method]); + lua_setfield(L, -2, "method"); + lua_rawgeti(L, LUA_REGISTRYINDEX, request->buffer_ref); + lua_setfield(L, -2, "buffer"); + if (request->skip_header) { + lua_pushboolean(L, true); + lua_setfield(L, -2, "skip_header"); + } + lua_rawgeti(L, LUA_REGISTRYINDEX, request->on_push_ref); + lua_setfield(L, -2, "on_push"); + lua_rawgeti(L, LUA_REGISTRYINDEX, request->on_push_ctx_ref); + lua_setfield(L, -2, "on_push_ctx"); + if (request->result_ref != LUA_NOREF) { + lua_rawgeti(L, LUA_REGISTRYINDEX, request->result_ref); + lua_setfield(L, -2, "result"); + } + if (request->error != NULL) { + diag_set_error(diag_get(), request->error); + luaT_pusherror(L, request->error); + lua_setfield(L, -2, "error"); + } + return 1; +} + +static int +luaT_netbox_request_tostring(struct lua_State *L) +{ + char buf[32]; + struct netbox_request *request = luaT_check_netbox_request(L, 1); + snprintf(buf, sizeof(buf), "%s: %llu", netbox_request_typename, + (unsigned long long)request->sync); + lua_pushstring(L, buf); + return 1; +} + /** * Returns true if the response was received for the given request. */ @@ -2052,6 +2118,8 @@ luaopen_net_box(struct lua_State *L) static const struct luaL_Reg netbox_request_meta[] = { { "__gc", luaT_netbox_request_gc }, + {"__serialize", luaT_netbox_request_serialize }, + {"__tostring", luaT_netbox_request_tostring }, { "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..f2fbbde3d1bd 100644 --- a/test/box/net.box_fiber-async_gh-3107.result +++ b/test/box/net.box_fiber-async_gh-3107.result @@ -1,3 +1,6 @@ +test_run = require('test_run').new() +--- +... fiber = require 'fiber' --- ... @@ -10,10 +13,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 +104,92 @@ err:find('Usage') ~= nil --- - true ... -box.schema.func.drop('long_function') +-- +-- __serialize and __tostring future methods +-- +future = c:call('long_function', {1, 2, 3}, {is_async = true}) +--- +... +tostring(future) +--- +- 'net.box.request: 5' +... +future +--- +- on_push_ctx: [] + method: call + sync: 5 + on_push: 'function: builtin#91' +... +finalize_long() +--- +... +future:wait_result() +--- +- [1, 2, 3] +... +future +--- +- method: call + on_push_ctx: [] + result: [1, 2, 3] + on_push: 'function: builtin#91' + sync: 5 +... +future = c:eval('assert(false)', {}, {is_async = true}) +--- +... +tostring(future) +--- +- 'net.box.request: 6' +... +future:wait_result() +--- +- null +- 'eval:1: assertion failed!' +... +future +--- +- error: 'eval:1: assertion failed!' + method: eval + on_push_ctx: [] + on_push: 'function: builtin#91' + sync: 6 +... +future = c:eval('return 123', {}, {is_async = true, skip_header = true, \ + buffer = require('buffer').ibuf()}) +--- +... +tostring(future) +--- +- 'net.box.request: 7' +... +future:wait_result() +--- +- 6 +... +test_run:cmd("push filter '0x[a-f0-9]+' to ''") +--- +- true +... +future +--- +- method: eval + result: 6 + on_push_ctx: [] + buffer: + ibuf: + rpos: 'cdata: ' + wpos: 'cdata: ' + on_push: 'function: builtin#91' + sync: 7 + skip_header: true +... +test_run:cmd("clear filter") +--- +- 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..55f9afabad10 100644 --- a/test/box/net.box_fiber-async_gh-3107.test.lua +++ b/test/box/net.box_fiber-async_gh-3107.test.lua @@ -1,3 +1,5 @@ +test_run = require('test_run').new() + fiber = require 'fiber' net = require('net.box') @@ -5,8 +7,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 +37,28 @@ err:find('Usage') ~= nil _, err = pcall(future.wait_result, future, '100') err:find('Usage') ~= nil -box.schema.func.drop('long_function') +-- +-- __serialize and __tostring future methods +-- +future = c:call('long_function', {1, 2, 3}, {is_async = true}) +tostring(future) +future +finalize_long() +future:wait_result() +future +future = c:eval('assert(false)', {}, {is_async = true}) +tostring(future) +future:wait_result() +future +future = c:eval('return 123', {}, {is_async = true, skip_header = true, \ + buffer = require('buffer').ibuf()}) +tostring(future) +future:wait_result() +test_run:cmd("push filter '0x[a-f0-9]+' to ''") +future +test_run:cmd("clear filter") + +box.schema.user.revoke('guest', 'execute', 'universe') c:close() s:drop() -- 2.25.1