diff options
| author | chenzizhan <[email protected]> | 2024-10-16 15:16:02 +0800 |
|---|---|---|
| committer | chenzizhan <[email protected]> | 2024-10-16 15:16:02 +0800 |
| commit | 0881c354b6ea011f6213c0f0af237e9260ca798a (patch) | |
| tree | 6a558385af24905fd907c1baca18f2408c244ead /src | |
| parent | c0e5467f94d11365e2c90c4fedf2c0d4f12862c4 (diff) | |
incrby batch
Diffstat (limited to 'src')
| -rw-r--r-- | src/cube.c | 63 | ||||
| -rw-r--r-- | src/cube.h | 1 | ||||
| -rw-r--r-- | src/fieldstat.c | 11 |
3 files changed, 75 insertions, 0 deletions
@@ -554,6 +554,9 @@ int cube_set_sampling(struct cube *cube, enum sampling_mode mode, int max_n_cell break; } } + if (mode == SAMPLING_MODE_COMPREHENSIVE) { + primary_metric_id = 0; + } switch (mode) { @@ -962,6 +965,66 @@ int cube_counter_incrby(struct cube *cube, int metric_id, const struct field *di return FS_OK; } +int cube_counter_incrby_batch(struct cube *cube, int metric_ids[], const struct field *dimensions, size_t n_dimensions, long long increments[], size_t n_metrics) { + if (cube->primary_metric_id == -1) { + return FS_ERR_CUBE_SAMPLING_NOT_INITIALIZED; + } + + int total_increment = 0; + if (cube->sampling_mode == SAMPLING_MODE_TOPK) { + for (int i = 0; i < n_metrics; i++) { + assert (cube->sampling_mode != SAMPLING_MODE_TOPK || cube->primary_metric_id != metric_ids[i] || increments[i] >= 0); + assert (cube->sampling_mode != SAMPLING_MODE_TOP_CARDINALITY || cube->primary_metric_id != metric_ids[i]); + assert (metric_manifest_manager_get_by_id(cube->manifest_manager, metric_ids[i])->type == METRIC_TYPE_COUNTER); + + if (cube->primary_metric_id == metric_ids[i]) { + total_increment += increments[i]; + } + } + } + + struct cell *cell_data = NULL; + if (total_increment > 0) { + char *compound_dimension; + size_t compound_dimension_len; + field_array_to_key(dimensions, n_dimensions, &compound_dimension, &compound_dimension_len); + struct exdata_new_args args; + args.cell_dimensions = dimensions; + args.n_dimensions = n_dimensions; + + int tmp_ret = heavy_keeper_add(cube->heavykeeper, compound_dimension, compound_dimension_len, total_increment, (void *)&args); + if (tmp_ret != 1) { + free(compound_dimension); + return FS_ERR_TOO_MANY_CELLS; + } + cell_data = heavy_keeper_get0_exdata(cube->heavykeeper, compound_dimension, compound_dimension_len); + free(compound_dimension); + } else { + cell_data = get_cell_in_cube_generic(cube, dimensions, n_dimensions); + } + if (cell_data == NULL) { + return FS_ERR_TOO_MANY_CELLS; + } + + for (int i = 0; i < n_metrics; i++) { + if (increments[i] == 0) { + continue; + } + if (cube->primary_metric_id == metric_ids[i] && cube->sampling_mode == SAMPLING_MODE_TOPK) { + continue; // primary metric is recorded directly in heavy keeper + } + + const struct metric_manifest *manifest = metric_manifest_manager_get_by_id(cube->manifest_manager, metric_ids[i]); + struct metric *metric = add_or_find_metric_in_cell(manifest, cell_data); + + printf("metric: %d, increment: %lld\n", metric_ids[i], increments[i]); + metric_counter_incrby(metric, increments[i]); + } + + return FS_OK; +} + + int cube_counter_set(struct cube *cube, int metric_id, const struct field *dimensions, size_t n_dimensions, long long value) { if (cube->primary_metric_id == -1) { return FS_ERR_CUBE_SAMPLING_NOT_INITIALIZED; @@ -32,6 +32,7 @@ int cube_hll_add(struct cube *cube, int metric_id, const struct field *dimension int cube_hll_add_field(struct cube *cube, int metric_id, const struct field *dimensions, size_t n_dimensions, const struct field *item_fields, size_t n_item); int cube_counter_incrby(struct cube *cube, int metric_id, const struct field *dimensions, size_t n_dimensions, long long increment); int cube_counter_set(struct cube *cube, int metric_id, const struct field *dimensions, size_t n_dimensions, long long value); +int cube_counter_incrby_batch(struct cube *cube, int metric_ids[], const struct field *dimensions, size_t n_dimensions, long long increments[], size_t n_metrics); /* ---------------------------------- query --------------------------------- */ int cube_counter_get(const struct cube *cube, int metric_id, const struct field_list *dimensions, long long *value); diff --git a/src/fieldstat.c b/src/fieldstat.c index 1becadd..7782051 100644 --- a/src/fieldstat.c +++ b/src/fieldstat.c @@ -158,6 +158,17 @@ int fieldstat_counter_incrby(struct fieldstat *instance, int cube_id, int metric } // cppcheck-suppress [constParameterPointer, unmatchedSuppression] +int fieldstat_counter_incrby_batch(struct fieldstat *instance, int cube_id, const struct field *cell_dimensions, size_t n_dimensions, int metric_ids[], long long increments[], size_t n_metrics) +{ + 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_counter_incrby_batch(cube, metric_ids, cell_dimensions, n_dimensions, increments, n_metrics); +} + +// cppcheck-suppress [constParameterPointer, unmatchedSuppression] int fieldstat_counter_set(struct fieldstat *instance, int cube_id, int metric_id, const struct field *cell_dimensions, size_t n_dimensions, long long value) { struct cube *cube = cube_manager_get_cube_by_id(instance->cube_manager, cube_id); |
