2023-02-07 11:25:31 +08:00
|
|
|
/*
|
|
|
|
|
**********************************************************************************************
|
|
|
|
|
* File: maat_interval.cpp
|
|
|
|
|
* Description:
|
|
|
|
|
* Authors: Liu WenTan <liuwentan@geedgenetworks.com>
|
|
|
|
|
* Date: 2022-10-31
|
|
|
|
|
* Copyright: (c) 2018-2022 Geedge Networks, Inc. All rights reserved.
|
|
|
|
|
***********************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
#include "maat_rule.h"
|
2023-02-24 17:29:38 +08:00
|
|
|
#include "maat_utils.h"
|
2023-02-07 11:25:31 +08:00
|
|
|
#include "rcu_hash.h"
|
|
|
|
|
#include "alignment.h"
|
|
|
|
|
#include "maat_garbage_collection.h"
|
|
|
|
|
#include "maat_compile.h"
|
|
|
|
|
#include "interval_matcher.h"
|
2023-02-24 17:29:38 +08:00
|
|
|
#include "maat_interval.h"
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
#define MODULE_INTERVAL module_name_str("maat.interval")
|
|
|
|
|
|
|
|
|
|
struct interval_schema {
|
|
|
|
|
int item_id_column;
|
|
|
|
|
int group_id_column;
|
|
|
|
|
int district_column;
|
|
|
|
|
int low_bound_column;
|
|
|
|
|
int up_bound_column;
|
|
|
|
|
int table_id;
|
|
|
|
|
struct table_manager *ref_tbl_mgr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct interval_item {
|
2023-02-22 15:22:41 +08:00
|
|
|
long long item_id;
|
|
|
|
|
long long group_id;
|
2023-03-15 11:36:54 +08:00
|
|
|
long long district_id;
|
2023-02-07 11:25:31 +08:00
|
|
|
int low_bound;
|
|
|
|
|
int up_bound;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct interval_runtime {
|
|
|
|
|
struct interval_matcher *matcher;
|
2023-03-15 11:36:54 +08:00
|
|
|
struct rcu_hash_table *htable; //store interval rule for rebuild interval_matcher instance
|
|
|
|
|
struct rcu_hash_table *item_htable; //store this interval table's all maat_item which will be used in interval_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;
|
|
|
|
|
|
|
|
|
|
long long *scan_cnt;
|
|
|
|
|
long long *hit_cnt;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void *interval_schema_new(cJSON *json, struct table_manager *tbl_mgr,
|
|
|
|
|
const char *table_name, struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
struct interval_schema *schema = ALLOC(struct interval_schema, 1);
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
char table_type[NAME_MAX] = {0};
|
2023-02-07 11:25:31 +08:00
|
|
|
cJSON *custom_item = NULL;
|
|
|
|
|
cJSON *item = cJSON_GetObjectItem(json, "table_id");
|
|
|
|
|
if (item != NULL && item->type == cJSON_Number) {
|
|
|
|
|
schema->table_id = item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] table %s has no table_id column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
/* table_type already validate in maat_table_new() */
|
|
|
|
|
item = cJSON_GetObjectItem(json, "table_type");
|
|
|
|
|
memcpy(table_type, item->valuestring, strlen(item->valuestring));
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
item = cJSON_GetObjectItem(json, "custom");
|
|
|
|
|
if (item == NULL || item->type != cJSON_Object) {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
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;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] table %s has no item_id column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "group_id");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->group_id_column = custom_item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] table %s has no group_id column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
/* interval_plus has district */
|
|
|
|
|
if (strcmp(table_type, "intval_plus") == 0) {
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "district");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->district_column = custom_item->valueint;
|
|
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] interval_plus table %s has no district column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "low_bound");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->low_bound_column = custom_item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] table %s has no low_bound column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
|
|
|
|
goto error;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
custom_item = cJSON_GetObjectItem(item, "up_bound");
|
|
|
|
|
if (custom_item != NULL && custom_item->type == cJSON_Number) {
|
|
|
|
|
schema->up_bound_column = custom_item->valueint;
|
2023-03-27 15:52:47 +08:00
|
|
|
} else {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] table %s has no up_bound column",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
schema->ref_tbl_mgr = tbl_mgr;
|
2023-02-07 11:25:31 +08:00
|
|
|
return schema;
|
|
|
|
|
error:
|
|
|
|
|
FREE(schema);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interval_schema_free(void *interval_schema)
|
|
|
|
|
{
|
|
|
|
|
FREE(interval_schema);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interval_ex_data_free(void *user_ctx, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct interval_item *item = (struct interval_item *)data;
|
|
|
|
|
FREE(item);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 13:30:39 +08:00
|
|
|
void interval_maat_item_free(void *user_ctx, void *data)
|
|
|
|
|
{
|
|
|
|
|
struct maat_item *item = (struct maat_item *)data;
|
|
|
|
|
maat_item_free(item);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 11:25:31 +08:00
|
|
|
void *interval_runtime_new(void *interval_schema, int max_thread_num,
|
|
|
|
|
struct maat_garbage_bin *garbage_bin,
|
|
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == interval_schema) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct interval_runtime *interval_rt = ALLOC(struct interval_runtime, 1);
|
|
|
|
|
|
|
|
|
|
interval_rt->htable = rcu_hash_new(interval_ex_data_free);
|
2023-03-15 13:30:39 +08:00
|
|
|
interval_rt->item_htable = rcu_hash_new(interval_maat_item_free);
|
2023-02-07 11:25:31 +08:00
|
|
|
interval_rt->ref_garbage_bin = garbage_bin;
|
|
|
|
|
interval_rt->logger = logger;
|
|
|
|
|
|
|
|
|
|
interval_rt->hit_cnt = alignment_int64_array_alloc(max_thread_num);
|
|
|
|
|
interval_rt->scan_cnt = alignment_int64_array_alloc(max_thread_num);
|
|
|
|
|
|
|
|
|
|
return interval_rt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interval_runtime_free(void *interval_runtime)
|
|
|
|
|
{
|
|
|
|
|
if (NULL == interval_runtime) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct interval_runtime *interval_rt = (struct interval_runtime *)interval_runtime;
|
|
|
|
|
if (interval_rt->htable != NULL) {
|
|
|
|
|
rcu_hash_free(interval_rt->htable);
|
|
|
|
|
interval_rt->htable = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
if (interval_rt->item_htable != NULL) {
|
|
|
|
|
rcu_hash_free(interval_rt->item_htable);
|
|
|
|
|
interval_rt->item_htable = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (interval_rt->matcher != NULL) {
|
|
|
|
|
interval_matcher_free(interval_rt->matcher);
|
|
|
|
|
interval_rt->matcher = NULL;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (interval_rt->hit_cnt != NULL) {
|
|
|
|
|
alignment_int64_array_free(interval_rt->hit_cnt);
|
|
|
|
|
interval_rt->hit_cnt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (interval_rt->scan_cnt != NULL) {
|
|
|
|
|
alignment_int64_array_free(interval_rt->scan_cnt);
|
|
|
|
|
interval_rt->scan_cnt = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FREE(interval_rt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct interval_item *interval_item_new(const char *line, struct interval_schema *schema,
|
|
|
|
|
struct log_handle *logger)
|
|
|
|
|
{
|
|
|
|
|
size_t column_offset = 0;
|
|
|
|
|
size_t column_len = 0;
|
|
|
|
|
enum table_type table_type = TABLE_TYPE_INVALID;
|
|
|
|
|
struct interval_item *item = ALLOC(struct interval_item, 1);
|
|
|
|
|
|
|
|
|
|
int ret = get_column_pos(line, schema->item_id_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] interval 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-02-22 15:08:52 +08:00
|
|
|
item->item_id = atoll(line + column_offset);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->group_id_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] interval 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-02-22 15:08:52 +08:00
|
|
|
item->group_id = atoll(line + column_offset);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
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_INTERVAL,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] interval_plus table(table_id:%d) line:%s district length too long",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2023-02-23 19:08:26 +08:00
|
|
|
} else {
|
|
|
|
|
item->district_id = DISTRICT_ANY;
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->low_bound_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] interval table(table_id:%d) line:%s has no low_bound",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
item->low_bound = atoi(line + column_offset);
|
|
|
|
|
|
|
|
|
|
ret = get_column_pos(line, schema->up_bound_column, &column_offset, &column_len);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(logger, MODULE_INTERVAL,
|
2023-03-02 14:52:31 +08:00
|
|
|
"[%s:%d] interval table(table_id:%d) line:%s has no up_bound",
|
|
|
|
|
__FUNCTION__, __LINE__, schema->table_id, line);
|
2023-02-07 11:25:31 +08:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
item->up_bound = atoi(line + column_offset);
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
error:
|
|
|
|
|
FREE(item);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interval_item_free(void *interval_item)
|
|
|
|
|
{
|
|
|
|
|
FREE(interval_item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct interval_rule *interval_item_to_interval_rule(struct interval_item *item, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct interval_rule *rule = ALLOC(struct interval_rule, 1);
|
|
|
|
|
|
|
|
|
|
rule->start = item->low_bound;
|
|
|
|
|
rule->end = item->up_bound;
|
|
|
|
|
rule->result.rule_id = item->item_id;
|
|
|
|
|
rule->result.user_tag = user_data;
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interval_rule_free(struct interval_rule *rule)
|
|
|
|
|
{
|
|
|
|
|
FREE(rule);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int interval_runtime_update_row(struct interval_runtime *interval_rt, char *key, size_t key_len,
|
2023-02-22 15:22:41 +08:00
|
|
|
long long item_id, struct interval_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(interval_rt->htable, key, key_len);
|
|
|
|
|
} else {
|
|
|
|
|
//add
|
2023-03-15 13:30:39 +08:00
|
|
|
ret = rcu_hash_add(interval_rt->htable, key, key_len, (void *)rule);
|
|
|
|
|
if (ret < 0) {
|
2023-03-15 11:36:54 +08:00
|
|
|
log_error(interval_rt->logger, MODULE_INTERVAL,
|
2023-03-15 13:30:39 +08:00
|
|
|
"[%s:%d] interval rule(rule_id:%lld) add to interval runtime htable failed",
|
2023-03-15 11:36:54 +08:00
|
|
|
__FUNCTION__, __LINE__, item_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int interval_runtime_update(void *interval_runtime, void *interval_schema,
|
2023-03-29 22:25:14 +08:00
|
|
|
const char *table_name, const char *line,
|
|
|
|
|
int valid_column)
|
2023-02-07 11:25:31 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == interval_runtime || NULL == interval_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 interval_rule *interval_rule = NULL;
|
|
|
|
|
struct interval_schema *schema = (struct interval_schema *)interval_schema;
|
|
|
|
|
struct interval_runtime *interval_rt = (struct interval_runtime *)interval_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 11:36:54 +08:00
|
|
|
rcu_hash_del(interval_rt->item_htable, (char *)&item_id, sizeof(item_id));
|
2023-02-07 11:25:31 +08:00
|
|
|
} else {
|
|
|
|
|
//add
|
|
|
|
|
struct interval_item *interval_item = interval_item_new(line, schema, interval_rt->logger);
|
|
|
|
|
if (NULL == interval_item) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 16:45:06 +08:00
|
|
|
u_para = maat_item_inner_new(interval_item->group_id, item_id, interval_item->district_id);
|
2023-03-15 11:36:54 +08:00
|
|
|
item = maat_item_new(item_id, interval_item->group_id, u_para, maat_item_inner_free);
|
2023-03-15 13:30:39 +08:00
|
|
|
ret = rcu_hash_add(interval_rt->item_htable, (char *)&(item_id), sizeof(item_id), item);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
log_error(interval_rt->logger, MODULE_INTERVAL,
|
2023-03-29 22:25:14 +08:00
|
|
|
"[%s:%d] [table:%s] interval runtime add item(item_id:%lld) failed",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, item_id);
|
2023-03-15 13:30:39 +08:00
|
|
|
interval_item_free(interval_item);
|
|
|
|
|
maat_item_free(item);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
interval_rule = interval_item_to_interval_rule(interval_item, u_para);
|
|
|
|
|
interval_item_free(interval_item);
|
|
|
|
|
if (NULL == interval_rule) {
|
|
|
|
|
log_error(interval_rt->logger, MODULE_INTERVAL,
|
2023-03-29 22:25:14 +08:00
|
|
|
"[%s:%d] [table:%s] transform interval_item(item_id:%lld) to interval_rule failed",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, 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 = interval_runtime_update_row(interval_rt, key, sizeof(long long), item_id, interval_rule, is_valid);
|
2023-02-07 11:25:31 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
|
if (interval_rule != NULL) {
|
|
|
|
|
interval_rule_free(interval_rule);
|
|
|
|
|
interval_rule = NULL;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-29 22:25:14 +08:00
|
|
|
void garbage_interval_matcher_free(void *interval_matcher, void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct interval_matcher *matcher = (struct interval_matcher *)interval_matcher;
|
|
|
|
|
interval_matcher_free(matcher);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 22:13:15 +08:00
|
|
|
int interval_runtime_commit(void *interval_runtime, const char *table_name)
|
2023-02-07 11:25:31 +08:00
|
|
|
{
|
|
|
|
|
if (NULL == interval_runtime) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct interval_runtime *interval_rt = (struct interval_runtime *)interval_runtime;
|
|
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
int updating_flag = rcu_hash_is_updating(interval_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(interval_rt->htable);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
void **ex_data_array = NULL;
|
|
|
|
|
struct interval_rule *rules = NULL;
|
|
|
|
|
size_t rule_cnt = rcu_hash_list(interval_rt->htable, &ex_data_array);
|
|
|
|
|
if (rule_cnt > 0) {
|
|
|
|
|
rules = ALLOC(struct interval_rule, rule_cnt);
|
|
|
|
|
for (size_t i = 0; i < rule_cnt; i++) {
|
|
|
|
|
rules[i] = *(struct interval_rule *)ex_data_array[i];
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info(interval_rt->logger, MODULE_INTERVAL,
|
2023-02-09 22:13:15 +08:00
|
|
|
"table[%s] committing %zu interval rules for rebuilding interval_matcher engine",
|
|
|
|
|
table_name, rule_cnt);
|
2023-02-07 11:25:31 +08:00
|
|
|
|
2023-03-15 11:36:54 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
struct interval_matcher *new_interval_matcher = NULL;
|
|
|
|
|
struct interval_matcher *old_interval_matcher = NULL;
|
|
|
|
|
|
2023-03-27 15:52:47 +08:00
|
|
|
if (rule_cnt > 0) {
|
|
|
|
|
new_interval_matcher = interval_matcher_new(rules, rule_cnt);
|
|
|
|
|
if (NULL == new_interval_matcher) {
|
|
|
|
|
log_error(interval_rt->logger, MODULE_INTERVAL,
|
|
|
|
|
"[%s:%d] table[%s]rebuild interval_matcher engine failed when update %zu interval rules",
|
|
|
|
|
__FUNCTION__, __LINE__, table_name, rule_cnt);
|
|
|
|
|
ret = -1;
|
|
|
|
|
}
|
2023-02-07 11:25:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
old_interval_matcher = interval_rt->matcher;
|
|
|
|
|
interval_rt->matcher = new_interval_matcher;
|
2023-03-15 11:36:54 +08:00
|
|
|
if (old_interval_matcher != NULL) {
|
2023-03-29 22:25:14 +08:00
|
|
|
maat_garbage_bagging(interval_rt->ref_garbage_bin, old_interval_matcher, NULL,
|
|
|
|
|
garbage_interval_matcher_free);
|
2023-03-15 11:36:54 +08:00
|
|
|
}
|
|
|
|
|
rcu_hash_commit(interval_rt->item_htable);
|
|
|
|
|
interval_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-15 11:53:46 +08:00
|
|
|
int interval_runtime_scan(struct interval_runtime *interval_rt, int thread_id,
|
2023-02-22 15:22:41 +08:00
|
|
|
long long integer, int vtable_id, struct maat_state *state)
|
2023-02-07 11:25:31 +08:00
|
|
|
{
|
2023-03-06 10:45:36 +08:00
|
|
|
if (0 == interval_rt->rule_num) {
|
|
|
|
|
//empty interval table
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-03 19:01:26 +08:00
|
|
|
struct interval_result hit_results[MAX_SCANNER_HIT_ITEM_NUM];
|
|
|
|
|
memset(hit_results, 0, sizeof(hit_results));
|
2023-02-07 11:25:31 +08:00
|
|
|
|
|
|
|
|
int n_hit_item = interval_matcher_match(interval_rt->matcher, integer,
|
|
|
|
|
hit_results, MAX_SCANNER_HIT_ITEM_NUM);
|
|
|
|
|
if (n_hit_item <= 0) {
|
|
|
|
|
return n_hit_item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n_hit_item > MAX_SCANNER_HIT_ITEM_NUM) {
|
|
|
|
|
log_info(interval_rt->logger, MODULE_INTERVAL,
|
|
|
|
|
"hit interval 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-02-16 16:45:06 +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-02-16 16:45:06 +08:00
|
|
|
|
2023-03-30 22:53:56 +08:00
|
|
|
memset(hit_item_ids, -1, 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-02-16 16:45:06 +08:00
|
|
|
item = (struct maat_item_inner *)(hit_results[i].user_tag);
|
2023-02-23 19:10:23 +08:00
|
|
|
if (item->district_id == district_id || item->district_id == DISTRICT_ANY) {
|
2023-02-16 16:45:06 +08:00
|
|
|
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(interval_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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void interval_runtime_scan_hit_inc(struct interval_runtime *interval_rt, int thread_id)
|
|
|
|
|
{
|
|
|
|
|
alignment_int64_array_add(interval_rt->hit_cnt, thread_id, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long interval_runtime_scan_hit_sum(struct interval_runtime *interval_rt, int n_thread)
|
|
|
|
|
{
|
|
|
|
|
return alignment_int64_array_sum(interval_rt->hit_cnt, n_thread);
|
2023-02-22 15:22:41 +08:00
|
|
|
}
|