From 1368e65988c2877834cbfb473b180adc72ace4a6 Mon Sep 17 00:00:00 2001 From: Scott Godin Date: Wed, 11 Jun 2025 17:15:36 -0400 Subject: [PATCH] Fix issues with Global Allocation Count for drain mode (#1699) Fix issues with Global Allocation Count for drain mode - move increment/decrement logic out of userdb.c and tie to Prometheus logic for allocation tracking instead - log global allocation count decrements at INFO level, when drain mode is on --- src/apps/relay/mainrelay.c | 33 ++++++++++++++++++++++++++ src/apps/relay/mainrelay.h | 11 +++++++++ src/apps/relay/ns_ioalib_engine_impl.c | 3 +++ src/apps/relay/userdb.c | 29 ---------------------- src/apps/relay/userdb.h | 8 ------- 5 files changed, 47 insertions(+), 37 deletions(-) diff --git a/src/apps/relay/mainrelay.c b/src/apps/relay/mainrelay.c index 215201b..ef59df0 100644 --- a/src/apps/relay/mainrelay.c +++ b/src/apps/relay/mainrelay.c @@ -44,6 +44,13 @@ #define MAX_TRIES 3 #endif +#ifdef _MSC_VER +volatile +#else +_Atomic +#endif + size_t global_allocation_count = 0; // used for drain mode, to know when all allocations have gone away + ////// TEMPORARY data ////////// static int use_lt_credentials = 0; @@ -3857,4 +3864,30 @@ static void drain_handler(evutil_socket_t sock, short events, void *args) { UNUSED_ARG(args); } +void increment_global_allocation_count(void) { +#ifdef _MSC_VER + size_t cur_count = InterlockedIncrement(&global_allocation_count); +#else + size_t cur_count = ++global_allocation_count; +#endif + if (turn_params.verbose > TURN_VERBOSE_NONE) { + TURN_LOG_FUNC(TURN_LOG_LEVEL_DEBUG, "Global turn allocation count incremented, now %zu\n", cur_count); + } +} + +void decrement_global_allocation_count(void) { + int log_level = TURN_LOG_LEVEL_DEBUG; + if (turn_params.drain_turn_server) { + log_level = TURN_LOG_LEVEL_INFO; + } +#ifdef _MSC_VER + size_t cur_count = InterlockedDecrement(&global_allocation_count); +#else + size_t cur_count = --global_allocation_count; +#endif + if (turn_params.drain_turn_server || turn_params.verbose > TURN_VERBOSE_NONE) { + TURN_LOG_FUNC(log_level, "Global turn allocation count decremented, now %zu\n", cur_count); + } +} + /////////////////////////////// diff --git a/src/apps/relay/mainrelay.h b/src/apps/relay/mainrelay.h index d80b033..35a0f22 100644 --- a/src/apps/relay/mainrelay.h +++ b/src/apps/relay/mainrelay.h @@ -101,6 +101,14 @@ extern "C" { #endif +#ifdef _MSC_VER +extern volatile +#else +#include +extern _Atomic +#endif + size_t global_allocation_count; // used for drain mode, to know when all allocations have gone away + ////////////// DEFINES //////////////////////////// #define DEFAULT_CONFIG_FILE "turnserver.conf" @@ -409,6 +417,9 @@ int init_ctr(struct ctr_state *state, const unsigned char iv[8]); /////////////////////////////// +void increment_global_allocation_count(void); +void decrement_global_allocation_count(void); + #ifdef __cplusplus } #endif diff --git a/src/apps/relay/ns_ioalib_engine_impl.c b/src/apps/relay/ns_ioalib_engine_impl.c index d97926a..851bd62 100644 --- a/src/apps/relay/ns_ioalib_engine_impl.c +++ b/src/apps/relay/ns_ioalib_engine_impl.c @@ -42,6 +42,7 @@ #include "ns_ioalib_impl.h" +#include "mainrelay.h" #include "prom_server.h" #if TLS_SUPPORTED @@ -3668,6 +3669,7 @@ void turn_report_allocation_set(void *a, turn_time_t lifetime, int refresh) { { if (!refresh) { prom_inc_allocation(get_ioa_socket_type(ss->client_socket)); + increment_global_allocation_count(); } } } @@ -3745,6 +3747,7 @@ void turn_report_allocation_delete(void *a, SOCKET_TYPE socket_type) { true); } prom_dec_allocation(socket_type); + decrement_global_allocation_count(); } } } diff --git a/src/apps/relay/userdb.c b/src/apps/relay/userdb.c index 104e05f..2269e2c 100644 --- a/src/apps/relay/userdb.c +++ b/src/apps/relay/userdb.c @@ -72,13 +72,6 @@ static TURN_MUTEX_DECLARE(o_to_realm_mutex); static ur_string_map *o_to_realm = NULL; static secrets_list_t realms_list; -#ifndef _MSC_VER -_Atomic -#else -volatile -#endif - size_t global_allocation_count = 0; // used for drain mode, to know when all allocations have gone away - static char userdb_type_unknown[] = "Unknown"; static char userdb_type_sqlite[] = "SQLite"; static char userdb_type_postgresql[] = "PostgreSQL"; @@ -695,15 +688,6 @@ int check_new_allocation_quota(uint8_t *user, int oauth, uint8_t *realm) { ur_string_map_unlock(rp->status.alloc_counters); } -#ifndef _MSC_VER - size_t cur_count = ++global_allocation_count; -#else - size_t cur_count = (size_t)InterlockedIncrement((volatile LONG *)&global_allocation_count); -#endif - if (turn_params.verbose > TURN_VERBOSE_NONE) { - TURN_LOG_FUNC(TURN_LOG_LEVEL_DEBUG, "Global turn allocation count incremented, now %ld\n", cur_count); - } - return ret; } @@ -730,19 +714,6 @@ void release_allocation_quota(uint8_t *user, int oauth, uint8_t *realm) { ur_string_map_unlock(rp->status.alloc_counters); free(username); } - - int log_level = TURN_LOG_LEVEL_DEBUG; - if (turn_params.drain_turn_server) { - log_level = TURN_LOG_LEVEL_INFO; - } -#ifndef _MSC_VER - size_t cur_count = --global_allocation_count; -#else - size_t cur_count = (size_t)InterlockedDecrement((volatile LONG *)&global_allocation_count); -#endif - if (turn_params.verbose > TURN_VERBOSE_NONE) { - TURN_LOG_FUNC(log_level, "Global turn allocation count decremented, now %ld\n", cur_count); - } } ////////////////////////////////// diff --git a/src/apps/relay/userdb.h b/src/apps/relay/userdb.h index 4bad8b9..ccd844b 100644 --- a/src/apps/relay/userdb.h +++ b/src/apps/relay/userdb.h @@ -50,14 +50,6 @@ extern "C" { #endif -#ifndef _MSC_VER -#include -extern _Atomic -#else -extern volatile -#endif - size_t global_allocation_count; - //////////// REALM ////////////// struct _realm_status_t;