From 67030d247ad9d97ce708ea980518beeca643e97e Mon Sep 17 00:00:00 2001 From: chenzizhan Date: Tue, 23 Jul 2024 18:03:57 +0800 Subject: fix: should not export reset metric --- src/cube.c | 28 +++++++++++++++++----------- src/metrics/metric.c | 20 ++++++++++++++++++++ src/metrics/metric.h | 1 + 3 files changed, 38 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/cube.c b/src/cube.c index bedf697..c482b6e 100644 --- a/src/cube.c +++ b/src/cube.c @@ -1289,22 +1289,28 @@ const struct cell *get_cell_by_dimension(const struct cube *cube, const struct f return ret; } -const struct metric *cube_find_metric_in_cell(const struct cube *cube, const struct field_list *fields, int metric_id,int *ret) +const struct metric *cube_find_uncleared_metric_in_cell(const struct cube *cube, const struct field_list *fields, int metric_id,int *ret_code) { const struct cell *data = get_cell_by_dimension(cube, fields); if (data == NULL) { - *ret = FS_ERR_INVALID_DIMENSION; + *ret_code = FS_ERR_INVALID_DIMENSION; return NULL; } if (metric_id < 0 || metric_id >= data->next_index) { - *ret = FS_ERR_INVALID_METRIC_ID; + *ret_code = FS_ERR_INVALID_METRIC_ID; return NULL; } - *ret = FS_OK; + *ret_code = FS_OK; - return data->slots[metric_id]; + const struct metric *ret_metric = data->slots[metric_id]; + if (ret_metric == NULL || metric_check_if_cleared(ret_metric)) { + *ret_code = FS_ERR_INVALID_METRIC_ID; + return NULL; + } + + return ret_metric; } int cube_counter_get(const struct cube *cube, int metric_id, const struct field_list *fields, long long *value) @@ -1324,7 +1330,7 @@ int cube_counter_get(const struct cube *cube, int metric_id, const struct field_ } int ret; - const struct metric *metric = cube_find_metric_in_cell(cube, fields, metric_id, &ret); + const struct metric *metric = cube_find_uncleared_metric_in_cell(cube, fields, metric_id, &ret); if (ret != FS_OK) { return ret; } @@ -1354,7 +1360,7 @@ int cube_hll_get(const struct cube *cube, int metric_id, const struct field_list } int ret; - const struct metric *metric = cube_find_metric_in_cell(cube, fields, metric_id, &ret); + const struct metric *metric = cube_find_uncleared_metric_in_cell(cube, fields, metric_id, &ret); if (ret != FS_OK) { return ret; } @@ -1369,7 +1375,7 @@ int cube_hll_get(const struct cube *cube, int metric_id, const struct field_list int cube_histogram_value_at_percentile(const struct cube *cube, int metric_id, const struct field_list *fields, double percentile, long long *value) { int ret; - const struct metric *metric = cube_find_metric_in_cell(cube, fields, metric_id, &ret); + const struct metric *metric = cube_find_uncleared_metric_in_cell(cube, fields, metric_id, &ret); if (ret != FS_OK) { return ret; } @@ -1383,7 +1389,7 @@ int cube_histogram_value_at_percentile(const struct cube *cube, int metric_id, c int cube_histogram_count_le_value(const struct cube *cube, int metric_id, const struct field_list *fields, long long value, long long *count) { int ret; - const struct metric *metric = cube_find_metric_in_cell(cube, fields, metric_id, &ret); + const struct metric *metric = cube_find_uncleared_metric_in_cell(cube, fields, metric_id, &ret); if (ret != FS_OK) { return ret; } @@ -1407,7 +1413,7 @@ int cube_get_serialization_as_base64(const struct cube *cube, int metric_id, con } int ret; - const struct metric *metric = cube_find_metric_in_cell(cube, fields, metric_id, &ret); + const struct metric *metric = cube_find_uncleared_metric_in_cell(cube, fields, metric_id, &ret); if (ret != FS_OK) { return ret; } @@ -1449,7 +1455,7 @@ void cube_get_metrics_in_cell(const struct cube *cube, const struct field_list * } for (int i = 0; i < cell_data->next_index; i++) { - if (cell_data->slots[i] != NULL) { + if (cell_data->slots[i] != NULL && !metric_check_if_cleared(cell_data->slots[i])) { (*metric_id_out)[n_metric] = i; n_metric++; } diff --git a/src/metrics/metric.c b/src/metrics/metric.c index 4b0c290..95dc646 100644 --- a/src/metrics/metric.c +++ b/src/metrics/metric.c @@ -45,6 +45,7 @@ struct metric_data { struct metric { enum metric_type type; struct metric_data *data; + bool operated_after_reset; }; /* -------------------------------------------------------------------------- */ @@ -278,18 +279,23 @@ void metric_free(struct metric *pthis) void metric_reset(struct metric *pthis) { g_metric_scheme_table[pthis->type].reset(pthis->data); + pthis->operated_after_reset = false; } struct metric *metric_copy(const struct metric *src) { struct metric *dest = (struct metric *)malloc(sizeof(struct metric)); dest->type = src->type; dest->data = g_metric_scheme_table[dest->type].copy(src->data); + dest->operated_after_reset = src->operated_after_reset; return dest; } //return -1 when merge error. 0 when success. 1 when src has no cell. int metric_merge(struct metric *dest, const struct metric *src) { + if (src->operated_after_reset) { + dest->operated_after_reset = true; + } return g_metric_scheme_table[dest->type].merge(dest->data, src->data); } @@ -307,6 +313,10 @@ void metric_serialize(const struct metric *pthis, char **blob, size_t *blob_size g_metric_scheme_table[type].serialize(data, blob, blob_size); } +bool metric_check_if_cleared(const struct metric *pthis) { + return !pthis->operated_after_reset; +} + /* -------------------------------------------------------------------------- */ /* specific metric operations */ /* -------------------------------------------------------------------------- */ @@ -314,11 +324,15 @@ void metric_serialize(const struct metric *pthis, char **blob, size_t *blob_size void metric_counter_incrby(struct metric *pthis, long long value) { struct metric_counter_or_gauge *counter = pthis->data->counter; counter->value += value; + + pthis->operated_after_reset = true; } void metric_counter_set(struct metric *pthis, long long value) { struct metric_counter_or_gauge *counter = pthis->data->counter; counter->value = value; + + pthis->operated_after_reset = true; } long long metric_counter_get(const struct metric *pthis) { @@ -327,10 +341,14 @@ long long metric_counter_get(const struct metric *pthis) { void metric_hll_add(struct metric *pthis, const char *key, size_t key_len) { hyperloglog_add(pthis->data->hll, key, key_len); + + pthis->operated_after_reset = true; } void metric_hll_add_hash(struct metric *pthis, unsigned long long hash) { hyperloglog_add_hash(pthis->data->hll, hash); + + pthis->operated_after_reset = true; } double metric_hll_get(const struct metric *pthis) { @@ -345,6 +363,8 @@ int metric_histogram_record(struct metric *pthis, long long value) { if (!ret) { return -1; } + + pthis->operated_after_reset = true; return 0; } diff --git a/src/metrics/metric.h b/src/metrics/metric.h index 30d5f1a..0dee126 100644 --- a/src/metrics/metric.h +++ b/src/metrics/metric.h @@ -13,6 +13,7 @@ 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_serialize(const struct metric *pthis, char **blob, size_t *blob_size); +bool metric_check_if_cleared(const struct metric *pthis); void metric_counter_incrby(struct metric *pthis, long long value); void metric_counter_set(struct metric *pthis, long long value); -- cgit v1.2.3