Tarantool development patches archive
 help / color / mirror / Atom feed
* [PATCH v3] iproto: report active connections number
@ 2019-02-26 18:04 Ilya Kosarev
  2019-02-27  8:50 ` Vladimir Davydov
  0 siblings, 1 reply; 2+ messages in thread
From: Ilya Kosarev @ 2019-02-26 18:04 UTC (permalink / raw)
  To: tarantool-patches; +Cc: georgy, vdavydov.dev, i.kosarev, Ilya Kosarev

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] iproto: report active connections number
  2019-02-26 18:04 [PATCH v3] iproto: report active connections number Ilya Kosarev
@ 2019-02-27  8:50 ` Vladimir Davydov
  0 siblings, 0 replies; 2+ messages in thread
From: Vladimir Davydov @ 2019-02-27  8:50 UTC (permalink / raw)
  To: Ilya Kosarev; +Cc: tarantool-patches, georgy, i.kosarev

On Tue, Feb 26, 2019 at 09:04:20PM +0300, Ilya Kosarev wrote:
> 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";

What's the point in having this string defined in iproto.cc?
It's only used in lua/stat.c.

>  
>  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;

No, please don't export iproto_connection_pool. Instead, add a helper
function to report the connection count, similar to iproto_mem_used().

> +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");

Why not simply print "CONNECTIONS: 10"? I don't see any point in the
"total" field.

> +	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;
> +}
> +

What's the point in making these functions look similar to
seek_stat_item/set_stat_item? They aren't callbacks. I don't
think we even need them - they're trivial so we could inline
them without hurting readability.

>  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;
>  }

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-02-27  8:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-26 18:04 [PATCH v3] iproto: report active connections number Ilya Kosarev
2019-02-27  8:50 ` Vladimir Davydov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox