feat(polling manager): support polling manager

This commit is contained in:
yangwei
2024-09-25 18:18:02 +08:00
parent 7291db5969
commit 74f77f3411
11 changed files with 224 additions and 100 deletions

View File

@@ -37,11 +37,7 @@ int stellar_module_manager_get_max_thread_num(struct stellar_module_manager* mod
const char *stellar_module_manager_get_toml_path(struct stellar_module_manager *mod_mgr);
struct mq_schema *stellar_module_manager_get_mq_schema(struct stellar_module_manager *mod_mgr);
typedef void module_on_polling_func(struct stellar_module_manager* mod_mgr, void *polling_arg);
//return 0 if success, otherwise return -1.
int stellar_module_manager_polling_subscribe(struct stellar_module_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg);
void stellar_module_manager_polling_dispatch(struct stellar_module_manager *mod_mgr);
void stellar_module_manager_polling_active(struct stellar_module_manager *mod_mgr);
#ifdef __cplusplus
}

View File

@@ -0,0 +1,21 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/module_manager.h"
struct stellar_polling_manager;
struct stellar_polling_manager *stellar_module_get_polling_manager(struct stellar_module_manager *mod_mgr);
typedef void module_on_polling_func(struct stellar_polling_manager* mod_mgr, void *polling_arg);
//return 0 if success, otherwise return -1.
int stellar_polling_subscribe(struct stellar_polling_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg);
void stellar_polling_active(struct stellar_polling_manager *mod_mgr);
#ifdef __cplusplus
}
#endif

View File

@@ -1,4 +1,4 @@
set(INFRA exdata mq tuple packet_manager packet_io ip_reassembly tcp_reassembly session_manager module_manager)
set(INFRA exdata mq tuple packet_manager packet_io ip_reassembly tcp_reassembly session_manager module_manager polling_manager)
set(DEPS bitmap dablooms interval_tree logger nmx_pool rbtree timeout toml)
#set(DECODERS http lpi)
set(WHOLE_ARCHIVE ${DEPS} ${INFRA} ${DECODERS})

View File

@@ -237,42 +237,4 @@ void stellar_module_set_name(struct stellar_module* mod, const char *name)
memcpy(mod->name, name, MIN(NAME_MAX, strlen(name)));
return;
}
#define TOPIC_POLLING "POLLING"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static void stellar_module_manager_on_polling_dispatch(int topic_id __unused,
void *msg __unused,
on_msg_cb_func* on_msg_cb,
void *on_msg_cb_arg,
void *dispatch_arg)
{
struct stellar_module_manager *mod_mgr=(struct stellar_module_manager *)dispatch_arg;
module_on_polling_func *polling = (module_on_polling_func *)on_msg_cb;
polling(mod_mgr, on_msg_cb_arg);
}
int stellar_module_manager_polling_subscribe(struct stellar_module_manager* mod_mgr, module_on_polling_func on_polling, void *polling_arg)
{
mod_mgr->schema.polling_topic_id=mq_schema_get_topic_id(mod_mgr->schema.mq_schema, TOPIC_POLLING);
if(mod_mgr->schema.polling_topic_id<0)
{
mod_mgr->schema.polling_topic_id=mq_schema_create_topic(mod_mgr->schema.mq_schema, TOPIC_POLLING, stellar_module_manager_on_polling_dispatch, mod_mgr, NULL, NULL);
}
return mq_schema_subscribe(mod_mgr->schema.mq_schema, mod_mgr->schema.polling_topic_id, (on_msg_cb_func *)on_polling, polling_arg);
}
#pragma GCC diagnostic pop
void stellar_module_manager_polling_active(struct stellar_module_manager *mod_mgr)
{
mq_runtime_publish_message(local_mq_rt, mod_mgr->schema.polling_topic_id, NULL);
}
void stellar_module_manager_polling_dispatch(struct stellar_module_manager *mod_mgr)
{
stellar_module_manager_polling_active(mod_mgr);
mq_runtime_dispatch(local_mq_rt);
mq_runtime_clean(local_mq_rt);
return;
}

View File

@@ -40,7 +40,6 @@ struct stellar_module_manager
int load_module_num;
int max_thread_num;
struct mq_schema *mq_schema;
int polling_topic_id;
}schema;
}__attribute__((aligned(sizeof(void*))));

