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 4E84E6EC57; Fri, 23 Jul 2021 14:13:33 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4E84E6EC57 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627038813; bh=Qf12eYmdUYkU2IZ5xbS4okLe7xox3JSBPTed+rh230A=; 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=UUQGRkX3rHbnpgPX8ZSrMw4CGRV+PsARTEKhXISAqCLfh62x+P77/12DqfedqWFBP bsSzGtQdPPMTis0kEQJFJSRHbMg2D9HqJ91sLtXFDNeGiYAftTRqIjZUN3YK7clGSA dijlW7QfTrZLd6kp/SABK8TqJ1Ta/qwjGO98dRFE= 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 385E26F3CB for ; Fri, 23 Jul 2021 14:07:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 385E26F3CB Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1m6t1u-0004dl-N0; Fri, 23 Jul 2021 14:07:47 +0300 To: tarantool-patches@dev.tarantool.org Date: Fri, 23 Jul 2021 14:07:22 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-174C08C4: 5188C02AEC42908C481ED7ADC579193296BBA28369E3F2D2713F3D5F7D406D31BCF678C7329BA986 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD941C43E597735A9C3A9514C5AE4B3B389A94BDFA06D40730D182A05F5380850407D23768CAE5BFF8A3B6B2596139F1B37BAACE177CF67CC8D7E0EA3C6C233A570 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C2204D4F9A221771EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637D7F1BE85E7B0CA818638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86FA88D9CA9B73B974BD3A35CAEB98EA4117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6045A9A90E9EED90B089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 8BD88D57C5CADBC8B2710865C386751094C72BDDC9A8ED5CA3B1A56EE2B804F6B226C914C9968946695E9D90444CEC264DCC8C77FBA9901322D2CEDE4E95CF1BDBE8DEE28BC9005C095FFBCAB1CFE8AABCA57AF85F7723F20DF0559363BAF8BC91EFB9E8DEEC7F3D589120F7DAE46353205367B2BCC23E5B968201D80A3E8F5EBDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D349A949488F6BF46DD0D8BB635F98B11C8BE9F4EB2554D9550677096C492E08B91C40AAC0F010AD8091D7E09C32AA3244C2177D0F05F56E371EEB085EEA8570FA7B038C9161EF167A1729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojbL9S8ysBdXiEX0g4jkpDtedd/KGhzbnP X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D69B9603ED8D7C7BB21882FE3DC7C5690274CEFED1673C562683ABF942079399BFB559BB5D741EB966A65DFF43FF7BE03240331F90058701C67EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH 12/20] net.box: rewrite error decoder in C 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" This patch moves response_decoder table (which decodes only errors despite its name) from Lua to C. This is a step towards rewriting performance-critical parts of net.box in C. --- src/box/lua/net_box.c | 60 +++++++++++++++++++++++++++++++++++++++ src/box/lua/net_box.lua | 62 ++--------------------------------------- 2 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c index c0c3725e5350..e88db6323afa 100644 --- a/src/box/lua/net_box.c +++ b/src/box/lua/net_box.c @@ -40,6 +40,8 @@ #include "box/xrow.h" #include "box/tuple.h" #include "box/execute.h" +#include "box/error.h" +#include "box/mp_error.h" #include "lua/msgpack.h" #include @@ -1082,6 +1084,63 @@ netbox_decode_method(struct lua_State *L) return 2; } +/* + * Decodes an error from raw data and pushes it to Lua stack. Takes a pointer + * to the data (char ptr) and an error code. + */ +static int +netbox_decode_error(struct lua_State *L) +{ + uint32_t ctypeid; + const char **data = luaL_checkcdata(L, 1, &ctypeid); + assert(ctypeid == CTID_CHAR_PTR || ctypeid == CTID_CONST_CHAR_PTR); + uint32_t errcode = lua_tointeger(L, 2); + struct error *error = NULL; + assert(mp_typeof(**data) == MP_MAP); + uint32_t map_size = mp_decode_map(data); + for (uint32_t i = 0; i < map_size; ++i) { + uint32_t key = mp_decode_uint(data); + if (key == IPROTO_ERROR) { + if (error != NULL) + error_unref(error); + error = error_unpack_unsafe(data); + if (error == NULL) + return luaT_error(L); + error_ref(error); + /* + * IPROTO_ERROR comprises error encoded with + * IPROTO_ERROR_24, so we may ignore content + * of that key. + */ + break; + } else if (key == IPROTO_ERROR_24) { + if (error != NULL) + error_unref(error); + const char *reason = ""; + uint32_t reason_len = 0; + if (mp_typeof(**data) == MP_STR) + reason = mp_decode_str(data, &reason_len); + box_error_raise(errcode, "%.*s", reason_len, reason); + error = box_error_last(); + error_ref(error); + continue; + } + mp_next(data); /* skip value */ + } + if (error == NULL) { + /* + * Error body is missing in the response. + * Set the error code without a 'reason' message + */ + box_error_raise(errcode, ""); + error = box_error_last(); + error_ref(error); + } + luaT_pusherror(L, error); + error_unref(error); + return 1; +} + int luaopen_net_box(struct lua_State *L) { @@ -1090,6 +1149,7 @@ luaopen_net_box(struct lua_State *L) { "encode_method", netbox_encode_method }, { "decode_greeting",netbox_decode_greeting }, { "decode_method", netbox_decode_method }, + { "decode_error", netbox_decode_error }, { "communicate", netbox_communicate }, { NULL, NULL} }; diff --git a/src/box/lua/net_box.lua b/src/box/lua/net_box.lua index 9e41d6c0844b..d7394b088752 100644 --- a/src/box/lua/net_box.lua +++ b/src/box/lua/net_box.lua @@ -27,6 +27,7 @@ local encode_auth = internal.encode_auth local encode_method = internal.encode_method local decode_greeting = internal.decode_greeting local decode_method = internal.decode_method +local decode_error = internal.decode_error local TIMEOUT_INFINITY = 500 * 365 * 86400 local VSPACE_ID = 281 @@ -73,14 +74,6 @@ local M_COUNT = 16 -- Injects raw data into connection. Used by console and tests. local M_INJECT = 17 -ffi.cdef[[ -struct error * -error_unpack_unsafe(const char **data); - -void -error_unref(struct error *e); -]] - -- utility tables local is_final_state = {closed = 1, error = 1} @@ -97,29 +90,6 @@ local function version_at_least(peer_version_id, major, minor, patch) return peer_version_id >= version_id(major, minor, patch) end -local function decode_error(raw_data) - local ptr = ffi.new('const char *[1]', raw_data) - local err = ffi.C.error_unpack_unsafe(ptr) - if err ~= nil then - err._refs = err._refs + 1 - -- From FFI it is returned as 'struct error *', which is - -- not considered equal to 'const struct error &', and is - -- is not accepted by functions like box.error(). Need to - -- cast explicitly. - err = ffi.cast('const struct error &', err) - err = ffi.gc(err, ffi.C.error_unref) - else - -- Error unpacker installs fail reason into diag. - box.error() - end - return err, ptr[0] -end - -local response_decoder = { - [IPROTO_ERROR_24] = decode, - [IPROTO_ERROR] = decode_error, -} - local function next_id(id) return band(id + 1, 0x7FFFFFFF) end -- @@ -538,42 +508,14 @@ local function create_transport(host, port, user, password, callback, return end local status = hdr[IPROTO_STATUS_KEY] - local body local body_len = body_end - body_rpos if status > IPROTO_CHUNK_KEY then -- Handle errors requests[id] = nil request.id = nil - local map_len, key - map_len, body_rpos = decode_map_header(body_rpos, body_len) - -- Reserve for 2 keys and 2 array indexes. Because no - -- any guarantees how Lua will decide to save the - -- sparse table. - body = table_new(2, 2) - for _ = 1, map_len do - key, body_rpos = decode(body_rpos) - local rdec = response_decoder[key] - if rdec then - body[key], body_rpos = rdec(body_rpos) - else - _, body_rpos = decode(body_rpos) - end - end - assert(body_end == body_rpos, "invalid xrow length") request.errno = band(status, IPROTO_ERRNO_MASK) - -- IPROTO_ERROR comprises error encoded with - -- IPROTO_ERROR_24, so we may ignore content of that - -- key. - if body[IPROTO_ERROR] ~= nil then - request.response = body[IPROTO_ERROR] - assert(type(request.response) == 'cdata') - else - request.response = box.error.new({ - code = request.errno, - reason = body[IPROTO_ERROR_24] - }) - end + request.response = decode_error(body_rpos, request.errno) request.cond:broadcast() return end -- 2.25.1