diff options
| author | chenzizhan <[email protected]> | 2024-07-03 18:16:57 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2024-07-03 18:16:57 +0800 |
| commit | 79ccdac89a7871c82946d9a784e479edd73c473d (patch) | |
| tree | 7841a79a46162ff87d09c7c0bb49e5f365f918e6 /src | |
| parent | 105fece68917e092f4c05131ca947bd5b3aec032 (diff) | |
remove cell_manager.c; mv cube manager to fieldstat
Diffstat (limited to 'src')
| -rw-r--r-- | src/cube.c | 2 | ||||
| -rw-r--r-- | src/fieldstat.c | 168 | ||||
| -rw-r--r-- | src/metrics/metric.c | 2 | ||||
| -rw-r--r-- | src/metrics/metric.h | 2 | ||||
| -rw-r--r-- | src/tags/cell_manager.c | 99 | ||||
| -rw-r--r-- | src/tags/cell_manager.h | 22 | ||||
| -rw-r--r-- | src/tags/my_ut_hash.c | 2 | ||||
| -rw-r--r-- | src/tags/readme.md | 90 |
8 files changed, 142 insertions, 245 deletions
@@ -680,7 +680,7 @@ int cube_get_serialization(const struct cube *cube, int metric_id, const struct return FS_ERR_INVALID_METRIC_ID; } - metric_get_plain_blob(metric, blob, blob_size); + metric_serialize(metric, blob, blob_size); return FS_OK; } diff --git a/src/fieldstat.c b/src/fieldstat.c index 71a1e0a..7a8a16e 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -7,10 +7,9 @@ #include "cjson/cJSON.h" #include "uthash.h" -#include "serializer.h" + #include "fieldstat.h" #include "metrics/metric.h" -#include "cell_manager.h" #include "my_ut_hash.h" #include "cube.h" #include "metric_manifest.h" @@ -19,6 +18,18 @@ #define DEFAULT_N_CUBE 64 +struct cube_manager_item { + char *key; + size_t key_len; + int cell_id; + UT_hash_handle hh; +}; + +struct cube_manager { + struct cube_manager_item *head; +}; + + struct fieldstat { struct cube **cube; unsigned long *cube_version; // increase from 0 every time the cube is deleted @@ -31,6 +42,89 @@ struct fieldstat { struct cube_manager *shared_tag_cube_manager; }; +void cube_manager_free(struct cube_manager *pthis) +{ + struct cube_manager_item *node = NULL; + struct cube_manager_item *tmp = NULL; + struct cube_manager_item *head = pthis->head; + HASH_ITER(hh, head, node, tmp) { + HASH_DEL(head, node); + free(node->key); + free(node); + } + free(pthis); +} + +struct cube_manager *cube_manager_new() +{ + struct cube_manager *pthis = (struct cube_manager *)malloc(sizeof(struct cube_manager)); + pthis->head = NULL; + return pthis; +} + +static char *key_dup(const char *key, size_t key_len) +{ + char *new_key = (char *)malloc(key_len); + memcpy(new_key, key, key_len); + return new_key; +} + +void cube_manager_add(struct cube_manager *pthis, const char *key, size_t key_len, int id) +{ + struct cube_manager_item *node = (struct cube_manager_item *)malloc(sizeof(struct cube_manager_item)); + node->key = key_dup(key, key_len); + node->key_len = key_len; + node->cell_id = id; + HASH_ADD_KEYPTR(hh, pthis->head, node->key, key_len, node); +} + +void cube_manager_delete(struct cube_manager *pthis, const char *key, size_t key_len) +{ + struct cube_manager_item *node = NULL; + HASH_FIND(hh, pthis->head, key, key_len, node); + if (node != NULL) { + HASH_DEL(pthis->head, node); + free(node->key); + free(node); + } +} + +int cube_manager_find(const struct cube_manager *pthis, const char *key, size_t key_len) +{ + struct cube_manager_item *node = NULL; + HASH_FIND(hh, pthis->head, key, key_len, node); + if (node == NULL) { + return -1; + } else { + return node->cell_id; + } +} + +void cube_manager_calibrate(struct cube_manager *pthis, const struct cube_manager *master) +{ + struct cube_manager_item *node = NULL; + struct cube_manager_item *tmp = NULL; + + // exist in self but not in master + HASH_ITER(hh, pthis->head, node, tmp) { + int cube_id = cube_manager_find(master, node->key, node->key_len); + if (cube_id == -1) { + HASH_DEL(pthis->head, node); + free(node->key); + free(node); + } else { + node->cell_id = cube_id; + } + } + + // exist in master but not in self + HASH_ITER(hh, master->head, node, tmp) { + int cube_id = cube_manager_find(pthis, node->key, node->key_len); + if (cube_id == -1) { + cube_manager_add(pthis, node->key, node->key_len, node->cell_id); + } + } +} union metric_parameter *construct_parameters(enum metric_type type, ...) { @@ -212,6 +306,13 @@ unsigned long fieldstat_get_version(const struct fieldstat *instance) return instance->cell_version; } +void get_cube_key(const struct cube *cube, char **key, size_t *key_len) +{ + struct fieldstat_tag_list *shared_tag = cube_get_identifier(cube); + build_dynamic_cell_key(shared_tag->tag, shared_tag->n_tag, key, key_len); + fieldstat_tag_list_arr_free(shared_tag, 1); +} + int fieldstat_destroy_cube(struct fieldstat *instance, int cube_id) { if (instance == NULL) { @@ -225,17 +326,17 @@ int fieldstat_destroy_cube(struct fieldstat *instance, int cube_id) } const struct cube *cube = instance->cube[cube_id]; - struct fieldstat_tag_list *shared_tag = cube_get_identifier(cube); - struct tag_hash_key shared_tag_key; - tag_hash_key_init_with_fieldstat_tag(&shared_tag_key, shared_tag->tag, shared_tag->n_tag, false); - cube_manager_delete(instance->shared_tag_cube_manager, &shared_tag_key); + char *shared_tag_key = NULL; + size_t shared_tag_key_len = 0; + get_cube_key(cube, &shared_tag_key, &shared_tag_key_len); + cube_manager_delete(instance->shared_tag_cube_manager, shared_tag_key, shared_tag_key_len); fieldstat_cube_free(instance, cube_id); instance->cube[cube_id] = NULL; instance->cube_version[cube_id]++; - fieldstat_tag_list_arr_free(shared_tag, 1); + free(shared_tag_key); return 0; } @@ -320,17 +421,21 @@ int fieldstat_create_cube(struct fieldstat *instance, const struct fieldstat_tag if (max_n_cell == 0) { max_n_cell = INT32_MAX; } - struct tag_hash_key shared_tag_key; - tag_hash_key_init_with_fieldstat_tag(&shared_tag_key, shared_tags, n_tag, false); - int ret = cube_manager_find(instance->shared_tag_cube_manager, &shared_tag_key); + + char *shared_tag_key = NULL; + size_t shared_tag_key_len = 0; + build_dynamic_cell_key(shared_tags, n_tag, &shared_tag_key, &shared_tag_key_len); + int ret = cube_manager_find(instance->shared_tag_cube_manager, shared_tag_key, shared_tag_key_len); if (ret != -1) { + free(shared_tag_key); return FS_ERR_INVALID_KEY; } struct cube *cube = cube_new(shared_tags, n_tag, mode, max_n_cell); int cube_id = fieldstat_append_cube_to_instance(instance, cube); - cube_manager_add(instance->shared_tag_cube_manager, &shared_tag_key, cube_id); + cube_manager_add(instance->shared_tag_cube_manager, shared_tag_key, shared_tag_key_len, cube_id); + free(shared_tag_key); return cube_id; } @@ -342,15 +447,14 @@ void fieldstat_cube_free(struct fieldstat *instance, int cube_id) return; } - struct fieldstat_tag_list *shared_tag = cube_get_identifier(cube); - struct tag_hash_key shared_tag_key; - tag_hash_key_init_with_fieldstat_tag(&shared_tag_key, shared_tag->tag, shared_tag->n_tag, false); - cube_manager_delete(instance->shared_tag_cube_manager, &shared_tag_key); + char *shared_tag_key = NULL; + size_t shared_tag_key_len = 0; + get_cube_key(cube, &shared_tag_key, &shared_tag_key_len); + cube_manager_delete(instance->shared_tag_cube_manager, shared_tag_key, shared_tag_key_len); + free(shared_tag_key); cube_free(cube); instance->cube[cube_id] = NULL; - - fieldstat_tag_list_arr_free(shared_tag, 1); } int fieldstat_cube_set_primary_metric(struct fieldstat *instance, int cube_id, int metric_id) @@ -611,20 +715,21 @@ int fieldstat_merge(struct fieldstat *instance, struct fieldstat *src) continue; } - // const struct tag_hash_key *shared_tag_key_src = cube_src->key_tag; - struct tag_hash_key shared_tag_key_src; struct fieldstat_tag_list *shared_tag_list = cube_get_identifier(cube_src); - tag_hash_key_init_with_fieldstat_tag(&shared_tag_key_src, shared_tag_list->tag, shared_tag_list->n_tag, false); - int cube_id_dest = cube_manager_find(tag_cube_id_map, &shared_tag_key_src); + char *shared_tag_key_src = NULL; + size_t shared_tag_key_len = 0; + build_dynamic_cell_key(shared_tag_list->tag, shared_tag_list->n_tag, &shared_tag_key_src, &shared_tag_key_len); + int cube_id_dest = cube_manager_find(tag_cube_id_map, shared_tag_key_src, shared_tag_key_len); if (cube_id_dest == -1) { struct cube *copied_cube = cube_copy(cube_src); cube_id_dest = fieldstat_append_cube_to_instance(instance, copied_cube); - cube_manager_add(tag_cube_id_map, &shared_tag_key_src, cube_id_dest); + cube_manager_add(tag_cube_id_map, shared_tag_key_src, shared_tag_key_len, cube_id_dest); } else { struct cube *cube_dst = instance->cube[cube_id_dest]; cube_merge(cube_dst, cube_src); } fieldstat_tag_list_arr_free(shared_tag_list, 1); + free(shared_tag_key_src); } return ret; @@ -646,11 +751,12 @@ struct fieldstat *fieldstat_fork(const struct fieldstat *instance) continue; } new_instance->cube[i] = cube_fork(cube); - struct fieldstat_tag_list *shared_tag = cube_get_identifier(cube); - struct tag_hash_key shared_tag_key; - tag_hash_key_init_with_fieldstat_tag(&shared_tag_key, shared_tag->tag, shared_tag->n_tag, false); - cube_manager_add(new_instance->shared_tag_cube_manager, &shared_tag_key, i); - fieldstat_tag_list_arr_free(shared_tag, 1); + + char *shared_tag_key = NULL; + size_t shared_tag_key_len = 0; + get_cube_key(cube, &shared_tag_key, &shared_tag_key_len); + cube_manager_add(new_instance->shared_tag_cube_manager, shared_tag_key, shared_tag_key_len, i); + free(shared_tag_key); } new_instance->cube_version = calloc(new_instance->max_n_cube, sizeof(unsigned long)); memcpy(new_instance->cube_version, instance->cube_version, sizeof(unsigned long) * new_instance->max_n_cube); @@ -971,9 +1077,11 @@ int fieldstat_find_cube(const struct fieldstat *instance, const struct fieldstat return FS_ERR_NULL_HANDLER; } const struct cube_manager *tag_cube_id_map = instance->shared_tag_cube_manager; - struct tag_hash_key shared_tag_key; - tag_hash_key_init_with_fieldstat_tag(&shared_tag_key, shared_tags, n_shared_tags, false); - int cube_id = cube_manager_find(tag_cube_id_map, &shared_tag_key); + char *shared_tag_key = NULL; + size_t shared_tag_key_len = 0; + build_dynamic_cell_key(shared_tags, n_shared_tags, &shared_tag_key, &shared_tag_key_len); + int cube_id = cube_manager_find(tag_cube_id_map, shared_tag_key, shared_tag_key_len); + free(shared_tag_key); if (cube_id == -1) { return FS_ERR_INVALID_KEY; } diff --git a/src/metrics/metric.c b/src/metrics/metric.c index aea96a1..d66444e 100644 --- a/src/metrics/metric.c +++ b/src/metrics/metric.c @@ -293,7 +293,7 @@ int metric_merge(struct metric *dest, const struct metric *src) { return g_metric_scheme_table[dest->type].merge(dest->data, src->data); } -void metric_get_plain_blob(const struct metric *pthis, char **blob, size_t *blob_size) { +void metric_serialize(const struct metric *pthis, char **blob, size_t *blob_size) { struct metric_data *data = pthis->data; enum metric_type type = pthis->type; if (type == METRIC_TYPE_HLL) { diff --git a/src/metrics/metric.h b/src/metrics/metric.h index 822735b..81629a3 100644 --- a/src/metrics/metric.h +++ b/src/metrics/metric.h @@ -12,7 +12,7 @@ void metric_reset(struct metric *pthis); struct metric *metric_copy(const struct metric *src); struct metric *metric_new(const struct metric_manifest *manifest); int metric_merge(struct metric *dest, const struct metric *src); -void metric_get_plain_blob(const struct metric *pthis, char **blob, size_t *blob_size); +void metric_serialize(const struct metric *pthis, char **blob, size_t *blob_size); void metric_counter_incrby(struct metric *pthis, long long value); void metric_counter_set(struct metric *pthis, long long value); diff --git a/src/tags/cell_manager.c b/src/tags/cell_manager.c deleted file mode 100644 index 0d39ccc..0000000 --- a/src/tags/cell_manager.c +++ /dev/null @@ -1,99 +0,0 @@ -#include <stdio.h> -#include <assert.h> -#include <stdlib.h> -#include <string.h> - -#include "fieldstat.h" -#include "cell_manager.h" -#include "my_ut_hash_inner.h" -#include "my_ut_hash.h" - -struct tag_id_map { - struct tag_hash_key *tag; - int cell_id; - bool dying; - UT_hash_handle hh; -}; - -struct cube_manager { - struct tag_id_map *head; -}; - -void tag_id_map_free(struct tag_id_map *head) -{ - struct tag_id_map *node = NULL; - struct tag_id_map *tmp = NULL; - HASH_ITER(hh, head, node, tmp) { - HASH_DEL(head, node); - tag_hash_key_free(node->tag); - free(node); - } -} - -void cube_manager_free(struct cube_manager *pthis) -{ - tag_id_map_free(pthis->head); - free(pthis); -} - -struct cube_manager *cube_manager_new() -{ - struct cube_manager *pthis = (struct cube_manager *)malloc(sizeof(struct cube_manager)); - pthis->head = NULL; - return pthis; -} - -void cube_manager_add(struct cube_manager *pthis, const struct tag_hash_key *tag, int id) -{ - struct tag_id_map *node = (struct tag_id_map *)malloc(sizeof(struct tag_id_map)); - node->tag = tag_hash_key_copy(tag); - node->cell_id = id; - node->dying = false; // this is not used in cube manager, just give it a value - HASH_ADD_TAG(pthis->head, tag, node); -} - -void cube_manager_delete(struct cube_manager *pthis, const struct tag_hash_key *tag) -{ - struct tag_id_map *node = NULL; - HASH_FIND_TAG(pthis->head, tag, node); - if (node != NULL) { - HASH_DEL(pthis->head, node); - tag_hash_key_free(node->tag); - free(node); - } -} - -int cube_manager_find(const struct cube_manager *pthis, const struct tag_hash_key *tag) -{ - struct tag_id_map *node = NULL; - HASH_FIND_TAG(pthis->head, tag, node); - if (node == NULL) { - return -1; - } else { - return node->cell_id; - } -} - -void cube_manager_calibrate(struct cube_manager *pthis, const struct cube_manager *master) -{ - struct tag_id_map *node = NULL; - struct tag_id_map *tmp = NULL; - HASH_ITER(hh, pthis->head, node, tmp) { - int cube_id = cube_manager_find(master, node->tag); - if (cube_id == -1) { - HASH_DEL(pthis->head, node); - tag_hash_key_free(node->tag); - free(node); - } else { - node->cell_id = cube_id; - } - } - - // exist in master but not in self - HASH_ITER(hh, master->head, node, tmp) { - int cube_id = cube_manager_find(pthis, node->tag); - if (cube_id == -1) { - cube_manager_add(pthis, node->tag, node->cell_id); - } - } -}
\ No newline at end of file diff --git a/src/tags/cell_manager.h b/src/tags/cell_manager.h deleted file mode 100644 index 3bdcbdf..0000000 --- a/src/tags/cell_manager.h +++ /dev/null @@ -1,22 +0,0 @@ - -#pragma once -#include "my_ut_hash.h" - -#ifdef __cplusplus -extern "C"{ -#endif - - -/* ------- a simpler cell_manager, which only support check if tag exist and add. ------- */ -struct cube_manager; - -struct cube_manager *cube_manager_new(); -void cube_manager_free(struct cube_manager *pthis); -void cube_manager_add(struct cube_manager *pthis, const struct tag_hash_key *tag, int id); -int cube_manager_find(const struct cube_manager *pthis, const struct tag_hash_key *tag); -void cube_manager_delete(struct cube_manager *pthis, const struct tag_hash_key *tag); -void cube_manager_calibrate(struct cube_manager *pthis, const struct cube_manager *master); - -#ifdef __cplusplus -} -#endif
\ No newline at end of file diff --git a/src/tags/my_ut_hash.c b/src/tags/my_ut_hash.c index 0da2a66..acc6ef7 100644 --- a/src/tags/my_ut_hash.c +++ b/src/tags/my_ut_hash.c @@ -489,7 +489,7 @@ void tag_hash_key_print(const struct tag_hash_key *tag) printf("n_my_tag: %zu\n", tag->n_my_tag); for (int i = 0; i < (int)tag->n_my_tag; i++) { - struct fieldstat_tag *tmp_tag = &tag->tags[i]; + const struct fieldstat_tag *tmp_tag = &tag->tags[i]; switch (tmp_tag->type) { case TAG_INTEGER: diff --git a/src/tags/readme.md b/src/tags/readme.md deleted file mode 100644 index af6e5e1..0000000 --- a/src/tags/readme.md +++ /dev/null @@ -1,90 +0,0 @@ -# 要做的 - -## exdata -typedef void *exdata_new_cb(void *arg); - -typedef void exdata_free(void *exdata); - -typedef void exdata_reset(void *exdata); -typedef void exdata_merge(void *exdata_dst, exdata_src); - -typedef void exdata_serialize(void *exdata, &blob, &blob_sz ); - -int hevay_keeper_set_exdata_schema(instance, new_callback, free_callback, reset_callback); - -int heavy_keeper_add(instance, key, key_len, count, void *arg); - -void *heavy_keeper_get0_exdata(instance, key, key_len); // 只是获得这个指针 - -void heavy_keeper_soft_reset(instance); - -heavy_keeper_get_count() // 获得一共有几个key - -heavy_keeper_list(instance, **key, *key_len, **exdata); - -typedef foreach_callback(const char * key, size_t keylen, void * exdata, void *arg); - -heavy_keeper_iterate(instance, foreach_callback, void *arg); - - -## add 逻辑 -1. 调用heavy keeper add。这里有几种情况,一个是存在dying 记录,其实就是count = 0的记录,sorted set 其实感知dying。肯定要感知,不然exdata reset 掉存哪。 -2. 尝试使用heavy_keeper_get0_exdata ,获得一个cell:struct fs_cell{long long metrics[MAX];int primay_metric_idx;} -3. 对metric 进行操作。 - -## merge 逻辑 -1. 直接cube对齐以后,直接拿出来里面的cell manager 数据,做merge。这里会有一个根据Sampling mode 的Switch。 -2. 在内部,不同的有不同策略,比如sorted set,得到两个cell 对应以后,对于内部的ex data,调用merge。 - -## reset 逻辑 -- dying cell 的处理 - - sorted set 在reset 时总是不直接删除dying cell,这部分流程不修改,单纯给它们所有count = 0 - - 所有相关的exdata 调用reset。 - -## 输出逻辑 -1. 输出逻辑其实就是查询逻辑,因为exporter 直接调用fieldstat 相关的查询接口。 -2. 涉及到的查询接口,主要是得到cube 里的所有tag,和根据tag 查询metric 的数据。通过heavy_keeper_get0_exdata 和 heavy_keeper_list 实现。另一个方案(修改比较多的),是把写json 输出的过程变成call back,然后每个cell 的数据实际上可以直接输出(包括tag 和 field)。 - -# 数据结构 -fieldstat -> cube -> cell -> exdata(metric) - -注意,fieldstat 知道exdata 的具体用法,但exdata 的所有权归cell, 这里,所有权和使用分离了。 - -cell 被heavy keeper,comprehensive 等等东西管理,传入的是单一的key,实际上就是tag。不过现在cell 层不感知tag。 - -metric 过去包含了一个cell (称为metric_measure_data) 的array,现在单纯就是counter、hll 或者histogram的包装。 - -# todo -- cell_manager get count by tag 已删除,这个和cell manager 职责不符,只用来测试。 - - 处理相关unit test(实际上可以去掉了) -- merge 是个大块儿 - - heavy_keeper_get_true_pst_tag 删除 - - 关于metric 的操作全部移动到topk 等 -- 目前先用tag_hash_key ,减少修改点,后面换成 char * -- sorted_set_get_cell_id 这个函数被大量使用,但是现在所有操作都直接定位entry 了,思考怎么替换他 -- 当添加的不是primary metric,fieldstat 直接查询heavy keeper,如果不存在,才调用add。 -- 如果闲得无聊,研究一下merge 时候的metric 复用(而不是copy)问题,现在就先copy 吧。 -- cube manager, 现在肯定还要继续用,得修改一下,把tag_hash_key 淘汰掉,还要换个地方。 - - -## further -在火焰图上,明显能观察到get cell id的调用个数太多,说明是太多dummy 操作了。可能跟贺岚风商量一下,用batch incrby。 - -## metric -metric - 的变化也是一套,现在metric 没有正常reset,而是每次reset 都删,挺恶心的。关键是,exdata 里面放着什么metric 其实是fieldstat 定的,所以具体reset 多少个counter 之类是fieldstat 操作决定的。所以,我单纯用传入的reset 函数,简简单单处理一下,具体真的add 是时候发现,某个metric 已经不用了(这个事现在感知不到,也就认为不可能),也有办法解决。反正instance 的metric 是只能register 不能unregister 的。metric 始终增加就始终增加吧。 - -还有一个问题,merge 的时候总不能一直创建新的metric_id_map,所以只能是让exdata 获得一个fieldstat metric id map 的指针。注意一下,exdata_new 的arg 里有这个指针。 - -# 查询相关 -- fieldstat_get_cells_used_by_metric -目前应该是get metric in cell 了。这个函数只在测试中使用,用get metric in cell 替换掉吧 - -- fieldstat_get_metrics_used_by_cube - 在测试中少量使用。另外主要是exporter 里。每进入一个新的cube,就调一遍。每个step,到新cell 时,就对每个metric 都查找数据。 - 当前,得到一个metric 的方法是:list exdata,然后其实就知道metric 都是什么了。但是注意,当前必须指定cell 才能得到metric。所以这个接口也不能用了。但是是好事,实际上,现在基于exdata 可以更简单的得到一个cell(也就是exporter 里的一次tags{} fields{} 输出)。 - -## 设计接口 -fieldstat_get_cell_used_by_cube() // 已经有这个接口了 -fieldstat_get_metric_in_cell() // 返回一系列metric id。 - |
