implement rcu for g2g runtime & fix maat_stat bug

This commit is contained in:
刘文坛
2023-04-24 02:14:26 +00:00
parent b8f98a1e9e
commit cb4502c698
10 changed files with 267 additions and 297 deletions

View File

@@ -36,21 +36,14 @@ void *group2group_runtime_new(void *g2g_schema, size_t max_thread_num,
struct log_handle *logger); struct log_handle *logger);
void group2group_runtime_free(void *g2g_runtime); void group2group_runtime_free(void *g2g_runtime);
void maat_group_ref_inc(struct group2group_runtime *g2g_rt, struct maat_group *group);
void maat_group_ref_dec(struct group2group_runtime *g2g_rt, struct maat_group *group);
struct maat_group *group2group_runtime_add_group(void *g2g_runtime, long long group_id);
void group2group_runtime_remove_group(void *g2g_runtime, struct maat_group *group);
struct maat_group *group2group_runtime_find_group(void *g2g_runtime, long long group_id);
int group2group_runtime_get_top_groups(void *g2g_runtime, long long *group_ids,
size_t n_group_ids, long long *top_group_ids);
int group2group_runtime_update(void *g2g_runtime, void *g2g_schema, const char *table_name, int group2group_runtime_update(void *g2g_runtime, void *g2g_schema, const char *table_name,
const char *line, int valid_column); const char *line, int valid_column);
int group2group_runtime_commit(void *g2g_runtime, const char *table_name, long long maat_rt_version); int group2group_runtime_commit(void *g2g_runtime, const char *table_name, long long maat_rt_version);
int group2group_runtime_get_top_groups(void *g2g_runtime, long long *group_ids,
size_t n_group_ids, long long *top_group_ids);
long long group2group_runtime_rule_count(void *g2g_runtime); long long group2group_runtime_rule_count(void *g2g_runtime);
long long group2group_runtime_update_err_count(void *g2g_runtime); long long group2group_runtime_update_err_count(void *g2g_runtime);

View File

@@ -23,7 +23,7 @@ struct maat_stat *maat_stat_new(const char *stat_file, size_t max_thread_num,
void maat_stat_free(struct maat_stat *stat); void maat_stat_free(struct maat_stat *stat);
void maat_stat_init(struct maat_stat *stat, struct table_manager *tbl_mgr, int maat_stat_init(struct maat_stat *stat, struct table_manager *tbl_mgr,
struct maat_garbage_bin *garbage_bin, const char *stat_inst_name); struct maat_garbage_bin *garbage_bin, const char *stat_inst_name);
void maat_stat_output(struct maat_stat *stat, long long maat_version, int perf_on); void maat_stat_output(struct maat_stat *stat, long long maat_version, int perf_on);

View File

@@ -371,6 +371,10 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path)
} }
system_cmd_mkdir(maat_instance->foreign_cont_dir); system_cmd_mkdir(maat_instance->foreign_cont_dir);
if (0 == strlen(opts->stat_file)) {
snprintf(opts->stat_file, sizeof(opts->stat_file), "maat.fs3");
}
maat_instance->input_mode = opts->input_mode; maat_instance->input_mode = opts->input_mode;
switch (maat_instance->input_mode) { switch (maat_instance->input_mode) {
@@ -429,8 +433,13 @@ struct maat *maat_new(struct maat_options *opts, const char *table_info_path)
} }
if (1 == maat_instance->stat_on) { if (1 == maat_instance->stat_on) {
maat_stat_init(maat_instance->stat, maat_instance->tbl_mgr, maat_instance->garbage_bin, int ret = maat_stat_init(maat_instance->stat, maat_instance->tbl_mgr, maat_instance->garbage_bin,
maat_instance->instance_name); maat_instance->instance_name);
if (ret < 0) {
log_error(maat_instance->logger, MODULE_MAAT_API,
"[%s:%d] maat_stat_init failed.", __FUNCTION__, __LINE__);
goto failed;
}
} }
pthread_create(&(maat_instance->cfg_mon_thread), NULL, rule_monitor_loop, (void *)maat_instance); pthread_create(&(maat_instance->cfg_mon_thread), NULL, rule_monitor_loop, (void *)maat_instance);

View File

