diff --git a/db/appsock_handler.c b/db/appsock_handler.c index d0bdcfdd1d..19d7ed742d 100644 --- a/db/appsock_handler.c +++ b/db/appsock_handler.c @@ -55,6 +55,8 @@ char appsock_unknown_old[] = "-1 #unknown command\n"; char appsock_unknown[] = "Error: -1 #unknown command\n"; char appsock_supported[] = "supported\n"; int32_t active_appsock_conns = 0; +int32_t pooled_appsock_conns = 0; /* number of connections owned by sockpool or local-cache */ +int32_t appsock_conns = 0; int64_t gbl_denied_appsock_connection_count = 0; pthread_mutex_t appsock_conn_lk = PTHREAD_MUTEX_INITIALIZER; @@ -113,6 +115,7 @@ void appsock_quick_stat(void) { logmsg(LOGMSG_USER, "num appsock connections %llu\n", total_appsock_conns); logmsg(LOGMSG_USER, "num active appsock connections %d\n", active_appsock_conns); + logmsg(LOGMSG_USER, "num pooled appsock connections %d\n", pooled_appsock_conns); logmsg(LOGMSG_USER, "num appsock commands %llu\n", total_toks); } @@ -336,8 +339,8 @@ void appsock_handler_start(struct dbenv *dbenv, SBUF2 *sb, int admin) logmsg( LOGMSG_WARN, - "%s: Maximum appsock connection limit approaching (%d/%d).\n", - __func__, active_appsock_conns, max); + "%s: Maximum appsock connection limit approaching (%d/%d/%d).\n", + __func__, (active_appsock_conns - pooled_appsock_conns), active_appsock_conns, max); lastprint = now; if ((now - last_thread_dump_time) > 10) { diff --git a/db/db_metrics.c b/db/db_metrics.c index cf9399831c..105ca6286e 100644 --- a/db/db_metrics.c +++ b/db/db_metrics.c @@ -570,7 +570,8 @@ int refresh_metrics(void) stats.sql_cost = gbl_nsql_steps + gbl_nnewsql_steps; stats.sql_count = gbl_nsql + gbl_nnewsql; stats.sql_ssl_count = gbl_nnewsql_ssl; - stats.current_connections = net_get_num_current_non_appsock_accepts(thedb->handle_sibling) + active_appsock_conns; + stats.current_connections = net_get_num_current_non_appsock_accepts(thedb->handle_sibling) + + (active_appsock_conns - pooled_appsock_conns); rc = bdb_get_lock_counters(thedb->bdb_env, &stats.deadlocks, &stats.locks_aborted, &stats.lockwaits, diff --git a/db/sqlinterfaces.c b/db/sqlinterfaces.c index 3dcc866b74..f9b80962f6 100644 --- a/db/sqlinterfaces.c +++ b/db/sqlinterfaces.c @@ -6214,6 +6214,10 @@ static const char* connstate_str(enum connection_state s) { void clnt_change_state(struct sqlclntstate *clnt, enum connection_state state) { clnt->state_start_time = comdb2_time_epochms(); Pthread_mutex_lock(&clnt->state_lk); + if (clnt->state == CONNECTION_RESET && state != CONNECTION_RESET) + ATOMIC_ADD32(pooled_appsock_conns, -1); + else if (clnt->state != CONNECTION_RESET && state == CONNECTION_RESET) + ATOMIC_ADD32(pooled_appsock_conns, 1); clnt->state = state; Pthread_mutex_unlock(&clnt->state_lk); } diff --git a/net/net_appsock.h b/net/net_appsock.h index 7afef63777..f78fac1df1 100644 --- a/net/net_appsock.h +++ b/net/net_appsock.h @@ -37,6 +37,7 @@ typedef void(*run_on_base_fn)(void *); void run_on_base(struct event_base *, run_on_base_fn, void *); extern int32_t active_appsock_conns; +extern int32_t pooled_appsock_conns; extern int64_t gbl_denied_appsock_connection_count; #undef SKIP_CHECK_THD diff --git a/plugins/newsql/newsql_evbuffer.c b/plugins/newsql/newsql_evbuffer.c index 7bb61071de..e762cee131 100644 --- a/plugins/newsql/newsql_evbuffer.c +++ b/plugins/newsql/newsql_evbuffer.c @@ -107,6 +107,8 @@ static void free_newsql_appdata_evbuffer(int dummyfd, short what, void *arg) { struct newsql_appdata_evbuffer *appdata = arg; struct sqlclntstate *clnt = &appdata->clnt; + if (clnt->state == CONNECTION_RESET) + ATOMIC_ADD32(pooled_appsock_conns, -1); int fd = appdata->fd; rem_sql_evbuffer(clnt); diff --git a/tests/comdb2sys.test/runit b/tests/comdb2sys.test/runit index f4598239c4..d08bbb5d53 100755 --- a/tests/comdb2sys.test/runit +++ b/tests/comdb2sys.test/runit @@ -130,6 +130,7 @@ tests=( "./testindexusage" "./testtransactionstate" "./testtablemetrics" +"./test_current_connections" ) for t in ${tests[@]}; do diff --git a/tests/comdb2sys.test/test_current_connections b/tests/comdb2sys.test/test_current_connections new file mode 100755 index 0000000000..fb1308d4c7 --- /dev/null +++ b/tests/comdb2sys.test/test_current_connections @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +DB=$1 +default=default +cdb2sql=cdb2sql +set -e + +hostflags= + +if [[ -n "$CLUSTER" ]]; then + read -ra nodes <<< "$CLUSTER" + hostflags="--host ${nodes[0]}" +fi + + +sql='SELECT SLEEP(2)' +for i in $(seq 1 10); do + $cdb2sql ${CDB2_OPTIONS} $hostflags $DB $default "$sql" & +done + +wait + +n=`$cdb2sql --tabs ${CDB2_OPTIONS} $hostflags $DB $default "SELECT value FROM comdb2_metrics WHERE name='current_connections'"` +if [[ $n -ne 1 ]]; then + echo current_connections $n >&2 + exit 1 +fi + +echo $(basename $0) success +exit 0