[Tarantool-patches] [PATCH 16/20] net.box: store next_request_id in C code

Vladimir Davydov vdavydov at tarantool.org
Fri Jul 23 14:07:26 MSK 2021


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



More information about the Tarantool-patches mailing list