Tarantool development patches archive
 help / color / mirror / Atom feed
From: Ilya Kosarev <i.kosarev@tarantool.org>
To: tarantool-patches@freelists.org
Cc: georgy@tarantool.org, vdavydov.dev@gmail.com,
	i.kosarev@corp.mail.ru, Ilya Kosarev <i.kosarev@tarantool.org>
Subject: [PATCH v3] iproto: report active connections number
Date: Tue, 26 Feb 2019 21:04:20 +0300	[thread overview]
Message-ID: <20190226180420.3129-1-i.kosarev@tarantool.org> (raw)

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 v3:
  - CONNECTIONS counter is not rmeanish anymore
  - fiber.sleep replaced with test_run:wait_cond in test

 src/box/iproto.cc          |  3 ++-
 src/box/lua/stat.c         | 44 ++++++++++++++++++++++++++++++++++++++
 test/box/stat_net.result   | 37 ++++++++++++++++++++++++++++++++
 test/box/stat_net.test.lua | 13 +++++++++++
 4 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index 863eb4f06..1c506d9c8 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -271,6 +271,7 @@ enum rmean_net_name {
 };
 
 const char *rmean_net_strings[IPROTO_LAST] = { "SENT", "RECEIVED" };
+const char *conn_n_name = "CONNECTIONS";
 
 static void
 tx_process_destroy(struct cmsg *m);
@@ -475,7 +476,7 @@ struct iproto_connection
 	char salt[IPROTO_SALT_SIZE];
 };
 
-static struct mempool iproto_connection_pool;
+struct mempool iproto_connection_pool;
 static RLIST_HEAD(stopped_connections);
 
 /**
diff --git a/src/box/lua/stat.c b/src/box/lua/stat.c
index 3fce81f61..cedfaba8a 100644
--- a/src/box/lua/stat.c
+++ b/src/box/lua/stat.c
@@ -45,12 +45,15 @@
 #include <info.h>
 #include "lua/info.h"
 #include "lua/utils.h"
+#include "small/mempool.h"
 
 extern struct rmean *rmean_box;
 extern struct rmean *rmean_error;
 /** network statistics (iproto & cbus) */
 extern struct rmean *rmean_net;
 extern struct rmean *rmean_tx_wal_bus;
+extern struct mempool iproto_connection_pool;
+extern const char *conn_n_name;
 
 static void
 fill_stat_item(struct lua_State *L, int rps, int64_t total)
@@ -79,6 +82,29 @@ set_stat_item(const char *name, int rps, int64_t total, void *cb_ctx)
 	return 0;
 }
 
+static void
+fill_conn_n(struct lua_State *L)
+{
+	lua_pushstring(L, "total");
+	lua_pushnumber(L, mempool_count(&iproto_connection_pool));
+	lua_settable(L, -3);
+}
+
+static int
+set_conn_n(void *cb_ctx)
+{
+	struct lua_State *L = (struct lua_State *) cb_ctx;
+
+	lua_pushstring(L, conn_n_name);
+	lua_newtable(L);
+
+	fill_conn_n(L);
+
+	lua_settable(L, -3);
+
+	return 0;
+}
+
 /**
  * A stat_foreach() callback used to handle access to e.g.
  * box.stats.DELETE.
@@ -96,6 +122,19 @@ seek_stat_item(const char *name, int rps, int64_t total, void *cb_ctx)
 	return 1;
 }
 
+static int
+seek_conn_n(void *cb_ctx)
+{
+	struct lua_State *L = (struct lua_State *) cb_ctx;
+	if (strcmp(conn_n_name, lua_tostring(L, -1)) != 0)
+		return 0;
+
+	lua_newtable(L);
+	fill_conn_n(L);
+
+	return 1;
+}
+
 static int
 lbox_stat_index(struct lua_State *L)
 {
@@ -140,6 +179,9 @@ static int
 lbox_stat_net_index(struct lua_State *L)
 {
 	luaL_checkstring(L, -1);
+	int res = seek_conn_n(L);
+	if (res)
+		return res;
 	return rmean_foreach(rmean_net, seek_stat_item, L);
 }
 
@@ -148,6 +190,8 @@ lbox_stat_net_call(struct lua_State *L)
 {
 	lua_newtable(L);
 	rmean_foreach(rmean_net, set_stat_item, L);
+	set_conn_n(L);
+
 	return 1;
 }
 
diff --git a/test/box/stat_net.result b/test/box/stat_net.result
index b3e3db11f..99d5efcdc 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.total == 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.total == 2 end, WAIT_COND_TIMEOUT)
+---
+- true
+...
+cn3:close()
+---
+...
+test_run:wait_cond(function() return box.stat.net.CONNECTIONS.total == 1 end, WAIT_COND_TIMEOUT)
+---
+- true
+...
 -- reset
 box.stat.reset()
 ---
@@ -60,6 +93,10 @@ box.stat.net.RECEIVED.total
 ---
 - 0
 ...
+box.stat.net.CONNECTIONS.total
+---
+- 1
+...
 space:drop() -- tweedledum
 ---
 ...
diff --git a/test/box/stat_net.test.lua b/test/box/stat_net.test.lua
index 808bb71e7..50558888f 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.total == 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.total == 2 end, WAIT_COND_TIMEOUT)
+cn3:close()
+test_run:wait_cond(function() return box.stat.net.CONNECTIONS.total == 1 end, WAIT_COND_TIMEOUT)
+
 -- reset
 box.stat.reset()
 box.stat.net.SENT.total
 box.stat.net.RECEIVED.total
+box.stat.net.CONNECTIONS.total
 
 space:drop() -- tweedledum
 cn:close()
-- 
2.17.1

             reply	other threads:[~2019-02-26 18:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-26 18:04 Ilya Kosarev [this message]
2019-02-27  8:50 ` Vladimir Davydov

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=20190226180420.3129-1-i.kosarev@tarantool.org \
    --to=i.kosarev@tarantool.org \
    --cc=georgy@tarantool.org \
    --cc=i.kosarev@corp.mail.ru \
    --cc=tarantool-patches@freelists.org \
    --cc=vdavydov.dev@gmail.com \
    --subject='Re: [PATCH v3] iproto: report active connections number' \
    /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