-
Notifications
You must be signed in to change notification settings - Fork 509
/
info_command.c
400 lines (349 loc) · 12.9 KB
/
info_command.c
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright Redis Ltd. 2016 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/
#include "info_command.h"
#include "resp3.h"
#include "info/field_spec_info.h"
// Type of field returned in INFO
typedef enum {
InfoField_WholeSum,
InfoField_DoubleSum,
InfoField_DoubleAverage,
InfoField_Max,
} InfoFieldType;
// Field specification
typedef struct {
const char *name;
InfoFieldType type;
} InfoFieldSpec;
static InfoFieldSpec toplevelSpecs_g[] = {
{.name = "num_docs", .type = InfoField_WholeSum},
{.name = "max_doc_id", .type = InfoField_Max},
{.name = "num_terms", .type = InfoField_WholeSum},
{.name = "num_records", .type = InfoField_WholeSum},
{.name = "inverted_sz_mb", .type = InfoField_DoubleSum},
{.name = "total_inverted_index_blocks", .type = InfoField_WholeSum},
{.name = "vector_index_sz_mb", .type = InfoField_DoubleSum},
{.name = "offset_vectors_sz_mb", .type = InfoField_DoubleSum},
{.name = "doc_table_size_mb", .type = InfoField_DoubleSum},
{.name = "sortable_values_size_mb", .type = InfoField_DoubleSum},
{.name = "key_table_size_mb", .type = InfoField_DoubleSum},
{.name = "geoshapes_sz_mb", .type = InfoField_DoubleSum},
{.name = "records_per_doc_avg", .type = InfoField_DoubleAverage},
{.name = "bytes_per_record_avg", .type = InfoField_DoubleAverage},
{.name = "offsets_per_term_avg", .type = InfoField_DoubleAverage},
{.name = "offset_bits_per_record_avg", .type = InfoField_DoubleAverage},
{.name = "indexing", .type = InfoField_WholeSum},
{.name = "percent_indexed", .type = InfoField_DoubleAverage},
{.name = "hash_indexing_failures", .type = InfoField_WholeSum},
{.name = "number_of_uses", .type = InfoField_Max},
{.name = "cleaning", .type = InfoField_WholeSum}};
static InfoFieldSpec gcSpecs[] = {
{.name = "current_hz", .type = InfoField_DoubleAverage},
{.name = "bytes_collected", .type = InfoField_WholeSum},
{.name = "effectiv_cycles_rate", .type = InfoField_DoubleAverage}};
static InfoFieldSpec cursorSpecs[] = {
{.name = "global_idle", .type = InfoField_WholeSum},
{.name = "global_total", .type = InfoField_WholeSum},
{.name = "index_capacity", .type = InfoField_WholeSum},
{.name = "index_total", .type = InfoField_WholeSum},
};
static InfoFieldSpec dialectSpecs[] = {
{.name = "dialect_1", .type = InfoField_Max},
{.name = "dialect_2", .type = InfoField_Max},
{.name = "dialect_3", .type = InfoField_Max},
{.name = "dialect_4", .type = InfoField_Max},
};
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*arr))
#define NUM_FIELDS_SPEC (ARRAY_SIZE(toplevelSpecs_g))
#define NUM_GC_FIELDS_SPEC (ARRAY_SIZE(gcSpecs))
#define NUM_CURSOR_FIELDS_SPEC (ARRAY_SIZE(cursorSpecs))
#define NUM_DIALECT_FIELDS_SPEC (ARRAY_SIZE(dialectSpecs))
// Variant value type
typedef struct {
int isSet;
union {
size_t total_l;
double total_d;
struct {
double sum;
double count;
} avg;
struct {
char *str;
size_t len;
} str;
} u;
} InfoValue;
// State object for parsing and replying INFO
typedef struct {
const char *indexName;
size_t indexNameLen;
MRReply *indexDef;
MRReply *indexSchema;
MRReply *indexOptions;
size_t *errorIndexes;
InfoValue toplevelValues[NUM_FIELDS_SPEC];
FieldSpecInfo *fieldSpecInfo_arr;
IndexError indexError;
InfoValue gcValues[NUM_GC_FIELDS_SPEC];
InfoValue cursorValues[NUM_CURSOR_FIELDS_SPEC];
InfoValue dialectValues[NUM_DIALECT_FIELDS_SPEC];
} InfoFields;
/**
* Read a single KV array (i.e. array with alternating key-value entries)
* - array is the source array type
* - dsts is the destination
* - specs describes the destination types and corresponding field names
* - numFields - the number of specs and dsts
* - onlyScalars - because special handling is done in toplevel mode
*/
static void processKvArray(InfoFields *fields, MRReply *array, InfoValue *dsts,
InfoFieldSpec *specs, size_t numFields, int onlyScalars);
/** Reply with a KV array, the values are emitted per name and type */
static void replyKvArray(RedisModule_Reply *reply, InfoFields *fields, InfoValue *values,
InfoFieldSpec *specs, size_t numFields);
// Writes field data to the target
static void convertField(InfoValue *dst, MRReply *src, const InfoFieldSpec *spec) {
int type = spec->type;
switch (type) {
case InfoField_WholeSum: {
long long tmp;
MRReply_ToInteger(src, &tmp);
dst->u.total_l += tmp;
break;
}
case InfoField_DoubleSum: {
double d;
MRReply_ToDouble(src, &d);
dst->u.total_d += d;
break;
}
case InfoField_DoubleAverage: {
dst->u.avg.count++;
double d;
MRReply_ToDouble(src, &d);
dst->u.avg.sum += d;
break;
}
case InfoField_Max: {
long long newVal;
MRReply_ToInteger(src, &newVal);
if (dst->u.total_l < newVal) {
dst->u.total_l = newVal;
}
break;
}
}
dst->isSet = 1;
}
// Extract an array of FieldSpecInfo from MRReply
void handleFieldStatistics(MRReply *src, InfoFields *fields) {
// Input validations
RedisModule_Assert(src && fields);
RedisModule_Assert(MRReply_Type(src) == MR_REPLY_ARRAY);
size_t len = MRReply_Length(src);
if (!fields->fieldSpecInfo_arr) {
// Lazy initialization
fields->fieldSpecInfo_arr = array_new(FieldSpecInfo, len);
for (size_t i = 0; i < len; i++) {
FieldSpecInfo fieldSpecInfo = FieldSpecInfo_Init();
fields->fieldSpecInfo_arr = array_append(fields->fieldSpecInfo_arr, fieldSpecInfo);
}
}
for (size_t i = 0; i < len; i++) {
MRReply *serializedFieldSpecInfo = MRReply_ArrayElement(src, i);
FieldSpecInfo fieldSpecInfo = FieldSpecInfo_Deserialize(serializedFieldSpecInfo);
FieldSpecInfo_OpPlusEquals(&fields->fieldSpecInfo_arr[i], &fieldSpecInfo);
FieldSpecInfo_Clear(&fieldSpecInfo); // Free Resources
}
}
static void handleIndexError(InfoFields *fields, MRReply *src) {
// Check if indexError is initialized
if (!IndexError_LastError(&fields->indexError)) {
fields->indexError = IndexError_Init();
}
IndexError indexError = IndexError_Deserialize(src);
IndexError_OpPlusEquals(&fields->indexError, &indexError);
IndexError_Clear(indexError); // Free Resources
}
// Handle fields which aren't InfoValue types
static void handleSpecialField(InfoFields *fields, const char *name, MRReply *value) {
if (!strcmp(name, "index_name")) {
if (fields->indexName) {
return;
}
fields->indexName = MRReply_String(value, &fields->indexNameLen);
const char *curlyIdx = index(fields->indexName, '{');
if (curlyIdx != NULL) {
fields->indexNameLen = curlyIdx - fields->indexName;
}
} else if (!strcmp(name, "attributes")) {
if (!fields->indexSchema) {
fields->indexSchema = value;
}
} else if (!strcmp(name, "index_definition")) {
if (!fields->indexDef) {
fields->indexDef = value;
}
} else if (!strcmp(name, "index_options")) {
if (!fields->indexOptions) {
fields->indexOptions = value;
}
} else if (!strcmp(name, "gc_stats")) {
processKvArray(fields, value, fields->gcValues, gcSpecs, NUM_GC_FIELDS_SPEC, 1);
} else if (!strcmp(name, "cursor_stats")) {
processKvArray(fields, value, fields->cursorValues, cursorSpecs, NUM_CURSOR_FIELDS_SPEC, 1);
} else if (!strcmp(name, "dialect_stats")) {
processKvArray(fields, value, fields->dialectValues, dialectSpecs, NUM_DIALECT_FIELDS_SPEC, 1);
} else if (!strcmp(name, "field statistics")) {
handleFieldStatistics(value, fields);
} else if (!strcmp(name, IndexError_ObjectName)) {
handleIndexError(fields, value);
}
}
static void processKvArray(InfoFields *fields, MRReply *array, InfoValue *dsts, InfoFieldSpec *specs,
size_t numFields, int onlyScalarValues) {
if (MRReply_Type(array) != MR_REPLY_ARRAY && MRReply_Type(array) != MR_REPLY_MAP) {
return;
}
size_t numElems = MRReply_Length(array);
if (numElems % 2 != 0) {
return;
}
for (size_t ii = 0; ii < numElems; ii += 2) {
// @@ MapElementByIndex
MRReply *value = MRReply_ArrayElement(array, ii + 1);
const char *s = MRReply_String(MRReply_ArrayElement(array, ii), NULL);
for (size_t jj = 0; jj < numFields; ++jj) {
const char *name = specs[jj].name;
if (!strcmp(s, name)) {
convertField(dsts + jj, value, specs + jj);
goto next_elem;
}
}
if (!onlyScalarValues) {
handleSpecialField(fields, s, value);
}
next_elem:
continue;
}
}
static void cleanInfoReply(InfoFields *fields) {
if (fields->fieldSpecInfo_arr) {
// Clear the info fields
for (size_t i = 0; i < array_len(fields->fieldSpecInfo_arr); i++) {
FieldSpecInfo_Clear(&fields->fieldSpecInfo_arr[i]);
}
array_free(fields->fieldSpecInfo_arr);
fields->fieldSpecInfo_arr = NULL;
}
IndexError_Clear(fields->indexError);
rm_free(fields->errorIndexes);
}
static void replyKvArray(RedisModule_Reply *reply, InfoFields *fields, InfoValue *values,
InfoFieldSpec *specs, size_t numSpecs) {
for (size_t ii = 0; ii < numSpecs; ++ii) {
InfoValue *source = values + ii;
if (!source->isSet) {
continue;
}
const char *key = specs[ii].name;
int type = specs[ii].type;
if (type == InfoField_WholeSum || type == InfoField_Max) {
RedisModule_ReplyKV_LongLong(reply, key, source->u.total_l);
} else if (type == InfoField_DoubleSum) {
RedisModule_ReplyKV_Double(reply, key, source->u.total_d);
} else if (type == InfoField_DoubleAverage) {
if (source->u.avg.count) {
RedisModule_ReplyKV_Double(reply, key, source->u.avg.sum / source->u.avg.count);
} else {
RedisModule_ReplyKV_Double(reply, key, 0);
}
} else {
RedisModule_ReplyKV_Null(reply, key);
}
}
}
static void generateFieldsReply(InfoFields *fields, RedisModule_Reply *reply) {
RedisModule_Reply_Map(reply);
// Respond with the name, schema, and options
if (fields->indexName) {
RedisModule_ReplyKV_StringBuffer(reply, "index_name", fields->indexName, fields->indexNameLen);
}
if (fields->indexDef) {
RedisModule_ReplyKV_MRReply(reply, "index_definition", fields->indexDef);
}
if (fields->indexSchema) {
RedisModule_ReplyKV_MRReply(reply, "attributes", fields->indexSchema);
}
if (fields->indexOptions) {
RedisModule_ReplyKV_MRReply(reply, "index_options", fields->indexOptions);
}
RedisModule_ReplyKV_Map(reply, "gc_stats");
replyKvArray(reply, fields, fields->gcValues, gcSpecs, NUM_GC_FIELDS_SPEC);
RedisModule_Reply_MapEnd(reply);
RedisModule_ReplyKV_Map(reply, "cursor_stats");
replyKvArray(reply, fields, fields->cursorValues, cursorSpecs, NUM_CURSOR_FIELDS_SPEC);
RedisModule_Reply_MapEnd(reply);
RedisModule_ReplyKV_Map(reply, "dialect_stats");
replyKvArray(reply, fields, fields->dialectValues, dialectSpecs, NUM_DIALECT_FIELDS_SPEC);
RedisModule_Reply_MapEnd(reply);
replyKvArray(reply, fields, fields->toplevelValues, toplevelSpecs_g, NUM_FIELDS_SPEC);
// Global index error stats
RedisModule_Reply_SimpleString(reply, IndexError_ObjectName);
IndexError_Reply(&fields->indexError, reply, 0);
if (fields->fieldSpecInfo_arr) {
RedisModule_ReplyKV_Array(reply, "field statistics"); //Field statistics
for (size_t i = 0; i < array_len(fields->fieldSpecInfo_arr); ++i) {
FieldSpecInfo_Reply(&fields->fieldSpecInfo_arr[i], reply, 0);
}
RedisModule_Reply_ArrayEnd(reply); // >Field statistics
}
RedisModule_Reply_MapEnd(reply);
}
int InfoReplyReducer(struct MRCtx *mc, int count, MRReply **replies) {
// Summarize all aggregate replies
InfoFields fields = { .indexError = IndexError_Init() };
size_t numErrored = 0;
MRReply *firstError = NULL;
RedisModuleCtx *ctx = MRCtx_GetRedisCtx(mc);
if (count == 0) {
return RedisModule_ReplyWithError(ctx, "ERR no responses received");
}
RedisModule_Reply _reply = RedisModule_NewReply(ctx), *reply = &_reply;
for (size_t ii = 0; ii < count; ++ii) {
int type = MRReply_Type(replies[ii]);
if (type == MR_REPLY_ERROR) {
if (!fields.errorIndexes) {
fields.errorIndexes = rm_calloc(count, sizeof(*fields.errorIndexes));
}
fields.errorIndexes[ii] = 1;
numErrored++;
if (!firstError) {
firstError = replies[ii];
}
continue;
}
if (type != MR_REPLY_ARRAY && type != MR_REPLY_MAP) {
continue; // Ooops!
}
size_t numElems = MRReply_Length(replies[ii]);
if (numElems % 2 != 0) {
printf("Uneven INFO Reply!!!?\n");
}
processKvArray(&fields, replies[ii], fields.toplevelValues, toplevelSpecs_g, NUM_FIELDS_SPEC, 0);
}
// Now we've received all the replies.
if (numErrored == count) {
// Reply with error
MR_ReplyWithMRReply(reply, firstError);
} else {
generateFieldsReply(&fields, reply);
}
cleanInfoReply(&fields);
RedisModule_EndReply(reply);
return REDISMODULE_OK;
}