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
This commit is contained in:
Scott Godin 2025-06-11 17:15:36 -04:00 committed by GitHub
parent 14e6919996
commit 1368e65988
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 37 deletions

View File

@ -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);
}
}
///////////////////////////////

View File

@ -101,6 +101,14 @@
extern "C" {
#endif
#ifdef _MSC_VER
extern volatile
#else
#include <stdatomic.h>
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

View File

@ -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();
}
}
}

View File

@ -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);
}
}
//////////////////////////////////

View File

@ -50,14 +50,6 @@
extern "C" {
#endif
#ifndef _MSC_VER
#include <stdatomic.h>
extern _Atomic
#else
extern volatile
#endif
size_t global_allocation_count;
//////////// REALM //////////////
struct _realm_status_t;