@@ -103,7 +103,6 @@ struct group2compile_runtime {
long long rule_num; long long rule_num;
long long update_err_cnt; long long update_err_cnt;
struct compile_runtime *ref_compile_rt; struct compile_runtime *ref_compile_rt;
struct group2group_runtime *ref_g2g_rt;
}; };
struct maat_clause_state { struct maat_clause_state {
@@ -668,7 +667,6 @@ void group2compile_runtime_init(void *g2c_runtime, void *compile_runtime,
struct group2compile_runtime *g2c_rt = (struct group2compile_runtime *)g2c_runtime; struct group2compile_runtime *g2c_rt = (struct group2compile_runtime *)g2c_runtime;
g2c_rt->ref_compile_rt = (struct compile_runtime *)compile_runtime; g2c_rt->ref_compile_rt = (struct compile_runtime *)compile_runtime;
g2c_rt->ref_g2g_rt = (struct group2group_runtime *)g2g_runtime;
} }
void group2compile_runtime_free(void *g2c_runtime) void group2compile_runtime_free(void *g2c_runtime)
@@ -1509,12 +1507,17 @@ size_t compile_runtime_get_hit_paths(struct compile_runtime *compile_rt,
for (size_t j = 0; j < n_internal_hit_path && (n_internal_hit_path + new_hit_path_cnt) < array_size; j++) { for (size_t j = 0; j < n_internal_hit_path && (n_internal_hit_path + new_hit_path_cnt) < array_size; j++) {
if (hit_path_array[j].top_group_id < 0) { if (hit_path_array[j].top_group_id < 0) {
continue; literal_id.group_id = hit_path_array[j].sub_group_id;
} } else {
literal_id.group_id = hit_path_array[j].top_group_id;
}
literal_id.group_id = hit_path_array[j].top_group_id;
literal_id.vtable_id = hit_path_array[j].vtable_id; literal_id.vtable_id = hit_path_array[j].vtable_id;
if (maat_compile_has_literal(compile, &literal_id)) { if (maat_compile_has_literal(compile, &literal_id)) {
if (hit_path_array[j].top_group_id < 0) {
hit_path_array[j].top_group_id = hit_path_array[j].sub_group_id;
}
if (hit_path_array[j].compile_id < 0) { if (hit_path_array[j].compile_id < 0) {
hit_path_array[j].compile_id = compile->compile_id; hit_path_array[j].compile_id = compile->compile_id;
} else { } else {
@@ -1810,7 +1813,7 @@ int group2compile_runtime_update(void *g2c_runtime, void *g2c_schema,
struct group2compile_schema *schema = (struct group2compile_schema *)g2c_schema; struct group2compile_schema *schema = (struct group2compile_schema *)g2c_schema;
struct group2compile_runtime *g2c_rt = (struct group2compile_runtime *)g2c_runtime; struct group2compile_runtime *g2c_rt = (struct group2compile_runtime *)g2c_runtime;
struct compile_runtime *compile_rt = g2c_rt->ref_compile_rt; struct compile_runtime *compile_rt = g2c_rt->ref_compile_rt;
struct group2group_runtime *g2g_rt = g2c_rt->ref_g2g_rt;
int is_valid = get_column_value(line, valid_column); int is_valid = get_column_value(line, valid_column);
if (is_valid < 0) { if (is_valid < 0) {
g2c_rt->update_err_cnt++; g2c_rt->update_err_cnt++;
@@ -1825,42 +1828,24 @@ int group2compile_runtime_update(void *g2c_runtime, void *g2c_schema,
return -1; return -1;
} }
struct maat_group *group = NULL;
if (0 == is_valid) { if (0 == is_valid) {
//delete //delete
group = group2group_runtime_find_group(g2g_rt, g2c_item->group_id);
if (!group) {
log_error(compile_rt->logger, MODULE_COMPILE,
"[%s:%d] Remove group %d from compile %d failed, group is not exisited.",
__FUNCTION__, __LINE__, g2c_item->group_id, g2c_item->compile_id);
group2compile_item_free(g2c_item);
g2c_rt->update_err_cnt++;
return -1;
}
ret = maat_remove_group_from_compile(compile_rt->cfg_hash_tbl, g2c_item, compile_rt->logger); ret = maat_remove_group_from_compile(compile_rt->cfg_hash_tbl, g2c_item, compile_rt->logger);
if (0 == ret) { if (0 == ret) {
if (g2c_item->not_flag) { if (g2c_item->not_flag) {
g2c_rt->not_flag_group--; g2c_rt->not_flag_group--;
} }
maat_group_ref_dec(g2g_rt, group);
g2c_rt->rule_num--; g2c_rt->rule_num--;
} else { } else {
g2c_rt->update_err_cnt++; g2c_rt->update_err_cnt++;
} }
} else { } else {
//add //add
group = group2group_runtime_find_group(g2g_rt, g2c_item->group_id);
if (!group) {
group = group2group_runtime_add_group(g2g_rt, g2c_item->group_id);
}
ret = maat_add_group_to_compile(compile_rt->cfg_hash_tbl, g2c_item, compile_rt->logger); ret = maat_add_group_to_compile(compile_rt->cfg_hash_tbl, g2c_item, compile_rt->logger);
if (0 == ret) { if (0 == ret) {
if (g2c_item->not_flag) { if (g2c_item->not_flag) {
g2c_rt->not_flag_group++; g2c_rt->not_flag_group++;
} }
maat_group_ref_inc(g2g_rt, group);
g2c_rt->rule_num++; g2c_rt->rule_num++;
} else { } else {
g2c_rt->update_err_cnt++; g2c_rt->update_err_cnt++;
@@ -2091,6 +2076,11 @@ int maat_compile_state_update(struct rcu_hash_table *item_htable, int vtable_id,
top_group_ids[j], vtable_id); top_group_ids[j], vtable_id);
} }
for (int j = 0; j < hit_group_cnt; j++) {
maat_compile_state_update_hit_clause(state->compile_state, compile_rt,
hit_group_ids[j], vtable_id);
}
return 0; return 0;
} }
@@ -2129,7 +2119,7 @@ size_t maat_compile_state_get_internal_hit_paths(struct maat_compile_state *comp
} }
struct maat_hit_path tmp_path; struct maat_hit_path tmp_path;
for (int j = 0; j < top_group_cnt && hit_path_cnt < array_size; j++, hit_path_cnt++) { for (int j = 0; j < top_group_cnt && hit_path_cnt < array_size; j++) {
memset(&tmp_path, 0, sizeof(tmp_path)); memset(&tmp_path, 0, sizeof(tmp_path));
tmp_path.Nth_scan = internal_path->Nth_scan; tmp_path.Nth_scan = internal_path->Nth_scan;
tmp_path.item_id = internal_path->item_id; tmp_path.item_id = internal_path->item_id;
@@ -2138,6 +2128,10 @@ size_t maat_compile_state_get_internal_hit_paths(struct maat_compile_state *comp
tmp_path.vtable_id = internal_path->vtable_id; tmp_path.vtable_id = internal_path->vtable_id;
tmp_path.compile_id = -1; tmp_path.compile_id = -1;
if (tmp_path.sub_group_id == tmp_path.top_group_id) {
continue;
}
/* check if internal_path is duplicated from hit_path_array[] element */ /* check if internal_path is duplicated from hit_path_array[] element */
if (hit_path_cnt > 0) { if (hit_path_cnt > 0) {
if (maat_compile_is_hit_path_existed(hit_path_array, hit_path_cnt, &tmp_path)) { if (maat_compile_is_hit_path_existed(hit_path_array, hit_path_cnt, &tmp_path)) {
@@ -2146,6 +2140,7 @@ size_t maat_compile_state_get_internal_hit_paths(struct maat_compile_state *comp
} }
hit_path_array[hit_path_cnt] = tmp_path; hit_path_array[hit_path_cnt] = tmp_path;
hit_path_cnt++;
} }
} }

View File

@@ -35,7 +35,6 @@ struct group2group_schema {
struct maat_group { struct maat_group {
igraph_integer_t vertex_id; igraph_integer_t vertex_id;
long long group_id; long long group_id;
int ref_by_compile_cnt;
int ref_by_super_group_cnt; int ref_by_super_group_cnt;
int ref_by_sub_group_cnt; int ref_by_sub_group_cnt;
@@ -50,7 +49,7 @@ struct maat_group_topology {
struct maat_group *hash_group_by_id; //key: group_id, value: struct maat_group *. struct maat_group *hash_group_by_id; //key: group_id, value: struct maat_group *.
struct maat_group *hash_group_by_vertex; //key: vetex_id, value: struct maat_group *. Multimap (Items with multiple keys). struct maat_group *hash_group_by_vertex; //key: vetex_id, value: struct maat_group *. Multimap (Items with multiple keys).
igraph_t group_graph; igraph_t group_graph;
igraph_integer_t group_graph_vcount; igraph_integer_t group_graph_vcount;
igraph_vector_t dfs_vids; igraph_vector_t dfs_vids;
igraph_integer_t grp_vertex_id_generator; igraph_integer_t grp_vertex_id_generator;
@@ -59,11 +58,12 @@ struct maat_group_topology {
struct group2group_runtime { struct group2group_runtime {
struct maat_group_topology *group_topo; struct maat_group_topology *group_topo;
struct maat_group_topology *updating_group_topo;
long long version; long long version;
long long rule_num; long long rule_num;
long long update_err_cnt; long long update_err_cnt;
int updating_flag; int updating_flag;
pthread_rwlock_t rwlock;
struct maat_garbage_bin *ref_garbage_bin; struct maat_garbage_bin *ref_garbage_bin;
struct log_handle *logger; struct log_handle *logger;
}; };
@@ -121,6 +121,12 @@ void group2group_schema_free(void *g2g_schema)
FREE(g2g_schema); FREE(g2g_schema);
} }
void group_vertex_free(struct maat_group *group)
{
FREE(group->top_group_ids);
FREE(group);
}
struct maat_group_topology *maat_group_topology_new(struct log_handle *logger) struct maat_group_topology *maat_group_topology_new(struct log_handle *logger)
{ {
struct maat_group_topology *group_topo = ALLOC(struct maat_group_topology, 1); struct maat_group_topology *group_topo = ALLOC(struct maat_group_topology, 1);
@@ -137,30 +143,6 @@ struct maat_group_topology *maat_group_topology_new(struct log_handle *logger)
return group_topo; return group_topo;
} }
void *group2group_runtime_new(void *g2g_schema, size_t max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger)
{
if (NULL == g2g_schema) {
return NULL;
}
struct group2group_runtime *g2g_rt = ALLOC(struct group2group_runtime, 1);
g2g_rt->group_topo = maat_group_topology_new(logger);
g2g_rt->ref_garbage_bin = garbage_bin;
g2g_rt->logger = logger;
pthread_rwlock_init(&g2g_rt->rwlock, NULL);
return g2g_rt;
}
void group_vertex_free(struct maat_group *group)
{
FREE(group->top_group_ids);
FREE(group);
}
void maat_group_topology_free(struct maat_group_topology *group_topo) void maat_group_topology_free(struct maat_group_topology *group_topo)
{ {
struct maat_group *group = NULL, *tmp_group = NULL; struct maat_group *group = NULL, *tmp_group = NULL;
@@ -176,6 +158,65 @@ void maat_group_topology_free(struct maat_group_topology *group_topo)
FREE(group_topo); FREE(group_topo);
} }
struct maat_group *maat_group_clone(struct maat_group *group)
{
struct maat_group *copy_group = ALLOC(struct maat_group, 1);
copy_group->group_id = group->group_id;
copy_group->vertex_id = group->vertex_id;
copy_group->ref_by_sub_group_cnt = group->ref_by_sub_group_cnt;
copy_group->ref_by_super_group_cnt = group->ref_by_super_group_cnt;
copy_group->top_group_cnt = group->top_group_cnt;
if (copy_group->top_group_cnt > 0) {
copy_group->top_group_ids = ALLOC(long long, copy_group->top_group_cnt);
memcpy(copy_group->top_group_ids, group->top_group_ids,
copy_group->top_group_cnt * sizeof(long long));
}
return copy_group;
}
struct maat_group_topology *maat_group_topology_clone(struct maat_group_topology *src_group_topo)
{
if (NULL == src_group_topo) {
return NULL;
}
struct maat_group_topology *copy_group_topo = ALLOC(struct maat_group_topology, 1);
struct maat_group *group = NULL, *tmp_group = NULL;
HASH_ITER(hh_group_id, copy_group_topo->hash_group_by_id, group, tmp_group) {
struct maat_group *copy_group = maat_group_clone(group);
HASH_ADD(hh_group_id, copy_group_topo->hash_group_by_id, group_id,
sizeof(copy_group->group_id), copy_group);
HASH_ADD(hh_vertex_id, copy_group_topo->hash_group_by_vertex, vertex_id,
sizeof(copy_group->vertex_id), copy_group);
}
igraph_copy(&(copy_group_topo->group_graph), &(src_group_topo->group_graph));
copy_group_topo->grp_vertex_id_generator = src_group_topo->grp_vertex_id_generator;
return copy_group_topo;
}
void *group2group_runtime_new(void *g2g_schema, size_t max_thread_num,
struct maat_garbage_bin *garbage_bin,
struct log_handle *logger)
{
if (NULL == g2g_schema) {
return NULL;
}
struct group2group_runtime *g2g_rt = ALLOC(struct group2group_runtime, 1);
g2g_rt->group_topo = maat_group_topology_new(logger);
g2g_rt->ref_garbage_bin = garbage_bin;
g2g_rt->logger = logger;
return g2g_rt;
}
void group2group_runtime_free(void *g2g_runtime) void group2group_runtime_free(void *g2g_runtime)
{ {
if (NULL == g2g_runtime) { if (NULL == g2g_runtime) {
@@ -184,33 +225,19 @@ void group2group_runtime_free(void *g2g_runtime)
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime; struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
pthread_rwlock_wrlock(&g2g_rt->rwlock);
if (g2g_rt->group_topo != NULL) { if (g2g_rt->group_topo != NULL) {
maat_group_topology_free(g2g_rt->group_topo); maat_group_topology_free(g2g_rt->group_topo);
g2g_rt->group_topo = NULL; g2g_rt->group_topo = NULL;
} }
pthread_rwlock_unlock(&g2g_rt->rwlock);
pthread_rwlock_destroy(&g2g_rt->rwlock);
if (g2g_rt->updating_group_topo != NULL) {
maat_group_topology_free(g2g_rt->updating_group_topo);
g2g_rt->updating_group_topo = NULL;
}
FREE(g2g_rt); FREE(g2g_rt);
} }
void maat_group_ref_inc(struct group2group_runtime *g2g_rt, struct maat_group *group)
{
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
g2g_rt->updating_flag = 1;
group->ref_by_compile_cnt++;
pthread_rwlock_unlock(&(g2g_rt->rwlock));
}
void maat_group_ref_dec(struct group2group_runtime *g2g_rt, struct maat_group *group)
{
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
g2g_rt->updating_flag = 1;
group->ref_by_compile_cnt--;
pthread_rwlock_unlock(&(g2g_rt->rwlock));
}
struct group2group_item * struct group2group_item *
group2group_item_new(const char *line, struct group2group_schema *g2g_schema, group2group_item_new(const char *line, struct group2group_schema *g2g_schema,
const char *table_name, struct log_handle *logger) const char *table_name, struct log_handle *logger)
@@ -260,15 +287,9 @@ size_t print_igraph_vector(igraph_vector_t *v, char *buff, size_t sz) {
return printed; return printed;
} }
struct maat_group *_group2group_runtime_add_group(void *g2g_runtime, long long group_id, int lock_flag) struct maat_group *group_topology_add_group(struct maat_group_topology *group_topo,
long long group_id)
{ {
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
if (1 == lock_flag) {
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
}
struct maat_group_topology *group_topo = g2g_rt->group_topo;
assert(group_topo != NULL); assert(group_topo != NULL);
struct maat_group *group = ALLOC(struct maat_group, 1); struct maat_group *group = ALLOC(struct maat_group, 1);
@@ -280,37 +301,22 @@ struct maat_group *_group2group_runtime_add_group(void *g2g_runtime, long long g
HASH_ADD(hh_group_id, group_topo->hash_group_by_id, group_id, sizeof(group->group_id), group); HASH_ADD(hh_group_id, group_topo->hash_group_by_id, group_id, sizeof(group->group_id), group);
HASH_ADD(hh_vertex_id, group_topo->hash_group_by_vertex, vertex_id, sizeof(group->vertex_id), group); HASH_ADD(hh_vertex_id, group_topo->hash_group_by_vertex, vertex_id, sizeof(group->vertex_id), group);
g2g_rt->updating_flag = 1;
if (1 == lock_flag) {
pthread_rwlock_unlock(&(g2g_rt->rwlock));
}
return group; return group;
} }
struct maat_group *group2group_runtime_add_group(void *g2g_runtime, long long group_id) void group_topology_remove_group(struct maat_group_topology *group_topo,
struct maat_group *group)
{ {
if (NULL == g2g_runtime) { if (NULL == group_topo || NULL == group) {
return NULL; return;
} }
return _group2group_runtime_add_group(g2g_runtime, group_id, 1);
}
void _group2group_runtime_remove_group(void *g2g_runtime, struct maat_group *group, int lock_flag)
{
igraph_vector_t v; igraph_vector_t v;
char buff[4096] = {0}; char buff[4096] = {0};
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
if (1 == lock_flag) { assert(group->ref_by_super_group_cnt == 0);
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
}
struct maat_group_topology *group_topo = g2g_rt->group_topo;
assert(group_topo != NULL);
assert(group->ref_by_compile_cnt == 0 && group->ref_by_super_group_cnt == 0);
igraph_vector_init(&v, 8); igraph_vector_init(&v, 8);
igraph_neighbors(&group_topo->group_graph, &v, group->vertex_id, IGRAPH_ALL); igraph_neighbors(&group_topo->group_graph, &v, group->vertex_id, IGRAPH_ALL);
if (igraph_vector_size(&v) > 0) { if (igraph_vector_size(&v) > 0) {
@@ -330,86 +336,45 @@ void _group2group_runtime_remove_group(void *g2g_runtime, struct maat_group *gro
HASH_DELETE(hh_vertex_id, group_topo->hash_group_by_vertex, group); HASH_DELETE(hh_vertex_id, group_topo->hash_group_by_vertex, group);
group_vertex_free(group); group_vertex_free(group);
g2g_rt->updating_flag = 1;
if (1 == lock_flag) {
pthread_rwlock_unlock(&(g2g_rt->rwlock));
}
} }
void group2group_runtime_remove_group(void *g2g_runtime, struct maat_group *group) struct maat_group *group_topology_find_group(struct maat_group_topology *group_topo,
long long group_id)
{ {
if (NULL == g2g_runtime || NULL == group) { if (NULL == group_topo || group_id < 0) {
return;
}
_group2group_runtime_remove_group(g2g_runtime, group, 1);
}
struct maat_group *_group2group_runtime_find_group(void *g2g_runtime, long long group_id, int lock_flag)
{
if (NULL == g2g_runtime) {
return NULL; return NULL;
} }
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
if (1 == lock_flag) {
pthread_rwlock_rdlock(&(g2g_rt->rwlock));
}
struct maat_group_topology *group_topo = g2g_rt->group_topo;
assert(group_topo != NULL);
struct maat_group *group = NULL; struct maat_group *group = NULL;
HASH_FIND(hh_group_id, group_topo->hash_group_by_id, &group_id, sizeof(group_id), group); HASH_FIND(hh_group_id, group_topo->hash_group_by_id, &group_id, sizeof(group_id), group);
if (1 == lock_flag) {
pthread_rwlock_unlock(&(g2g_rt->rwlock));
}
return group; return group;
} }
struct maat_group *group2group_runtime_find_group(void *g2g_runtime, long long group_id) int group_topology_add_group_to_group(struct maat_group_topology *group_topo,
long long group_id, long long super_group_id)
{ {
if (NULL == g2g_runtime) { if (NULL == group_topo) {
return NULL;
}
return _group2group_runtime_find_group(g2g_runtime, group_id, 1);
}
int group2group_runtime_add_group_to_group(void *g2g_runtime, long long group_id,
long long super_group_id)
{
if (NULL == g2g_runtime) {
return -1; return -1;
} }
int ret = 0; struct maat_group *group = group_topology_find_group(group_topo, group_id);
igraph_integer_t edge_id;
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
struct maat_group_topology *group_topo = g2g_rt->group_topo;
assert(group_topo != NULL);
struct maat_group *group = _group2group_runtime_find_group(g2g_runtime, group_id, 0);
if (NULL == group) { if (NULL == group) {
group = _group2group_runtime_add_group(g2g_runtime, group_id, 0); group = group_topology_add_group(group_topo, group_id);
} }
struct maat_group *super_group = _group2group_runtime_find_group(g2g_runtime, struct maat_group *super_group = group_topology_find_group(group_topo, super_group_id);
super_group_id, 0);
if (NULL == super_group) { if (NULL == super_group) {
super_group = _group2group_runtime_add_group(g2g_runtime, super_group_id, 0); super_group = group_topology_add_group(group_topo, super_group_id);
} }
ret = igraph_get_eid(&group_topo->group_graph, &edge_id, group->vertex_id, igraph_integer_t edge_id;
int ret = igraph_get_eid(&group_topo->group_graph, &edge_id, group->vertex_id,
super_group->vertex_id, IGRAPH_DIRECTED, /*error*/ 0); super_group->vertex_id, IGRAPH_DIRECTED, /*error*/ 0);
//No duplicated edges between two groups. //No duplicated edges between two groups.
if (edge_id > 0) { if (edge_id > 0) {
log_error(g2g_rt->logger, MODULE_GROUP, log_error(group_topo->logger, MODULE_GROUP,
"[%s:%d] Add group %d to group %d failed, relation already exisited.", "[%s:%d] Add group %d to group %d failed, relation already exisited.",
__FUNCTION__, __LINE__, group->group_id, super_group->group_id); __FUNCTION__, __LINE__, group->group_id, super_group->group_id);
ret = -1; ret = -1;
@@ -420,45 +385,36 @@ int group2group_runtime_add_group_to_group(void *g2g_runtime, long long group_id
super_group->ref_by_sub_group_cnt++; super_group->ref_by_sub_group_cnt++;
ret = 0; ret = 0;
} }
g2g_rt->updating_flag = 1;
pthread_rwlock_unlock(&(g2g_rt->rwlock));
return ret; return ret;
} }
int group2group_runtime_remove_group_from_group(void *g2g_runtime, long long group_id, int group_topology_remove_group_from_group(struct maat_group_topology *group_topo,
long long super_group_id) long long group_id, long long super_group_id)
{ {
if (NULL == g2g_runtime) { if (NULL == group_topo) {
return -1; return -1;
} }
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
//No hash write operation, LOCK protection is unnecessary. //No hash write operation, LOCK protection is unnecessary.
struct maat_group *group = _group2group_runtime_find_group(g2g_runtime, group_id, 0); struct maat_group *group = group_topology_add_group(group_topo, group_id);
if (NULL == group) { if (NULL == group) {
log_error(g2g_rt->logger, MODULE_GROUP, log_error(group_topo->logger, MODULE_GROUP,
"[%s:%d] Del group %d from group %d failed, group %d not exisited.", "[%s:%d] Del group %d from group %d failed, group %d not exisited.",
__FUNCTION__, __LINE__, group_id, super_group_id, group_id); __FUNCTION__, __LINE__, group_id, super_group_id, group_id);
pthread_rwlock_unlock(&(g2g_rt->rwlock));
return -1; return -1;
} }
struct maat_group *super_group = _group2group_runtime_find_group(g2g_runtime, struct maat_group *super_group = group_topology_add_group(group_topo, super_group_id);
super_group_id, 0);
if (NULL == super_group) { if (NULL == super_group) {
log_error(g2g_rt->logger, MODULE_GROUP, log_error(group_topo->logger, MODULE_GROUP,
"[%s:%d] Del group %d from group %d failed, superior group %d not exisited.", "[%s:%d] Del group %d from group %d failed, superior group %d not exisited.",
__FUNCTION__, __LINE__, group_id, super_group_id, super_group_id); __FUNCTION__, __LINE__, group_id, super_group_id, super_group_id);
pthread_rwlock_unlock(&(g2g_rt->rwlock));
return -1; return -1;
} }
igraph_es_t es; igraph_es_t es;
igraph_integer_t edge_num_before = 0, edge_num_after = 0; igraph_integer_t edge_num_before = 0, edge_num_after = 0;
struct maat_group_topology *group_topo = g2g_rt->group_topo;
edge_num_before = igraph_ecount(&group_topo->group_graph); edge_num_before = igraph_ecount(&group_topo->group_graph);
// The edges between the given pairs of vertices will be included in the edge selection. // The edges between the given pairs of vertices will be included in the edge selection.
@@ -477,15 +433,12 @@ int group2group_runtime_remove_group_from_group(void *g2g_runtime, long long gro
igraph_es_destroy(&es); igraph_es_destroy(&es);
if (ret != IGRAPH_SUCCESS || edge_num_before - edge_num_after != 1) { if (ret != IGRAPH_SUCCESS || edge_num_before - edge_num_after != 1) {
pthread_rwlock_unlock(&(g2g_rt->rwlock));
assert(0); assert(0);
return -1; return -1;
} }
group->ref_by_super_group_cnt--; group->ref_by_super_group_cnt--;
super_group->ref_by_sub_group_cnt--; super_group->ref_by_sub_group_cnt--;
g2g_rt->updating_flag = 1;
pthread_rwlock_unlock(&(g2g_rt->rwlock));
return 0; return 0;
} }
@@ -505,9 +458,9 @@ static size_t effective_vertices_count(igraph_vector_t *vids)
return i; return i;
} }
int group2group_runtime_build_top_groups(void *g2g_runtime, long long maat_rt_version) int group_topology_build_top_groups(struct maat_group_topology *group_topo)
{ {
if (NULL == g2g_runtime) { if (NULL == group_topo) {
return -1; return -1;
} }
@@ -516,18 +469,12 @@ int group2group_runtime_build_top_groups(void *g2g_runtime, long long maat_rt_ve
int tmp_vid = 0; int tmp_vid = 0;
size_t top_group_cnt = 0; size_t top_group_cnt = 0;
long long *temp_group_ids = NULL; long long *temp_group_ids = NULL;
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
pthread_rwlock_wrlock(&(g2g_rt->rwlock));
struct maat_group_topology *group_topo = g2g_rt->group_topo;
assert(group_topo != NULL);
igraph_bool_t is_dag; igraph_bool_t is_dag;
igraph_is_dag(&(group_topo->group_graph), &is_dag); igraph_is_dag(&(group_topo->group_graph), &is_dag);
if (!is_dag) { if (!is_dag) {
log_error(g2g_rt->logger, MODULE_GROUP, "[%s:%d] Sub group cycle detected!", log_error(group_topo->logger, MODULE_GROUP, "[%s:%d] Sub group cycle detected!",
__FUNCTION__, __LINE__); __FUNCTION__, __LINE__);
pthread_rwlock_unlock(&(g2g_rt->rwlock));
return -1; return -1;
} }
@@ -539,58 +486,48 @@ int group2group_runtime_build_top_groups(void *g2g_runtime, long long maat_rt_ve
temp_group_ids = NULL; temp_group_ids = NULL;
//Orphan, Not reference by any one, free it. //Orphan, Not reference by any one, free it.
if (0 == group->ref_by_compile_cnt if (0 == group->ref_by_super_group_cnt
&& 0 == group->ref_by_super_group_cnt
&& 0 == group->ref_by_sub_group_cnt) { && 0 == group->ref_by_sub_group_cnt) {
FREE(group->top_group_ids); FREE(group->top_group_ids);
_group2group_runtime_remove_group(g2g_runtime, group, 0); group_topology_remove_group(group_topo, group);
continue; continue;
} }
//A group is need to build top groups when it has items and referenced by superior groups or compiles. //A group is referenced by superior groups.
if (group->ref_by_compile_cnt > 0 || group->ref_by_super_group_cnt > 0) { if (group->ref_by_super_group_cnt > 0) {
if (0 == group->ref_by_super_group_cnt) { igraph_vector_t *vids = &(group_topo->dfs_vids);
//fast path, group is only referenced by compile rules. igraph_dfs(&group_topo->group_graph, group->vertex_id, IGRAPH_OUT,
top_group_cnt = 1; 0, vids, NULL, NULL, NULL, NULL, NULL, NULL);
temp_group_ids = ALLOC(long long, top_group_cnt);
temp_group_ids[0] = group->group_id;
} else {
igraph_vector_t *vids = &(group_topo->dfs_vids);
igraph_dfs(&group_topo->group_graph, group->vertex_id, IGRAPH_OUT,
0, vids, NULL, NULL, NULL, NULL, NULL, NULL);
temp_group_ids = ALLOC(long long, effective_vertices_count(vids));
for (size_t i = 0; i < (size_t)igraph_vector_size(vids); i++) {
tmp_vid = (int) VECTOR(*vids)[i];
if (tmp_vid < 0) {
break;
}
HASH_FIND(hh_vertex_id, group_topo->hash_group_by_vertex, &tmp_vid, temp_group_ids = ALLOC(long long, effective_vertices_count(vids));
sizeof(tmp_vid), super_group);
//including itself for (size_t i = 0; i < (size_t)igraph_vector_size(vids); i++) {
if (super_group->ref_by_compile_cnt > 0) { tmp_vid = (int)VECTOR(*vids)[i];
temp_group_ids[top_group_cnt] = super_group->group_id; if (tmp_vid < 0) {
top_group_cnt++; break;
} }
}
}
}
free(group->top_group_ids); HASH_FIND(hh_vertex_id, group_topo->hash_group_by_vertex, &tmp_vid,
sizeof(tmp_vid), super_group);
temp_group_ids[top_group_cnt] = super_group->group_id;
top_group_cnt++;
}
}
FREE(group->top_group_ids);
group->top_group_cnt = top_group_cnt; group->top_group_cnt = top_group_cnt;
group->top_group_ids = ALLOC(long long, group->top_group_cnt); if (top_group_cnt > 0) {
memcpy(group->top_group_ids, temp_group_ids, sizeof(long long)*group->top_group_cnt); group->top_group_ids = ALLOC(long long, group->top_group_cnt);
memcpy(group->top_group_ids, temp_group_ids, sizeof(long long)*group->top_group_cnt);
FREE(temp_group_ids); }
}
igraph_vector_destroy(&group_topo->dfs_vids); if (temp_group_ids != NULL) {
g2g_rt->version = maat_rt_version; FREE(temp_group_ids);
g2g_rt->updating_flag = 0; }
pthread_rwlock_unlock(&(g2g_rt->rwlock)); }
igraph_vector_destroy(&(group_topo->dfs_vids));
return 0; return 0;
} }
@@ -620,10 +557,16 @@ int group2group_runtime_update(void *g2g_runtime, void *g2g_schema,
return -1; return -1;
} }
if (0 == g2g_rt->updating_flag) {
assert(g2g_rt->updating_group_topo == NULL);
g2g_rt->updating_group_topo = maat_group_topology_clone(g2g_rt->group_topo);
g2g_rt->updating_flag = 1;
}
if (0 == is_valid) { if (0 == is_valid) {
//delete //delete
ret = group2group_runtime_remove_group_from_group(g2g_runtime, g2g_item->group_id, ret = group_topology_remove_group_from_group(g2g_rt->updating_group_topo, g2g_item->group_id,
g2g_item->super_group_id); g2g_item->super_group_id);
if (0 == ret) { if (0 == ret) {
g2g_rt->rule_num--; g2g_rt->rule_num--;
} else { } else {
@@ -631,8 +574,8 @@ int group2group_runtime_update(void *g2g_runtime, void *g2g_schema,
} }
} else { } else {
//add //add
ret = group2group_runtime_add_group_to_group(g2g_runtime, g2g_item->group_id, ret = group_topology_add_group_to_group(g2g_rt->updating_group_topo, g2g_item->group_id,
g2g_item->super_group_id); g2g_item->super_group_id);
if (0 == ret) { if (0 == ret) {
g2g_rt->rule_num++; g2g_rt->rule_num++;
} else { } else {
@@ -644,6 +587,12 @@ int group2group_runtime_update(void *g2g_runtime, void *g2g_schema,
return ret; return ret;
} }
void garbage_maat_group_topology_free(void *data, void *arg)
{
struct maat_group_topology *group_topo = (struct maat_group_topology *)data;
maat_group_topology_free(group_topo);
}
int group2group_runtime_commit(void *g2g_runtime, const char *table_name, long long maat_rt_version) int group2group_runtime_commit(void *g2g_runtime, const char *table_name, long long maat_rt_version)
{ {
if (NULL == g2g_runtime) { if (NULL == g2g_runtime) {
@@ -655,7 +604,7 @@ int group2group_runtime_commit(void *g2g_runtime, const char *table_name, long l
return 0; return 0;
} }
int ret = group2group_runtime_build_top_groups(g2g_runtime, maat_rt_version); int ret = group_topology_build_top_groups(g2g_rt->updating_group_topo);
if (ret < 0) { if (ret < 0) {
log_error(g2g_rt->logger, MODULE_GROUP, log_error(g2g_rt->logger, MODULE_GROUP,
"[%s:%d] table[%s] group2group runtime commit failed", "[%s:%d] table[%s] group2group runtime commit failed",
@@ -663,6 +612,15 @@ int group2group_runtime_commit(void *g2g_runtime, const char *table_name, long l
return -1; return -1;
} }
struct maat_group_topology *old_group_topo = g2g_rt->group_topo;
g2g_rt->group_topo = g2g_rt->updating_group_topo;
g2g_rt->updating_group_topo = NULL;
g2g_rt->updating_flag = 0;
maat_garbage_bagging(g2g_rt->ref_garbage_bin, old_group_topo, NULL,
garbage_maat_group_topology_free);
g2g_rt->version = maat_rt_version;
log_info(g2g_rt->logger, MODULE_GROUP, log_info(g2g_rt->logger, MODULE_GROUP,
"table[%s] commit %zu g2g rules and rebuild top_groups completed, version:%lld", "table[%s] commit %zu g2g rules and rebuild top_groups completed, version:%lld",
table_name, g2g_rt->rule_num, g2g_rt->version); table_name, g2g_rt->rule_num, g2g_rt->version);
@@ -700,9 +658,8 @@ int group2group_runtime_get_top_groups(void *g2g_runtime, long long *group_ids,
size_t top_group_index = 0; size_t top_group_index = 0;
struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime; struct group2group_runtime *g2g_rt = (struct group2group_runtime *)g2g_runtime;
pthread_rwlock_rdlock(&(g2g_rt->rwlock));
for (size_t i = 0; i < n_group_ids; i++) { for (size_t i = 0; i < n_group_ids; i++) {
struct maat_group *group = _group2group_runtime_find_group(g2g_runtime, group_ids[i], 0); struct maat_group *group = group_topology_find_group(g2g_rt->group_topo, group_ids[i]);
if (!group) { if (!group) {
continue; continue;
} }
@@ -711,7 +668,6 @@ int group2group_runtime_get_top_groups(void *g2g_runtime, long long *group_ids,
top_group_ids[top_group_index++] = group->top_group_ids[j]; top_group_ids[top_group_index++] = group->top_group_ids[j];
} }
} }
pthread_rwlock_unlock(&(g2g_rt->rwlock));
return top_group_index; return top_group_index;
} }

View File

@@ -111,7 +111,7 @@ void maat_fieldstat_register(struct maat_stat *stat)
"line_cmd/s", NULL, 0); "line_cmd/s", NULL, 0);
} }
void maat_fieldstat_table_row_register(struct maat_stat *stat, struct table_manager *tbl_mgr, int maat_fieldstat_table_row_register(struct maat_stat *stat, struct table_manager *tbl_mgr,
int table_id) int table_id)
{ {
size_t max_table_cnt = table_manager_table_size(tbl_mgr); size_t max_table_cnt = table_manager_table_size(tbl_mgr);
@@ -119,7 +119,7 @@ void maat_fieldstat_table_row_register(struct maat_stat *stat, struct table_mana
stat->total_stat_id); stat->total_stat_id);
if (ret < 0) { if (ret < 0) {
log_error(stat->logger, MODULE_MAAT_STAT, "fieldstat_register_table_row Sum failed."); log_error(stat->logger, MODULE_MAAT_STAT, "fieldstat_register_table_row Sum failed.");
return; return -1;
} }
for (size_t i = 0; i < max_table_cnt; i++) { for (size_t i = 0; i < max_table_cnt; i++) {
@@ -136,9 +136,11 @@ void maat_fieldstat_table_row_register(struct maat_stat *stat, struct table_mana
if (ret < 0) { if (ret < 0) {
log_error(stat->logger, MODULE_MAAT_STAT, log_error(stat->logger, MODULE_MAAT_STAT,
"fieldstat_register_table_row %s failed.", table_name); "fieldstat_register_table_row %s failed.", table_name);
return; return -1;
} }
} }
return 0;
} }
struct maat_stat *maat_stat_new(const char *stat_file, size_t max_thread_num, struct log_handle *logger) struct maat_stat *maat_stat_new(const char *stat_file, size_t max_thread_num, struct log_handle *logger)
@@ -186,28 +188,30 @@ void maat_stat_free(struct maat_stat *stat)
free(stat); free(stat);
} }
void maat_stat_init(struct maat_stat *stat, struct table_manager *tbl_mgr, int maat_stat_init(struct maat_stat *stat, struct table_manager *tbl_mgr,
struct maat_garbage_bin *garbage_bin, const char *stat_inst_name) struct maat_garbage_bin *garbage_bin, const char *stat_inst_name)
{ {
if (NULL == stat || NULL == tbl_mgr || NULL == garbage_bin ||
NULL == stat_inst_name) {
return -1;
}
stat->fs_handle = fieldstat_instance_new(stat_inst_name); stat->fs_handle = fieldstat_instance_new(stat_inst_name);
if (NULL == stat->fs_handle) { if (NULL == stat->fs_handle) {
log_error(stat->logger, MODULE_MAAT_STAT, log_error(stat->logger, MODULE_MAAT_STAT, "fieldstat_instance_new failed.");
"fieldstat_instance_new failed."); return -1;
return;
} }
int ret = fieldstat_set_local_output(stat->fs_handle, stat->stat_file, "default"); int ret = fieldstat_set_local_output(stat->fs_handle, stat->stat_file, "default");
if (ret < 0) { if (ret < 0) {
log_error(stat->logger, MODULE_MAAT_STAT, log_error(stat->logger, MODULE_MAAT_STAT, "fieldstat_set_local_output failed.");
"fieldstat_set_local_output failed."); return -1;
return;
} }
ret = fieldstat_disable_background_thread(stat->fs_handle); ret = fieldstat_disable_background_thread(stat->fs_handle);
if (ret < 0) { if (ret < 0) {
log_error(stat->logger, MODULE_MAAT_STAT, log_error(stat->logger, MODULE_MAAT_STAT, "fieldstat_disable_background_thread failed.");
"fieldstat_disable_background_thread failed."); return -1;
return;
} }
maat_fieldstat_register(stat); maat_fieldstat_register(stat);
@@ -218,10 +222,19 @@ void maat_stat_init(struct maat_stat *stat, struct table_manager *tbl_mgr,
int fs_table_id = fieldstat_register_table(stat->fs_handle, stat_inst_name, int fs_table_id = fieldstat_register_table(stat->fs_handle, stat_inst_name,
common_column_name, common_column_type, common_column_name, common_column_type,
n_column); n_column);
if (fs_table_id < 0) {
log_error(stat->logger, MODULE_MAAT_STAT, "fieldstat_register_table failed.");
return -1;
}
maat_fieldstat_table_row_register(stat, tbl_mgr, fs_table_id); ret = maat_fieldstat_table_row_register(stat, tbl_mgr, fs_table_id);
if (ret < 0) {
return -1;
}
fieldstat_instance_start(stat->fs_handle); fieldstat_instance_start(stat->fs_handle);
return 0;
} }
void maat_fieldstat_table_row_output(struct maat_stat *stat, int perf_on) void maat_fieldstat_table_row_output(struct maat_stat *stat, int perf_on)

View File

@@ -762,9 +762,6 @@ int table_manager_get_table_id(struct table_manager *tbl_mgr, const char *name)
long long table_id = -1; long long table_id = -1;
int ret = maat_kv_read(tbl_mgr->tablename2id_map, name, &table_id); int ret = maat_kv_read(tbl_mgr->tablename2id_map, name, &table_id);
if (ret < 0) { if (ret < 0) {
log_error(tbl_mgr->logger, MODULE_TABLE,
"[%s:%d] table:%s is not registered",
__FUNCTION__, __LINE__, name);
return -1; return -1;
} }

View File

@@ -35,4 +35,7 @@ file(COPY maat_json.json DESTINATION ./)
file(COPY maat_json.json DESTINATION ../tools/) file(COPY maat_json.json DESTINATION ../tools/)
file(COPY ntcrule DESTINATION ./) file(COPY ntcrule DESTINATION ./)
file(COPY testdata DESTINATION ./) file(COPY testdata DESTINATION ./)
file(COPY test_streamfiles DESTINATION ./) file(COPY test_streamfiles DESTINATION ./)
include(GoogleTest)
gtest_discover_tests(maat_framework_gtest)

View File

@@ -15,7 +15,7 @@
#define ARRAY_SIZE 10 #define ARRAY_SIZE 10
#define HIT_PATH_SIZE 128 #define HIT_PATH_SIZE 128
#define WAIT_FOR_EFFECTIVE_S 1 #define WAIT_FOR_EFFECTIVE_S 2
const char *table_info_path = "./table_info.conf"; const char *table_info_path = "./table_info.conf";
const char *json_path="./maat_json.json"; const char *json_path="./maat_json.json";
@@ -432,6 +432,7 @@ TEST_F(MaatIris, basic) {
} }
#endif #endif
class MaatFlagScan : public testing::Test class MaatFlagScan : public testing::Test
{ {
protected: protected:
@@ -674,7 +675,7 @@ TEST_F(MaatFlagScan, FlagPlus) {
maat_state_free(state); maat_state_free(state);
state = NULL; state = NULL;
} }
#if 1
class MaatStringScan : public testing::Test class MaatStringScan : public testing::Test
{ {
protected: protected:
@@ -1344,7 +1345,7 @@ TEST_F(MaatStringScan, dynamic_config) {
keywords, NULL, 1, 0, 0, 0); /* EXPR_TYPE_AND MATCH_METHOD_SUB */ keywords, NULL, 1, 0, 0, 0); /* EXPR_TYPE_AND MATCH_METHOD_SUB */
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = maat_scan_string(maat_instance, table_id, data, strlen(data), results, ret = maat_scan_string(maat_instance, table_id, data, strlen(data), results,
ARRAY_SIZE, &n_hit_result, state); ARRAY_SIZE, &n_hit_result, state);
@@ -1367,7 +1368,7 @@ TEST_F(MaatStringScan, dynamic_config) {
ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0); ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = maat_scan_string(maat_instance, table_id, data, strlen(data), results, ret = maat_scan_string(maat_instance, table_id, data, strlen(data), results,
ARRAY_SIZE, &n_hit_result, state); ARRAY_SIZE, &n_hit_result, state);
@@ -1428,7 +1429,7 @@ TEST_F(MaatStreamScan, dynamic_config) {
int ret = test_add_expr_command(maat_instance, table_name, compile1_id, 0, keywords1); int ret = test_add_expr_command(maat_instance, table_name, compile1_id, 0, keywords1);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
int table_id = maat_get_table_id(maat_instance, table_name); int table_id = maat_get_table_id(maat_instance, table_name);
ASSERT_GT(table_id, 0); ASSERT_GT(table_id, 0);
@@ -1459,7 +1460,7 @@ TEST_F(MaatStreamScan, dynamic_config) {
EXPECT_EQ(results[0], compile1_id); EXPECT_EQ(results[0], compile1_id);
maat_state_reset(state); maat_state_reset(state);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
// Inc config has taken effect, stream reference old expr_runtime, should not hit compile // Inc config has taken effect, stream reference old expr_runtime, should not hit compile
ret = maat_stream_scan(sp, scan_data1, strlen(scan_data1), results, ARRAY_SIZE, ret = maat_stream_scan(sp, scan_data1, strlen(scan_data1), results, ARRAY_SIZE,
@@ -1669,7 +1670,7 @@ TEST_F(MaatIPScan, dynamic_config) {
IPv4, "100.100.100.100", "100.100.100.100", 100, 100, 0); IPv4, "100.100.100.100", "100.100.100.100", 100, 100, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = maat_scan_ipv4(maat_instance, table_id, sip, port, proto, results, ret = maat_scan_ipv4(maat_instance, table_id, sip, port, proto, results,
ARRAY_SIZE, &n_hit_result, state); ARRAY_SIZE, &n_hit_result, state);
@@ -1693,7 +1694,7 @@ TEST_F(MaatIPScan, dynamic_config) {
compile_id, NULL, 1, 0); compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = maat_scan_ipv4(maat_instance, table_id, sip, port, proto, results, ret = maat_scan_ipv4(maat_instance, table_id, sip, port, proto, results,
ARRAY_SIZE, &n_hit_result, state); ARRAY_SIZE, &n_hit_result, state);
@@ -2809,7 +2810,7 @@ TEST_F(CompileTable, CompileRuleUpdate) {
int ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_ADD, compile_id, NULL, 1, 0); int ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_ADD, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0); ret = compile_table_set_line(maat_instance, compile_table_name, MAAT_OP_DEL, compile_id, NULL, 1, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S);
@@ -3751,7 +3752,7 @@ TEST_F(MaatCmdTest, RuleIDRecycle) {
long long rule_id = maat_cmd_incrby(maat_instance, "TEST_SEQ", 1); long long rule_id = maat_cmd_incrby(maat_instance, "TEST_SEQ", 1);
test_add_expr_command(maat_instance, table_name, rule_id, 0, keywords); test_add_expr_command(maat_instance, table_name, rule_id, 0, keywords);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data), int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
results, ARRAY_SIZE, &n_hit_result, state); results, ARRAY_SIZE, &n_hit_result, state);
@@ -3761,7 +3762,7 @@ TEST_F(MaatCmdTest, RuleIDRecycle) {
EXPECT_EQ(results[0], rule_id); EXPECT_EQ(results[0], rule_id);
del_command(maat_instance, rule_id); del_command(maat_instance, rule_id);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data), ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
results, ARRAY_SIZE, &n_hit_result, state); results, ARRAY_SIZE, &n_hit_result, state);
@@ -3769,7 +3770,7 @@ TEST_F(MaatCmdTest, RuleIDRecycle) {
EXPECT_EQ(ret, MAAT_SCAN_HALF_HIT); EXPECT_EQ(ret, MAAT_SCAN_HALF_HIT);
test_add_expr_command(maat_instance, table_name, rule_id, 0, keywords); test_add_expr_command(maat_instance, table_name, rule_id, 0, keywords);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
memset(results, 0, sizeof(results)); memset(results, 0, sizeof(results));
ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data), ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
@@ -3806,7 +3807,7 @@ TEST_F(MaatCmdTest, ReturnRuleIDWithDescendingOrder) {
test_add_expr_command(maat_instance, table_name, expect_rule_id[i], 0, keywords); test_add_expr_command(maat_instance, table_name, expect_rule_id[i], 0, keywords);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
memset(results, 0, sizeof(results)); memset(results, 0, sizeof(results));
int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data), int ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
@@ -4354,7 +4355,7 @@ TEST_F(MaatCmdTest, SetFile) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2);//wait for callback triggered. sleep(WAIT_FOR_EFFECTIVE_S);//wait for callback triggered.
EXPECT_EQ(g_test_foregin_read_OK, 1); EXPECT_EQ(g_test_foregin_read_OK, 1);
g_test_foreign_del_OK = 0; g_test_foreign_del_OK = 0;
@@ -4395,7 +4396,7 @@ TEST_F(MaatCmdTest, SetFile) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2);//wait for callback triggered. sleep(WAIT_FOR_EFFECTIVE_S);//wait for callback triggered.
EXPECT_EQ(g_test_foregin_read_OK, 1); EXPECT_EQ(g_test_foregin_read_OK, 1);
} }
@@ -4467,7 +4468,7 @@ TEST_F(MaatCmdTest, PluginEXData) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
int ex_data_counter = 0; int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_instance, table_name, ret = maat_plugin_table_ex_schema_register(maat_instance, table_name,
plugin_ex_new_cb, plugin_ex_new_cb,
@@ -4495,7 +4496,7 @@ TEST_F(MaatCmdTest, PluginEXData) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
const char *key2 = "192.168.0.2"; const char *key2 = "192.168.0.2";
uinfo = (struct user_info *)maat_plugin_table_get_ex_data(maat_instance, uinfo = (struct user_info *)maat_plugin_table_get_ex_data(maat_instance,
table_id, key2); table_id, key2);
@@ -4536,7 +4537,7 @@ TEST_F(MaatCmdTest, UpdateIPPlugin) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
int ex_data_counter = 0; int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_instance, table_name, ret = maat_plugin_table_ex_schema_register(maat_instance, table_name,
@@ -4585,7 +4586,7 @@ TEST_F(MaatCmdTest, UpdateIPPlugin) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
memset(results, 0, sizeof(results)); memset(results, 0, sizeof(results));
ret = maat_ip_plugin_table_get_ex_data(maat_instance, table_id, &ipv4, (void **)results, ARRAY_SIZE); ret = maat_ip_plugin_table_get_ex_data(maat_instance, table_id, &ipv4, (void **)results, ARRAY_SIZE);
@@ -4628,7 +4629,7 @@ TEST_F(MaatCmdTest, UpdateFQDNPlugin) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
int ex_data_counter = 0; int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_instance, table_name, ret = maat_plugin_table_ex_schema_register(maat_instance, table_name,
@@ -4707,7 +4708,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
int ex_data_counter = 0; int ex_data_counter = 0;
ret = maat_plugin_table_ex_schema_register(maat_instance, table_name, ret = maat_plugin_table_ex_schema_register(maat_instance, table_name,
@@ -4738,7 +4739,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
ret = maat_cmd_set_line(maat_instance, &line_rule); ret = maat_cmd_set_line(maat_instance, &line_rule);
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
memset(results, 0, sizeof(results)); memset(results, 0, sizeof(results));
ret = maat_bool_plugin_table_get_ex_data(maat_instance, table_id, items, 3, ret = maat_bool_plugin_table_get_ex_data(maat_instance, table_id, items, 3,
@@ -4748,6 +4749,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
bool_plugin_ex_free_cb(0, (void**)&(results[i]), 0, NULL); bool_plugin_ex_free_cb(0, (void**)&(results[i]), 0, NULL);
} }
} }
#define COMPILE_ID_NUMS 1000 #define COMPILE_ID_NUMS 1000
TEST_F(MaatCmdTest, GroupInMassCompiles) { TEST_F(MaatCmdTest, GroupInMassCompiles) {
const char* g2c_table_name = "GROUP2COMPILE"; const char* g2c_table_name = "GROUP2COMPILE";
@@ -4817,7 +4819,7 @@ TEST_F(MaatCmdTest, GroupInMassCompiles) {
target_compile_id, 0, "null", 2, 0); target_compile_id, 0, "null", 2, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
const char* http_url1 = "https://www.baidu.com/s?wd=tsg"; const char* http_url1 = "https://www.baidu.com/s?wd=tsg";
const char* http_url2 = "https://www.baidu.com/s?wd=zhengzhou&rsv_spt=1&rsv_iqid=0x8b4cae8100000560&issp=1&f=8&rsv_bp=1"; const char* http_url2 = "https://www.baidu.com/s?wd=zhengzhou&rsv_spt=1&rsv_iqid=0x8b4cae8100000560&issp=1&f=8&rsv_bp=1";
@@ -4925,7 +4927,7 @@ TEST_F(MaatCmdTest, HitPath) {
NULL, 0, 0, 0, 0); /*EXPR_TYPE_STRING MATCH_METHOD_SUB*/ NULL, 0, 0, 0, 0); /*EXPR_TYPE_STRING MATCH_METHOD_SUB*/
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
const char* http_url = "en.wikipedia.org/wiki/Path_(graph_theory)"; const char* http_url = "en.wikipedia.org/wiki/Path_(graph_theory)";
const char* http_resp_hdr_cookie = "laptop=thinkpad X1 extrem;time=2020-02-11T15:34:00;main[XWJOKE]=hoho; Hm_lvt_bbac0322e6ee13093f98d5c4b5a10912=1578874808;"; const char* http_resp_hdr_cookie = "laptop=thinkpad X1 extrem;time=2020-02-11T15:34:00;main[XWJOKE]=hoho; Hm_lvt_bbac0322e6ee13093f98d5c4b5a10912=1578874808;";
@@ -4954,7 +4956,8 @@ TEST_F(MaatCmdTest, HitPath) {
EXPECT_EQ(hit_path[path_idx].Nth_scan, Nth_scan); EXPECT_EQ(hit_path[path_idx].Nth_scan, Nth_scan);
EXPECT_EQ(hit_path[path_idx].item_id, item1_id); EXPECT_EQ(hit_path[path_idx].item_id, item1_id);
EXPECT_EQ(hit_path[path_idx].sub_group_id, group1_id); EXPECT_EQ(hit_path[path_idx].sub_group_id, group1_id);
EXPECT_EQ(hit_path[path_idx].top_group_id, group1_id); //EXPECT_EQ(hit_path[path_idx].top_group_id, group1_id);
EXPECT_EQ(hit_path[path_idx].top_group_id, -1);
EXPECT_EQ(hit_path[path_idx].vtable_id, table_id); EXPECT_EQ(hit_path[path_idx].vtable_id, table_id);
EXPECT_EQ(hit_path[path_idx].compile_id, -1); EXPECT_EQ(hit_path[path_idx].compile_id, -1);
@@ -5096,7 +5099,7 @@ TEST_F(MaatCmdTest, SameScanStatusWhenClauseUpdate_TSG6419) {
ret = intval_table_set_line(maat_instance, app_id_table_name, MAAT_OP_ADD, item21_id, ret = intval_table_set_line(maat_instance, app_id_table_name, MAAT_OP_ADD, item21_id,
group21_id, 31, 31, NULL, 0); group21_id, 31, 31, NULL, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
long long results[ARRAY_SIZE] = {0}; long long results[ARRAY_SIZE] = {0};
size_t n_hit_result = 0; size_t n_hit_result = 0;
@@ -5137,7 +5140,7 @@ TEST_F(MaatCmdTest, SameScanStatusWhenClauseUpdate_TSG6419) {
group22_id, 32, 32, NULL, 0); group22_id, 32, 32, NULL, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
table_id = maat_get_table_id(maat_instance, app_id_table_name); table_id = maat_get_table_id(maat_instance, app_id_table_name);
ret = maat_scan_integer(maat_instance, table_id, 31, results, ARRAY_SIZE, ret = maat_scan_integer(maat_instance, table_id, 31, results, ARRAY_SIZE,
@@ -5224,7 +5227,7 @@ TEST_F(MaatCmdTest, GroupEdit) {
scan_app_id, scan_app_id, NULL, 0); scan_app_id, scan_app_id, NULL, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
table_id = maat_get_table_id(maat_instance, ip_table_name); table_id = maat_get_table_id(maat_instance, ip_table_name);
ASSERT_GT(table_id, 0); ASSERT_GT(table_id, 0);
@@ -5296,7 +5299,7 @@ TEST_F(MaatCmdTest, CompileDelete_TSG6548) {
IPv4, "192.168.73.163", "192.168.73.180", 0, 65535, 0); IPv4, "192.168.73.163", "192.168.73.180", 0, 65535, 0);
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
uint32_t ip_addr; uint32_t ip_addr;
inet_pton(AF_INET, "192.168.73.169", &ip_addr); inet_pton(AF_INET, "192.168.73.169", &ip_addr);
@@ -5368,7 +5371,7 @@ TEST_F(MaatCmdTest, UpdateDeadLockDetection) {
"part-1", NULL, 0, 0, 0, 0); /*EXPR_TYPE_STRING MATCH_METHOD_SUB*/ "part-1", NULL, 0, 0, 0, 0); /*EXPR_TYPE_STRING MATCH_METHOD_SUB*/
EXPECT_EQ(ret, 1); EXPECT_EQ(ret, 1);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
const char* scan_data1 = "scan string part-1."; const char* scan_data1 = "scan string part-1.";
const char* scan_data2 = "scan string part-2."; const char* scan_data2 = "scan string part-2.";
@@ -5643,7 +5646,7 @@ TEST_F(MaatRollbackTest, FullConfigRollback) {
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
redisFree(c); redisFree(c);
sleep(WAIT_FOR_EFFECTIVE_S * 2); sleep(WAIT_FOR_EFFECTIVE_S);
ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data), ret = maat_scan_string(maat_instance, table_id, scan_data, strlen(scan_data),
results, ARRAY_SIZE, &n_hit_result, state); results, ARRAY_SIZE, &n_hit_result, state);
@@ -5653,6 +5656,7 @@ TEST_F(MaatRollbackTest, FullConfigRollback) {
maat_state_free(state); maat_state_free(state);
state = NULL; state = NULL;
} }
#endif
int main(int argc, char ** argv) int main(int argc, char ** argv)
{ {

View File

@@ -12,7 +12,7 @@
#define MODULE_FRAMEWORK_PERF_GTEST module_name_str("maat.framework_perf_gtest") #define MODULE_FRAMEWORK_PERF_GTEST module_name_str("maat.framework_perf_gtest")
#define ARRAY_SIZE 10 #define ARRAY_SIZE 10
#define WAIT_FOR_EFFECTIVE_S 1 #define WAIT_FOR_EFFECTIVE_S 2
#define PERF_THREAD_NUM 5 #define PERF_THREAD_NUM 5
const char *table_info_path = "./table_info.conf"; const char *table_info_path = "./table_info.conf";