diff options
| author | chenzizhan <[email protected]> | 2024-07-04 16:36:32 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2024-07-04 16:36:32 +0800 |
| commit | ef42511b52486a2b79b28b8c9cd3bb68fec2b940 (patch) | |
| tree | d27d69af9c2c41f06b1db77f5e22bf1b3f449bb6 | |
| parent | 277de12fc277f1fc050eb5b92e8958126baa08e3 (diff) | |
metric name set; refactor on get or add cell
| -rw-r--r-- | src/cube.c | 223 | ||||
| -rw-r--r-- | src/cube.h | 2 | ||||
| -rw-r--r-- | src/fieldstat.c | 104 | ||||
| -rw-r--r-- | src/tags/heavy_keeper.c | 2 | ||||
| -rw-r--r-- | src/tags/tag_map.c | 26 | ||||
| -rw-r--r-- | src/tags/tag_map.h | 32 | ||||
| -rw-r--r-- | test/test_metric_counter.cpp | 62 | ||||
| -rw-r--r-- | test/test_register_and_reset.cpp | 16 | ||||
| -rw-r--r-- | vendors/mpack/mpack.h | 2 |
9 files changed, 164 insertions, 305 deletions
@@ -45,7 +45,7 @@ struct cube { enum sampling_mode sampling_mode; union { struct heavy_keeper *topk; - struct tag_map *comprehensive; + struct hash_table *comprehensive; }; size_t max_n_cell; @@ -463,8 +463,8 @@ struct cube *cube_new(const struct fieldstat_tag *shared_tags, size_t n_tag, enu heavy_keeper_set_exdata_schema(cube->topk, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i); break; case SAMPLING_MODE_COMPREHENSIVE: - cube->comprehensive = tag_map_new(max_n_cell); - tag_map_set_exdata_schema(cube->comprehensive, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i); + cube->comprehensive = hash_table_new(max_n_cell); + hash_table_set_exdata_schema(cube->comprehensive, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i); break; default: assert(0); @@ -481,7 +481,7 @@ void cube_free(struct cube *cube) { heavy_keeper_free(cube->topk); break; case SAMPLING_MODE_COMPREHENSIVE: - tag_map_free(cube->comprehensive); + hash_table_free(cube->comprehensive); break; default: assert(0); @@ -497,7 +497,7 @@ void cube_reset(struct cube *cube) { if (cube->sampling_mode == SAMPLING_MODE_TOPK) { heavy_keeper_reset(cube->topk); } else { - tag_map_reset(cube->comprehensive); + hash_table_reset(cube->comprehensive); } } @@ -505,53 +505,54 @@ void cube_set_primary_metric(struct cube *cube, int metric_id) { cube->primary_metric_id = metric_id; } -struct cell *find_or_add_cell_comprehensive(struct tag_map *comprehensive, const char *key, size_t key_len, struct exdata_new_args *args) -{ - struct cell *cell_data = tag_map_get0_exdata(comprehensive, key, key_len); - if (cell_data == NULL) { - int tmp_ret = tag_map_add(comprehensive, key, key_len, (void *)args); - if (tmp_ret != 1) { - return NULL; - } - cell_data = tag_map_get0_exdata(comprehensive, key, key_len); - } - return cell_data; -} - -struct cell *find_or_add_cell_none_primary_topk(struct heavy_keeper *topk, const char *key, size_t key_len, struct exdata_new_args *args) -{ - struct cell *cell_data = heavy_keeper_get0_exdata(topk, key, key_len); - if (cell_data == NULL) { - int tmp_ret = heavy_keeper_add(topk, key, key_len, 0, (void *)args); - if (tmp_ret != 1) { - return NULL; - } - cell_data = heavy_keeper_get0_exdata(topk, key, key_len); - } - return cell_data; -} -// TODO: 整个Switch case 改成 cube_get_cell - -int cube_histogram_record(struct cube *cube, const struct metric_manifest *manifest, const struct fieldstat_tag *tags, size_t n_tag, long long value) { +struct cell *get_cell(struct cube *cube, const struct fieldstat_tag *tags, size_t n_tag,long long increment, int metric_id) { char *tag_in_string; size_t tag_len; build_dynamic_cell_key(tags, n_tag, &tag_in_string, &tag_len); - struct exdata_new_args args; args.tags = tags; args.n_tags = n_tag; struct cell *cell_data = NULL; + switch (cube->sampling_mode) { - case SAMPLING_MODE_TOPK: - cell_data = find_or_add_cell_none_primary_topk(cube->topk, tag_in_string, tag_len, &args); - break; - case SAMPLING_MODE_COMPREHENSIVE: - cell_data = find_or_add_cell_comprehensive(cube->comprehensive, tag_in_string, tag_len, &args); - break; + case SAMPLING_MODE_TOPK: { + if (cube->primary_metric_id != metric_id) { + cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); + if (cell_data == NULL) { + int tmp_ret = heavy_keeper_add(cube->topk, tag_in_string, tag_len, 0, (void *)&args); + if (tmp_ret == 1) { + cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); + } + } + } else { + // heavy_keeper_add should be called anyway, to let the topk record update. + int tmp_ret = heavy_keeper_add(cube->topk, tag_in_string, tag_len, increment, (void *)&args); + if (tmp_ret == 1) { + cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); + } + } + break;} + case SAMPLING_MODE_COMPREHENSIVE: { + cell_data = hash_table_get0_exdata(cube->comprehensive, tag_in_string, tag_len); + if (cell_data == NULL) { + int tmp_ret = hash_table_add(cube->comprehensive, tag_in_string, tag_len, (void *)&args); + if (tmp_ret == 1) { + cell_data = hash_table_get0_exdata(cube->comprehensive, tag_in_string, tag_len); + } + } + break;} } + free(tag_in_string); + return cell_data; +} + +int cube_histogram_record(struct cube *cube, const struct metric_manifest *manifest, const struct fieldstat_tag *tags, size_t n_tag, long long value) { + assert(manifest->type == METRIC_HISTOGRAM); + assert(manifest->id != cube->primary_metric_id); + struct cell *cell_data = get_cell(cube, tags, n_tag, 0, manifest->id); if (cell_data == NULL) { return FS_ERR_TOO_MANY_CELLS; } @@ -565,147 +566,43 @@ int cube_histogram_record(struct cube *cube, const struct metric_manifest *manif } int cube_hll_add(struct cube *cube, const struct metric_manifest *manifest, const struct fieldstat_tag *tags, size_t n_tag, const char *key, size_t key_len) { - char *tag_in_string; - size_t tag_len; - build_dynamic_cell_key(tags, n_tag, &tag_in_string, &tag_len); - - struct exdata_new_args args; - args.tags = tags; - args.n_tags = n_tag; - - struct cell *cell_data = NULL; - switch (cube->sampling_mode) { - case SAMPLING_MODE_TOPK: - cell_data = find_or_add_cell_none_primary_topk(cube->topk, tag_in_string, tag_len, &args); - break; - case SAMPLING_MODE_COMPREHENSIVE: - cell_data = find_or_add_cell_comprehensive(cube->comprehensive, tag_in_string, tag_len, &args); - break; - } + assert(manifest->type == METRIC_HLL); + assert(manifest->id != cube->primary_metric_id); + struct cell *cell_data = get_cell(cube, tags, n_tag, 0, manifest->id); if (cell_data == NULL) { return FS_ERR_TOO_MANY_CELLS; } struct metric *metric = add_or_find_metric_in_cell(manifest, cell_data); metric_hll_add(metric, key, key_len); - free(tag_in_string); - return FS_OK; } int cube_counter_incrby(struct cube *cube, const struct metric_manifest *manifest, const struct fieldstat_tag *tags, size_t n_tag, long long increment) { - char *tag_in_string; - size_t tag_len; - build_dynamic_cell_key(tags, n_tag, &tag_in_string, &tag_len); + assert(manifest->type == METRIC_COUNTER); + assert(cube->primary_metric_id != manifest->id || increment >= 0); - struct exdata_new_args args; - args.tags = tags; - args.n_tags = n_tag; - - struct cell *cell_data = NULL; - switch (cube->sampling_mode) - { - case SAMPLING_MODE_TOPK: - if (cube->primary_metric_id != manifest->id) { - cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); - - if (cell_data == NULL) { - int tmp_ret = heavy_keeper_add(cube->topk, tag_in_string, tag_len, 0, &args); // TODO: 忘了提取函数 - if (tmp_ret != 1) { - free(tag_in_string); - return FS_ERR_TOO_MANY_CELLS; - } - cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); - } - } else { - if (increment < 0) { - free(tag_in_string); - return FS_ERR_INVALID_PARAM; - } else if (increment == 0) { - free(tag_in_string); - return FS_OK; - } - - // heavy_keeper_add should be called anyway, to let the topk record update. - int tmp_ret = heavy_keeper_add(cube->topk, tag_in_string, tag_len, increment, &args); - if (tmp_ret != 1) { - free(tag_in_string); - return FS_ERR_TOO_MANY_CELLS; - } - cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); - } - break; - case SAMPLING_MODE_COMPREHENSIVE: - cell_data = find_or_add_cell_comprehensive(cube->comprehensive, tag_in_string, tag_len, &args); - if (cell_data == NULL) { - free(tag_in_string); - return FS_ERR_TOO_MANY_CELLS; - } - break; - default: - assert(0); - break; + struct cell *cell_data = get_cell(cube, tags, n_tag, increment, manifest->id); + if (cell_data == NULL) { + return FS_ERR_TOO_MANY_CELLS; } struct metric *metric = add_or_find_metric_in_cell(manifest, cell_data); metric_counter_incrby(metric, increment); - free(tag_in_string); return FS_OK; } int cube_counter_set(struct cube *cube, const struct metric_manifest *manifest, const struct fieldstat_tag *tags, size_t n_tag, long long value) { - char *tag_in_string; - size_t tag_len; - build_dynamic_cell_key(tags, n_tag, &tag_in_string, &tag_len); - - struct exdata_new_args args; - args.tags = tags; - args.n_tags = n_tag; - int metric_id = manifest->id; - - struct cell *cell_data = NULL; - switch (cube->sampling_mode) - { - case SAMPLING_MODE_TOPK: { // TODO: 这个地方想办法拿到值以后进counter incrby 流程,重构 - if (cube->primary_metric_id != metric_id) { - cell_data = find_or_add_cell_none_primary_topk(cube->topk, tag_in_string, tag_len, &args); - } else { - long long current_count = 0; - cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); - if (cell_data != NULL) { - const struct metric *tmp_metric = find_metric_in_cell(cell_data, metric_id); - if (tmp_metric != NULL) { - current_count = metric_counter_get(tmp_metric); - } - } - long long increment = value - current_count; - if (increment < 0) { - free(tag_in_string); - return FS_ERR_INVALID_PARAM; - } else if (increment == 0) { - free(tag_in_string); - return FS_OK; - } - - int tmp_ret = heavy_keeper_add(cube->topk, tag_in_string, tag_len, increment, &args); - if (tmp_ret != 1) { - return FS_ERR_TOO_MANY_CELLS; - } - cell_data = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); - } - break;} - case SAMPLING_MODE_COMPREHENSIVE: { - cell_data = find_or_add_cell_comprehensive(cube->comprehensive, tag_in_string, tag_len, &args); - break;} - default: - assert(0); - break; + assert(manifest->type == METRIC_COUNTER); + assert(cube->primary_metric_id != manifest->id); + struct cell *cell_data = get_cell(cube, tags, n_tag, 0, manifest->id); + if (cell_data == NULL) { + return FS_ERR_TOO_MANY_CELLS; } struct metric *metric = add_or_find_metric_in_cell(manifest, cell_data); - free(tag_in_string); metric_counter_set(metric, value); return FS_OK; } @@ -721,7 +618,7 @@ struct cube *cube_copy(const struct cube *cube) cube_dup->topk = heavy_keeper_copy(cube->topk); break; case SAMPLING_MODE_COMPREHENSIVE: - cube_dup->comprehensive = tag_map_copy(cube->comprehensive); + cube_dup->comprehensive = hash_table_copy(cube->comprehensive); break; default: assert(0); @@ -741,7 +638,7 @@ void cube_merge(struct cube *dest, const struct cube *src) heavy_keeper_merge(dest->topk, src->topk); break; case SAMPLING_MODE_COMPREHENSIVE: - tag_map_merge(dest->comprehensive, src->comprehensive); + hash_table_merge(dest->comprehensive, src->comprehensive); break; default: assert(0); @@ -761,7 +658,7 @@ void cube_get_cells(const struct cube *cube, struct fieldstat_tag_list **tag_lis size_t n_cell_tmp = 0; switch (cube->sampling_mode) { case SAMPLING_MODE_COMPREHENSIVE: - n_cell_tmp = tag_map_get_count(cube->comprehensive); + n_cell_tmp = hash_table_get_count(cube->comprehensive); break; case SAMPLING_MODE_TOPK: n_cell_tmp = heavy_keeper_get_count(cube->topk); @@ -779,7 +676,7 @@ void cube_get_cells(const struct cube *cube, struct fieldstat_tag_list **tag_lis struct cell **cell_datas = (struct cell **)malloc(sizeof(struct cell *) * n_cell_tmp); switch (cube->sampling_mode) { case SAMPLING_MODE_COMPREHENSIVE: - tag_map_list(cube->comprehensive, (void **)cell_datas, n_cell_tmp); + hash_table_list(cube->comprehensive, (void **)cell_datas, n_cell_tmp); break; case SAMPLING_MODE_TOPK: heavy_keeper_list(cube->topk, (void **)cell_datas, n_cell_tmp); @@ -820,7 +717,7 @@ const struct cell *get_cell_by_tag_list(const struct cube *cube, const struct fi ret = heavy_keeper_get0_exdata(cube->topk, tag_in_string, tag_len); break; case SAMPLING_MODE_COMPREHENSIVE: - ret = tag_map_get0_exdata(cube->comprehensive, tag_in_string, tag_len); + ret = hash_table_get0_exdata(cube->comprehensive, tag_in_string, tag_len); break; default: assert(0); @@ -925,7 +822,7 @@ int cube_get_serialization(const struct cube *cube, int metric_id, const struct int cube_get_cell_count(const struct cube *cube) { switch (cube->sampling_mode) { case SAMPLING_MODE_COMPREHENSIVE: - return tag_map_get_count(cube->comprehensive); + return hash_table_get_count(cube->comprehensive); case SAMPLING_MODE_TOPK: return heavy_keeper_get_count(cube->topk); default: @@ -11,7 +11,7 @@ extern "C" #include "metric_manifest.h" struct cube; -struct cube_manager; // TODO 这个应该从fieldstat 移进来 +struct cube_manager; struct cube *cube_new(const struct fieldstat_tag *shared_tags, size_t n_tag, enum sampling_mode mode, size_t max_n_cell); void cube_free(struct cube *cube); diff --git a/src/fieldstat.c b/src/fieldstat.c index cd689c9..9b293f1 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -16,12 +16,16 @@ #define DEFAULT_N_METRIC 32 - +struct name_set { + char *name; + UT_hash_handle hh; +}; struct fieldstat { - struct metric_manifest **manifests; // TODO: 把那个哈希表再加回去 + struct metric_manifest **manifests; size_t manifest_size; size_t manifest_cnt; + struct name_set *metric_name_set; struct cube_manager *cube_manager; }; @@ -167,6 +171,13 @@ void fieldstat_free(struct fieldstat *instance) } free(instance->manifests); + struct name_set *name_set, *tmp; + HASH_ITER(hh, instance->metric_name_set, name_set, tmp) { + HASH_DEL(instance->metric_name_set, name_set); + free(name_set->name); + free(name_set); + } + free(instance); } @@ -274,6 +285,10 @@ void add_manifest_to_instance(struct fieldstat *instance, const struct metric_ma if (metric_id >= instance->manifest_cnt) { instance->manifest_cnt = metric_id + 1; } + + struct name_set *name_set = (struct name_set *)malloc(sizeof(struct name_set)); + name_set->name = strdup(manifest->name); + HASH_ADD_KEYPTR(hh, instance->metric_name_set, name_set->name, strlen(name_set->name), name_set); } static int append_manifest_to_instance(struct fieldstat *instance, const struct metric_manifest *metric) @@ -284,24 +299,20 @@ static int append_manifest_to_instance(struct fieldstat *instance, const struct return metric_id; } -int check_before_register_metric(const struct fieldstat *instance, const char *metric_name) +bool find_metric(const struct fieldstat *instance, const char *metric_name) { - if (instance == NULL) { - return FS_ERR_NULL_HANDLER; + struct name_set *name_set; + HASH_FIND_STR(instance->metric_name_set, metric_name, name_set); + if (name_set != NULL) { + return true; } - - if (is_metric_name_duplicate(instance, metric_name)) { - return FS_ERR_INVALID_KEY; - } - - return FS_OK; + return false; } int fieldstat_register_counter(struct fieldstat *instance, const char *metric_name) { - int ret = check_before_register_metric(instance, metric_name); - if (ret != FS_OK) { - return ret; + if (find_metric(instance, metric_name)) { + return FS_ERR_INVALID_KEY; } struct metric_manifest *metric = malloc(sizeof(struct metric_manifest)); @@ -317,13 +328,12 @@ int fieldstat_register_counter(struct fieldstat *instance, const char *metric_na int fieldstat_register_hll(struct fieldstat *instance, const char *metric_name, unsigned char precision) { - int ret = check_before_register_metric(instance, metric_name); - if (ret != FS_OK) { - return ret; - } if (precision < 4 || precision > 18) { return FS_ERR_INVALID_PARAM; } + if (find_metric(instance, metric_name)) { + return FS_ERR_INVALID_KEY; + } struct metric_manifest *metric = malloc(sizeof(struct metric_manifest)); metric->name = strdup(metric_name); @@ -338,10 +348,6 @@ int fieldstat_register_hll(struct fieldstat *instance, const char *metric_name, int fieldstat_register_hist(struct fieldstat *instance, const char *metric_name, long long lowest_trackable_value, long long highest_trackable_value, int significant_figures) { - int ret = check_before_register_metric(instance, metric_name); - if (ret != FS_OK) { - return ret; - } // refer to hdr_histogram.h for the rules of parameters. Just copy them here if (lowest_trackable_value < 1) { return FS_ERR_INVALID_PARAM; @@ -353,6 +359,9 @@ int fieldstat_register_hist(struct fieldstat *instance, const char *metric_name, { return FS_ERR_INVALID_PARAM; } + if (find_metric(instance, metric_name)) { + return FS_ERR_INVALID_KEY; + } struct metric_manifest *metric = malloc(sizeof(struct metric_manifest)); metric->name = strdup(metric_name); @@ -441,12 +450,6 @@ int fieldstat_hist_record(struct fieldstat *instance, int cube_id, int metric_id return cube_histogram_record(cube, manifest, tags, n_tag, value); } -/* -------------------------------------------------------------------------- */ -/* merge */ -/* -------------------------------------------------------------------------- */ - - - int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) { if (instance == NULL || src == NULL) { @@ -454,12 +457,11 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) } int metric_len_src = src->manifest_cnt; - for (int i = 0; i < metric_len_src; i++) { + int metric_len_dst = instance->manifest_cnt; + int len_min = metric_len_src < metric_len_dst ? metric_len_src : metric_len_dst; + for (int i = 0; i < len_min; i++) { const struct metric_manifest *metric_src = src->manifests[i]; const struct metric_manifest *metric_dst = instance->manifests[i]; - if (metric_src == NULL || metric_dst == NULL) { - break; - } if (metric_src->type != metric_dst->type || strcmp(metric_src->name, metric_dst->name) != 0 ) { @@ -467,10 +469,7 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) return FS_ERR_INVALID_PARAM; } } - for (int i = 0; i < metric_len_src; i++) { - if (instance->manifests[i] != NULL || src->manifests[i] == NULL) { - continue; - } + for (int i = metric_len_dst; i < metric_len_src; i++) { const struct metric_manifest *metric_src = src->manifests[i]; append_manifest_to_instance(instance, metric_manifest_copy(metric_src)); } @@ -480,6 +479,30 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) return FS_OK; } +void metric_name_set_callibrate(struct fieldstat *instance, const struct fieldstat *master) +{ + struct name_set *name_set_master, *tmp, *name_set_dest; + // name in dest but not in master + HASH_ITER(hh, instance->metric_name_set, name_set_dest, tmp) { + HASH_FIND_STR(master->metric_name_set, name_set_dest->name, name_set_master); + if (name_set_master == NULL) { + HASH_DEL(instance->metric_name_set, name_set_dest); + free(name_set_dest->name); + free(name_set_dest); + } + } + + // name in master but not in dest + HASH_ITER(hh, master->metric_name_set, name_set_master, tmp) { + HASH_FIND_STR(instance->metric_name_set, name_set_master->name, name_set_dest); + if (name_set_dest == NULL) { + name_set_dest = (struct name_set *)malloc(sizeof(struct name_set)); + name_set_dest->name = strdup(name_set_master->name); + HASH_ADD_KEYPTR(hh, instance->metric_name_set, name_set_dest->name, strlen(name_set_dest->name), name_set_dest); + } + } +} + struct fieldstat *fieldstat_fork(const struct fieldstat *instance) { if (instance == NULL) { @@ -491,9 +514,11 @@ struct fieldstat *fieldstat_fork(const struct fieldstat *instance) new_instance->manifests = calloc(instance->manifest_size, sizeof(struct metric_manifest *)); new_instance->manifest_size = instance->manifest_size; new_instance->manifest_cnt = instance->manifest_cnt; + for (size_t i = 0; i < instance->manifest_cnt; i++) { new_instance->manifests[i] = metric_manifest_copy(instance->manifests[i]); } + metric_name_set_callibrate(new_instance, instance); return new_instance; } @@ -519,15 +544,13 @@ void calibrate_metrics_in_instance(const struct fieldstat *master, struct fields continue; } if (metric_master != NULL && metric_target == NULL) { - const struct metric_manifest *metric_dup = metric_manifest_copy(metric_master); - add_manifest_to_instance(replica, metric_dup, i); + replica->manifests[i] = metric_manifest_copy(metric_master); continue; } if (metric_master->type != metric_target->type || strcmp(metric_master->name, metric_target->name) != 0) { metric_manifest_free(metric_target); - const struct metric_manifest *metric_dup = metric_manifest_copy(metric_master); - add_manifest_to_instance(replica, metric_dup, i); + replica->manifests[i] = metric_manifest_copy(metric_master); continue; } @@ -535,6 +558,7 @@ void calibrate_metrics_in_instance(const struct fieldstat *master, struct fields } replica->manifest_cnt = master->manifest_cnt; + metric_name_set_callibrate(replica, master); } int fieldstat_calibrate(const struct fieldstat *master, struct fieldstat *replica) diff --git a/src/tags/heavy_keeper.c b/src/tags/heavy_keeper.c index 85c9dad..c363199 100644 --- a/src/tags/heavy_keeper.c +++ b/src/tags/heavy_keeper.c @@ -790,7 +790,7 @@ void heavy_keeper_merge(struct heavy_keeper *dest, const struct heavy_keeper *sr if (sorted_set_check_is_full(new_rec)) { unsigned long long tmp_mincnt = sorted_set_get_min_count(new_rec); if (maxv > tmp_mincnt) { - sorted_set_pop(new_rec); // TODO: 如果dest 和 new 共用指针,这里pop 出来以后,dest 的内存好像会变得有点问题。先别纠结这么复杂的事,new 和 dest 使用copy后的data 先试试。 + sorted_set_pop(new_rec); sorted_set_insert_to_available_heap(new_rec, key_arr[i], key_lens[i], maxv, dest->copy_fn(exdatas_src[i])); } } else { diff --git a/src/tags/tag_map.c b/src/tags/tag_map.c index 0c655fa..677a3fb 100644 --- a/src/tags/tag_map.c +++ b/src/tags/tag_map.c @@ -19,7 +19,7 @@ struct tag_exdata_item { UT_hash_handle hh; }; -struct tag_map { +struct hash_table { struct tag_exdata_item *tag_id_map; int current_cell_num; int max_cell_num; @@ -51,8 +51,8 @@ static void *default_copy_fn(void *exdata) { } -struct tag_map *tag_map_new(int max_query_num) { - struct tag_map *pthis = calloc(1, sizeof(struct tag_map)); +struct hash_table *hash_table_new(int max_query_num) { + struct hash_table *pthis = calloc(1, sizeof(struct hash_table)); pthis->max_cell_num = max_query_num; @@ -64,7 +64,7 @@ struct tag_map *tag_map_new(int max_query_num) { return pthis; } -void tag_map_free(struct tag_map *pthis) { +void hash_table_free(struct hash_table *pthis) { struct tag_exdata_item *item, *tmp; HASH_ITER(hh, pthis->tag_id_map, item, tmp) { HASH_DEL(pthis->tag_id_map, item); @@ -75,7 +75,7 @@ void tag_map_free(struct tag_map *pthis) { free(pthis); } -void tag_map_reset(struct tag_map *pthis) { +void hash_table_reset(struct hash_table *pthis) { struct tag_exdata_item *node, *tmp; HASH_ITER(hh, pthis->tag_id_map, node, tmp) { if (!node->dying) { @@ -98,7 +98,7 @@ static char *my_keydup(const char *key, size_t key_len) { return ret; } -int tag_map_add(struct tag_map *pthis, const char *key, size_t key_len, void *arg) { +int hash_table_add(struct hash_table *pthis, const char *key, size_t key_len, void *arg) { struct tag_exdata_item *item; HASH_FIND(hh, pthis->tag_id_map, key, key_len, item); if (item != NULL && !item->dying) { @@ -127,7 +127,7 @@ int tag_map_add(struct tag_map *pthis, const char *key, size_t key_len, void *ar return 1; } -void tag_map_set_exdata_schema(struct tag_map *pthis, exdata_new_cb new_fn, exdata_free_cb free_fn, exdata_merge_cb merge_fn, exdata_reset_cb reset_fn, exdata_copy_cb copy_fn) { +void hash_table_set_exdata_schema(struct hash_table *pthis, exdata_new_cb new_fn, exdata_free_cb free_fn, exdata_merge_cb merge_fn, exdata_reset_cb reset_fn, exdata_copy_cb copy_fn) { pthis->new_fn = new_fn; pthis->free_fn = free_fn; pthis->merge_fn = merge_fn; @@ -135,7 +135,7 @@ void tag_map_set_exdata_schema(struct tag_map *pthis, exdata_new_cb new_fn, exda pthis->copy_fn = copy_fn; } -void *tag_map_get0_exdata(struct tag_map *pthis, const char *key, size_t key_len) { +void *hash_table_get0_exdata(struct hash_table *pthis, const char *key, size_t key_len) { struct tag_exdata_item *item; HASH_FIND(hh, pthis->tag_id_map, key, key_len, item); if (item == NULL || item->dying) { @@ -144,11 +144,11 @@ void *tag_map_get0_exdata(struct tag_map *pthis, const char *key, size_t key_len return item->exdata; } -int tag_map_get_count(const struct tag_map *pthis) { +int hash_table_get_count(const struct hash_table *pthis) { return pthis->current_cell_num; } -size_t tag_map_list(const struct tag_map *pthis, void **exdatas, size_t n_exdatas) { +size_t hash_table_list(const struct hash_table *pthis, void **exdatas, size_t n_exdatas) { size_t actual_len = pthis->current_cell_num; if (actual_len == 0) { return 0; @@ -169,7 +169,7 @@ size_t tag_map_list(const struct tag_map *pthis, void **exdatas, size_t n_exdata return actual_len < n_exdatas ? actual_len : n_exdatas; } -int tag_map_merge(struct tag_map *dest, struct tag_map *src) { +int hash_table_merge(struct hash_table *dest, struct hash_table *src) { struct tag_exdata_item *item_src, *tmp; struct tag_exdata_item *item_dst; HASH_ITER(hh, src->tag_id_map, item_src, tmp) { @@ -205,8 +205,8 @@ int tag_map_merge(struct tag_map *dest, struct tag_map *src) { return 0; } -struct tag_map *tag_map_copy(const struct tag_map *src) { - struct tag_map *pthis = calloc(1, sizeof(struct tag_map)); +struct hash_table *hash_table_copy(const struct hash_table *src) { + struct hash_table *pthis = calloc(1, sizeof(struct hash_table)); pthis->max_cell_num = src->max_cell_num; pthis->current_cell_num = src->current_cell_num; pthis->new_fn = src->new_fn; diff --git a/src/tags/tag_map.h b/src/tags/tag_map.h index 2c8cd47..6f1a31f 100644 --- a/src/tags/tag_map.h +++ b/src/tags/tag_map.h @@ -9,22 +9,22 @@ extern "C"{ #include "exdata.h" #include "my_ut_hash.h" -struct tag_map; // TODO: 重命名成hash table - -struct tag_map *tag_map_new(int max_query_num); -void tag_map_set_exdata_schema(struct tag_map *pthis, exdata_new_cb new_fn, exdata_free_cb free_fn, exdata_merge_cb merge_fn, exdata_reset_cb reset_fn, exdata_copy_cb copy_fn); -void tag_map_free(struct tag_map *pthis); -void tag_map_reset(struct tag_map *pthis); -int tag_map_merge(struct tag_map *dest, struct tag_map *src); -struct tag_map *tag_map_copy(const struct tag_map *src); - -// int tag_map_add(struct tag_map *pthis, const char *key, size_t key_len, int count, void *arg); -int tag_map_add(struct tag_map *pthis, const char *key, size_t key_len, void *arg); - -// void *tag_map_get0_exdata(struct tag_map *pthis, const char *key, size_t key_len); -void *tag_map_get0_exdata(struct tag_map *pthis, const char *key, size_t key_len); -int tag_map_get_count(const struct tag_map *pthis); -size_t tag_map_list(const struct tag_map *pthis, void **exdatas, size_t n_exdatas); +struct hash_table; + +struct hash_table *hash_table_new(int max_query_num); +void hash_table_set_exdata_schema(struct hash_table *pthis, exdata_new_cb new_fn, exdata_free_cb free_fn, exdata_merge_cb merge_fn, exdata_reset_cb reset_fn, exdata_copy_cb copy_fn); +void hash_table_free(struct hash_table *pthis); +void hash_table_reset(struct hash_table *pthis); +int hash_table_merge(struct hash_table *dest, struct hash_table *src); +struct hash_table *hash_table_copy(const struct hash_table *src); + +// int hash_table_add(struct hash_table *pthis, const char *key, size_t key_len, int count, void *arg); +int hash_table_add(struct hash_table *pthis, const char *key, size_t key_len, void *arg); + +// void *hash_table_get0_exdata(struct hash_table *pthis, const char *key, size_t key_len); +void *hash_table_get0_exdata(struct hash_table *pthis, const char *key, size_t key_len); +int hash_table_get_count(const struct hash_table *pthis); +size_t hash_table_list(const struct hash_table *pthis, void **exdatas, size_t n_exdatas); #ifdef __cplusplus } diff --git a/test/test_metric_counter.cpp b/test/test_metric_counter.cpp index efa42ae..f0ed7bc 100644 --- a/test/test_metric_counter.cpp +++ b/test/test_metric_counter.cpp @@ -212,36 +212,6 @@ TEST(metric_test_counter, add_and_query_on_dummy_cell_of_topk) fieldstat_free(instance); } -TEST(metric_test_counter, set_on_primary_metric_going_smaller) -{ - struct fieldstat *instance = fieldstat_new(); - int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); - int metric_id = fieldstat_register_counter(instance, "primary"); - - int ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 10); - EXPECT_EQ(ret, FS_OK); - ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 2); - EXPECT_EQ(ret, FS_ERR_INVALID_PARAM); - - fieldstat_free(instance); -} - -TEST(metric_test_counter, set_on_primary_metric_going_bigger) -{ - struct fieldstat *instance = fieldstat_new(); - int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); - int metric_id = fieldstat_register_counter(instance, "primary"); - - int ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 10); - EXPECT_EQ(ret, FS_OK); - ret = fieldstat_counter_set(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 20); - EXPECT_EQ(ret, FS_OK); - - EXPECT_EQ(my_fieldstat_counter_get(instance, cube_id, metric_id), 20); - - fieldstat_free(instance); -} - TEST(metric_test_counter, primary_counter_add_after_first) { struct fieldstat *instance = fieldstat_new(); @@ -274,38 +244,6 @@ TEST(metric_test_counter, primary_counter_add_after_first) fieldstat_free(instance); } -TEST(metric_test_counter, topk_set_and_test_accuracy) -{ - struct fieldstat *instance = fieldstat_new(); - fieldstat_create_cube(instance, &TEST_TAG_INT_collided, 1, SAMPLING_MODE_TOPK, 6); - fieldstat_register_counter(instance, "test"); - - struct fieldstat_tag tag = TEST_TAG_INT; - // tag : [0, 1, 2, 3, 4 ,5] - // value: [0, 1, 2, 3, 4 ,5] - for (int i = 0; i < 6; i++) { - tag.value_longlong = i; - EXPECT_EQ(fieldstat_counter_set(instance, 0, 0, &tag, 1, i), FS_OK); - } - // tag : [0, 1, 2, 3, 4 ,5, 6, 7, 8] - // value: [0, 1, 2, 100, 100, 100, 100, 100, 100] - for (int i = 0; i < 6; i++) { - tag.value_longlong = i + 3; - EXPECT_EQ(fieldstat_counter_set(instance, 0, 0, &tag, 1, 100), FS_OK); - } - - struct fieldstat_tag_list *tag_list = NULL; - size_t n_cell = 0; - fieldstat_get_cells_used_by_cube(instance, 0, &tag_list, &n_cell); - EXPECT_EQ(n_cell, 6); - for (size_t i = 0; i < n_cell; i++) { - EXPECT_EQ(my_fieldstat_counter_get(instance, 0, 0, &tag_list[i]), 100); - } - - fieldstat_tag_list_arr_free(tag_list, n_cell); - fieldstat_free(instance); -} - int main(int argc, char *argv[]) { testing::InitGoogleTest(&argc, argv); diff --git a/test/test_register_and_reset.cpp b/test/test_register_and_reset.cpp index 11c8abb..b284730 100644 --- a/test/test_register_and_reset.cpp +++ b/test/test_register_and_reset.cpp @@ -568,7 +568,7 @@ TEST(unit_test_tag_map, add_after_reset_and_ensure_performance_improvement) { clock_t start, end; const int TEST_ROUND = 100000; // struct cell_manager *hk = cell_manager_new(SAMPLING_MODE_COMPREHENSIVE, TEST_ROUND); - struct tag_map *hk = tag_map_new(TEST_ROUND); + struct hash_table *hk = hash_table_new(TEST_ROUND); vector<struct tag_hash_key *> keys; for (int i = 0; i < TEST_ROUND; i++) { @@ -577,32 +577,32 @@ TEST(unit_test_tag_map, add_after_reset_and_ensure_performance_improvement) { } for (int i = 0; i < TEST_ROUND; i++) { - tag_map_add(hk, keys[i], NULL); + hash_table_add(hk, keys[i], NULL); } - tag_map_reset(hk); + hash_table_reset(hk); start = clock(); for (int i = 0; i < TEST_ROUND; i++) { - tag_map_add(hk, keys[i], NULL); + hash_table_add(hk, keys[i], NULL); } end = clock(); clock_t time_reset_once = end - start; - tag_map_reset(hk); - tag_map_reset(hk); + hash_table_reset(hk); + hash_table_reset(hk); start = clock(); for (int i = 0; i < TEST_ROUND; i++) { - tag_map_add(hk, keys[i], NULL); + hash_table_add(hk, keys[i], NULL); } end = clock(); clock_t time_reset_twice = end - start; EXPECT_GE(time_reset_twice, time_reset_once); - tag_map_free(hk); + hash_table_free(hk); for (int i = 0; i < TEST_ROUND; i++) { tag_hash_key_free(keys[i]); } diff --git a/vendors/mpack/mpack.h b/vendors/mpack/mpack.h index 1f2386a..26694f5 100644 --- a/vendors/mpack/mpack.h +++ b/vendors/mpack/mpack.h @@ -2387,7 +2387,7 @@ MPACK_INLINE uint32_t mpack_tag_array_count(mpack_tag_t* tag) { * * @see mpack_type_map */ -MPACK_INLINE uint32_t mpack_tag_map_count(mpack_tag_t* tag) { +MPACK_INLINE uint32_t mpack_hash_table_count(mpack_tag_t* tag) { mpack_assert(tag->type == mpack_type_map, "tag is not a map!"); return tag->v.n; } |
