1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include "histogram_encoder.h"
#include "serializer.h"
#include "metric.h"
#include "hdr/hdr_histogram.h"
#include "hdr/hdr_histogram_log.h"
#include "hyperloglog.h"
#include "metric_manifest.h"
struct metric_counter_or_gauge {
long long value;
};
typedef struct metric_data *(*metric_func_new)(const union metric_parameter *);
typedef void (*metric_func_free)(struct metric_data *);
typedef int (*metric_func_merge)(struct metric_data *, const struct metric_data *);
typedef struct metric_data * (*metric_func_copy)(const struct metric_data *);
typedef void (*metric_func_serialize)(const struct metric_data *, char **/*blob*/, size_t */*blob_size*/);
// typedef struct metric_data * (*metric_func_deserialize)(const char *, size_t );
typedef void (*metric_func_reset)(struct metric_data *);
struct metric_scheme {
metric_func_new new;
metric_func_free free;
metric_func_merge merge;
metric_func_copy copy;
metric_func_serialize serialize;
// metric_func_deserialize deserialize;
metric_func_reset reset;
};
struct metric_data {
union {
struct metric_counter_or_gauge *counter;
struct hyperloglog *hll;
struct hdr_histogram *hdr;
};
};
struct metric {
enum metric_type type;
struct metric_data *data;
bool operated_after_reset;
};
/* -------------------------------------------------------------------------- */
/* scheme */
/* -------------------------------------------------------------------------- */
/* --------------------------------- counter -------------------------------- */
struct metric_data *metric_scheme_counter_new(const union metric_parameter *_para)
{
struct metric_data *data = (struct metric_data *)malloc(sizeof(struct metric_data));
struct metric_counter_or_gauge *counter = (struct metric_counter_or_gauge *)malloc(sizeof(struct metric_counter_or_gauge));
data->counter = counter;
counter->value = 0;
return data;
}
static void metric_scheme_counter_free(struct metric_data *data)
{
free(data->counter);
free(data);
}
static void metric_scheme_counter_serialize(const struct metric_data *data, char **blob, size_t *blob_size)
{
const struct metric_counter_or_gauge *counter = data->counter;
struct fs_reader *reader = fs_reader_new();
fs_reader_read_longlong(reader, counter->value);
fs_reader_finalize(reader, blob, blob_size);
}
static int metric_scheme_counter_merge(struct metric_data *pthis, const struct metric_data *from)
{
struct metric_counter_or_gauge *counter = pthis->counter;
const struct metric_counter_or_gauge *from_counter = from->counter;
counter->value += from_counter->value;
return 0;
}
static struct metric_data *metric_scheme_counter_copy(const struct metric_data *from)
{
struct metric_data *pthis = (struct metric_data *)malloc(sizeof(struct metric_data));
struct metric_counter_or_gauge *counter = (struct metric_counter_or_gauge *)malloc(sizeof(struct metric_counter_or_gauge));
pthis->counter = counter;
counter->value = from->counter->value;
return pthis;
}
struct metric_data *metric_scheme_counter_deserialize(const char *blob, size_t blob_size)
{
struct fs_writer *writer = fs_writer_new(blob, blob_size);
struct metric_data *ret = (struct metric_data *)malloc(sizeof(struct metric_data));
struct metric_counter_or_gauge *counter = (struct metric_counter_or_gauge *)malloc(sizeof(struct metric_counter_or_gauge));
ret->counter = counter;
fs_writer_write_longlong(writer, &counter->value);
fs_writer_free(writer);
return ret;
}
void metric_scheme_counter_reset(struct metric_data *data)
{
data->counter->value = 0;
}
/* --------------------------------- hll -------------------------------- */
struct metric_data *metric_scheme_hll_new(const union metric_parameter *para)
{
struct metric_data *data = (struct metric_data *)malloc(sizeof(struct metric_data));
struct hyperloglog *hll = hyperloglog_new(para->hll.precision);
data->hll = hll;
return data;
}
static void metric_scheme_hll_free(struct metric_data *data)
{
hyperloglog_free(data->hll);
free(data);
}
static void metric_scheme_hll_serialize(const struct metric_data *data, char **blob, size_t *blob_size)
{
hyperloglog_serialize(data->hll, blob, blob_size);
}
static int metric_scheme_hll_merge(struct metric_data *pthis, const struct metric_data *from)
{
return hyperloglog_merge(pthis->hll, from->hll);
}
struct metric_data *metric_scheme_hll_copy(const struct metric_data *from)
{
struct metric_data *pthis = (struct metric_data *)malloc(sizeof(struct metric_data));
struct hyperloglog *hll = hyperloglog_new(from->hll->cfg.precision);
pthis->hll = hll;
hyperloglog_merge(hll, from->hll);
return pthis;
}
struct metric_data *metric_scheme_hll_deserialize(const char *blob, size_t blob_size)
{
struct metric_data *ret = (struct metric_data *)malloc(sizeof(struct metric_data));
struct hyperloglog *hll = hyperloglog_deserialize(blob, blob_size);
ret->hll = hll;
return ret;
}
void metric_scheme_hll_reset(struct metric_data *data)
{
hyperloglog_reset(data->hll);
}
/* --------------------------------- histogram -------------------------------- */
struct metric_data *metric_scheme_histogram_new(const union metric_parameter *para)
{
struct hdr_histogram* tmp_p_hdr = NULL;
int ret = hdr_init((int64_t)para->hdr.lowest_trackable_value, (int64_t)para->hdr.highest_trackable_value, para->hdr.significant_figures, &tmp_p_hdr);
if (ret != 0) {
return NULL;
}
struct metric_data *data = (struct metric_data *)malloc(sizeof(struct metric_data));
data->hdr = tmp_p_hdr;
return data;
}
static void metric_scheme_histogram_free(struct metric_data *data)
{
hdr_close(data->hdr);
free(data);
}
static void metric_scheme_histogram_serialize(const struct metric_data *data, char **blob, size_t *blob_size)
{
histogram_encode_into_blob(data->hdr, blob, blob_size);
}
static int metric_scheme_histogram_merge(struct metric_data *pthis, const struct metric_data *from)
{
(void)hdr_add(pthis->hdr, from->hdr);
return 0;
}
static struct metric_data *metric_scheme_histogram_copy(const struct metric_data *from)
{
struct hdr_histogram* tmp_p_hdr = NULL;
hdr_init(from->hdr->lowest_discernible_value, from->hdr->highest_trackable_value, from->hdr->significant_figures, &tmp_p_hdr);
struct metric_data *data = (struct metric_data *)malloc(sizeof(struct metric_data));
data->hdr = tmp_p_hdr;
hdr_add(data->hdr, from->hdr);
return data;
}
struct metric_data *metric_scheme_histogram_deserialize(const char *blob, size_t blob_size)
{
struct hdr_histogram *hdr = histogram_decode_from_blob(blob, blob_size);
struct metric_data *data = (struct metric_data *)malloc(sizeof(struct metric_data));
data->hdr = hdr;
return data;
}
void metric_scheme_histogram_reset(struct metric_data *data)
{
hdr_reset(data->hdr);
}
static const struct metric_scheme g_metric_scheme_table[] = {
[METRIC_TYPE_COUNTER] = {
.new = metric_scheme_counter_new,
.free = metric_scheme_counter_free,
.serialize = metric_scheme_counter_serialize,
// .deserialize = metric_scheme_counter_deserialize,
.merge = metric_scheme_counter_merge,
.copy = metric_scheme_counter_copy,
.reset = metric_scheme_counter_reset,
},
[METRIC_TYPE_HLL] = {
.new = metric_scheme_hll_new,
.free = metric_scheme_hll_free,
.serialize = metric_scheme_hll_serialize,
// .deserialize = metric_scheme_hll_deserialize,
.merge = metric_scheme_hll_merge,
.copy = metric_scheme_hll_copy,
.reset = metric_scheme_hll_reset,
},
[METRIC_TYPE_HISTOGRAM] = {
.new = metric_scheme_histogram_new,
.free = metric_scheme_histogram_free,
.serialize = metric_scheme_histogram_serialize,
// .deserialize = metric_scheme_histogram_deserialize,
.merge = metric_scheme_histogram_merge,
.copy = metric_scheme_histogram_copy,
.reset = metric_scheme_histogram_reset,
},
};
/* -------------------------------------------------------------------------- */
/* metric */
/* -------------------------------------------------------------------------- */
struct metric *metric_new(const struct metric_manifest *manifest)
{
struct metric *pthis = (struct metric *)calloc(1, sizeof(struct metric));
pthis->data = g_metric_scheme_table[manifest->type].new(manifest->parameters);
pthis->type = manifest->type;
return pthis;
}
void metric_free(struct metric *pthis)
{
if (pthis == NULL) {
return;
}
g_metric_scheme_table[pthis->type].free(pthis->data);
free(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);
}
void metric_serialize(const struct metric *pthis, char **blob, size_t *blob_size) {
struct metric_data *data = pthis->data;
enum metric_type type = pthis->type;
if (type == METRIC_TYPE_HLL) {
hyperloglog_serialize_into_base64(data->hll, blob, blob_size);
return;
}
if (type == METRIC_TYPE_HISTOGRAM) {
histogram_encode_into_b64(data->hdr, blob, blob_size);
return;
}
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 */
/* -------------------------------------------------------------------------- */
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) {
return pthis->data->counter->value;
}
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) {
return hyperloglog_count(pthis->data->hll);
}
int metric_histogram_record(struct metric *pthis, long long value) {
if (value > pthis->data->hdr->highest_trackable_value) {
value = pthis->data->hdr->highest_trackable_value;
}
bool ret = hdr_record_value(pthis->data->hdr, value);
if (!ret) {
return -1;
}
pthis->operated_after_reset = true;
return 0;
}
long long metric_histogram_value_at_percentile(const struct metric *pthis, double percentile) {
return hdr_value_at_percentile(pthis->data->hdr, percentile);
}
long long metric_histogram_count_le_value(const struct metric *pthis, long long value) {
return hdr_count_le_value(pthis->data->hdr, value);
}
|