summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2024-07-23 18:03:57 +0800
committerchenzizhan <[email protected]>2024-07-23 18:03:57 +0800
commit67030d247ad9d97ce708ea980518beeca643e97e (patch)
tree6821ccd6e96461e8dad3f975f22b1765156c2b63
parent24299fa9d46f4d386d0423417b51f9e0d64224a0 (diff)
fix: should not export reset metric
-rw-r--r--src/cube.c28
-rw-r--r--src/metrics/metric.c20
-rw-r--r--src/metrics/metric.h1
-rw-r--r--test/test_register_and_reset.cpp26
4 files changed, 64 insertions, 11 deletions
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);
diff --git a/test/test_register_and_reset.cpp b/test/test_register_and_reset.cpp
index 3ffae8e..b79da54 100644
--- a/test/test_register_and_reset.cpp
+++ b/test/test_register_and_reset.cpp
@@ -104,6 +104,32 @@ TEST(test_register, reset_and_try_to_query_cell_comprehensive)
fieldstat_free(instance);
}
+TEST(test_register, reset_and_try_to_query_metric)
+{
+ struct fieldstat *instance = fieldstat_new();
+ int cube_id = test_fieldstat_cube_create(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_COMPREHENSIVE, 10);
+ int metric_counter = fieldstat_register_counter(instance, cube_id, "counter");
+ int metric_hll = fieldstat_register_hll(instance, cube_id, "hll", 5);
+ int metric_hist = fieldstat_register_histogram(instance, cube_id, "hist", 0, 10000, 5);
+ int metric_operated_after_reset = fieldstat_register_counter(instance, cube_id, "counter_after_reset");
+
+ fieldstat_counter_incrby(instance, cube_id, metric_counter, &TEST_FIELD_INT, 1, 1);
+ fieldstat_hll_add(instance, cube_id, metric_counter, &TEST_FIELD_INT, 1, "1", 1);
+ fieldstat_histogram_record(instance, cube_id, metric_hist, &TEST_FIELD_INT, 1, 1000);
+
+ fieldstat_reset(instance);
+ fieldstat_counter_incrby(instance, cube_id, metric_operated_after_reset, &TEST_FIELD_INT, 1, 1);
+
+ long long value;
+ double value_hll;
+ EXPECT_EQ(fieldstat_counter_get(instance, cube_id, &TEST_FIELD_LIST_INT, metric_counter, &value), FS_ERR_INVALID_METRIC_ID);
+ EXPECT_EQ(fieldstat_hll_get(instance, cube_id, &TEST_FIELD_LIST_INT, metric_hll, &value_hll), FS_ERR_INVALID_METRIC_ID);
+ EXPECT_EQ(fieldstat_histogram_value_at_percentile(instance, cube_id, &TEST_FIELD_LIST_INT, metric_hist, 0.5), FS_ERR_INVALID_METRIC_ID);
+ EXPECT_EQ(fieldstat_counter_get(instance, cube_id, &TEST_FIELD_LIST_INT, metric_operated_after_reset, &value), FS_OK);
+
+ fieldstat_free(instance);
+}
+
TEST(test_register, reset_and_try_to_query_cell_topk)
{
struct fieldstat *instance = fieldstat_new();