From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Ilya Kosarev Subject: [PATCH v4] iproto: report active connections number Date: Wed, 27 Feb 2019 17:37:57 +0300 Message-Id: <20190227143757.30000-1-i.kosarev@tarantool.org> To: tarantool-patches@freelists.org Cc: georgy@tarantool.org, vdavydov.dev@gmail.com, i.kosarev@corp.mail.ru, Ilya Kosarev List-ID: Now there is new member in box.stat.net() called "CONNECTIONS" which is a number of active iproto connections. Closes #3905 --- https://github.com/tarantool/tarantool/tree/i.kosarev/gh-3905-report-connections-number https://github.com/tarantool/tarantool/issues/3905 Changes in v4: - CONNECTIONS counter doesn't have extra lua tables inside anymore - refactoring src/box/iproto.cc | 6 ++++++ src/box/iproto.h | 6 ++++++ src/box/lua/stat.c | 11 +++++++++++ test/box/stat_net.result | 37 +++++++++++++++++++++++++++++++++++++ test/box/stat_net.test.lua | 13 +++++++++++++ 5 files changed, 73 insertions(+) diff --git a/src/box/iproto.cc b/src/box/iproto.cc index 863eb4f06..d6dafe155 100644 --- a/src/box/iproto.cc +++ b/src/box/iproto.cc @@ -2121,6 +2121,12 @@ iproto_mem_used(void) return slab_cache_used(&net_cord.slabc) + slab_cache_used(&net_slabc); } +size_t +iproto_connections_n(void) +{ + return mempool_count(&iproto_connection_pool); +} + void iproto_reset_stat(void) { diff --git a/src/box/iproto.h b/src/box/iproto.h index 8f3607ffc..0d53d13ed 100644 --- a/src/box/iproto.h +++ b/src/box/iproto.h @@ -62,6 +62,12 @@ extern unsigned iproto_readahead; size_t iproto_mem_used(void); +/** + * Return number of active iproto connections + */ +size_t +iproto_connections_n(void); + /** * Reset network statistics. */ diff --git a/src/box/lua/stat.c b/src/box/lua/stat.c index 3fce81f61..f8b9b581e 100644 --- a/src/box/lua/stat.c +++ b/src/box/lua/stat.c @@ -52,6 +52,8 @@ extern struct rmean *rmean_error; extern struct rmean *rmean_net; extern struct rmean *rmean_tx_wal_bus; +static const char *conn_n_name = "CONNECTIONS"; + static void fill_stat_item(struct lua_State *L, int rps, int64_t total) { @@ -140,6 +142,10 @@ static int lbox_stat_net_index(struct lua_State *L) { luaL_checkstring(L, -1); + if (strcmp(conn_n_name, lua_tostring(L, -1)) == 0) { + lua_pushnumber(L, iproto_connections_n()); + return 1; + } return rmean_foreach(rmean_net, seek_stat_item, L); } @@ -148,6 +154,11 @@ lbox_stat_net_call(struct lua_State *L) { lua_newtable(L); rmean_foreach(rmean_net, set_stat_item, L); + + lua_pushstring(L, conn_n_name); + lua_pushnumber(L, iproto_connections_n()); + lua_settable(L, -3); + return 1; } diff --git a/test/box/stat_net.result b/test/box/stat_net.result index b3e3db11f..639a830d8 100644 --- a/test/box/stat_net.result +++ b/test/box/stat_net.result @@ -34,6 +34,15 @@ LISTEN = require('uri').parse(box.cfg.listen) cn = remote.connect(LISTEN.host, LISTEN.service) --- ... +cn1 = remote.connect(LISTEN.host, LISTEN.service) +--- +... +cn2 = remote.connect(LISTEN.host, LISTEN.service) +--- +... +cn3 = remote.connect(LISTEN.host, LISTEN.service) +--- +... cn.space.tweedledum:select() --small request --- - [] @@ -46,8 +55,32 @@ box.stat.net.RECEIVED.total > 0 --- - true ... +box.stat.net.CONNECTIONS == 4 +--- +- true +... -- box.stat.net.EVENTS.total > 0 -- box.stat.net.LOCKS.total > 0 +WAIT_COND_TIMEOUT = 10 +--- +... +cn1:close() +--- +... +cn2:close() +--- +... +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 2 end, WAIT_COND_TIMEOUT) +--- +- true +... +cn3:close() +--- +... +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 1 end, WAIT_COND_TIMEOUT) +--- +- true +... -- reset box.stat.reset() --- @@ -60,6 +93,10 @@ box.stat.net.RECEIVED.total --- - 0 ... +box.stat.net.CONNECTIONS +--- +- 1 +... space:drop() -- tweedledum --- ... diff --git a/test/box/stat_net.test.lua b/test/box/stat_net.test.lua index 808bb71e7..997376997 100644 --- a/test/box/stat_net.test.lua +++ b/test/box/stat_net.test.lua @@ -13,18 +13,31 @@ remote = require 'net.box' LISTEN = require('uri').parse(box.cfg.listen) cn = remote.connect(LISTEN.host, LISTEN.service) +cn1 = remote.connect(LISTEN.host, LISTEN.service) +cn2 = remote.connect(LISTEN.host, LISTEN.service) +cn3 = remote.connect(LISTEN.host, LISTEN.service) cn.space.tweedledum:select() --small request box.stat.net.SENT.total > 0 box.stat.net.RECEIVED.total > 0 +box.stat.net.CONNECTIONS == 4 -- box.stat.net.EVENTS.total > 0 -- box.stat.net.LOCKS.total > 0 +WAIT_COND_TIMEOUT = 10 + +cn1:close() +cn2:close() +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 2 end, WAIT_COND_TIMEOUT) +cn3:close() +test_run:wait_cond(function() return box.stat.net.CONNECTIONS == 1 end, WAIT_COND_TIMEOUT) + -- reset box.stat.reset() box.stat.net.SENT.total box.stat.net.RECEIVED.total +box.stat.net.CONNECTIONS space:drop() -- tweedledum cn:close() -- 2.17.1