summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorchenzizhan <[email protected]>2024-07-17 15:46:47 +0800
committerchenzizhan <[email protected]>2024-07-17 15:46:47 +0800
commit8ce45ff4f96186c5d9e0f3e82addd2085d7c8788 (patch)
treeccd28d85c7024d2dd4474ae8e6b873adca74b5eb /src
parentdccb4ce1fd92b1f142383e585487af08831264d3 (diff)
cube set sampling
Diffstat (limited to 'src')
-rw-r--r--src/cells/hash_table.c3
-rw-r--r--src/cells/heavy_keeper.c3
-rw-r--r--src/cells/spread_sketch.c3
-rw-r--r--src/cube.c86
-rw-r--r--src/cube.h3
-rw-r--r--src/fieldstat.c46
-rw-r--r--src/fieldstat_easy.c3
7 files changed, 98 insertions, 49 deletions
diff --git a/src/cells/hash_table.c b/src/cells/hash_table.c
index d6dbfb5..60d2465 100644
--- a/src/cells/hash_table.c
+++ b/src/cells/hash_table.c
@@ -64,6 +64,9 @@ struct hash_table *hash_table_new(int max_query_num) {
}
void hash_table_free(struct hash_table *pthis) {
+ if (pthis == NULL) {
+ return;
+ }
struct tag_exdata_item *item, *tmp;
HASH_ITER(hh, pthis->tag_id_map, item, tmp) {
HASH_DEL(pthis->tag_id_map, item);
diff --git a/src/cells/heavy_keeper.c b/src/cells/heavy_keeper.c
index b606a67..c62e550 100644
--- a/src/cells/heavy_keeper.c
+++ b/src/cells/heavy_keeper.c
@@ -518,6 +518,9 @@ struct heavy_keeper *heavy_keeper_new(int max_query_num) {
}
void heavy_keeper_free(struct heavy_keeper *hk) {
+ if (hk == NULL) {
+ return;
+ }
sorted_set_free(hk->top_K_heap);
free(hk->sketch);
diff --git a/src/cells/spread_sketch.c b/src/cells/spread_sketch.c
index 5d5dd72..3e9b11e 100644
--- a/src/cells/spread_sketch.c
+++ b/src/cells/spread_sketch.c
@@ -382,6 +382,9 @@ struct spread_sketch *duplicate_and_step(const struct spread_sketch *ss, const s
}
void spread_sketch_free(struct spread_sketch *ss) {
+ if (ss == NULL) {
+ return;
+ }
smart_ptr_table_free(ss->table);
for (int i = 0; i < ss->depth * ss->width; i++) {
hll_free_register(ss->buckets[i].sthll_register);
diff --git a/src/cube.c b/src/cube.c
index 4bd3305..abc3a66 100644
--- a/src/cube.c
+++ b/src/cube.c
@@ -497,10 +497,9 @@ void *exdata_copy_i(void *exdata) {
return cell_copy((struct cell *)exdata);
}
-struct cube *cube_info_new(const struct field *dimensions, size_t n_dimensions, enum sampling_mode mode, size_t max_n_cell)
+struct cube *cube_info_new(const struct field *dimensions, size_t n_dimensions)
{
struct cube *cube = calloc(1, sizeof(struct cube));
- cube->sampling_mode = mode;
if (n_dimensions == 0) {
cube->cube_dimensions = NULL;
@@ -509,42 +508,81 @@ struct cube *cube_info_new(const struct field *dimensions, size_t n_dimensions,
}
cube->n_dimensions = n_dimensions;
- cube->max_n_cell = max_n_cell;
field_array_to_key(dimensions, n_dimensions, &cube->serialized_dimensions, &cube->serialized_dimensions_len);
cube->id = -1;
+ cube->primary_metric_id = -1;
return cube;
}
-struct cube *cube_new(const struct field *dimensions, size_t n_dimensions, enum sampling_mode mode, size_t max_n_cell)
+struct cube *cube_new(const struct field *dimensions, size_t n_dimensions)
{
- struct cube *cube = cube_info_new(dimensions, n_dimensions, mode, max_n_cell);
+ struct cube *cube = cube_info_new(dimensions, n_dimensions);
cube->manifest_manager = metric_manifest_manager_new();
- switch (mode)
- {
+ return cube;
+}
+
+int cube_set_sampling_mode(struct cube *cube, enum sampling_mode mode, int max_n_cell, int primary_metric_id) {
+ if (cube->sampling_mode == mode && cube->max_n_cell == max_n_cell && cube->primary_metric_id == primary_metric_id) {
+ return FS_OK;
+ }
+ const struct metric_manifest *manifest = metric_manifest_manager_get_by_id(cube->manifest_manager, primary_metric_id);
+ if (manifest == NULL && mode != SAMPLING_MODE_COMPREHENSIVE) {
+ return FS_ERR_INVALID_METRIC_ID;
+ }
+ if ((mode == SAMPLING_MODE_TOPK && manifest->type != METRIC_TYPE_COUNTER) ||
+ (mode == SAMPLING_MODE_TOP_CARDINALITY && manifest->type != METRIC_TYPE_HLL)) {
+ return FS_ERR_INVALID_PARAM;
+ }
+
+ if (cube->primary_metric_id != -1) {
+ // delete previous settings
+ switch (cube->sampling_mode)
+ {
case SAMPLING_MODE_TOPK:
- cube->heavykeeper = heavy_keeper_new(max_n_cell);
- heavy_keeper_set_exdata_schema(cube->heavykeeper, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i);
+ heavy_keeper_free(cube->heavykeeper);
break;
case SAMPLING_MODE_COMPREHENSIVE:
- cube->table = hash_table_new(max_n_cell);
- hash_table_set_exdata_schema(cube->table, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i);
+ hash_table_free(cube->table);
+ break;
+ case SAMPLING_MODE_TOP_CARDINALITY:
+ spread_sketch_free(cube->spread_sketch);
break;
- case SAMPLING_MODE_TOP_CARDINALITY: {
- int width, depth;
- unsigned char precision;
- spread_sketch_get_parameter_recommendation(max_n_cell, &depth, &width, &precision);
- cube->spread_sketch = spread_sketch_new(depth, width, precision, 0, DUMMY_TIME_VAL);
- spread_sketch_set_exdata_schema(cube->spread_sketch, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i);
- break; }
default:
assert(0);
break;
+ }
}
- return cube;
+ switch (mode)
+ {
+ case SAMPLING_MODE_TOPK:
+ cube->heavykeeper = heavy_keeper_new(max_n_cell);
+ heavy_keeper_set_exdata_schema(cube->heavykeeper, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i);
+ break;
+ case SAMPLING_MODE_COMPREHENSIVE:
+ cube->table = hash_table_new(max_n_cell);
+ hash_table_set_exdata_schema(cube->table, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i);
+ break;
+ case SAMPLING_MODE_TOP_CARDINALITY: {
+ int width, depth;
+ unsigned char precision;
+ spread_sketch_get_parameter_recommendation(max_n_cell, &depth, &width, &precision);
+ cube->spread_sketch = spread_sketch_new(depth, width, precision, 0, DUMMY_TIME_VAL);
+ spread_sketch_set_exdata_schema(cube->spread_sketch, exdata_new_i, exdata_free_i, exdata_merge_i, exdata_reset_i, exdata_copy_i);
+ break; }
+ default:
+ assert(0);
+ break;
+ }
+
+ cube->sampling_mode = mode;
+ cube->max_n_cell = max_n_cell;
+ cube->primary_metric_id = primary_metric_id;
+
+ return FS_OK;
}
void cube_free(struct cube *cube) {
@@ -932,6 +970,7 @@ int cube_hll_add_field(struct cube *cube, int metric_id, const struct field *dim
}
int cube_counter_incrby(struct cube *cube, int metric_id, const struct field *dimensions, size_t n_dimensions, long long increment) {
+ assert(cube->primary_metric_id != -1);
assert(cube->sampling_mode == SAMPLING_MODE_COMPREHENSIVE ||
(cube->sampling_mode == SAMPLING_MODE_TOPK && (cube->primary_metric_id != metric_id || increment >= 0)) ||
(cube->sampling_mode == SAMPLING_MODE_TOP_CARDINALITY && cube->primary_metric_id != metric_id)
@@ -1020,8 +1059,10 @@ int cube_counter_set(struct cube *cube, int metric_id, const struct field *dimen
struct cube *cube_copy(const struct cube *cube)
{
- struct cube *cube_dup = cube_info_new(cube->cube_dimensions, cube->n_dimensions, cube->sampling_mode, cube->max_n_cell);
+ struct cube *cube_dup = cube_info_new(cube->cube_dimensions, cube->n_dimensions);
cube_dup->primary_metric_id = cube->primary_metric_id;
+ cube_dup->sampling_mode = cube->sampling_mode;
+ cube_dup->max_n_cell = cube->max_n_cell;
switch (cube->sampling_mode)
{
@@ -1087,8 +1128,10 @@ int cube_merge(struct cube *dest, const struct cube *src)
}
struct cube *cube_fork(const struct cube *cube) {
- struct cube *ret = cube_info_new(cube->cube_dimensions, cube->n_dimensions, cube->sampling_mode, cube->max_n_cell);
+ struct cube *ret = cube_info_new(cube->cube_dimensions, cube->n_dimensions);
ret->primary_metric_id = cube->primary_metric_id;
+ ret->sampling_mode = cube->sampling_mode;
+ ret->max_n_cell = cube->max_n_cell;
ret->manifest_manager = metric_manifest_manager_copy(cube->manifest_manager);
switch (cube->sampling_mode) {
@@ -1431,7 +1474,6 @@ struct field_list *cube_get_identifier(const struct cube *cube) {
const char *cube_get_metric_name(const struct cube *cube, int metric_id) {
const struct metric_manifest *metric = metric_manifest_manager_get_by_id(cube->manifest_manager, metric_id);
if (metric == NULL) {
- printf("metric is null\n");
return NULL;
}
diff --git a/src/cube.h b/src/cube.h
index 134ede8..842f064 100644
--- a/src/cube.h
+++ b/src/cube.h
@@ -13,7 +13,8 @@ extern "C"
struct cube;
struct cube_manager;
-struct cube *cube_new(const struct field *dimensions, size_t n_dimensions, enum sampling_mode mode, size_t max_n_cell);
+struct cube *cube_new(const struct field *dimensions, size_t n_dimensions);
+int cube_set_sampling_mode(struct cube *cube, enum sampling_mode mode, int max_n_cell, int primary_metric_id);
void cube_free(struct cube *cube);
void cube_reset(struct cube *cube);
struct cube *cube_copy(const struct cube *cube);
diff --git a/src/fieldstat.c b/src/fieldstat.c
index 4f4582b..002d754 100644
--- a/src/fieldstat.c
+++ b/src/fieldstat.c
@@ -48,7 +48,7 @@ void fieldstat_reset(struct fieldstat *instance)
cube_manager_reset(instance->cube_manager);
}
-int fieldstat_destroy_cube(struct fieldstat *instance, int cube_id)
+int fieldstat_cube_destroy(struct fieldstat *instance, int cube_id)
{
struct cube *cube = cube_manager_get_cube_by_id(instance->cube_manager, cube_id);
if (cube == NULL) {
@@ -76,7 +76,24 @@ void fieldstat_free_tag_array(struct field *fields, size_t n_tags)
free(fields);
}
-int fieldstat_create_cube(struct fieldstat *instance, const struct field *cube_dimensions, size_t n_dimension, enum sampling_mode mode, size_t max_n_cell)
+// cppcheck-suppress [constParameterPointer, unmatchedSuppression]
+int fieldstat_cube_set_sampling(struct fieldstat *instance, int cube_id, enum sampling_mode mode, int max_n_cell, int primary_metric_id) {
+ if (max_n_cell <= 0) {
+ if (mode != SAMPLING_MODE_COMPREHENSIVE) {
+ return FS_ERR_INVALID_PARAM;
+ }
+ max_n_cell = INT32_MAX;
+ }
+
+ struct cube *cube = cube_manager_get_cube_by_id(instance->cube_manager, cube_id);
+ if (cube == NULL) {
+ return FS_ERR_INVALID_CUBE_ID;
+ }
+
+ return cube_set_sampling_mode(cube, mode, max_n_cell, primary_metric_id);
+}
+
+int fieldstat_cube_create(struct fieldstat *instance, const struct field *cube_dimensions, size_t n_dimension)
{
if (instance == NULL) {
return FS_ERR_NULL_HANDLER;
@@ -86,14 +103,8 @@ int fieldstat_create_cube(struct fieldstat *instance, const struct field *cube_d
cube_dimensions = NULL;
n_dimension = 0;
}
- if (mode == SAMPLING_MODE_TOPK && max_n_cell == 0) {
- return FS_ERR_INVALID_PARAM;
- }
- if (max_n_cell == 0) {
- max_n_cell = INT32_MAX;
- }
-
- struct cube *cube = cube_new(cube_dimensions, n_dimension, mode, max_n_cell);
+
+ struct cube *cube = cube_new(cube_dimensions, n_dimension);
int ret = cube_manager_add(instance->cube_manager, cube);
if (ret < 0) {
cube_free(cube);
@@ -103,21 +114,6 @@ int fieldstat_create_cube(struct fieldstat *instance, const struct field *cube_d
return ret; //ret is the cube_id
}
-// cppcheck-suppress [constParameterPointer, unmatchedSuppression]
-int fieldstat_cube_set_primary_metric(struct fieldstat *instance, int cube_id, int metric_id)
-{
- if (instance == NULL) {
- return FS_ERR_NULL_HANDLER;
- }
-
- struct cube *cube = cube_manager_get_cube_by_id(instance->cube_manager, cube_id);
- if (cube == NULL) {
- return FS_ERR_INVALID_CUBE_ID;
- }
-
- return cube_set_primary_metric(cube, metric_id);
-}
-
/* -------------------------------------------------------------------------- */
/* metric register */
/* -------------------------------------------------------------------------- */
diff --git a/src/fieldstat_easy.c b/src/fieldstat_easy.c
index 00a569a..46133f4 100644
--- a/src/fieldstat_easy.c
+++ b/src/fieldstat_easy.c
@@ -122,7 +122,8 @@ struct fieldstat_easy *fieldstat_easy_new(int max_thread_num, const char *name,
fse->fsu = malloc(sizeof(struct fs_easy_thread) * max_thread_num);
fse->max_thread_num = max_thread_num;
fse->delta = fieldstat_new();
- fieldstat_create_cube(fse->delta, NULL, 0, SAMPLING_MODE_COMPREHENSIVE, 0);
+ fieldstat_cube_create(fse->delta, NULL, 0);
+ fieldstat_cube_set_sampling(fse->delta, 0, SAMPLING_MODE_COMPREHENSIVE, 0, 0);
fse->accumulate = fieldstat_fork(fse->delta);
fse->exporter = fieldstat_json_exporter_new();