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 16/20] net.box: store next_request_id in C code
Date: Fri, 23 Jul 2021 14:07:26 +0300	[thread overview]
Message-ID: <15f3d09cd20eb1b5b7e50f41925d8a51fb0f12a4.1627024646.git.vdavydov@tarantool.org> (raw)
In-Reply-To: <cover.1627024646.git.vdavydov@tarantool.org>

Needed to rewrite performance-critical parts of net.box in C, e.g.
iproto_schema_sm.
---
 src/box/lua/net_box.c   | 31 +++++++++++++++++++++++++++++++
 src/box/lua/net_box.lua | 24 +++++++-----------------
 2 files changed, 38 insertions(+), 17 deletions(-)

diff --git a/src/box/lua/net_box.c b/src/box/lua/net_box.c
index 044f7d337ca7..1a615797d485 100644
--- a/src/box/lua/net_box.c
+++ b/src/box/lua/net_box.c
@@ -79,6 +79,8 @@ enum netbox_method {
 };
 
 struct netbox_registry {
+	/* Next request id. */
+	uint64_t next_sync;
 	/* sync -> netbox_request */
 	struct mh_i64ptr_t *requests;
 };
@@ -278,6 +280,7 @@ netbox_request_push_result(struct netbox_request *request, struct lua_State *L)
 static int
 netbox_registry_create(struct netbox_registry *registry)
 {
+	registry->next_sync = 1;
 	registry->requests = mh_i64ptr_new();
 	if (registry->requests == NULL) {
 		diag_set(OutOfMemory, 0, "mhash", "netbox_registry");
@@ -1444,6 +1447,32 @@ luaT_netbox_registry_gc(struct lua_State *L)
 	return 0;
 }
 
+/* Allocates a new id (sync). */
+static int
+luaT_netbox_registry_new_id(struct lua_State *L)
+{
+	struct netbox_registry *registry = luaT_check_netbox_registry(L, 1);
+	luaL_pushuint64(L, registry->next_sync++);
+	return 1;
+}
+
+/*
+ * Returns the next id (sync) without reserving it.
+ * If called with an argument, returns the id following its value.
+ */
+static int
+luaT_netbox_registry_next_id(struct lua_State *L)
+{
+	struct netbox_registry *registry = luaT_check_netbox_registry(L, 1);
+	uint64_t next_sync;
+	if (lua_isnoneornil(L, 2))
+		next_sync = registry->next_sync;
+	else
+		next_sync = luaL_touint64(L, 2) + 1;
+	luaL_pushuint64(L, next_sync);
+	return 1;
+}
+
 static int
 luaT_netbox_registry_reset(struct lua_State *L)
 {
@@ -1798,6 +1827,8 @@ luaopen_net_box(struct lua_State *L)
 {
 	static const struct luaL_Reg netbox_registry_meta[] = {
 		{ "__gc",           luaT_netbox_registry_gc },
+		{ "new_id",         luaT_netbox_registry_new_id },
+		{ "next_id",        luaT_netbox_registry_next_id },
 		{ "reset",          luaT_netbox_registry_reset },
 		{ NULL, NULL }
 	};
diff --git a/src/box/lua/net_box.lua b/src/box/lua/net_box.lua
index 4bc66940ea2a..0643477cbc9c 100644
--- a/src/box/lua/net_box.lua
+++ b/src/box/lua/net_box.lua
@@ -82,8 +82,6 @@ local function version_at_least(peer_version_id, major, minor, patch)
     return peer_version_id >= version_id(major, minor, patch)
 end
 
-local function next_id(id) return band(id + 1, 0x7FFFFFFF) end
-
 --
 -- Connect to a remote server, do handshake.
 -- @param host Hostname.
@@ -192,7 +190,6 @@ local function create_transport(host, port, user, password, callback,
     -- Sync requests are implemented as async call + immediate
     -- wait for a result.
     local requests         = internal.new_registry()
-    local next_request_id  = 1
 
     local worker_fiber
     local send_buf         = buffer.ibuf(buffer.READAHEAD)
@@ -301,9 +298,8 @@ local function create_transport(host, port, user, password, callback,
         if send_buf:size() == 0 then
             worker_fiber:wakeup()
         end
-        local id = next_request_id
+        local id = requests:new_id()
         encode_method(method, send_buf, id, ...)
-        next_request_id = next_id(id)
         return internal.new_request(requests, id, buffer, skip_header, method,
                                     on_push, on_push_ctx, format)
     end
@@ -335,12 +331,6 @@ local function create_transport(host, port, user, password, callback,
         internal.dispatch_response_console(requests, rid, response)
     end
 
-    local function new_request_id()
-        local id = next_request_id;
-        next_request_id = next_id(id)
-        return id
-    end
-
     -- IO (WORKER FIBER) --
     local function send_and_recv_iproto(timeout)
         local hdr, body_rpos, body_end = internal.send_and_recv_iproto(
@@ -396,7 +386,7 @@ local function create_transport(host, port, user, password, callback,
             elseif response ~= '---\n...\n' then
                 return error_sm(E_NO_CONNECTION, 'Unexpected response')
             end
-            local rid = next_request_id
+            local rid = requests:next_id()
             set_state('active')
             return console_sm(rid)
         elseif greeting.protocol == 'Binary' then
@@ -413,7 +403,7 @@ local function create_transport(host, port, user, password, callback,
             return error_sm(err, response)
         else
             dispatch_response_console(rid, response)
-            return console_sm(next_id(rid))
+            return console_sm(requests:next_id(rid))
         end
     end
 
@@ -423,7 +413,7 @@ local function create_transport(host, port, user, password, callback,
             set_state('fetch_schema')
             return iproto_schema_sm()
         end
-        encode_auth(send_buf, new_request_id(), user, password, salt)
+        encode_auth(send_buf, requests:new_id(), user, password, salt)
         local err, hdr, body_rpos = send_and_recv_iproto()
         if err then
             return error_sm(err, hdr)
@@ -444,8 +434,8 @@ local function create_transport(host, port, user, password, callback,
         -- _vcollation view was added in 2.2.0-389-g3e3ef182f
         local peer_has_vcollation = version_at_least(greeting.version_id,
                                                      2, 2, 1)
-        local select1_id = new_request_id()
-        local select2_id = new_request_id()
+        local select1_id = requests:new_id()
+        local select2_id = requests:new_id()
         local select3_id
         local response = {}
         -- fetch everything from space _vspace, 2 = ITER_ALL
@@ -456,7 +446,7 @@ local function create_transport(host, port, user, password, callback,
                       0xFFFFFFFF, nil)
         -- fetch everything from space _vcollation, 2 = ITER_ALL
         if peer_has_vcollation then
-            select3_id = new_request_id()
+            select3_id = requests:new_id()
             encode_method(M_SELECT, send_buf, select3_id, VCOLLATION_ID,
                           0, 2, 0, 0xFFFFFFFF, nil)
         end
-- 
2.25.1


  parent reply	other threads:[~2021-07-23 11:15 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 in C 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 ` [Tarantool-patches] [PATCH 12/20] net.box: rewrite error " Vladimir Davydov via Tarantool-patches
2021-07-30 22:13   ` 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 ` Vladimir Davydov via Tarantool-patches [this message]
2021-08-03 23:06   ` [Tarantool-patches] [PATCH 16/20] net.box: store next_request_id in C code 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=15f3d09cd20eb1b5b7e50f41925d8a51fb0f12a4.1627024646.git.vdavydov@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH 16/20] net.box: store next_request_id in C code' \
    /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