View File

@@ -200,61 +200,6 @@ TEST(module_manager, basic_module) {
}
/***********************************
* TEST MODULE MANAGER POLLING API *
***********************************/
struct test_module_polling_env
{
int N_round;
int polling_count;
int polling_active_count;
};
void test_module_on_polling(struct stellar_module_manager* mod_mgr, void *polling_arg)
{
struct test_module_polling_env *env = (struct test_module_polling_env*)polling_arg;
env->polling_count++;
if(env->polling_count%2==0)
{
stellar_module_manager_polling_active(mod_mgr);
env->polling_active_count++;
}
}
TEST(module_manager_polling, basic_polling_module) {
struct mq_schema *mq_schema=mq_schema_new();
struct stellar_module_manager *mod_mgr=stellar_module_manager_new(NULL, 10, mq_schema);
EXPECT_TRUE(mod_mgr!=NULL);
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema);
struct test_module_polling_env env={};
env.N_round=10;
stellar_module_manager_polling_subscribe(mod_mgr, test_module_on_polling, &env);
struct mq_runtime *mq_rt = mq_runtime_new(mq_schema);
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
EXPECT_EQ((long)stellar_module_manager_get_thread_id(mod_mgr), 1);
EXPECT_EQ(stellar_module_manager_get_mq_runtime(mod_mgr), mq_rt);
for(int i=0; i<env.N_round; i++)
{
stellar_module_manager_polling_dispatch(mod_mgr);
}
stellar_module_manager_free(mod_mgr);
EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count);
}
/**********************************************
* GTEST MAIN *
**********************************************/

View File

