From 83bffdd008091a493c17d9ed7eb0690c2eaafb4e Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Tue, 25 Jun 2024 19:12:21 +0800 Subject: [PATCH] refactor stellar config --- conf/stellar.toml | 6 +- src/id_generator/id_generator.cpp | 46 ++++++------- src/id_generator/id_generator.h | 18 ++--- src/stellar/CMakeLists.txt | 2 +- src/stellar/config.h | 31 --------- src/stellar/stellar.cpp | 28 ++++---- .../{config.cpp => stellar_config.cpp} | 68 +++++++++---------- src/stellar/stellar_config.h | 26 +++++++ test/packet_inject/conf/stellar.toml | 6 +- 9 files changed, 113 insertions(+), 118 deletions(-) delete mode 100644 src/stellar/config.h rename src/stellar/{config.cpp => stellar_config.cpp} (88%) create mode 100644 src/stellar/stellar_config.h diff --git a/conf/stellar.toml b/conf/stellar.toml index 2b14c2f..d50c2cf 100644 --- a/conf/stellar.toml +++ b/conf/stellar.toml @@ -1,6 +1,6 @@ -[device] -base = 1 # [0, 31] -offset = 2 # [0, 127] +[id_generator] +snowflake_worker_id_base = 1 # [0, 31] +snowflake_worker_id_offset = 2 # [0, 127] [packet_io] mode = dumpfile # dumpfile, marsio diff --git a/src/id_generator/id_generator.cpp b/src/id_generator/id_generator.cpp index 8cc68ab..4460735 100644 --- a/src/id_generator/id_generator.cpp +++ b/src/id_generator/id_generator.cpp @@ -12,51 +12,51 @@ struct id_generator { - uint8_t device_base; // 5bit [0, 31] - uint8_t device_offset; // 7bit [0, 127] - uint64_t device_id; // 12bit [0, 4095] ( base << 7 | offset ) + uint8_t snowflake_worker_id_base; // 5bit [0, 31] + uint8_t snowflake_worker_id_offset; // 7bit [0, 127] + uint64_t device_id; // 12bit [0, 4095] ( base << 7 | offset ) uint64_t thread_volatile[MAX_THREAD_NUM]; }; struct id_generator global_id_generator; -/* - * device_base (5bit) : [0, 31] - * device_offset (7bit) : [0, 127] - * device_id (12bit) : (base << 7 | offset) - * - * return 0: success - * return -1: failed - */ -int id_generator_init(uint8_t device_base, uint8_t device_offset) +// return 0: success +// return -1: failed +int id_generator_init(const struct id_generator_options *opts) { memset(&global_id_generator, 0, sizeof(struct id_generator)); - if (device_base > 31) + if (opts == NULL) { - ID_GENERATOR_LOG_ERROR("device_base %u is invalid, range [0, 31]", device_base); + ID_GENERATOR_LOG_ERROR("opts is NULL"); return -1; } - if (device_offset > 127) + if (opts->snowflake_worker_id_base > 31) { - ID_GENERATOR_LOG_ERROR("device_offset %u is invalid, range [0, 127]", device_offset); + ID_GENERATOR_LOG_ERROR("snowflake_worker_id_base %u is invalid, range [0, 31]", opts->snowflake_worker_id_base); return -1; } - global_id_generator.device_base = device_base; - global_id_generator.device_offset = device_offset; - global_id_generator.device_id = ((global_id_generator.device_base << 7) | global_id_generator.device_offset) & 0xFFF; + if (opts->snowflake_worker_id_offset > 127) + { + ID_GENERATOR_LOG_ERROR("snowflake_worker_id_offset %u is invalid, range [0, 127]", opts->snowflake_worker_id_offset); + return -1; + } - ID_GENERATOR_LOG_DEBUG("device_base: %u, device_offset: %u, device_id: %u", - global_id_generator.device_base, - global_id_generator.device_offset, + global_id_generator.snowflake_worker_id_base = opts->snowflake_worker_id_base; + global_id_generator.snowflake_worker_id_offset = opts->snowflake_worker_id_offset; + global_id_generator.device_id = ((global_id_generator.snowflake_worker_id_base << 7) | global_id_generator.snowflake_worker_id_offset) & 0xFFF; + + ID_GENERATOR_LOG_DEBUG("snowflake_worker_id_base: %u, snowflake_worker_id_offset: %u, device_id: %u", + global_id_generator.snowflake_worker_id_base, + global_id_generator.snowflake_worker_id_offset, global_id_generator.device_id); return 0; } /* - * 高位 --- 低位 + * high -> low * * +------+------------------+----------------+------------------------+---------------------------+ * | 1bit | 12bit device_id | 8bit thread_id | 28bit timestamp in sec | 15bit sequence per thread | diff --git a/src/id_generator/id_generator.h b/src/id_generator/id_generator.h index 1f356ef..2627860 100644 --- a/src/id_generator/id_generator.h +++ b/src/id_generator/id_generator.h @@ -7,15 +7,15 @@ extern "C" #include -/* - * device_base (5bit) : [0, 31] - * device_offset (7bit) : [0, 127] - * device_id (12bit) : (base << 7 | offset) - * - * return 0: success - * return -1: failed - */ -int id_generator_init(uint8_t device_base, uint8_t device_offset); +struct id_generator_options +{ + uint8_t snowflake_worker_id_base; + uint8_t snowflake_worker_id_offset; +}; + +// return 0: success +// return -1: failed +int id_generator_init(const struct id_generator_options *opts); uint64_t id_generator_alloc(); #ifdef __cplusplus diff --git a/src/stellar/CMakeLists.txt b/src/stellar/CMakeLists.txt index c199117..588a1e9 100644 --- a/src/stellar/CMakeLists.txt +++ b/src/stellar/CMakeLists.txt @@ -1,4 +1,4 @@ -set(SOURCE config.cpp stellar_stat.cpp stellar.cpp inject.cpp) +set(SOURCE stellar_config.cpp stellar_stat.cpp stellar.cpp inject.cpp) set(LIBRARY times plugin_manager session_manager ip_reassembly packet_io pthread fieldstat4 toml) add_library(stellar_core STATIC ${SOURCE}) diff --git a/src/stellar/config.h b/src/stellar/config.h deleted file mode 100644 index abc61f8..0000000 --- a/src/stellar/config.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "packet_io.h" -#include "ip_reassembly.h" -#include "session_manager.h" - -struct device_options -{ - uint8_t base; - uint8_t offset; -}; - -struct stellar_config -{ - struct device_options dev_opts; - struct packet_io_options io_opts; - struct ip_reassembly_options ip_opts; - struct session_manager_options sess_mgr_opts; -}; - -int stellar_load_config(const char *file, struct stellar_config *config); -void stellar_print_config(struct stellar_config *config); - -#ifdef __cplusplus -} -#endif diff --git a/src/stellar/stellar.cpp b/src/stellar/stellar.cpp index 9e9d419..f85377c 100644 --- a/src/stellar/stellar.cpp +++ b/src/stellar/stellar.cpp @@ -10,13 +10,13 @@ #include "log.h" #include "logo.h" #include "times.h" -#include "config.h" #include "packet_def.h" #include "packet_utils.h" #include "session_utils.h" #include "id_generator.h" #include "stellar_stat.h" #include "stellar_utils.h" +#include "stellar_config.h" #include "plugin_manager.h" #define STELLAR_LOG_STATE(format, ...) LOG_STATE("stellar", format, ##__VA_ARGS__) @@ -319,7 +319,7 @@ static void signal_handler(int signo) static int all_session_have_freed(struct stellar_runtime *runtime, struct stellar_config *config) { - for (int i = 0; i < config->io_opts.nr_threads; i++) + for (int i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct session_manager *sess_mgr = runtime->threads[i].sess_mgr; struct session_manager_stat *sess_stat = session_manager_stat(sess_mgr); @@ -341,7 +341,7 @@ static int all_session_have_freed(struct stellar_runtime *runtime, struct stella static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config) { uint64_t now = stellar_get_monotonic_time_msec(); - for (uint16_t i = 0; i < config->io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; thread->idx = i; @@ -353,7 +353,7 @@ static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_c STELLAR_LOG_ERROR("unable to create session manager"); return -1; } - thread->ip_mgr = ip_reassembly_new(&config->ip_opts); + thread->ip_mgr = ip_reassembly_new(&config->ip_reass_opts); if (thread->ip_mgr == NULL) { STELLAR_LOG_ERROR("unable to create ip reassemble manager"); @@ -367,7 +367,7 @@ static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_c static void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar_config *config) { - for (uint16_t i = 0; i < config->io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; if (ATOMIC_READ(&thread->is_runing) == 0) @@ -381,7 +381,7 @@ static void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar static int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_config *config) { - for (uint16_t i = 0; i < config->io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; if (pthread_create(&thread->tid, NULL, work_thread, (void *)thread) < 0) @@ -396,7 +396,7 @@ static int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_co static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config *config) { - for (uint16_t i = 0; i < config->io_opts.nr_threads; i++) + for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; while (ATOMIC_READ(&thread->is_runing) == 1) @@ -410,8 +410,8 @@ static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_ int stellar_main(int argc, char **argv) { static struct stellar st = {0}; - static struct stellar_runtime *runtime = &st.runtime; - static struct stellar_config *config = &st.config; + struct stellar_runtime *runtime = &st.runtime; + struct stellar_config *config = &st.config; stellar_update_time_cache(); @@ -430,20 +430,20 @@ int stellar_main(int argc, char **argv) STELLAR_LOG_STATE("main config file : %s", main_config_file); STELLAR_LOG_STATE("plugin config file : %s", plugin_config_file); - if (stellar_load_config(main_config_file, config) != 0) + if (stellar_config_load(config, main_config_file) != 0) { STELLAR_LOG_ERROR("unable to load config file"); goto error_out; } - stellar_print_config(config); + stellar_config_print(config); - if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0) + if (id_generator_init(&config->id_gen_opts) != 0) { STELLAR_LOG_ERROR("unable to init id generator"); goto error_out; } - runtime->stat = stellar_stat_new(config->io_opts.nr_threads); + runtime->stat = stellar_stat_new(config->pkt_io_opts.nr_threads); if (runtime->stat == NULL) { STELLAR_LOG_ERROR("unable to create stellar stat"); @@ -456,7 +456,7 @@ int stellar_main(int argc, char **argv) goto error_out; } - runtime->packet_io = packet_io_new(&config->io_opts); + runtime->packet_io = packet_io_new(&config->pkt_io_opts); if (runtime->packet_io == NULL) { STELLAR_LOG_ERROR("unable to create packet io"); diff --git a/src/stellar/config.cpp b/src/stellar/stellar_config.cpp similarity index 88% rename from src/stellar/config.cpp rename to src/stellar/stellar_config.cpp index d211257..6dc9177 100644 --- a/src/stellar/config.cpp +++ b/src/stellar/stellar_config.cpp @@ -4,40 +4,40 @@ #include "log.h" #include "toml.h" -#include "config.h" +#include "stellar_config.h" #define CONFIG_LOG_ERROR(format, ...) LOG_ERROR("config", format, ##__VA_ARGS__) #define CONFIG_LOG_DEBUG(format, ...) LOG_DEBUG("config", format, ##__VA_ARGS__) // return 0: success // retuun -1: failed -static int parse_device_section(toml_table_t *root, struct device_options *opts) +static int parse_id_generator_section(toml_table_t *root, struct id_generator_options *opts) { const char *ptr; toml_table_t *table; - table = toml_table_in(root, "device"); + table = toml_table_in(root, "id_generator"); if (table == NULL) { - CONFIG_LOG_ERROR("config file missing device section"); + CONFIG_LOG_ERROR("config file missing id_generator section"); return -1; } - ptr = toml_raw_in(table, "base"); + ptr = toml_raw_in(table, "snowflake_worker_id_base"); if (ptr == NULL) { - CONFIG_LOG_ERROR("config file missing device->base"); + CONFIG_LOG_ERROR("config file missing id_generator->snowflake_worker_id_base"); return -1; } - opts->base = atoi(ptr); + opts->snowflake_worker_id_base = atoi(ptr); - ptr = toml_raw_in(table, "offset"); + ptr = toml_raw_in(table, "snowflake_worker_id_offset"); if (ptr == NULL) { - CONFIG_LOG_ERROR("config file missing device->offset"); + CONFIG_LOG_ERROR("config file missing id_generator->snowflake_worker_id_offset"); return -1; } - opts->offset = atoi(ptr); + opts->snowflake_worker_id_offset = atoi(ptr); return 0; } @@ -407,7 +407,7 @@ static int parse_session_manager_section(toml_table_t *root, struct session_mana // return 0: success // retuun -1: failed -int stellar_load_config(const char *file, struct stellar_config *config) +int stellar_config_load(struct stellar_config *config, const char *file) { int ret = -1; char errbuf[200]; @@ -429,17 +429,17 @@ int stellar_load_config(const char *file, struct stellar_config *config) goto error_out; } - if (parse_device_section(table, &config->dev_opts) != 0) + if (parse_id_generator_section(table, &config->id_gen_opts) != 0) { goto error_out; } - if (parse_packet_io_section(table, &config->io_opts) != 0) + if (parse_packet_io_section(table, &config->pkt_io_opts) != 0) { goto error_out; } - if (parse_ip_reassembly_section(table, &config->ip_opts) != 0) + if (parse_ip_reassembly_section(table, &config->ip_reass_opts) != 0) { goto error_out; } @@ -465,44 +465,44 @@ error_out: return ret; } -void stellar_print_config(struct stellar_config *config) +void stellar_config_print(const struct stellar_config *config) { if (config == NULL) { return; } - struct device_options *dev_opts = &config->dev_opts; - struct packet_io_options *io_opts = &config->io_opts; - struct ip_reassembly_options *ip_opts = &config->ip_opts; - struct session_manager_options *sess_mgr_opts = &config->sess_mgr_opts; + const struct packet_io_options *pkt_io_opts = &config->pkt_io_opts; + const struct id_generator_options *id_gen_opts = &config->id_gen_opts; + const struct ip_reassembly_options *ip_reass_opts = &config->ip_reass_opts; + const struct session_manager_options *sess_mgr_opts = &config->sess_mgr_opts; - // device config - CONFIG_LOG_DEBUG("device->base : %d", dev_opts->base); - CONFIG_LOG_DEBUG("device->offset : %d", dev_opts->offset); + // id_generator config + CONFIG_LOG_DEBUG("id_generator->snowflake_worker_id_base : %d", id_gen_opts->snowflake_worker_id_base); + CONFIG_LOG_DEBUG("id_generator->snowflake_worker_id_offset : %d", id_gen_opts->snowflake_worker_id_offset); // packet io config - CONFIG_LOG_DEBUG("packet_io->mode : %s", io_opts->mode == PACKET_IO_DUMPFILE ? "dumpfile" : "marsio"); - if (io_opts->mode == PACKET_IO_DUMPFILE) + CONFIG_LOG_DEBUG("packet_io->mode : %s", pkt_io_opts->mode == PACKET_IO_DUMPFILE ? "dumpfile" : "marsio"); + if (pkt_io_opts->mode == PACKET_IO_DUMPFILE) { - CONFIG_LOG_DEBUG("packet_io->dumpfile_dir : %s", io_opts->dumpfile_dir); + CONFIG_LOG_DEBUG("packet_io->dumpfile_dir : %s", pkt_io_opts->dumpfile_dir); } else { - CONFIG_LOG_DEBUG("packet_io->app_symbol : %s", io_opts->app_symbol); - CONFIG_LOG_DEBUG("packet_io->dev_symbol : %s", io_opts->dev_symbol); + CONFIG_LOG_DEBUG("packet_io->app_symbol : %s", pkt_io_opts->app_symbol); + CONFIG_LOG_DEBUG("packet_io->dev_symbol : %s", pkt_io_opts->dev_symbol); } - CONFIG_LOG_DEBUG("packet_io->nr_threads : %d", io_opts->nr_threads); - for (uint16_t i = 0; i < io_opts->nr_threads; i++) + CONFIG_LOG_DEBUG("packet_io->nr_threads : %d", pkt_io_opts->nr_threads); + for (uint16_t i = 0; i < pkt_io_opts->nr_threads; i++) { - CONFIG_LOG_DEBUG("packet_io->cpu_mask[%3d] : %d", i, io_opts->cpu_mask[i]); + CONFIG_LOG_DEBUG("packet_io->cpu_mask[%3d] : %d", i, pkt_io_opts->cpu_mask[i]); } // ip reassemble config - CONFIG_LOG_DEBUG("ip_reassembly->enable : %d", ip_opts->enable); - CONFIG_LOG_DEBUG("ip_reassembly->timeout : %d", ip_opts->timeout); - CONFIG_LOG_DEBUG("ip_reassembly->bucket_entries : %d", ip_opts->bucket_entries); - CONFIG_LOG_DEBUG("ip_reassembly->bucket_num : %d", ip_opts->bucket_num); + CONFIG_LOG_DEBUG("ip_reassembly->enable : %d", ip_reass_opts->enable); + CONFIG_LOG_DEBUG("ip_reassembly->timeout : %d", ip_reass_opts->timeout); + CONFIG_LOG_DEBUG("ip_reassembly->bucket_entries : %d", ip_reass_opts->bucket_entries); + CONFIG_LOG_DEBUG("ip_reassembly->bucket_num : %d", ip_reass_opts->bucket_num); // session manager config -> max session number CONFIG_LOG_DEBUG("session_manager->max_tcp_session_num : %ld", sess_mgr_opts->max_tcp_session_num); diff --git a/src/stellar/stellar_config.h b/src/stellar/stellar_config.h new file mode 100644 index 0000000..d0724d1 --- /dev/null +++ b/src/stellar/stellar_config.h @@ -0,0 +1,26 @@ +#pragma once + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "packet_io.h" +#include "id_generator.h" +#include "ip_reassembly.h" +#include "session_manager.h" + +struct stellar_config +{ + struct packet_io_options pkt_io_opts; + struct id_generator_options id_gen_opts; + struct ip_reassembly_options ip_reass_opts; + struct session_manager_options sess_mgr_opts; +}; + +int stellar_config_load(struct stellar_config *config, const char *file); +void stellar_config_print(const struct stellar_config *config); + +#ifdef __cplusplus +} +#endif diff --git a/test/packet_inject/conf/stellar.toml b/test/packet_inject/conf/stellar.toml index c6f94ea..171a42a 100644 --- a/test/packet_inject/conf/stellar.toml +++ b/test/packet_inject/conf/stellar.toml @@ -1,6 +1,6 @@ -[device] -base = 1 # [0, 31] -offset = 2 # [0, 127] +[id_generator] +snowflake_worker_id_base = 1 # [0, 31] +snowflake_worker_id_offset = 2 # [0, 127] [packet_io] mode = marsio # dumpfile, marsio