diff options
| -rw-r--r-- | include/fieldstat/fieldstat.h | 8 | ||||
| -rw-r--r-- | src/exporter/cjson_exporter.c | 4 | ||||
| -rw-r--r-- | src/fieldstat.c | 69 | ||||
| -rw-r--r-- | test/test_register_and_reset.cpp | 189 | ||||
| -rw-r--r-- | test/utils.hpp | 1 |
5 files changed, 110 insertions, 161 deletions
diff --git a/include/fieldstat/fieldstat.h b/include/fieldstat/fieldstat.h index d5e14fe..6f84377 100644 --- a/include/fieldstat/fieldstat.h +++ b/include/fieldstat/fieldstat.h @@ -116,7 +116,7 @@ int fieldstat_register_hist(struct fieldstat *instance, const char *metric_name, /* * @brief Delete the cell added by fieldstat_cube_add. Increase the cell_version by 1. Also, delete all the metric records of this cell. The cell id will not be reused. Does not support topk sampling mode. * @param refer to fieldstat_cube_add. - * @return the deleted cell id >= 0, if success; + * @return FS_OK if success; * return FS_ERR_NULL_HANDLER, FS_ERR_INVALID_CUBE_ID, * return FS_ERR_INVALID_PARAM when mode is TOPK. * return FS_ERR_INVALID_KEY when the cell is not found. @@ -200,7 +200,7 @@ void fieldstat_get_cubes(const struct fieldstat *instance, int **cube_ids, int * * @brief Get all the registered metrics of a cube. * @return return FS_ERR_NULL_HANDLER or FS_ERR_INVALID_CUBE_ID. Or FS_OK */ -int fieldstat_get_metrics_using_by_cube(const struct fieldstat *instance, int cube_id, int **metric_id_out, size_t *n_metric); +int fieldstat_get_metrics_used_by_cube(const struct fieldstat *instance, int cube_id, int **metric_id_out, size_t *n_metric); void fieldstat_get_metrics(const struct fieldstat *instance, int **metric_id_out, size_t *n_metric); @@ -210,10 +210,10 @@ const char *fieldstat_get_metric_name(const struct fieldstat *instance, int metr // query the type of the metric. return -1 if cube_id or metric_id is invalid. enum metric_type fieldstat_get_metric_type(const struct fieldstat *instance, int metric_id); -void fieldstat_get_cells_using_by_metric(const struct fieldstat *instance, int cube_id, int metric_id, +void fieldstat_get_cells_used_by_metric(const struct fieldstat *instance, int cube_id, int metric_id, struct fieldstat_tag_list **tag_list, size_t *n_cell); -void fieldstat_get_cells_using_by_cube(const struct fieldstat *instance, int cube_id, struct fieldstat_tag_list **tag_list, size_t *n_cell); +void fieldstat_get_cells_used_by_cube(const struct fieldstat *instance, int cube_id, struct fieldstat_tag_list **tag_list, size_t *n_cell); /* * @brief Get all the cells in a cube and their tags in a specific metric. Different metrics may have different cells. diff --git a/src/exporter/cjson_exporter.c b/src/exporter/cjson_exporter.c index 125f5d0..e08d8ad 100644 --- a/src/exporter/cjson_exporter.c +++ b/src/exporter/cjson_exporter.c @@ -428,13 +428,13 @@ int cell_iter_next_cube(struct cell_iter *iter) { while (iter->curr_cube_idx < iter->n_cube - 1) { int cube_id_next = iter->cube_ids[++iter->curr_cube_idx]; - fieldstat_get_metrics_using_by_cube(instance, cube_id_next, &iter->metric_ids, &iter->n_metric); + fieldstat_get_metrics_used_by_cube(instance, cube_id_next, &iter->metric_ids, &iter->n_metric); if (iter->n_metric == 0) { continue; } // get cell info - fieldstat_get_cells_using_by_cube(instance, cube_id_next, &iter->tag_list, &iter->n_cell); + fieldstat_get_cells_used_by_cube(instance, cube_id_next, &iter->tag_list, &iter->n_cell); if (iter->n_cell == 0) { continue; } diff --git a/src/fieldstat.c b/src/fieldstat.c index 83506fc..fe254cd 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -82,6 +82,16 @@ void name_id_map_free(struct metric_name_id_map *map) } } +struct metric_name_id_map *name_id_map_copy(struct metric_name_id_map *map) +{ + struct metric_name_id_map *map_dup = NULL; + struct metric_name_id_map *entry, *tmp; + HASH_ITER(hh, map, entry, tmp) { + name_id_map_add(&map_dup, entry->name, entry->id); + } + return map_dup; +} + struct fieldstat *fieldstat_new() { struct fieldstat *instance = calloc(1, sizeof(struct fieldstat)); @@ -115,6 +125,7 @@ void fieldstat_free(struct fieldstat *instance) for (size_t i = 0; i < instance->n_metric_master; i++) { metric_free(instance->metric_masters[i]); } + free(instance->metric_masters); name_id_map_free(instance->metric_name_id_map); free(instance); @@ -380,7 +391,7 @@ int fieldstat_cube_remove(struct fieldstat *instance, int cube_id, const struct tag_hash_key_free(tag_key); instance->cell_version++; - return id; + return FS_OK; } void fieldstat_cube_free(struct fieldstat *instance, int cube_id) @@ -868,48 +879,17 @@ struct fieldstat *fieldstat_fork(const struct fieldstat *instance) add_metric_to_cube(new_instance->cube[i], new_metric, j); } } - new_instance->cube_version = calloc(new_instance->max_n_cube, sizeof(unsigned long)); - return new_instance; -} - -void calibrate_metrics_in_cube(const struct fs_cube *master, struct fs_cube *replica) -{ - if (replica->max_n_metric < master->max_n_metric) { - replica->metrics = (struct metric **)realloc(replica->metrics, sizeof(struct metric *) * master->max_n_metric); - memset(replica->metrics + replica->max_n_metric, 0, sizeof(struct metric *) * (master->max_n_metric - replica->max_n_metric)); - replica->max_n_metric = master->max_n_metric; + new_instance->metric_masters = calloc(instance->max_n_metric_master, sizeof(struct metric *)); + new_instance->max_n_metric_master = instance->max_n_metric_master; + new_instance->n_metric_master = instance->n_metric_master; + for (size_t i = 0; i < instance->n_metric_master; i++) { + new_instance->metric_masters[i] = metric_fork(instance->metric_masters[i]); } + new_instance->metric_name_id_map = name_id_map_copy(instance->metric_name_id_map); - size_t longer_arr_len = master->n_metric > replica->n_metric ? master->n_metric : replica->n_metric; - for (size_t i = 0; i < longer_arr_len; i++) { - const struct metric *metric_master = i >= master->n_metric ? NULL : master->metrics[i]; - struct metric *metric_target = i >= replica->n_metric ? NULL : replica->metrics[i]; - if (metric_master == NULL && metric_target == NULL) { - continue; - } - if (metric_master == NULL && metric_target != NULL) { - metric_free(metric_target); - replica->metrics[i] = NULL; - continue; - } - if (metric_master != NULL && metric_target == NULL) { - struct metric *metric_dup = metric_fork(metric_master); - add_metric_to_cube(replica, metric_dup, i); - continue; - } - - if (metric_get_type(metric_master) != metric_get_type(metric_target) || - strcmp(metric_get_name(metric_master), metric_get_name(metric_target)) != 0 - ) { - metric_free(metric_target); - struct metric *metric_dup = metric_fork(metric_master); - add_metric_to_cube(replica, metric_dup, i); - continue; - } - } - replica->n_metric = master->n_metric; + return new_instance; } void calibrate_metrics_in_instance(const struct fieldstat *master, struct fieldstat *replica) @@ -969,7 +949,7 @@ int fieldstat_calibrate(const struct fieldstat *master, struct fieldstat *replic size_t longer_arr_len = master->valid_cube_arr_length > replica->valid_cube_arr_length ? master->valid_cube_arr_length : replica->valid_cube_arr_length; for (size_t i = 0; i < longer_arr_len; i++) { const struct fs_cube *cube_master = i >= master->valid_cube_arr_length ? NULL : master->cube[i]; - struct fs_cube *cube_target = i >= replica->valid_cube_arr_length ? NULL : replica->cube[i]; + const struct fs_cube *cube_target = i >= replica->valid_cube_arr_length ? NULL : replica->cube[i]; if (cube_master == NULL && cube_target == NULL) { continue; } @@ -979,19 +959,16 @@ int fieldstat_calibrate(const struct fieldstat *master, struct fieldstat *replic } if (cube_master != NULL && cube_target == NULL) { struct fs_cube *cube_dup = fieldstat_cube_fork(cube_master); - calibrate_metrics_in_cube(cube_master, cube_dup); add_cube_to_position(replica, cube_dup, i); continue; } if (master->cube_version[i] == replica->cube_version[i] && tag_hash_key_cmp(cube_master->key_tag, cube_target->key_tag) == 0) { - calibrate_metrics_in_cube(cube_master, cube_target); continue; } fieldstat_cube_free(replica, i); struct fs_cube *cube_dup = fieldstat_cube_fork(cube_master); - calibrate_metrics_in_cube(cube_master, cube_dup); add_cube_to_position(replica, cube_dup, i); } @@ -1038,7 +1015,7 @@ void fieldstat_get_cubes(const struct fieldstat *instance, int **cube_ids, int * free(tmp_ids); } -int fieldstat_get_metrics_using_by_cube(const struct fieldstat *instance, int cube_id, int **metric_id_out, size_t *n_metric) +int fieldstat_get_metrics_used_by_cube(const struct fieldstat *instance, int cube_id, int **metric_id_out, size_t *n_metric) { *metric_id_out = NULL; *n_metric = 0; @@ -1092,7 +1069,7 @@ void fieldstat_get_metrics(const struct fieldstat *instance, int **metric_id_out return; } -void fieldstat_get_cells_using_by_metric(const struct fieldstat *instance, int cube_id, int metric_id, +void fieldstat_get_cells_used_by_metric(const struct fieldstat *instance, int cube_id, int metric_id, struct fieldstat_tag_list **tag_list, size_t *n_cell) { int ret = FS_OK; @@ -1335,7 +1312,7 @@ enum metric_type fieldstat_get_metric_type(const struct fieldstat *instance, int return metric_get_type(metric); } -void fieldstat_get_cells_using_by_cube(const struct fieldstat *instance, int cube_id, struct fieldstat_tag_list **tag_list, size_t *n_cell) +void fieldstat_get_cells_used_by_cube(const struct fieldstat *instance, int cube_id, struct fieldstat_tag_list **tag_list, size_t *n_cell) { if (cube_id < 0 || cube_id >= instance->valid_cube_arr_length) { return; diff --git a/test/test_register_and_reset.cpp b/test/test_register_and_reset.cpp index 1967d75..233d7e9 100644 --- a/test/test_register_and_reset.cpp +++ b/test/test_register_and_reset.cpp @@ -55,9 +55,8 @@ TEST(test_register, delete_comprehensive_cube_with_cells_and_metrics) { struct fieldstat *instance = fieldstat_new(); int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1); fieldstat_destroy_cube(instance, cube_id); fieldstat_free(instance); @@ -67,26 +66,33 @@ TEST(test_register, delete_topk_cube_with_cells_and_metrics) { struct fieldstat *instance = fieldstat_new(); int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); - int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1); fieldstat_destroy_cube(instance, cube_id); fieldstat_free(instance); } +int test_get_max_metric_id(const struct fieldstat *instance) +{ + int *metric_id_out; + size_t n_metric; + (void)fieldstat_get_metrics(instance, &metric_id_out, &n_metric); + free(metric_id_out); + return n_metric - 1; +} + TEST(test_register, reset_and_try_to_query_cell) { struct fieldstat *instance = fieldstat_new(); int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1); fieldstat_reset(instance); - EXPECT_EQ(fieldstat_get_max_metric_id(instance, cube_id), 0); + EXPECT_EQ(test_get_max_metric_id(instance), 0); long long value; - EXPECT_EQ(fieldstat_counter_get(instance, cube_id, metric_id, cell_id, &value), -1); + EXPECT_EQ(fieldstat_counter_get(instance, cube_id, metric_id, &TEST_TAG_LIST_INT, &value), FS_ERR_INVALID_TAG); fieldstat_free(instance); } @@ -102,14 +108,14 @@ TEST(test_register, register_many_cubes) EXPECT_EQ(cube_id, i); } // try to use the cube + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); for (int i = 0; i < registered_cube; i++) { - int cell_id = fieldstat_cube_add(instance, i, &TEST_TAG_INT, 1, 1); - int metric_id = fieldstat_register_counter(instance, i, "counter", COUNTER_MERGE_BY_SUM); - fieldstat_counter_incrby(instance, i, metric_id, cell_id, i); + fieldstat_counter_incrby(instance, i, metric_id, &TEST_TAG_INT, 1, i); } for (int i = 0; i < registered_cube; i++) { - long long result = my_fieldstat_counter_get(instance, i, 0, 0); + long long result; + fieldstat_counter_get(instance, i, 0, &TEST_TAG_LIST_INT, &result); EXPECT_EQ(result, i); } @@ -124,13 +130,14 @@ TEST(test_register, add_long_tagged_cells) for (int i = 0; i < 100; i++) { test_tag_long[i] = TEST_TAG_INT; } - int cell_id = fieldstat_cube_add(instance, cube_id, test_tag_long, 100, 1); - EXPECT_EQ(cell_id, 0); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 10086); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, test_tag_long, 100, 10086); - EXPECT_EQ(my_fieldstat_counter_get(instance, cube_id, metric_id, cell_id), 10086); + long long result; + struct fieldstat_tag_list tag_list = {test_tag_long, 100}; + fieldstat_counter_get(instance, cube_id, metric_id, &tag_list, &result); + EXPECT_EQ(result, 10086); fieldstat_free(instance); } @@ -141,13 +148,12 @@ TEST(test_register, register_too_many_metrics) int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); int metric_id = 0; for (int i = 0; i < 50; i++) { - metric_id = fieldstat_register_counter(instance, cube_id, (std::string("counter ") + std::to_string(i)).c_str(), COUNTER_MERGE_BY_SUM); + metric_id = fieldstat_register_counter(instance, (std::string("counter ") + std::to_string(i)).c_str(), COUNTER_MERGE_BY_SUM); EXPECT_EQ(metric_id, i); } for (int i = 0; i < 50; i++) { - int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - EXPECT_EQ(fieldstat_counter_incrby(instance, cube_id, i, cell_id, 1), 0); + EXPECT_EQ(fieldstat_counter_incrby(instance, cube_id, i, &TEST_TAG_INT, 1, 1), 0); } fieldstat_free(instance); @@ -172,10 +178,9 @@ TEST(test_register, dup_registered_info_with_cube_and_metric_without_cell) { struct fieldstat *instance = fieldstat_new(); int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - int metric_id2 = fieldstat_register_counter(instance, cube_id, "counter2", COUNTER_MERGE_BY_SUM); - int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, "counter2", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1); int cube_id_del = fieldstat_create_cube(instance, &TEST_TAG_DOUBLE_collided, 1, SAMPLING_MODE_COMPREHENSIVE, 10); int cube_id2 = fieldstat_create_cube(instance, &TEST_TAG_DOUBLE, 1, SAMPLING_MODE_COMPREHENSIVE, 10); fieldstat_destroy_cube(instance, cube_id_del); @@ -190,13 +195,12 @@ TEST(test_register, dup_registered_info_with_cube_and_metric_without_cell) EXPECT_EQ(cube_ids[1], cube_id2); free(cube_ids); - EXPECT_STREQ(fieldstat_get_metric_name(dup, cube_id, metric_id), "counter"); - EXPECT_STREQ(fieldstat_get_metric_name(dup, cube_id, metric_id2), "counter2"); + EXPECT_STREQ(fieldstat_get_metric_name(dup, metric_id), "counter"); + EXPECT_STREQ(fieldstat_get_metric_name(dup, metric_id2), "counter2"); - int *cell_ids = NULL; struct fieldstat_tag_list *tag_list = NULL; size_t n_cell = 0; - fieldstat_get_cells(dup, cube_id, metric_id, &cell_ids, &tag_list, &n_cell); + fieldstat_get_cells_used_by_metric(dup, cube_id, metric_id, &tag_list, &n_cell); EXPECT_EQ(n_cell, 0); fieldstat_free(dup); @@ -210,12 +214,10 @@ TEST(test_register, unregister_cube_on_wrong_instance) int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); int cube_id2 = fieldstat_create_cube(instance, &TEST_TAG_DOUBLE, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - int metric_id2 = fieldstat_register_counter(instance, cube_id2, "counter2", COUNTER_MERGE_BY_SUM); - int cell_id = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - int cell_id2 = fieldstat_cube_add(instance, cube_id2, &TEST_TAG_INT, 1, 1); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); - fieldstat_counter_incrby(instance, cube_id2, metric_id2, cell_id2, 1); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, "counter2", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1); + fieldstat_counter_incrby(instance, cube_id2, metric_id2, &TEST_TAG_INT, 1, 1); fieldstat_merge(instance_dst, instance); @@ -229,9 +231,11 @@ TEST(test_register, unregister_cube_on_wrong_instance) fieldstat_merge(instance_dst, instance); - long long val_deleted_once = my_fieldstat_counter_get(instance_dst, cube_id, metric_id, cell_id); + long long val_deleted_once; + fieldstat_counter_get(instance_dst, cube_id, metric_id, &TEST_TAG_LIST_INT, &val_deleted_once); EXPECT_EQ(val_deleted_once, 1); - long long val_merged_twice = my_fieldstat_counter_get(instance_dst, cube_id2, metric_id2, cell_id2); + long long val_merged_twice; + fieldstat_counter_get(instance_dst, cube_id2, metric_id2, &TEST_TAG_LIST_INT, &val_merged_twice); EXPECT_EQ(val_merged_twice, 2); fieldstat_free(instance); @@ -243,18 +247,19 @@ TEST(test_register, register_many_cells_on_unlimited_sized_cube) { struct fieldstat *instance = fieldstat_new(); int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 0); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - int cell_id = 0; + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat_tag test_tag = {"abc", TAG_INTEGER, {.value_longlong = 0}}; for (int i = 0; i < 10000; i++) { test_tag.value_longlong = i; - cell_id = fieldstat_cube_add(instance, cube_id, &test_tag, 1, 1); - EXPECT_EQ(cell_id, i); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, &test_tag, 1, 1); } for (int i = 0; i < 10000; i++) { - EXPECT_EQ(my_fieldstat_counter_get(instance, cube_id, metric_id, i), 1); + test_tag.value_longlong = i; + struct fieldstat_tag_list tag_list = {&test_tag, 1}; + long long value; + fieldstat_counter_get(instance, cube_id, metric_id, &tag_list, &value); + EXPECT_EQ(value, 1); } fieldstat_free(instance); @@ -264,53 +269,30 @@ TEST(test_register, unregister_cell) { struct fieldstat *instance = fieldstat_new(); int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - int metric_id = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - int cell_id1 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1); - int cell_id2 = fieldstat_cube_add(instance, cube_id, &TEST_TAG_DOUBLE, 1, 1); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id1, 1); - fieldstat_counter_incrby(instance, cube_id, metric_id, cell_id2, 1); - - int cell_id_removed = fieldstat_cube_remove(instance, cube_id, &TEST_TAG_INT, 1); - EXPECT_EQ(cell_id_removed, cell_id1); + int metric_id = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_INT, 1, 1); + fieldstat_counter_incrby(instance, cube_id, metric_id, &TEST_TAG_DOUBLE, 1, 1); + + fieldstat_cube_remove(instance, cube_id, &TEST_TAG_INT, 1); long long value; - EXPECT_EQ(fieldstat_counter_get(instance, cube_id, metric_id, cell_id1, &value), -1); + EXPECT_EQ(fieldstat_counter_get(instance, cube_id, metric_id, &TEST_TAG_LIST_INT, &value), FS_ERR_INVALID_TAG); - int *cell_ids = NULL; struct fieldstat_tag_list *tag_list = NULL; size_t n_cell = 0; - fieldstat_get_cells_from_cube(instance, cube_id, &cell_ids, &tag_list, &n_cell); + fieldstat_get_cells_used_by_cube(instance, cube_id, &tag_list, &n_cell); EXPECT_EQ(n_cell, 1); - EXPECT_EQ(cell_ids[0], cell_id2); EXPECT_EQ(tag_list[0].tag->type, TEST_TAG_DOUBLE.type); EXPECT_STREQ(tag_list[0].tag->key, TEST_TAG_DOUBLE.key); - free(cell_ids); fieldstat_tag_list_arr_free(tag_list, n_cell); // test merge and serialize to make sure no changes on basic functions struct fieldstat *instance_dst = fieldstat_new(); fieldstat_merge(instance_dst, instance); - fieldstat_get_cells_from_cube(instance, cube_id, &cell_ids, &tag_list, &n_cell); + fieldstat_get_cells_used_by_cube(instance, cube_id, &tag_list, &n_cell); EXPECT_EQ(n_cell, 1); EXPECT_STREQ(tag_list[0].tag->key, TEST_TAG_DOUBLE.key); fieldstat_free(instance_dst); - free(cell_ids); fieldstat_tag_list_arr_free(tag_list, n_cell); - - char *blob; - size_t blob_size; - fieldstat_serialize(instance, &blob, &blob_size); - struct fieldstat *instance_ser = fieldstat_deserialize(blob, blob_size); - free(blob); - fieldstat_get_cells_from_cube(instance_ser, cube_id, &cell_ids, &tag_list, &n_cell); - EXPECT_EQ(n_cell, 1); - EXPECT_STREQ(tag_list[0].tag->key, TEST_TAG_DOUBLE.key); - fieldstat_free(instance_ser); - free(cell_ids); - fieldstat_tag_list_arr_free(tag_list, n_cell); - - // completely new tag even with original tag. New posision(not original 0) - EXPECT_EQ(fieldstat_cube_add(instance, cube_id, &TEST_TAG_INT, 1, 1), 2); - fieldstat_free(instance); } @@ -336,9 +318,8 @@ TEST(test_register, find_cube) { TEST(test_register, register_metric_twice) { struct fieldstat *instance = fieldstat_new(); - int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); - fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); - int metric_id2 = fieldstat_register_counter(instance, cube_id, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); + int metric_id2 = fieldstat_register_counter(instance, "counter", COUNTER_MERGE_BY_SUM); EXPECT_EQ(metric_id2, FS_ERR_INVALID_KEY); fieldstat_free(instance); } @@ -347,14 +328,14 @@ TEST(calibrate, target_one_more_metric) { struct fieldstat *master = fieldstat_new(); int cube_id = fieldstat_create_cube(master, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(master, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat *target = fieldstat_fork(master); - EXPECT_EQ(fieldstat_register_counter(target, cube_id, "counter2", COUNTER_MERGE_BY_SUM), 1); + EXPECT_EQ(fieldstat_register_counter(target, "counter2", COUNTER_MERGE_BY_SUM), 1); fieldstat_calibrate(master, target); - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id), 0); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 0), "counter"); + EXPECT_EQ(test_get_max_metric_id(target), 0); + EXPECT_STREQ(fieldstat_get_metric_name(target, 0), "counter"); struct fieldstat_tag_list *tag_list = fieldstat_get_shared_tags(target, cube_id); EXPECT_STREQ(tag_list->tag[0].key, TEST_SHARED_TAG.key); @@ -367,15 +348,15 @@ TEST(calibrate, master_one_more_metric) { struct fieldstat *master = fieldstat_new(); int cube_id = fieldstat_create_cube(master, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(master, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat *target = fieldstat_fork(master); - EXPECT_EQ(fieldstat_register_counter(master, cube_id, "counter2", COUNTER_MERGE_BY_SUM), 1); + EXPECT_EQ(fieldstat_register_counter(master, "counter2", COUNTER_MERGE_BY_SUM), 1); fieldstat_calibrate(master, target); - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id), 1); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 0), "counter"); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 1), "counter2"); + EXPECT_EQ(test_get_max_metric_id(target), 1); + EXPECT_STREQ(fieldstat_get_metric_name(target, 0), "counter"); + EXPECT_STREQ(fieldstat_get_metric_name(target, 1), "counter2"); struct fieldstat_tag_list *tag_list = fieldstat_get_shared_tags(target, cube_id); EXPECT_STREQ(tag_list->tag[0].key, TEST_SHARED_TAG.key); @@ -389,16 +370,16 @@ TEST(calibrate, different_metric) { struct fieldstat *master = fieldstat_new(); int cube_id = fieldstat_create_cube(master, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(master, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat *target = fieldstat_fork(master); - EXPECT_EQ(fieldstat_register_counter(target, cube_id, "counter2", COUNTER_MERGE_BY_SUM), 1); - EXPECT_EQ(fieldstat_register_counter(master, cube_id, "hi i am master new", COUNTER_MERGE_BY_SUM), 1); + EXPECT_EQ(fieldstat_register_counter(target, "counter2", COUNTER_MERGE_BY_SUM), 1); + EXPECT_EQ(fieldstat_register_counter(master, "hi i am master new", COUNTER_MERGE_BY_SUM), 1); fieldstat_calibrate(master, target); - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id), 1); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 0), "counter"); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 1), "hi i am master new"); + EXPECT_EQ(test_get_max_metric_id(target), 1); + EXPECT_STREQ(fieldstat_get_metric_name(target, 0), "counter"); + EXPECT_STREQ(fieldstat_get_metric_name(target, 1), "hi i am master new"); struct fieldstat_tag_list *tag_list = fieldstat_get_shared_tags(target, cube_id); EXPECT_STREQ(tag_list->tag[0].key, TEST_SHARED_TAG.key); @@ -411,11 +392,11 @@ TEST(calibrate, target_more_cube) { struct fieldstat *master = fieldstat_new(); int cube_id = fieldstat_create_cube(master, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(master, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat *target = fieldstat_fork(master); int cube_id2 = fieldstat_create_cube(target, &TEST_TAG_STRING, 1, SAMPLING_MODE_COMPREHENSIVE, 10); EXPECT_EQ(cube_id2, 1); - fieldstat_register_counter(target, cube_id2, "counter2", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(target, "counter2", COUNTER_MERGE_BY_SUM); fieldstat_calibrate(master, target); @@ -426,8 +407,8 @@ TEST(calibrate, target_more_cube) EXPECT_EQ(cube_ids[0], cube_id); free(cube_ids); - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id), 0); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 0), "counter"); + EXPECT_EQ(test_get_max_metric_id(target), 0); + EXPECT_STREQ(fieldstat_get_metric_name(target, 0), "counter"); struct fieldstat_tag_list *tag_list = fieldstat_get_shared_tags(target, cube_id); EXPECT_STREQ(tag_list->tag[0].key, TEST_SHARED_TAG.key); @@ -442,10 +423,8 @@ TEST(calibrate, master_more_cube) { struct fieldstat *master = fieldstat_new(); int cube_id = fieldstat_create_cube(master, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat *target = fieldstat_fork(master); int cube_id2 = fieldstat_create_cube(master, &TEST_TAG_STRING, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id2, "counter2", COUNTER_MERGE_BY_SUM); fieldstat_calibrate(master, target); @@ -457,13 +436,8 @@ TEST(calibrate, master_more_cube) EXPECT_EQ(cube_ids[1], cube_id2); free(cube_ids); - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id), 0); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 0), "counter"); struct fieldstat_tag_list *tag_list = fieldstat_get_shared_tags(target, cube_id); EXPECT_STREQ(tag_list->tag[0].key, TEST_SHARED_TAG.key); - - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id2), 0); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id2, 0), "counter2"); struct fieldstat_tag_list *tag_list2 = fieldstat_get_shared_tags(target, cube_id2); EXPECT_STREQ(tag_list2->tag[0].key, TEST_TAG_STRING.key); @@ -480,16 +454,13 @@ TEST(calibrate, master_change_cube) { struct fieldstat *master = fieldstat_new(); int cube_id = fieldstat_create_cube(master, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter", COUNTER_MERGE_BY_SUM); + fieldstat_register_counter(master, "counter", COUNTER_MERGE_BY_SUM); struct fieldstat *target = fieldstat_fork(master); fieldstat_destroy_cube(master, cube_id); fieldstat_create_cube(master, &TEST_TAG_STRING, 1, SAMPLING_MODE_COMPREHENSIVE, 10); - fieldstat_register_counter(master, cube_id, "counter2", COUNTER_MERGE_BY_SUM); fieldstat_calibrate(master, target); - EXPECT_EQ(fieldstat_get_max_metric_id(target, cube_id), 0); - EXPECT_STREQ(fieldstat_get_metric_name(target, cube_id, 0), "counter2"); struct fieldstat_tag_list *tag_list = fieldstat_get_shared_tags(target, cube_id); EXPECT_STREQ(tag_list->tag[0].key, TEST_TAG_STRING.key); diff --git a/test/utils.hpp b/test/utils.hpp index 5734bf6..92d6705 100644 --- a/test/utils.hpp +++ b/test/utils.hpp @@ -6,6 +6,7 @@ const struct fieldstat_tag TEST_TAG_STRING = {"STRING KEY_", TAG_CSTRING, {.value_str = "100.1"}}; const struct fieldstat_tag TEST_TAG_STRING_collided = {"collided", TAG_CSTRING, {.value_str = "2"}}; const struct fieldstat_tag TEST_TAG_INT = {"INT key_", TAG_INTEGER, {.value_longlong = 100}}; +const struct fieldstat_tag_list TEST_TAG_LIST_INT = {(struct fieldstat_tag *)&TEST_TAG_INT, 1}; const struct fieldstat_tag TEST_TAG_INT_collided = {"collided", TAG_INTEGER, {.value_longlong = 2}}; const struct fieldstat_tag TEST_TAG_DOUBLE = {"DOUBLE key_", TAG_DOUBLE, {.value_double = 100.1}}; const struct fieldstat_tag TEST_TAG_DOUBLE_collided = {"collided", TAG_DOUBLE, {.value_double = 2.0}}; |
