diff options
| author | chenzizhan <[email protected]> | 2024-05-16 11:26:10 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2024-05-16 11:26:10 +0800 |
| commit | ca3fdbc380f8b3cdf45b7bd20bacc346cf8b0c03 (patch) | |
| tree | ee4f7af6d56f58d2e2dbe753c0579eab9ba62a65 | |
| parent | 69baa07be7d9684968e82dfdd3135bfd5c8e6663 (diff) | |
fix: key in sorted set error when record primary metric last; remove selfdefined assertv4.5.8
| -rw-r--r-- | src/tags/heavy_keeper.c | 8 | ||||
| -rw-r--r-- | src/tags/sorted_set.c | 11 | ||||
| -rw-r--r-- | src/tags/sorted_set.h | 2 | ||||
| -rw-r--r-- | test/test_metric_counter.cpp | 32 |
4 files changed, 41 insertions, 12 deletions
diff --git a/src/tags/heavy_keeper.c b/src/tags/heavy_keeper.c index bfef493..d2eaa2d 100644 --- a/src/tags/heavy_keeper.c +++ b/src/tags/heavy_keeper.c @@ -18,10 +18,6 @@ #include "tags/sorted_set.h" #include "tags/my_ut_hash.h" -#define assert(s) do { if (!(s)) { printf("assert failed: %s\n", #s); \ - *((int *)0) = 0; \ - } \ - } while (0) struct Bucket { unsigned int finger_print; @@ -226,7 +222,7 @@ static int heavy_keeper_add_by_recording_popped_data(struct heavy_keeper *heavy_ } unsigned long long int tmp_cnt = sorted_set_get_count(summary, tag); - if (tmp_cnt == 0) { // the flow is not in sorted set + if (tmp_cnt == INVALID_COUNT) { // the flow is not in sorted set if ((maxv - nMin <= count && maxv != nMin) || sorted_set_cardinality(summary) != heavy_keeper->K) { assert(cell_id >= 0); assert(tag != NULL); @@ -302,7 +298,7 @@ int heavy_keeper_query_one(const struct heavy_keeper *hk, const struct tag_hash_ { if (count_out != NULL) { unsigned long long int count = sorted_set_get_count(hk->top_K_heap, tag); - if (count == 0) { + if (count == INVALID_COUNT) { *count_out = 0; return -1; } diff --git a/src/tags/sorted_set.c b/src/tags/sorted_set.c index 31320f7..6315bac 100644 --- a/src/tags/sorted_set.c +++ b/src/tags/sorted_set.c @@ -11,10 +11,6 @@ #include "sorted_set.h"
#include "my_ut_hash.h"
-#define assert(s) do { if (!(s)) { printf("assert failed: %s\n", #s); \
- *((int *)0) = 0; \
- } \
- } while (0)
struct entry_data {
struct tag_hash_key *key;
@@ -312,11 +308,14 @@ unsigned long long sorted_set_get_min_count(const struct sorted_set *ss) unsigned long long sorted_set_get_count(const struct sorted_set *ss, const struct tag_hash_key *tag)
{
if (sorted_set_cardinality(ss) == 0) {
- return 0;
+ return INVALID_COUNT;
}
const heap_entry *entry = sorted_set_find_entry(ss, tag);
if (entry == NULL) {
- return 0;
+ return INVALID_COUNT;
+ }
+ if (sorted_set_entry_dying(entry)) {
+ return INVALID_COUNT;
}
return sorted_set_entry_get_count(entry);
diff --git a/src/tags/sorted_set.h b/src/tags/sorted_set.h index 4403c7b..6dee8c7 100644 --- a/src/tags/sorted_set.h +++ b/src/tags/sorted_set.h @@ -7,6 +7,8 @@ extern "C"{
#endif
+#define INVALID_COUNT UINT64_MAX
+
struct sorted_set;
/**
diff --git a/test/test_metric_counter.cpp b/test/test_metric_counter.cpp index b9f0533..de0f42b 100644 --- a/test/test_metric_counter.cpp +++ b/test/test_metric_counter.cpp @@ -268,6 +268,38 @@ TEST(metric_test_counter, set_on_primary_metric_going_bigger) fieldstat_free(instance); } +TEST(metric_test_counter, primary_counter_add_after_first) +{ + struct fieldstat *instance = fieldstat_new(); + int cube_id = fieldstat_create_cube(instance, &TEST_SHARED_TAG, 1, SAMPLING_MODE_TOPK, 10); + int metric_id_primary = fieldstat_register_counter(instance, "primary"); + int metric_id2 = fieldstat_register_counter(instance, "using"); + fieldstat_cube_set_primary_metric(instance, cube_id, metric_id_primary); + + + int ret = fieldstat_counter_incrby(instance, cube_id, metric_id2, &TEST_TAG_INT, 1, 10); + EXPECT_EQ(ret, FS_OK); + ret = fieldstat_counter_incrby(instance, cube_id, metric_id_primary, &TEST_TAG_INT, 1, 20); + EXPECT_EQ(ret, FS_OK); + + EXPECT_EQ(my_fieldstat_counter_get(instance, cube_id, metric_id_primary), 20); + EXPECT_EQ(my_fieldstat_counter_get(instance, cube_id, metric_id2), 10); + + // TOPK functions well + struct fieldstat_tag tag = TEST_TAG_INT; + for (int i = 0; i < 9; i++) { + tag.value_longlong = i + 123; + ret = fieldstat_counter_incrby(instance, cube_id, metric_id_primary, &tag, 1, 1 + i); + EXPECT_EQ(ret, FS_OK); + } + // now the TOPK is full, add a new one + tag.value_longlong = 321; + ret = fieldstat_counter_incrby(instance, cube_id, metric_id_primary, &tag, 1, 1); + EXPECT_EQ(ret, FS_ERR_TOO_MANY_CELLS); + + fieldstat_free(instance); +} + TEST(metric_test_counter, topk_set_and_test_accuracy) { struct fieldstat *instance = fieldstat_new(); |
