item_uthash -> item_rcu && add foreign cont dir API

This commit is contained in:
liuwentan
2023-03-15 11:36:54 +08:00
parent 33c9c10467
commit 90d0764845
41 changed files with 2789 additions and 1603 deletions

View File

@@ -34,20 +34,16 @@ struct interval_schema {
struct interval_item {
long long item_id;
long long group_id;
int district_id;
long long district_id;
int low_bound;
int up_bound;
};
struct interval_runtime {
struct interval_matcher *matcher;
struct rcu_hash_table *htable;
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
uint32_t rule_num;
struct maat_item *item_hash;
void (*item_user_data_free)(void *);
struct maat_garbage_bin *ref_garbage_bin;
struct log_handle *logger;
@@ -140,7 +136,7 @@ void *interval_runtime_new(void *interval_schema, int max_thread_num,
struct interval_runtime *interval_rt = ALLOC(struct interval_runtime, 1);
interval_rt->htable = rcu_hash_new(interval_ex_data_free);
interval_rt->item_user_data_free = maat_item_inner_free;
interval_rt->item_htable = rcu_hash_new(maat_item_free);
interval_rt->ref_garbage_bin = garbage_bin;
interval_rt->logger = logger;
@@ -162,10 +158,14 @@ void interval_runtime_free(void *interval_runtime)
interval_rt->htable = NULL;
}
struct maat_item *item = NULL, *tmp = NULL;
HASH_ITER(hh, interval_rt->item_hash, item, tmp) {
HASH_DELETE(hh, interval_rt->item_hash, item);
maat_item_free(item, interval_rt->item_user_data_free);
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;
}
if (interval_rt->hit_cnt != NULL) {
@@ -285,21 +285,34 @@ int interval_runtime_update_row(struct interval_runtime *interval_rt, char *key,
//delete
data = rcu_hash_find(interval_rt->htable, key, key_len);
if (NULL == data) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"[%s:%d] the key of interval rule not exist, can't be deleted, item_id:%llu",
data = rcu_hash_updating_find(interval_rt->htable, key, key_len);
if (NULL == data) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"[%s:%d] the key of interval rule(rule_id:%lld) not exist, can't be deleted from interval runtime htable",
__FUNCTION__, __LINE__, item_id);
return -1;
return -1;
}
}
rcu_hash_del(interval_rt->htable, key, key_len);
} else {
//add
data = rcu_hash_find(interval_rt->htable, key, key_len);
if (data != NULL) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"[%s:%d] the key of interval rule already exist, can't be added, item_id:%llu",
"[%s:%d] the key of interval rule(rule_id:%lld) already exist, can't be added to interval runtime htable",
__FUNCTION__, __LINE__, item_id);
return -1;
}
data = rcu_hash_updating_find(interval_rt->htable, key, key_len);
if (data != NULL) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"[%s:%d] the key of interval rule(rule_id:%lld) already exist, can't be added to interval runtime htable",
__FUNCTION__, __LINE__, item_id);
return -1;
}
rcu_hash_add(interval_rt->htable, key, key_len, (void *)rule);
}
@@ -330,26 +343,33 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema,
return -1;
} else if (0 == is_valid) {
//delete
HASH_FIND(hh, interval_rt->item_hash, &item_id, sizeof(long long), item);
item = (struct maat_item *)rcu_hash_find(interval_rt->item_htable,
(char *)&item_id, sizeof(item_id));
if (NULL == item) {
item = (struct maat_item *)rcu_hash_updating_find(interval_rt->item_htable,
(char *)&item_id, sizeof(item_id));
if (NULL == item) {
return -1;
}
}
rcu_hash_del(interval_rt->item_htable, (char *)&item_id, sizeof(item_id));
} else {
//add
item = (struct maat_item *)rcu_hash_find(interval_rt->item_htable,
(char *)&item_id, sizeof(item_id));;
if (item) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"[%s:%d] interval runtime add item(item_id:%lld) to item_htable failed, already exist",
__FUNCTION__, __LINE__, item_id);
return -1;
}
u_para = (struct maat_item_inner *)item->user_data;
item->user_data = NULL;
if (NULL == u_para) {
return -1;
}
HASH_DELETE(hh, interval_rt->item_hash, item);
maat_garbage_bagging(interval_rt->ref_garbage_bin, u_para, maat_item_inner_free);
} else {
//add
HASH_FIND(hh, interval_rt->item_hash, &item_id, sizeof(long long), item);
item = (struct maat_item *)rcu_hash_updating_find(interval_rt->item_htable,
(char *)&item_id, sizeof(item_id));;
if (item) {
log_error(interval_rt->logger, MODULE_INTERVAL,
"[%s:%d] interval runtime add item %llu to item_hash failed, already exist",
"[%s:%d] interval runtime add item(item_id:%lld) to item_htable failed, already exist",
__FUNCTION__, __LINE__, item_id);
return -1;
}
@@ -360,14 +380,14 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema,
}
u_para = maat_item_inner_new(interval_item->group_id, item_id, interval_item->district_id);
item = maat_item_new(item_id, interval_item->group_id, u_para);
HASH_ADD(hh, interval_rt->item_hash, item_id, sizeof(long long), item);
item = maat_item_new(item_id, interval_item->group_id, u_para, maat_item_inner_free);
rcu_hash_add(interval_rt->item_htable, (char *)&(item_id), sizeof(item_id), item);
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,
"[%s:%d] transform interval table(table_id:%d) item to interval_rule failed, item_id:%llu",
"[%s:%d] transform interval table(table_id:%d) item(item_id:%lld) to interval_rule failed",
__FUNCTION__, __LINE__, schema->table_id, item_id);
return -1;
}
@@ -381,12 +401,6 @@ int interval_runtime_update(void *interval_runtime, void *interval_schema,
interval_rule = NULL;
}
return -1;
} else {
if (0 == is_valid) {
interval_rt->rule_num--;
} else {
interval_rt->rule_num++;
}
}
return 0;
@@ -398,29 +412,33 @@ int interval_runtime_commit(void *interval_runtime, const char *table_name)
return -1;
}
int ret = 0;
struct interval_runtime *interval_rt = (struct interval_runtime *)interval_runtime;
void **ex_data_array = NULL;
size_t rule_cnt = rcu_hash_list_updating_data(interval_rt->htable, &ex_data_array);
if (0 == rule_cnt) {
FREE(ex_data_array);
int updating_flag = rcu_hash_is_updating(interval_rt->htable);
if (0 == updating_flag) {
return 0;
}
struct interval_rule *rules = ALLOC(struct interval_rule, rule_cnt);
rcu_hash_commit(interval_rt->htable);
for (size_t i = 0; i < rule_cnt; i++) {
rules[i] = *(struct interval_rule *)ex_data_array[i];
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];
}
}
struct interval_matcher *new_interval_matcher = NULL;
struct interval_matcher *old_interval_matcher = NULL;
log_info(interval_rt->logger, MODULE_INTERVAL,
"table[%s] committing %zu interval rules for rebuilding interval_matcher engine",
table_name, rule_cnt);
int ret = 0;
struct interval_matcher *new_interval_matcher = NULL;
struct interval_matcher *old_interval_matcher = NULL;
new_interval_matcher = interval_matcher_new(rules, rule_cnt);
if (NULL == new_interval_matcher) {
log_error(interval_rt->logger, MODULE_INTERVAL,
@@ -431,15 +449,20 @@ int interval_runtime_commit(void *interval_runtime, const char *table_name)
old_interval_matcher = interval_rt->matcher;
interval_rt->matcher = new_interval_matcher;
maat_garbage_bagging(interval_rt->ref_garbage_bin, old_interval_matcher,
(void (*)(void*))interval_matcher_free);
rcu_hash_commit(interval_rt->htable);
rule_cnt = rcu_hash_updating_count(interval_rt->htable);
assert(rule_cnt == 0);
if (old_interval_matcher != NULL) {
maat_garbage_bagging(interval_rt->ref_garbage_bin, old_interval_matcher,
(void (*)(void*))interval_matcher_free);
}
rcu_hash_commit(interval_rt->item_htable);
interval_rt->rule_num = rule_cnt;
FREE(rules);
FREE(ex_data_array);
if (rules != NULL) {
FREE(rules);
}
if (ex_data_array != NULL) {
FREE(ex_data_array);
}
return ret;
}
@@ -470,7 +493,7 @@ int interval_runtime_scan(struct interval_runtime *interval_rt, int thread_id,
long long hit_item_ids[MAX_SCANNER_HIT_ITEM_NUM];
struct maat_item_inner *item = NULL;
int real_hit_item_cnt = 0;
int district_id = state->district_id;
long long district_id = state->district_id;
memset(hit_item_ids, 0, sizeof(hit_item_ids));
@@ -482,7 +505,7 @@ int interval_runtime_scan(struct interval_runtime *interval_rt, int thread_id,
}
size_t group_hit_cnt = 0;
int ret = maat_compile_state_update(interval_rt->item_hash, vtable_id, hit_item_ids,
int ret = maat_compile_state_update(interval_rt->item_htable, vtable_id, hit_item_ids,
n_hit_item, &group_hit_cnt, state);
if (ret < 0) {
return -1;