@@ -0,0 +1,6 @@
add_library(polling_manager polling_manager.c)
target_include_directories(polling_manager PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_include_directories(polling_manager PUBLIC ${CMAKE_SOURCE_DIR}/include/)
target_link_libraries(polling_manager PUBLIC module_manager ${CMAKE_DL_LIBS})
add_subdirectory(test)

View File

@@ -0,0 +1,70 @@
#include "polling_manager_internal.h"
#include "stellar/utils.h"
struct stellar_polling_manager *stellar_module_get_polling_manager(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return NULL;
struct stellar_module *mod=stellar_module_manager_get_module(mod_mgr, MODULE_POLLING);
if(mod==NULL)return NULL;
return (struct stellar_polling_manager *)stellar_module_get_ctx(mod);
}
struct stellar_module *polling_manager_on_init(struct stellar_module_manager *mod_mgr)
{
if(mod_mgr==NULL)return NULL;
struct stellar_polling_manager *polling_mgr=CALLOC(struct stellar_polling_manager, 1);
polling_mgr->mod_mgr=mod_mgr;
struct stellar_module *mod=stellar_module_new(MODULE_POLLING, polling_mgr);
return mod;
}
void polling_manager_on_exit(struct stellar_module_manager *mod_mgr, struct stellar_module *mod)
{
if(mod_mgr==NULL || mod==NULL)return;
struct stellar_polling_manager *polling_mgr=(struct stellar_polling_manager *)stellar_module_get_ctx(mod);
if(polling_mgr==NULL)return;
FREE(polling_mgr);
stellar_module_free(mod);
return;
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
static void on_polling_dispatch(int topic_id __unused,
void *msg __unused,
on_msg_cb_func* on_msg_cb,
void *on_msg_cb_arg,
void *dispatch_arg)
{
struct stellar_polling_manager *polling_mgr=(struct stellar_polling_manager *)dispatch_arg;
module_on_polling_func *polling = (module_on_polling_func *)on_msg_cb;
polling(polling_mgr, on_msg_cb_arg);
}
int stellar_polling_subscribe(struct stellar_polling_manager* polling_mgr, module_on_polling_func on_polling, void *polling_arg)
{
polling_mgr->polling_topic_id=mq_schema_get_topic_id(stellar_module_manager_get_mq_schema(polling_mgr->mod_mgr), TOPIC_POLLING);
if(polling_mgr->polling_topic_id<0)
{
polling_mgr->polling_topic_id=mq_schema_create_topic(stellar_module_manager_get_mq_schema(polling_mgr->mod_mgr), TOPIC_POLLING, on_polling_dispatch, polling_mgr, NULL, NULL);
}
return mq_schema_subscribe(stellar_module_manager_get_mq_schema(polling_mgr->mod_mgr), polling_mgr->polling_topic_id, (on_msg_cb_func *)on_polling, polling_arg);
}
#pragma GCC diagnostic pop
void stellar_polling_active(struct stellar_polling_manager *polling_mgr)
{
mq_runtime_publish_message(stellar_module_manager_get_mq_runtime(polling_mgr->mod_mgr), polling_mgr->polling_topic_id, NULL);
}
void stellar_polling_dispatch(struct stellar_polling_manager *polling_mgr)
{
stellar_polling_active(polling_mgr);
mq_runtime_dispatch(stellar_module_manager_get_mq_runtime(polling_mgr->mod_mgr));
mq_runtime_clean(stellar_module_manager_get_mq_runtime(polling_mgr->mod_mgr));
return;
}

View File

@@ -0,0 +1,22 @@
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
#include "stellar/polling_manager.h"
#define TOPIC_POLLING "POLLING"
#define MODULE_POLLING "POLLING"
struct stellar_polling_manager
{
struct stellar_module_manager *mod_mgr;
int polling_topic_id;
};
void stellar_polling_dispatch(struct stellar_polling_manager *polling_mgr);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,18 @@
add_executable(gtest_polling_manager
gtest_polling_manager_main.cpp
)
include_directories(${CMAKE_SOURCE_DIR}/infra/module_manager/)
target_link_libraries(
gtest_polling_manager
polling_manager
dl
"-rdynamic"
gtest
gmock
)
include(GoogleTest)
gtest_discover_tests(gtest_polling_manager)

View File

@@ -0,0 +1,85 @@
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <gtest/gtest.h>
#include "polling_manager/polling_manager_internal.h"
#include "module_manager/module_manager_interna.h"
/***********************************
* TEST POLLING MANAGER POLLING API *
***********************************/
struct test_module_polling_env
{
int N_round;
int polling_count;
int polling_active_count;
};
void test_module_on_polling(struct stellar_polling_manager* mod_mgr, void *polling_arg)
{
struct test_module_polling_env *env = (struct test_module_polling_env*)polling_arg;
env->polling_count++;
if(env->polling_count%2==0)
{
stellar_polling_active(mod_mgr);
env->polling_active_count++;
}
}
const char *gtest_mock_spec_toml =
"[[module]]\n"
"path = \"\"\n"
"init = \"polling_manager_on_init\"\n"
"exit = \"polling_manager_on_exit\"\n";
TEST(polling_manager, basic_polling_module) {
struct mq_schema *mq_schema=mq_schema_new();
toml_table_t *conf = toml_parse((char*)gtest_mock_spec_toml, NULL, 0);
EXPECT_TRUE(conf!=NULL);
struct stellar_module_manager *mod_mgr=stellar_module_manager_new_with_toml(conf, 10, mq_schema);
EXPECT_TRUE(mod_mgr!=NULL);
struct stellar_polling_manager *polling_mgr=stellar_module_get_polling_manager(mod_mgr);
EXPECT_TRUE(polling_mgr!=NULL);
EXPECT_EQ(stellar_module_manager_get_max_thread_num(mod_mgr), 10);
EXPECT_EQ(stellar_module_manager_get_mq_schema(mod_mgr), mq_schema);
struct test_module_polling_env env={};
env.N_round=10;
stellar_polling_subscribe(polling_mgr, test_module_on_polling, &env);
struct mq_runtime *mq_rt = mq_runtime_new(mq_schema);
stellar_module_manager_register_thread(mod_mgr, 1, mq_rt);
EXPECT_EQ((long)stellar_module_manager_get_thread_id(mod_mgr), 1);
EXPECT_EQ(stellar_module_manager_get_mq_runtime(mod_mgr), mq_rt);
for(int i=0; i<env.N_round; i++)
{
stellar_polling_dispatch(polling_mgr);
}
stellar_module_manager_free(mod_mgr);
toml_free(conf);
EXPECT_EQ(env.polling_count, env.N_round+env.polling_active_count);
}
/**********************************************
* GTEST MAIN *
**********************************************/
int main(int argc, char ** argv)
{
int ret=0;
::testing::InitGoogleTest(&argc, argv);
ret=RUN_ALL_TESTS();
return ret;
}