Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH 12/20] net.box: rewrite error decoder in C
Date: Fri, 23 Jul 2021 14:07:22 +0300	[thread overview]
Message-ID: <d74d9cdf3962cee77bc96a73e6b07eeb4a87d6b2.1627024646.git.vdavydov@tarantool.org> (raw)
In-Reply-To: <cover.1627024646.git.vdavydov@tarantool.org>

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 <base64.h>
@@ -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


  parent reply	other threads:[~2021-07-23 11:13 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-23 11:07 [Tarantool-patches] [PATCH 00/20] Rewrite performance critical parts of net.box " Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 01/20] net.box: fix console connection breakage when request is discarded Vladimir Davydov via Tarantool-patches
2021-07-28 22:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 10:40     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 02/20] net.box: wake up wait_result callers " Vladimir Davydov via Tarantool-patches
2021-07-29 10:47   ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 03/20] net.box: do not check worker_fiber in request:result, is_ready Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 04/20] net.box: remove decode_push from method_decoder table Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 05/20] net.box: use decode_tuple instead of decode_get Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 06/20] net.box: rename request.ctx to request.format Vladimir Davydov via Tarantool-patches
2021-07-28 22:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 10:54     ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:39       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30  8:15         ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 07/20] net.box: use integer id instead of method name Vladimir Davydov via Tarantool-patches
2021-07-28 22:50   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 11:30     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 08/20] net.box: remove useless encode optimization Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 09/20] net.box: rewrite request encoder in C Vladimir Davydov via Tarantool-patches
2021-07-28 22:51   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 14:08     ` Vladimir Davydov via Tarantool-patches
2021-07-29 14:10       ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 10/20] lua/utils: make char ptr Lua CTIDs public Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 11/20] net.box: rewrite response decoder in C Vladimir Davydov via Tarantool-patches
2021-07-27 14:07   ` Cyrill Gorcunov via Tarantool-patches
2021-07-27 14:14     ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:39   ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30  8:44     ` Vladimir Davydov via Tarantool-patches
2021-07-30 22:12       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-02  7:36         ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` Vladimir Davydov via Tarantool-patches [this message]
2021-07-30 22:13   ` [Tarantool-patches] [PATCH 12/20] net.box: rewrite error " Vladislav Shpilevoy via Tarantool-patches
2021-08-02  8:00     ` Vladimir Davydov via Tarantool-patches
2021-08-02 21:47       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 13/20] net.box: rewrite send_and_recv_{iproto, console} " Vladimir Davydov via Tarantool-patches
2021-08-02 21:49   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-03 15:44     ` Vladimir Davydov via Tarantool-patches
2021-08-03 23:06       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 13:56         ` Vladimir Davydov via Tarantool-patches
2021-08-04 21:18           ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05  8:37             ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 14/20] net.box: rename netbox_{prepare, encode}_request to {begin, end} Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 15/20] net.box: rewrite request implementation in C Vladimir Davydov via Tarantool-patches
2021-08-02 21:54   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 12:30     ` Vladimir Davydov via Tarantool-patches
2021-08-04 15:35       ` Vladimir Davydov via Tarantool-patches
2021-08-04 16:14         ` Vladimir Davydov via Tarantool-patches
2021-08-04 21:20       ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 12:46         ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 16/20] net.box: store next_request_id in C code Vladimir Davydov via Tarantool-patches
2021-08-03 23:06   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 16:25     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 17/20] net.box: rewrite console handlers in C Vladimir Davydov via Tarantool-patches
2021-08-03 23:07   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 11:53     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 18/20] net.box: rewrite iproto " Vladimir Davydov via Tarantool-patches
2021-08-03 23:08   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 11:54     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 19/20] net.box: merge new_id, new_request and encode_method Vladimir Davydov via Tarantool-patches
2021-08-03 23:08   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 11:55     ` Vladimir Davydov via Tarantool-patches
2021-07-23 11:07 ` [Tarantool-patches] [PATCH 20/20] net.box: do not create request object in Lua for sync requests Vladimir Davydov via Tarantool-patches
2021-08-03 23:09   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-05 12:23     ` Vladimir Davydov via Tarantool-patches
2021-07-23 12:48 ` [Tarantool-patches] [PATCH 00/20] Rewrite performance critical parts of net.box in C Vladimir Davydov via Tarantool-patches
2021-07-26  7:26 ` Kirill Yukhin via Tarantool-patches
2021-07-27  9:59   ` Vladimir Davydov via Tarantool-patches
2021-07-28 22:51 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-29 11:33   ` Vladimir Davydov via Tarantool-patches
2021-07-29 15:23     ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:38       ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30 10:04         ` Vladimir Davydov via Tarantool-patches
2021-07-29 22:40 ` Vladislav Shpilevoy via Tarantool-patches
2021-07-30  8:16   ` Vladimir Davydov via Tarantool-patches
2021-08-03 23:05 ` Vladislav Shpilevoy via Tarantool-patches
2021-08-04 12:40   ` Vladimir Davydov via Tarantool-patches
2021-08-05 20:59 ` Vladislav Shpilevoy via Tarantool-patches
2021-08-09 11:22 ` Igor Munkin via Tarantool-patches
2021-08-09 11:48   ` Vitaliia Ioffe via Tarantool-patches
2021-08-09 13:56     ` Vladimir Davydov via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d74d9cdf3962cee77bc96a73e6b07eeb4a87d6b2.1627024646.git.vdavydov@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 12/20] net.box: rewrite error decoder in C' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox