2023-02-06 08:14:25 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: maat_flag.cpp
|
|
|
|
|
* Description:
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
2023-02-06 08:14:25 +08:00
|
|
|
#include "maat_flag.h"
|
2023-02-07 11:25:31 +08:00
|
|
|
#include "flag_matcher.h"
|
|
|
|
|
#include "maat_utils.h"
|
|
|
|
|
#include "maat_rule.h"
|
|
|
|
|
#include "log/log.h"
|
|
|
|
|
#include "uthash/uthash.h"
|
|
|
|
|
#include "rcu_hash.h"
|
|
|
|
|
#include "maat_table.h"
|
|
|
|
|
#include "alignment.h"
|
|
|
|
|
#include "maat_compile.h"
|
|
|
|
|
#include "maat_garbage_collection.h"
|
|
|
|
|
|
|
|
|
|
#define MODULE_FLAG module_name_str("maat.flag")
|
2023-02-06 08:14:25 +08:00
|
|
|
|
|
|
|
|
struct flag_schema {
|
2023-02-07 11:25:31 +08:00
|
|
|
int item_id_column;
|
|
|
|
|
int group_id_column;
|
2023-03-01 17:44:07 +08:00
|
|
|
int district_column;
|
2023-02-07 11:25:31 +08:00
|
|
|
int flag_column;
|
|
|
|
|
int flag_mask_column;
|
|
|
|
|
int table_id;
|
|
|
|
|
struct table_manager *ref_tbl_mgr;
|
|
|
|
|
};
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
struct flag_item {
|
2023-02-24 18:20:04 +08:00
|
|
|
long long item_id;
|
|
|
|
|
long long group_id;
|
2023-03-15 11:36:54 +08:00
|
|
|
long long district_id;
|
2023-02-24 18:20:04 +08:00
|
|
|
long long flag;
|
|
|
|
|
long long flag_mask;
|
2023-02-06 08:14:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct flag_runtime {
|
2023-02-07 11:25:31 +08:00
|
|
|
struct flag_matcher *matcher;
|
2023-03-15 11:36:54 +08:00
|
|
|
struct rcu_hash_table *htable; //store flag rule for rebuild flag_matcher instance
|
|
|
|
|
struct rcu_hash_table *item_htable; //store this flag table's all maat_item which will be used in flag_runtime_scan
|
2023-02-07 11:25:31 +08:00
|
|
|
uint32_t rule_num;
|
|
|
|
|
struct maat_garbage_bin *ref_garbage_bin;
|
|
|
|
|
struct log_handle *logger;
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
long long *scan_cnt;
|
|
|
|
|
long long *hit_cnt;
|
2023-02-06 08:14:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void *flag_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
|
|
|
|
const char *table_name, struct log_handle *logger)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
int read_cnt = 0;
|
|
|
|
|
struct flag_schema *schema = ALLOC(struct flag_schema, 1);
|
|
|
|
|
|
|
|
|
|
cJSON *custom_item = NULL;
|
|
|
|
|
cJSON *item = cJSON_GetObjectItem(json, "table_id");
|
|
|
|
|
if (item != NULL && item->type == cJSON_Number) {
|
|
|
|
|
schema->table_id = item->valueint;
|
|
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item = cJSON_GetObjectItem(json, "custom");
|
|
|
|
|
if (item == NULL || item->type != cJSON_Object) {
|
|
|
|
|
log_error(logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] table %s has no custom column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "item_id");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->item_id_column = custom_item->valueint;
|
|
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "group_id");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->group_id_column = custom_item->valueint;
|
|
|
|
|
read_cnt++;
|
|
|
|
|
}
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
custom_item = cJSON_GetObjectItem(item, "district");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->district_column = custom_item->valueint;
|
|
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
custom_item = cJSON_GetObjectItem(item, "flag");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->flag_column = custom_item->valueint;
|
|
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "flag_mask");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->flag_mask_column = custom_item->valueint;
|
|
|
|
|
read_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
schema->ref_tbl_mgr = tbl_mgr;
|
|
|
|
|
|
|
|
|
|
if (read_cnt < 5) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return schema;
|
|
|
|
|
error:
|
|
|
|
|
FREE(schema);
|
|
|
|
|
return NULL;
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flag_schema_free(void *flag_schema)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
FREE(flag_schema);
|
|
|
|
|
}
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
void flag_ex_data_free(void *user_ctx, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct flag_item *flag_item = (struct flag_item *)data;
|
|
|
|
|
FREE(flag_item);
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 13:30:39 +08:00
|
|
|
void flag_maat_item_free(void *user_ctx, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct maat_item *item = (struct maat_item *)data;
|
|
|
|
|
maat_item_free(item);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 08:14:25 +08:00
|
|
|
void *flag_runtime_new(void *flag_schema, int max_thread_num,
|
|
|
|
|
struct maat_garbage_bin *garbage_bin,
|
|
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
if (NULL == flag_schema) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct flag_runtime *flag_rt = ALLOC(struct flag_runtime, 1);
|
|
|
|
|
|
|
|
|
|
flag_rt->htable = rcu_hash_new(flag_ex_data_free);
|
2023-03-15 13:30:39 +08:00
|
|
|
flag_rt->item_htable = rcu_hash_new(flag_maat_item_free);
|
2023-02-07 11:25:31 +08:00
|
|
|
flag_rt->ref_garbage_bin = garbage_bin;
|
|
|
|
|
flag_rt->logger = logger;
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
flag_rt->hit_cnt = alignment_int64_array_alloc(max_thread_num);
|
|
|
|
|
flag_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
|
|
|
|
|
|
|
|
|
|
return flag_rt;
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flag_runtime_free(void *flag_runtime)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
if (NULL == flag_runtime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct flag_runtime *flag_rt = (struct flag_runtime *)flag_runtime;
|
|
|
|
|
if (flag_rt->htable != NULL) {
|
|
|
|
|
rcu_hash_free(flag_rt->htable);
|
|
|
|
|
flag_rt->htable = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
if (flag_rt->item_htable != NULL) {
|
|
|
|
|
rcu_hash_free(flag_rt->item_htable);
|
|
|
|
|
flag_rt->item_htable = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag_rt->matcher != NULL) {
|
|
|
|
|
flag_matcher_free(flag_rt->matcher);
|
|
|
|
|
flag_rt->matcher = NULL;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag_rt->hit_cnt != NULL) {
|
|
|
|
|
alignment_int64_array_free(flag_rt->hit_cnt);
|
|
|
|
|
flag_rt->hit_cnt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag_rt->scan_cnt != NULL) {
|
|
|
|
|
alignment_int64_array_free(flag_rt->scan_cnt);
|
|
|
|
|
flag_rt->scan_cnt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FREE(flag_rt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int flag_runtime_update_row(struct flag_runtime *flag_rt, char *key, size_t key_len,
|
2023-02-24 18:20:04 +08:00
|
|
|
long long item_id, struct flag_rule *rule, int is_valid)
|
2023-02-07 11:25:31 +08:00
|
|
|
{
|
2023-03-15 13:30:39 +08:00
|
|
|
int ret = -1;
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
if (0 == is_valid) {
|
|
|
|
|
//delete
|
|
|
|
|
rcu_hash_del(flag_rt->htable, key, key_len);
|
|
|
|
|
} else {
|
|
|
|
|
//add
|
2023-03-15 13:30:39 +08:00
|
|
|
ret = rcu_hash_add(flag_rt->htable, key, key_len, (void *)rule);
|
|
|
|
|
if (ret < 0) {
|
2023-03-15 11:36:54 +08:00
|
|
|
log_error(flag_rt->logger, MODULE_FLAG,
|
2023-03-15 13:30:39 +08:00
|
|
|
"[%s:%d] flag rule(rule_id:%lld) add to flag runtime htable failed",
|
2023-03-02 14:52:31 +08:00
|
|
|
__FUNCTION__, __LINE__, item_id);
|
2023-02-07 11:25:31 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
struct flag_item *flag_item_new(const char *line, struct flag_schema *schema,
|
2023-02-07 11:25:31 +08:00
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
size_t column_offset = 0;
|
|
|
|
|
size_t column_len = 0;
|
2023-03-01 17:44:07 +08:00
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
struct flag_item *item = ALLOC(struct flag_item, 1);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
int ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] flag table(table_id:%d) line:%s has no item_id",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-03-01 17:44:07 +08:00
|
|
|
item->item_id = atoll(line + column_offset);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
ret = get_column_pos(line, schema->group_id_column, &column_offset, &column_len);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] flag table(table_id:%d) line:%s has no group_id",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-03-01 17:44:07 +08:00
|
|
|
item->group_id = atoll(line + column_offset);
|
|
|
|
|
|
|
|
|
|
table_type = table_manager_get_table_type(schema->ref_tbl_mgr, schema->table_id);
|
|
|
|
|
if (table_type == TABLE_TYPE_INTERVAL_PLUS) {
|
|
|
|
|
ret = get_column_pos(line, schema->district_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (column_len >= MAX_DISTRICT_STR) {
|
|
|
|
|
log_error(logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] flag_plus table(table_id:%d) line:%s district length too long",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-03-01 17:44:07 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
char district[MAX_DISTRICT_STR] = {0};
|
|
|
|
|
memcpy(district, (line + column_offset), column_len);
|
|
|
|
|
assert(strlen(district) > 0);
|
|
|
|
|
str_unescape(district);
|
|
|
|
|
item->district_id = table_manager_get_district_id(schema->ref_tbl_mgr, district);
|
|
|
|
|
} else {
|
|
|
|
|
item->district_id = DISTRICT_ANY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->flag_column, &column_offset, &column_len);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] flag table(table_id:%d) line:%s has no flag",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
item->flag = strtoull(line + column_offset, NULL, 0);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
ret = get_column_pos(line, schema->flag_mask_column, &column_offset, &column_len);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] flag table(table_id:%d) line:%s has no flag_mask",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
2023-03-01 17:44:07 +08:00
|
|
|
item->flag_mask = strtoull(line + column_offset, NULL, 0);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
return item;
|
2023-02-07 11:25:31 +08:00
|
|
|
error:
|
2023-03-01 17:44:07 +08:00
|
|
|
FREE(item);
|
2023-02-07 11:25:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flag_item_free(struct flag_item *item)
|
|
|
|
|
{
|
|
|
|
|
FREE(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct flag_rule *flag_item_to_flag_rule(struct flag_item *item, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct flag_rule *rule = ALLOC(struct flag_rule, 1);
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
rule->rule_id = item->item_id;
|
|
|
|
|
rule->flag = item->flag;
|
|
|
|
|
rule->mask = item->flag_mask;
|
|
|
|
|
rule->user_tag = user_data;
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flag_rule_free(struct flag_rule *rule)
|
|
|
|
|
{
|
|
|
|
|
FREE(rule);
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int flag_runtime_update(void *flag_runtime, void *flag_schema,
|
|
|
|
|
const char *line, int valid_column)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
if (NULL == flag_runtime || NULL == flag_schema ||
|
|
|
|
|
NULL == line) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 13:30:39 +08:00
|
|
|
int ret = -1;
|
2023-02-07 11:25:31 +08:00
|
|
|
struct maat_item_inner *u_para = NULL;
|
|
|
|
|
struct maat_item *item = NULL;
|
|
|
|
|
struct flag_rule *flag_rule = NULL;
|
|
|
|
|
struct flag_schema *schema = (struct flag_schema *)flag_schema;
|
|
|
|
|
struct flag_runtime *flag_rt = (struct flag_runtime *)flag_runtime;
|
|
|
|
|
|
2023-02-22 15:08:52 +08:00
|
|
|
long long item_id = get_column_value(line, schema->item_id_column);
|
|
|
|
|
if (item_id < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
int is_valid = get_column_value(line, valid_column);
|
|
|
|
|
if (is_valid < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
} else if (0 == is_valid) {
|
|
|
|
|
//delete
|
2023-03-15 13:30:39 +08:00
|
|
|
rcu_hash_del(flag_rt->item_htable, (char *)&item_id, sizeof(item_id));
|
2023-02-07 11:25:31 +08:00
|
|
|
} else {
|
|
|
|
|
//add
|
|
|
|
|
struct flag_item *flag_item = flag_item_new(line, schema, flag_rt->logger);
|
|
|
|
|
if (NULL == flag_item) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 17:44:07 +08:00
|
|
|
u_para = maat_item_inner_new(flag_item->group_id, item_id, flag_item->district_id);
|
2023-03-15 11:36:54 +08:00
|
|
|
item = maat_item_new(item_id, flag_item->group_id, u_para, maat_item_inner_free);
|
2023-03-15 13:30:39 +08:00
|
|
|
ret = rcu_hash_add(flag_rt->item_htable, (char *)&(item_id), sizeof(item_id), item);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(flag_rt->logger, MODULE_FLAG,
|
|
|
|
|
"[%s:%d] flag runtime add item(item_id:%lld) to item_htable failed",
|
|
|
|
|
__FUNCTION__, __LINE__, item_id);
|
|
|
|
|
flag_item_free(flag_item);
|
|
|
|
|
maat_item_free(item);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
flag_rule = flag_item_to_flag_rule(flag_item, u_para);
|
|
|
|
|
flag_item_free(flag_item);
|
|
|
|
|
if (NULL == flag_rule) {
|
|
|
|
|
log_error(flag_rt->logger, MODULE_FLAG,
|
2023-03-15 11:36:54 +08:00
|
|
|
"[%s:%d] transform flag table(table_id:%d) item(item_id:%lld) to flag_rule failed",
|
2023-03-02 14:52:31 +08:00
|
|
|
__FUNCTION__, __LINE__, schema->table_id, item_id);
|
2023-02-07 11:25:31 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *key = (char *)&item_id;
|
2023-03-15 13:30:39 +08:00
|
|
|
ret = flag_runtime_update_row(flag_rt, key, sizeof(long long), item_id, flag_rule, is_valid);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (flag_rule != NULL) {
|
|
|
|
|
flag_rule_free(flag_rule);
|
|
|
|
|
flag_rule = NULL;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
int flag_runtime_commit(void *flag_runtime, const char *table_name)
|
2023-02-06 08:14:25 +08:00
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
if (NULL == flag_runtime) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct flag_runtime *flag_rt = (struct flag_runtime *)flag_runtime;
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
int updating_flag = rcu_hash_is_updating(flag_rt->htable);
|
|
|
|
|
if (0 == updating_flag) {
|
2023-02-07 11:25:31 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
rcu_hash_commit(flag_rt->htable);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
struct flag_rule *rules = NULL;
|
|
|
|
|
void **ex_data_array = NULL;
|
|
|
|
|
size_t rule_cnt = rcu_hash_list(flag_rt->htable, &ex_data_array);
|
|
|
|
|
if (rule_cnt > 0) {
|
|
|
|
|
rules = ALLOC(struct flag_rule, rule_cnt);
|
|
|
|
|
for (size_t i = 0; i < rule_cnt; i++) {
|
|
|
|
|
rules[i] = *(struct flag_rule *)ex_data_array[i];
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
log_info(flag_rt->logger, MODULE_FLAG,
|
2023-02-09 22:13:15 +08:00
|
|
|
"table[%s] committing %zu flag rules for rebuilding flag_matcher engine",
|
|
|
|
|
table_name, rule_cnt);
|
2023-03-15 11:36:54 +08:00
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
struct flag_matcher *new_flag_matcher = NULL;
|
|
|
|
|
struct flag_matcher *old_flag_matcher = NULL;
|
2023-02-07 11:25:31 +08:00
|
|
|
new_flag_matcher = flag_matcher_new(rules, rule_cnt);
|
|
|
|
|
if (NULL == new_flag_matcher) {
|
|
|
|
|
log_error(flag_rt->logger, MODULE_FLAG,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] table[%s] rebuild flag_matcher engine failed when update %zu flag rules",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, rule_cnt);
|
2023-02-07 11:25:31 +08:00
|
|
|
ret = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
old_flag_matcher = flag_rt->matcher;
|
|
|
|
|
flag_rt->matcher = new_flag_matcher;
|
2023-03-15 11:36:54 +08:00
|
|
|
if (old_flag_matcher != NULL) {
|
|
|
|
|
maat_garbage_bagging(flag_rt->ref_garbage_bin, old_flag_matcher,
|
2023-02-07 11:25:31 +08:00
|
|
|
(void (*)(void*))flag_matcher_free);
|
2023-03-15 11:36:54 +08:00
|
|
|
}
|
|
|
|
|
rcu_hash_commit(flag_rt->item_htable);
|
|
|
|
|
flag_rt->rule_num = rule_cnt;
|
|
|
|
|
|
|
|
|
|
if (rules != NULL) {
|
|
|
|
|
FREE(rules);
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
if (ex_data_array != NULL) {
|
|
|
|
|
FREE(ex_data_array);
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
return ret;
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 11:53:46 +08:00
|
|
|
int flag_runtime_scan(struct flag_runtime *flag_rt, int thread_id,
|
2023-02-22 15:22:41 +08:00
|
|
|
long long flag, int vtable_id, struct maat_state *state)
|
2023-02-06 08:14:25 +08:00
|
|
|
{
|
2023-03-06 10:45:36 +08:00
|
|
|
if (0 == flag_rt->rule_num) {
|
|
|
|
|
//empty flag table
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
struct flag_result hit_results[MAX_SCANNER_HIT_ITEM_NUM] = {0};
|
|
|
|
|
|
|
|
|
|
int n_hit_item = flag_matcher_match(flag_rt->matcher, flag,
|
|
|
|
|
hit_results, MAX_SCANNER_HIT_ITEM_NUM);
|
|
|
|
|
if (n_hit_item <= 0) {
|
|
|
|
|
return n_hit_item;
|
|
|
|
|
}
|
2023-02-06 08:14:25 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
if (n_hit_item > MAX_SCANNER_HIT_ITEM_NUM) {
|
|
|
|
|
log_info(flag_rt->logger, MODULE_FLAG,
|
|
|
|
|
"hit flag item count:%d exceed maxium:%d",
|
|
|
|
|
n_hit_item, MAX_SCANNER_HIT_ITEM_NUM);
|
|
|
|
|
n_hit_item = MAX_SCANNER_HIT_ITEM_NUM;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 15:22:41 +08:00
|
|
|
long long hit_item_ids[MAX_SCANNER_HIT_ITEM_NUM];
|
2023-03-01 17:44:07 +08:00
|
|
|
struct maat_item_inner *item = NULL;
|
|
|
|
|
int real_hit_item_cnt = 0;
|
2023-03-15 11:36:54 +08:00
|
|
|
long long district_id = state->district_id;
|
2023-03-01 17:44:07 +08:00
|
|
|
|
2023-02-22 15:08:52 +08:00
|
|
|
memset(hit_item_ids, 0, sizeof(hit_item_ids));
|
2023-02-16 16:45:06 +08:00
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
for (int i = 0; i < n_hit_item; i++) {
|
2023-03-01 17:44:07 +08:00
|
|
|
item = (struct maat_item_inner *)(hit_results[i].user_tag);
|
|
|
|
|
if (item->district_id == district_id || item->district_id == DISTRICT_ANY) {
|
|
|
|
|
hit_item_ids[real_hit_item_cnt++] = hit_results[i].rule_id;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t group_hit_cnt = 0;
|
2023-03-15 11:36:54 +08:00
|
|
|
int ret = maat_compile_state_update(flag_rt->item_htable, vtable_id, hit_item_ids,
|
2023-02-22 15:08:52 +08:00
|
|
|
n_hit_item, &group_hit_cnt, state);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return group_hit_cnt;
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flag_runtime_scan_hit_inc(struct flag_runtime *flag_rt, int thread_id)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
alignment_int64_array_add(flag_rt->hit_cnt, thread_id, 1);
|
2023-02-06 08:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long flag_runtime_scan_hit_sum(struct flag_runtime *flag_rt, int n_thread)
|
|
|
|
|
{
|
2023-02-07 11:25:31 +08:00
|
|
|
return alignment_int64_array_sum(flag_rt->hit_cnt, n_thread);
|
2023-02-24 18:20:04 +08:00
|
|
|
}
|