From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Ilya Kosarev Subject: [PATCH v5] iproto: report active connections number Date: Thu, 28 Feb 2019 14:06:44 +0300 Message-Id: <20190228110644.21074-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 v5: - review-based polishing src/box/iproto.cc | 6 ++++++ src/box/iproto.h | 6 ++++++ src/box/lua/stat.c | 9 +++++++++ test/box/stat_net.result | 37 +++++++++++++++++++++++++++++++++++++ test/box/stat_net.test.lua | 13 +++++++++++++ 5 files changed, 71 insertions(+) diff --git a/src/box/iproto.cc b/src/box/iproto.cc index 863eb4f06..c8b83b16b 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_connection_count(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..7916705ee 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 the number of active iproto connections. + */ +size_t +iproto_connection_count(void); + /** * Reset network statistics. */ diff --git a/src/box/lua/stat.c b/src/box/lua/stat.c index c00315cdd..1015253f4 100644 --- a/src/box/lua/stat.c +++ b/src/box/lua/stat.c @@ -140,6 +140,10 @@ static int lbox_stat_net_index(struct lua_State *L) { luaL_checkstring(L, -1); + if (strcmp("CONNECTIONS", lua_tostring(L, -1)) == 0) { + lua_pushnumber(L, iproto_connection_count()); + return 1; + } return rmean_foreach(rmean_net, seek_stat_item, L); } @@ -148,6 +152,11 @@ lbox_stat_net_call(struct lua_State *L) { lua_newtable(L); rmean_foreach(rmean_net, set_stat_item, L); + + lua_pushstring(L, "CONNECTIONS"); + lua_pushnumber(L, iproto_connection_count()); + 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