snowflake changes from global static variables to one instance per thread

This commit is contained in:
luwenpeng
2024-08-23 15:21:07 +08:00
parent 3014e0feef
commit 6808e6ba29
30 changed files with 420 additions and 167 deletions

View File

@@ -59,6 +59,7 @@ struct stellar_thread
uint16_t idx;
uint64_t is_runing;
struct schedule_data sched_data;
struct snowflake *snowflake;
struct ip_reassembly *ip_mgr;
struct session_manager *sess_mgr;
struct stellar *st;
@@ -84,7 +85,10 @@ struct stellar
struct stellar_config config;
};
static thread_local uint16_t __thread_id = 0; // TODO
static thread_local uint16_t __current_thread_idx = UINT16_MAX;
static thread_local struct snowflake *__current_thread_snowflake = NULL;
uint64_t stellar_generate_session_id(uint64_t now_sec);
/******************************************************************************
* Stellar Thread Main Loop
@@ -191,7 +195,9 @@ static void *work_thread(void *arg)
struct packet_io *packet_io = runtime->packet_io;
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr;
uint16_t thr_idx = thread->idx;
__thread_id = thr_idx;
__current_thread_idx = thr_idx;
__current_thread_snowflake = thread->snowflake;
memset(packets, 0, sizeof(packets));
@@ -407,12 +413,19 @@ static int stellar_thread_init(struct stellar *st)
thread->sched_data.merge_stat_interval = config->sched_opts.merge_stat_interval;
thread->sched_data.packet_io_yield_interval = config->sched_opts.packet_io_yield_interval;
thread->snowflake = snowflake_new(i, config->snowflake_opts.snowflake_base, config->snowflake_opts.snowflake_offset);
if (thread->snowflake == NULL)
{
STELLAR_LOG_ERROR("unable to create snowflake id generator");
return -1;
}
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms);
if (thread->sess_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create session manager");
return -1;
}
session_manager_set_session_id_generator(thread->sess_mgr, stellar_generate_session_id);
thread->ip_mgr = ip_reassembly_new(&config->ip_reass_opts);
if (thread->ip_mgr == NULL)
{
@@ -436,8 +449,9 @@ static void stellar_thread_clean(struct stellar *st)
struct stellar_thread *thread = &runtime->threads[i];
if (ATOMIC_READ(&thread->is_runing) == 0)
{
session_manager_free(thread->sess_mgr);
ip_reassembly_free(thread->ip_mgr);
session_manager_free(thread->sess_mgr);
snowflake_free(thread->snowflake);
}
}
STELLAR_LOG_FATAL("worker thread context cleaned");
@@ -526,12 +540,6 @@ struct stellar *stellar_new(const char *stellar_cfg_file, const char *plugin_cfg
}
stellar_config_print(config);
if (snowflake_id_init(&config->snowflake_opts) != 0)
{
STELLAR_LOG_ERROR("unable to init id generator");
goto error_out;
}
runtime->stat = stellar_stat_new(config->pkt_io_opts.nr_threads);
if (runtime->stat == NULL)
{
@@ -666,7 +674,30 @@ struct session_manager *stellar_get_session_manager(const struct stellar *st)
uint16_t stellar_get_current_thread_index()
{
return __thread_id;
if (__current_thread_idx == UINT16_MAX)
{
printf("get current thread index before set\n");
abort();
return UINT16_MAX;
}
else
{
return __current_thread_idx;
}
}
uint64_t stellar_generate_session_id(uint64_t now_sec)
{
if (__current_thread_snowflake == NULL)
{
printf("get current thread snowflake before set\n");
abort();
return 0;
}
else
{
return snowflake_generate(__current_thread_snowflake, now_sec);
}
}
// only send user crafted packet, can't send packet which come from network