include/punk_observe/po_qexec.h

filter: Statementstmt 96.0% 192 / 200 Branchbran 85.0% 182 / 214 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.3% 374 / 414 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_labels.h ↑ Summary include/punk_observe/po_query.h »
1 /* po_qexec.h - planning, refusing, and executing in steps.
2 *
3 * A QUERY THAT BLOCKS THE LOOP TAKES THE WHOLE WORKER WITH IT.
4 *
5 * A Hyperman worker holds hundreds of connections. A query that scans two
6 * gigabytes synchronously stalls every one of them, and the operator's
7 * experience is that the service froze because somebody opened a dashboard.
8 *
9 * So this is a RESUMABLE STATE MACHINE, not a function. po_qexec_step
10 * processes at most `budget` rows and returns PO_Q_MORE; the caller drives it
11 * from a zero-second timer so the event loop runs in between.
12 */
13 #ifndef PO_QEXEC_H
14 #define PO_QEXEC_H
15  
16 #include "punk_observe/po_compat.h"
17 #include "punk_observe/po_query.h"
18 #include "punk_observe/po_expr.h"
19 #include "punk_observe/po_result.h"
20  
21 #define PO_Q_MORE 0
22 #define PO_Q_DONE 1
23 #define PO_Q_ERR 2
24  
25 /* ---- planning --------------------------------------------------------------
26 *
27 * FOUR DECISIONS, AND THE FOURTH IS REFUSING.
28 *
29 * A refused query with an actionable message is a better product than a
30 * thirty-second one, and far better than a timeout. So the planner estimates
31 * what a query will cost and, over budget, says what to add rather than what
32 * went wrong.
33 */
34 typedef struct {
35 po_u64 max_rows; /* the scan budget; 0 = unlimited */
36 po_u64 rows_available; /* what the store says is in range */
37 } po_budget;
38  
39 #define PO_PLAN_MAX_WHERE 8
40  
41 typedef struct {
42 const po_query *q;
43  
44 /* pushed-down predicates, in evaluation order.
45 *
46 * EVERY where, not the first one. The selector and each `| where` stage
47 * land here and a row must pass ALL of them - they read as a pipeline
48 * of filters, so they are a conjunction. The single-slot version kept
49 * the first and SILENTLY DROPPED the rest, so
50 * `log {service="api"} | where severity >= error` answered with every
51 * severity - a wrong answer in the right shape, from the two spellings
52 * an idiomatic query combines. `where` stays as the first entry for
53 * the pushdown walk; wheres[] is what the executor evaluates. */
54 const po_expr *where;
55 const po_expr *wheres[PO_PLAN_MAX_WHERE];
56 int nwhere;
57 const char *search; size_t search_len;
58  
59 int agg, has_agg;
60 const char *by[8]; size_t by_len[8]; int nby;
61  
62 /* THE BUCKET IS JUST ANOTHER GROUPING DIMENSION.
63 *
64 * It is prepended to the group key rather than given a structure of its
65 * own, so every aggregate composes with it for free - including the
66 * percentiles, which need the samples of each bucket kept separately and
67 * would otherwise need their whole reservoir story written a second time.
68 *
69 * BOUNDARIES ARE ALIGNED TO THE EPOCH, NOT TO THE QUERY.
70 *
71 * Aligning bucket 0 to the start of the window is the obvious choice and
72 * the wrong one: panning a chart by thirty seconds would move every
73 * boundary, so the same minute of traffic reports a different number
74 * before and after, and two panels on one dashboard whose ranges differ
75 * slightly disagree about the same data with nothing to explain it.
76 *
77 * At multiples of the window a bucket means the same span whoever asks,
78 * which is the same reasoning block_start already uses for retention. It
79 * also means the executor needs no window: the row's own t is enough. */
80 po_u64 bucket_ns; /* 0 when the query is not bucketed */
81 int bucket_rate; /* divide by the window: rate(), not bucket() */
82 po_u64 limit;
83 po_u64 slowest;
84 po_u64 topn; /* `top N by <agg>`: 0 when the stage is absent */
85 int sort_desc;
86 const char *sort_field; size_t sort_field_len;
87 int rekey; /* the last re-keying stage seen */
88  
89 char err[256];
90 int refused;
91 } po_plan;
92  
93 16 static void po_plan_refuse(po_plan *p, const char *why, const char *fix) {
94 16 size_t n = 0, i;
95 718 ▶ for (i = 0; why[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = why[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; why[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = why[i]; 702 16 718
for (i = 0; why[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = why[i]; 702 0 702
96 16 ▶ if (fix) {
Branch: 1 branch
BranchTrueFalseTotal
if (fix) { 16 0 16
97 16 const char *sep = " - try ";
98 128 ▶ for (i = 0; sep[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = sep[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; sep[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = sep[i]; 112 16 128
for (i = 0; sep[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = sep[i]; 112 0 112
99 962 ▶ for (i = 0; fix[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = fix[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; fix[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = fix[i]; 946 16 962
for (i = 0; fix[i] && n < sizeof(p->err) - 1; i++) p->err[n++] = fix[i]; 946 0 946
100 }
101 16 p->err[n] = '\0';
102 16 p->refused = 1;
103 16 }
104  
105 1502 static int po_plan_build(po_plan *p, const po_query *q, const po_budget *b) {
106 const po_stage *s;
107  
108 1502 memset(p, 0, sizeof(*p));
109 1502 p->q = q;
110 1502 ▶ if (q->selector) { /* the selector is a leading where */
Branch: 1 branch
BranchTrueFalseTotal
if (q->selector) { /* the selector is a leading where */ 2 1500 1502
111 2 p->where = q->selector;
112 2 p->wheres[p->nwhere++] = q->selector;
113 }
114  
115 3091 ▶ for (s = q->stages; s; s = s->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = q->stages; s; s = s->next) { 1590 1501 3091
116 1590 switch (s->kind) {
117 249 case PO_ST_WHERE:
118 /* ALL of them - see the struct comment. More than the array
119 * holds is refused by name, never silently dropped: a
120 * dropped filter is a wrong answer in the right shape. */
121 249 ▶ if (p->nwhere >= PO_PLAN_MAX_WHERE) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->nwhere >= PO_PLAN_MAX_WHERE) { 1 248 249
122 1 po_plan_refuse(p,
123 "too many where stages",
124 "combine them with `and` - eight filters is the "
125 "most one query carries");
126 1 return 0;
127 }
128 248 ▶ if (!p->where) p->where = s->expr;
Branch: 1 branch
BranchTrueFalseTotal
if (!p->where) p->where = s->expr; 226 22 248
129 248 p->wheres[p->nwhere++] = s->expr;
130 248 break;
131 631 case PO_ST_SEARCH:
132 631 p->search = s->str; p->search_len = s->str_len;
133 631 break;
134 /* `by` IS ITS OWN STAGE, and the planner has to read it.
135 *
136 * The language offers two spellings of the same thing - a
137 * trailing `count by service` and a separate `| by service |
138 * count` - and only the first was ever collected, off the
139 * aggregate's own field list. The second parsed cleanly, planned
140 * to nothing, and returned ONE group with an empty key: the right
141 * total, presented as though the grouping had been applied and
142 * every row had happened to fall in the same bucket.
143 *
144 * A wrong answer that is the right shape, on the stage whose
145 * whole purpose is to split the answer up. */
146 625 case PO_ST_BY:
147 case PO_ST_AGG:
148 case PO_ST_BUCKET:
149 case PO_ST_RATE: {
150 int i;
151 625 ▶ if (s->kind != PO_ST_BY) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->kind != PO_ST_BY) { 591 34 625
152 591 p->has_agg = 1;
153 591 ▶ if (s->kind == PO_ST_AGG) p->agg = s->agg;
Branch: 1 branch
BranchTrueFalseTotal
if (s->kind == PO_ST_AGG) p->agg = s->agg; 99 492 591
154 }
155 /* THE WINDOW WAS PARSED AND THEN DROPPED ON THE FLOOR.
156 *
157 * `rate` reached here, set has_agg, collected its `by` fields
158 * and never once looked at s->dur - so `rate(5m) by status`
159 * was a plain count over the whole range, wearing the name of
160 * something time-binned. The grammar advertised it, the POD's
161 * own synopsis used it, and no test ever executed one. */
162 625 ▶ if (s->kind == PO_ST_BUCKET || s->kind == PO_ST_RATE) {
Branch: 2 branches
BranchTrueFalseTotal
if (s->kind == PO_ST_BUCKET || s->kind == PO_ST_RATE) { 139 486 625
if (s->kind == PO_ST_BUCKET || s->kind == PO_ST_RATE) { 6 133 139
163 492 p->bucket_ns = s->dur;
164 492 p->bucket_rate = (s->kind == PO_ST_RATE);
165 /* Its own aggregate wins; otherwise count, but only if no
166 * other stage has named one. `| p95 | bucket(1m)` is the
167 * p95 per bucket, not a row count wearing its name. A
168 * later `| p95` overwrites this in its own turn. */
169 492 ▶ if (s->agg) p->agg = s->agg;
Branch: 1 branch
BranchTrueFalseTotal
if (s->agg) p->agg = s->agg; 478 14 492
170 14 ▶ else if (!p->agg) p->agg = PO_AGG_COUNT;
Branch: 1 branch
BranchTrueFalseTotal
else if (!p->agg) p->agg = PO_AGG_COUNT; 13 1 14
171 }
172 1151 ▶ for (i = 0; i < s->nfields && p->nby < 8; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < s->nfields && p->nby < 8; i++) { 526 625 1151
for (i = 0; i < s->nfields && p->nby < 8; i++) { 526 0 526
173 526 p->by[p->nby] = s->fields[i];
174 526 p->by_len[p->nby] = s->field_lens[i];
175 526 p->nby++;
176 }
177 625 break;
178 }
179 30 case PO_ST_LIMIT: p->limit = s->num; break;
180 7 case PO_ST_SLOWEST: p->slowest = s->num; break;
181 /* `top N by <agg>` PLANNED TO NOTHING until now: the stage was
182 * parsed, validated and named, and had no case here - so the
183 * query was accepted and then neither ranked nor limited, and the
184 * answer came back with the right shape and the wrong contents.
185 *
186 * The grouping comes from a preceding `| by`; this stage supplies
187 * the aggregate to rank on and how many to keep. Its aggregate
188 * wins, as `by`'s does, so `by service | top 5 by count` ranks on
189 * count whatever came before it. */
190 8 case PO_ST_TOPN:
191 8 p->topn = s->num;
192 /* AN AGGREGATING STAGE, which is what makes the answer a set
193 * of groups rather than rows. Setting p->agg without this
194 * leaves has_agg clear, the shape stays ROWS, and `by service
195 * | top 5 by count` comes back as every matching span. */
196 8 p->has_agg = 1;
197 8 ▶ if (s->agg) p->agg = s->agg;
Branch: 1 branch
BranchTrueFalseTotal
if (s->agg) p->agg = s->agg; 8 0 8
198 0 ▶ else if (!p->agg) p->agg = PO_AGG_COUNT;
Branch: 1 branch
BranchTrueFalseTotal
else if (!p->agg) p->agg = PO_AGG_COUNT; 0 0 0
199 8 break;
200 10 case PO_ST_SORT:
201 10 p->sort_field = s->fields[0];
202 10 p->sort_field_len = s->field_lens[0];
203 10 p->sort_desc = s->desc;
204 10 break;
205 30 case PO_ST_EXEMPLARS:
206 case PO_ST_TRACES:
207 case PO_ST_LOGS:
208 case PO_ST_SPANS:
209 30 p->rekey = s->kind;
210 30 break;
211 0 default: break;
212 }
213 }
214  
215 /* THE CROSS-SIGNAL STAGES ARE EXECUTED IN A SECOND PASS, in `post_query`
216 * - the join needs to read the store again, and the executor is handed
217 * rows and cannot. `p->rekey` records that the pass is owed; the store
218 * walks the stage list itself to run the chain in order.
219 *
220 * They were refused here for a release, and the refusal was right while
221 * it stood: the stages parsed, the planner recorded the re-key and
222 * nothing consumed it, so the answer was the metric stream unchanged -
223 * the wrong rows, silently, on the query this project's overview calls
224 * its differentiator. */
225  
226 /* A pattern the matcher cannot honour is refused, not approximated -
227 * in ANY of the wheres, because each one runs. */
228 {
229 int i;
230 1732 ▶ for (i = 0; i < p->nwhere; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->nwhere; i++) { 242 1490 1732
231 242 ▶ if (po_expr_bad_pattern(p->wheres[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_expr_bad_pattern(p->wheres[i])) { 11 231 242
232 11 po_plan_refuse(p,
233 "that pattern needs a full regular expression engine",
234 "an anchored prefix like \"^api-\", a suffix, or a plain substring");
235 11 return 0;
236 }
237 }
238 }
239  
240 /* THE COST REFUSAL. Over budget, say what to ADD. The wheres are a
241 * conjunction, so a bound from any one of them bounds the query. */
242 1490 ▶ if (b && b->max_rows && b->rows_available > b->max_rows) {
Branch: 3 branches
BranchTrueFalseTotal
if (b && b->max_rows && b->rows_available > b->max_rows) { 485 1005 1490
if (b && b->max_rows && b->rows_available > b->max_rows) { 360 125 485
if (b && b->max_rows && b->rows_available > b->max_rows) { 4 356 360
243 4 int i, bounded = 0, selective = 0;
244 7 ▶ for (i = 0; i < p->nwhere; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->nwhere; i++) { 3 4 7
245 3 ▶ if (po_expr_time_bounded(p->wheres[i])) bounded = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (po_expr_time_bounded(p->wheres[i])) bounded = 1; 2 1 3
246 3 ▶ if (po_expr_selective(p->wheres[i])) selective = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (po_expr_selective(p->wheres[i])) selective = 1; 1 2 3
247 }
248 4 ▶ if (!bounded)
Branch: 1 branch
BranchTrueFalseTotal
if (!bounded) 2 2 4
249 2 po_plan_refuse(p, "this query would scan too much",
250 "narrowing the time range, as in | where t > ...");
251 2 ▶ else if (!selective)
Branch: 1 branch
BranchTrueFalseTotal
else if (!selective) 1 1 2
252 1 po_plan_refuse(p, "this query would scan too much",
253 "an equality filter, as in | where service = \"...\"");
254 else
255 1 po_plan_refuse(p, "this query would scan too much",
256 "a shorter time range or a narrower filter");
257 4 return 0;
258 }
259 1486 return 1;
260 }
261  
262 /* ---- the resumable executor ----------------------------------------------- */
263  
264 typedef struct {
265 const po_plan *plan;
266 const po_row *rows; /* the scan's input */
267 po_u64 n;
268 po_u64 i; /* the cursor: THIS is what makes it resumable */
269 po_u64 budget; /* rows per step */
270 po_u64 hard_max; /* total rows before truncating */
271 po_result *res;
272 int done;
273 } po_qexec;
274  
275 499 static void po_qexec_init(po_qexec *x, const po_plan *p, const po_row *rows,
276 po_u64 n, po_result *res, po_u64 budget,
277 po_u64 hard_max) {
278 499 memset(x, 0, sizeof(*x));
279 499 x->plan = p; x->rows = rows; x->n = n; x->res = res;
280 499 ▶ x->budget = budget ? budget : 1024;
Branch: 1 branch
BranchTrueFalseTotal
x->budget = budget ? budget : 1024; 19 480 499
281 499 x->hard_max = hard_max;
282 499 ▶ res->shape = p->has_agg ? PO_RES_SERIES : PO_RES_ROWS;
Branch: 1 branch
BranchTrueFalseTotal
res->shape = p->has_agg ? PO_RES_SERIES : PO_RES_ROWS; 328 171 499
283 /* A BUCKETED QUERY IS NEVER A SCALAR, however few fields it groups by.
284 * `log | bucket(1m)` has no `by` at all and is still a line over time;
285 * collapsing it to one number because nby is zero would answer a chart
286 * with a total. */
287 499 ▶ if (p->has_agg && p->nby == 0 && !p->bucket_ns) res->shape = PO_RES_SCALAR;
Branch: 3 branches
BranchTrueFalseTotal
if (p->has_agg && p->nby == 0 && !p->bucket_ns) res->shape = PO_RES_SCALAR; 328 171 499
if (p->has_agg && p->nby == 0 && !p->bucket_ns) res->shape = PO_RES_SCALAR; 51 277 328
if (p->has_agg && p->nby == 0 && !p->bucket_ns) res->shape = PO_RES_SCALAR; 17 34 51
288 499 res->bucket_ns = p->bucket_ns;
289 499 }
290  
291 /* Build the group key from the `by` fields of a row.
292 *
293 * A FIELD WITH NO STRING FORM IS STILL A FIELD. `severity`, `duration`,
294 * `status` and `t` are numbers on the row, and po_row_str does not know about
295 * them - so `by severity` produced an empty key for every row, which is one
296 * bucket containing everything, labelled with nothing. The right total, split
297 * the way somebody asked for, except not split at all.
298 *
299 * So a field the string accessor cannot answer is tried as a number and
300 * rendered. Integral values print without a fraction, because "17" is the
301 * severity and "17.0000" is a rendering artefact somebody would then have to
302 * explain. */
303 64467 static size_t po_group_key(const po_plan *p, const po_row *r,
304 char *out, size_t cap) {
305 64467 size_t n = 0;
306 int i;
307  
308 /* The bucket goes FIRST, and under its own separator. The reader that
309 * splits this back apart has to tell "the bucket, then the fields" from
310 * "a first field that happens to contain a separator", and one delimiter
311 * for both jobs cannot say which it is. */
312 64467 ▶ if (p->bucket_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->bucket_ns) { 47517 16950 64467
313 char buf[24];
314 47517 int len = snprintf(buf, sizeof(buf), "%llu",
315 47517 (unsigned long long)(r->t / p->bucket_ns));
316 int k;
317 395139 ▶ for (k = 0; k < len && n < cap; k++) out[n++] = buf[k];
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < len && n < cap; k++) out[n++] = buf[k]; 347622 47517 395139
for (k = 0; k < len && n < cap; k++) out[n++] = buf[k]; 347622 0 347622
318 47517 ▶ if (n < cap) out[n++] = '\x1e';
Branch: 1 branch
BranchTrueFalseTotal
if (n < cap) out[n++] = '\x1e'; 47517 0 47517
319 }
320  
321 101460 ▶ for (i = 0; i < p->nby; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < p->nby; i++) { 36993 64467 101460
322 36993 size_t vl = 0;
323 36993 const char *v = po_row_str(r, p->by[i], p->by_len[i], &vl);
324 36993 ▶ if (i && n < cap) out[n++] = '\x1f';
Branch: 2 branches
BranchTrueFalseTotal
if (i && n < cap) out[n++] = '\x1f'; 1168 35825 36993
if (i && n < cap) out[n++] = '\x1f'; 1168 0 1168
325 36993 ▶ if (v && vl) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && vl) { 32028 4965 36993
if (v && vl) { 32028 0 32028
326 size_t k;
327 162508 ▶ for (k = 0; k < vl && n < cap; k++) out[n++] = v[k];
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < vl && n < cap; k++) out[n++] = v[k]; 130480 32028 162508
for (k = 0; k < vl && n < cap; k++) out[n++] = v[k]; 130480 0 130480
328 32158 continue;
329 }
330 {
331 4965 double d = 0;
332 char buf[40];
333 int len;
334 4965 ▶ if (!po_row_num(r, p->by[i], p->by_len[i], &d)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_row_num(r, p->by[i], p->by_len[i], &d)) continue; 130 4835 4965
335 4835 ▶ if (d == (double)(po_u64)d && d >= 0)
Branch: 2 branches
BranchTrueFalseTotal
if (d == (double)(po_u64)d && d >= 0) 4835 0 4835
if (d == (double)(po_u64)d && d >= 0) 4835 0 4835
336 4835 len = snprintf(buf, sizeof(buf), "%llu",
337 4835 (unsigned long long)(po_u64)d);
338 else
339 0 len = snprintf(buf, sizeof(buf), "%.6g", d);
340 4835 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 4835 0 4835
341 int k;
342 9684 ▶ for (k = 0; k < len && n < cap; k++) out[n++] = buf[k];
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < len && n < cap; k++) out[n++] = buf[k]; 4849 4835 9684
for (k = 0; k < len && n < cap; k++) out[n++] = buf[k]; 4849 0 4849
343 }
344 }
345 }
346 64467 return n;
347 }
348  
349 2857 static int po_qexec_step(po_qexec *x) {
350 2857 po_u64 processed = 0;
351 2857 const po_plan *p = x->plan;
352  
353 2857 ▶ if (x->done) return PO_Q_DONE;
Branch: 1 branch
BranchTrueFalseTotal
if (x->done) return PO_Q_DONE; 0 2857 2857
354 2857 x->res->steps++;
355  
356 90109 ▶ while (x->i < x->n && processed < x->budget) {
Branch: 2 branches
BranchTrueFalseTotal
while (x->i < x->n && processed < x->budget) { 89638 471 90109
while (x->i < x->n && processed < x->budget) { 87280 2358 89638
357 87280 const po_row *r = &x->rows[x->i++];
358 87280 processed++;
359 87280 x->res->scanned_rows++;
360 87280 x->res->scanned_bytes += (po_u64)(r->body_len + r->service_len + 64);
361  
362 /* THE METRIC NAME SELECTS THE SERIES, and until now it did not.
363 *
364 * `metric http.server.duration` parsed, stored the name in the AST at
365 * po_parse.h:619, and no planner or executor code ever read it - so
366 * the source verb chose the SIGNAL and nothing chose the metric. Every
367 * metric query returned every metric point in the store: a chart of
368 * one metric drawn from all of them, and `metric nosuch` answering
369 * with somebody else's data rather than with nothing.
370 *
371 * It stayed invisible because a store with one metric name in it
372 * cannot show the difference, which is what a demo has and what a
373 * production store never does.
374 *
375 * The name lives in the row's `body`, which for a metric record is
376 * the metric name. Compared exactly: a prefix match would make
377 * `punk.health.ok` also answer for `punk.health.ok_total`. */
378 87280 ▶ if (p->q && p->q->name_len && r->kind == PO_METRIC) {
Branch: 3 branches
BranchTrueFalseTotal
if (p->q && p->q->name_len && r->kind == PO_METRIC) { 87280 0 87280
if (p->q && p->q->name_len && r->kind == PO_METRIC) { 5649 81631 87280
if (p->q && p->q->name_len && r->kind == PO_METRIC) { 5649 0 5649
379 5649 ▶ if (r->body_len != p->q->name_len
Branch: 1 branch
BranchTrueFalseTotal
if (r->body_len != p->q->name_len 5643 6 5649
380 5649 ▶ || memcmp(r->body, p->q->name, p->q->name_len) != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
|| memcmp(r->body, p->q->name, p->q->name_len) != 0) continue; 615 5028 5643
381 }
382  
383 { /* Every where, in order: they are a conjunction (see po_plan). */
384 86659 int wi, drop = 0;
385 88661 ▶ for (wi = 0; wi < p->nwhere; wi++)
Branch: 1 branch
BranchTrueFalseTotal
for (wi = 0; wi < p->nwhere; wi++) 7561 81100 88661
386 7561 ▶ if (!po_eval(p->wheres[wi], r)) { drop = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_eval(p->wheres[wi], r)) { drop = 1; break; } 5559 2002 7561
387 86659 ▶ if (drop) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (drop) continue; 5559 81100 86659
388 }
389  
390 81100 ▶ if (p->search_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->search_len) { 1753 79347 81100
391 /* The bloom prunes blocks; here the survivor is matched EXACTLY,
392 * case-folded to agree with how the filter was built. */
393 3506 if (!r->body || !po_memfind(r->body, r->body_len,
394 3110 p->search, p->search_len)) continue;
395 }
396  
397 /* THE ROW HAS SURVIVED EVERY FILTER, and this is the only moment the
398 * cross-signal jump can see it: an aggregate consumes the row a line
399 * below, so a jump that waited for the result would find buckets and
400 * no ids at all. Collected only when a re-key was asked for. */
401 79743 ▶ if (p->rekey && !po_result_exemplar(x->res, r->trace_hi, r->trace_lo))
Branch: 2 branches
BranchTrueFalseTotal
if (p->rekey && !po_result_exemplar(x->res, r->trace_hi, r->trace_lo)) 4217 75526 79743
if (p->rekey && !po_result_exemplar(x->res, r->trace_hi, r->trace_lo)) 0 4217 4217
402 0 return PO_Q_ERR;
403  
404 79743 ▶ if (p->has_agg) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->has_agg) { 64467 15276 79743
405 char key[128];
406 64467 size_t klen = po_group_key(p, r, key, sizeof(key));
407 64467 po_group *g = po_result_group(x->res, key, klen);
408 64467 double v = 0;
409 64467 ▶ if (!g) return PO_Q_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return PO_Q_ERR; 0 64467 64467
410 /* Checked AFTER the lookup, so it fires only when a new group
411 * took it over the line. Testing before would refuse a query that
412 * has exactly the cap and whose remaining rows all land in groups
413 * it already has. */
414 64467 ▶ if (x->res->ng > PO_MAX_GROUPS) {
Branch: 1 branch
BranchTrueFalseTotal
if (x->res->ng > PO_MAX_GROUPS) { 1 64466 64467
415 1 x->res->too_many_groups = 1;
416 1 x->done = 1;
417 1 break;
418 }
419 64466 g->count++;
420 64466 switch (p->agg) {
421 60199 case PO_AGG_COUNT: g->value = (double)g->count; break;
422 4264 case PO_AGG_SUM: case PO_AGG_AVG:
423 case PO_AGG_MIN: case PO_AGG_MAX:
424 case PO_AGG_P50: case PO_AGG_P90:
425 case PO_AGG_P95: case PO_AGG_P99: {
426 4264 const char *f = "value"; size_t fl = 5;
427 4264 ▶ if (r->kind == PO_SPAN) { f = "duration"; fl = 8; }
Branch: 1 branch
BranchTrueFalseTotal
if (r->kind == PO_SPAN) { f = "duration"; fl = 8; } 3897 367 4264
428 367 ▶ else if (r->kind == PO_LOG) { f = "severity"; fl = 8; }
Branch: 1 branch
BranchTrueFalseTotal
else if (r->kind == PO_LOG) { f = "severity"; fl = 8; } 0 367 367
429 4264 ▶ if (po_row_num(r, f, fl, &v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_row_num(r, f, fl, &v)) { 4264 0 4264
430 4264 ▶ if (!po_group_sample(g, v)) return PO_Q_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_group_sample(g, v)) return PO_Q_ERR; 0 4264 4264
431 4264 ▶ if (p->agg == PO_AGG_SUM) g->value += v;
Branch: 1 branch
BranchTrueFalseTotal
if (p->agg == PO_AGG_SUM) g->value += v; 2 4262 4264
432 4262 ▶ else if (p->agg == PO_AGG_MIN)
Branch: 1 branch
BranchTrueFalseTotal
else if (p->agg == PO_AGG_MIN) 555 3707 4262
433 555 ▶ g->value = (g->count == 1 || v < g->value) ? v : g->value;
Branch: 2 branches
BranchTrueFalseTotal
g->value = (g->count == 1 || v < g->value) ? v : g->value; 325 230 555
g->value = (g->count == 1 || v < g->value) ? v : g->value; 313 12 325
434 3707 ▶ else if (p->agg == PO_AGG_MAX)
Branch: 1 branch
BranchTrueFalseTotal
else if (p->agg == PO_AGG_MAX) 785 2922 3707
435 785 ▶ g->value = (g->count == 1 || v > g->value) ? v : g->value;
Branch: 2 branches
BranchTrueFalseTotal
g->value = (g->count == 1 || v > g->value) ? v : g->value; 669 116 785
g->value = (g->count == 1 || v > g->value) ? v : g->value; 464 205 669
436 }
437 4264 break;
438 }
439 3 case PO_AGG_DISTINCT: g->value = (double)x->res->ng; break;
440 0 default: g->value = (double)g->count; break;
441 }
442 }
443 else {
444 15276 ▶ if (!po_result_row(x->res, r)) return PO_Q_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_result_row(x->res, r)) return PO_Q_ERR; 0 15276 15276
445 }
446  
447 /* THE HARD CAP. Over it the scan stops and `truncated` is set - the
448 * partial answer is the correct PREFIX of the real one, and the
449 * result says so rather than looking complete. */
450 79742 ▶ if (x->hard_max && x->res->scanned_rows >= x->hard_max) {
Branch: 2 branches
BranchTrueFalseTotal
if (x->hard_max && x->res->scanned_rows >= x->hard_max) { 25807 53935 79742
if (x->hard_max && x->res->scanned_rows >= x->hard_max) { 27 25780 25807
451 27 x->res->truncated = 1;
452 27 x->done = 1;
453 27 break;
454 }
455 }
456  
457 2857 ▶ if (x->i >= x->n) x->done = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (x->i >= x->n) x->done = 1; 496 2361 2857
458 2857 ▶ if (!x->done) return PO_Q_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (!x->done) return PO_Q_MORE; 2358 499 2857
459  
460 /* ORDERING, and it happens HERE - before the limit is applied, which is
461 * the whole point.
462 *
463 * `slowest 20` and `sort` were both recorded by the planner and then used
464 * by nobody: `slowest` acted as a bare limit and `sort` did nothing at
465 * all. Taking the first twenty of an unordered scan does not give the
466 * slowest twenty, it gives whichever twenty the scan reached first - and
467 * because the store hands rows over newest first, `slowest` returned the
468 * NEWEST twenty while looking exactly like it had worked.
469 *
470 * A wrong answer that is the right SHAPE is the worst kind: nothing in
471 * the result says it is not ordered, and the reader is looking at the
472 * screen precisely because they want the extreme. */
473 499 ▶ if (x->res->shape == PO_RES_ROWS && x->res->nrow > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (x->res->shape == PO_RES_ROWS && x->res->nrow > 1) { 171 328 499
if (x->res->shape == PO_RES_ROWS && x->res->nrow > 1) { 113 58 171
474 113 ▶ if (p->slowest) po_rows_sort_dur(x->res->row, x->res->nrow);
Branch: 1 branch
BranchTrueFalseTotal
if (p->slowest) po_rows_sort_dur(x->res->row, x->res->nrow); 3 110 113
475 110 ▶ else if (p->sort_field)
Branch: 1 branch
BranchTrueFalseTotal
else if (p->sort_field) 7 103 110
476 7 po_rows_sort_field(x->res->row, x->res->nrow,
477 7 p->sort_field, p->sort_field_len, p->sort_desc);
478 }
479  
480 /* finish: the aggregates that need every sample */
481 499 ▶ if (p->has_agg) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->has_agg) { 328 171 499
482 uint32_t i;
483 11972 ▶ for (i = 0; i < x->res->ng; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->res->ng; i++) { 11644 328 11972
484 11644 po_group *g = &x->res->g[i];
485 11644 switch (p->agg) {
486 3 case PO_AGG_AVG: {
487 3 double s = 0; uint32_t k;
488 303 ▶ for (k = 0; k < g->nsamp; k++) s += g->samples[k];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < g->nsamp; k++) s += g->samples[k]; 300 3 303
489 3 ▶ g->value = g->nsamp ? s / g->nsamp : 0;
Branch: 1 branch
BranchTrueFalseTotal
g->value = g->nsamp ? s / g->nsamp : 0; 3 0 3
490 3 break;
491 }
492 1 case PO_AGG_P50: g->value = po_percentile(g, 0.50, &x->res->exact); break;
493 0 case PO_AGG_P90: g->value = po_percentile(g, 0.90, &x->res->exact); break;
494 17 case PO_AGG_P95: g->value = po_percentile(g, 0.95, &x->res->exact); break;
495 1 case PO_AGG_P99: g->value = po_percentile(g, 0.99, &x->res->exact); break;
496 11622 default: break;
497 }
498 }
499  
500 /* RATE IS THE AGGREGATE DIVIDED BY THE SPAN, and it is done here
501 * rather than per row because the aggregate is not final until the
502 * loop above has run: dividing as we went would divide a running
503 * count and then divide it again on the next row.
504 *
505 * Only count and sum are rates. A p95 per second is not a quantity -
506 * it is a latency, and dividing it by three hundred would report a
507 * service as getting faster because somebody widened the bucket.
508 */
509 /* THE RANKING, and it has to be here rather than earlier: the
510 * aggregate is not final until the loop above has run, so ordering
511 * on it before that would rank on a running count. */
512 328 ▶ if (p->topn && x->res->ng > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (p->topn && x->res->ng > 1) { 5 323 328
if (p->topn && x->res->ng > 1) { 5 0 5
513 /* An insertion sort on the group array. The group count is
514 * bounded by the cardinality cap long before it is large, and
515 * this keeps the comparison in one readable place. Descending by
516 * value; ties keep the order they arrived in, so the answer is
517 * at least stable between two runs over the same segments. */
518 uint32_t a;
519 19 ▶ for (a = 1; a < x->res->ng; a++) {
Branch: 1 branch
BranchTrueFalseTotal
for (a = 1; a < x->res->ng; a++) { 14 5 19
520 14 po_group k = x->res->g[a];
521 14 uint32_t b = a;
522 14 ▶ while (b > 0 && x->res->g[b - 1].value < k.value) {
Branch: 2 branches
BranchTrueFalseTotal
while (b > 0 && x->res->g[b - 1].value < k.value) { 14 0 14
while (b > 0 && x->res->g[b - 1].value < k.value) { 0 14 14
523 0 x->res->g[b] = x->res->g[b - 1];
524 0 b--;
525 }
526 14 x->res->g[b] = k;
527 }
528 }
529 328 ▶ if (p->topn && x->res->ng > (uint32_t)p->topn)
Branch: 2 branches
BranchTrueFalseTotal
if (p->topn && x->res->ng > (uint32_t)p->topn) 5 323 328
if (p->topn && x->res->ng > (uint32_t)p->topn) 4 1 5
530 4 x->res->ng = (uint32_t)p->topn;
531  
532 328 ▶ if (p->bucket_rate
Branch: 1 branch
BranchTrueFalseTotal
if (p->bucket_rate 5 323 328
533 5 ▶ && (p->agg == PO_AGG_COUNT || p->agg == PO_AGG_SUM)) {
Branch: 2 branches
BranchTrueFalseTotal
&& (p->agg == PO_AGG_COUNT || p->agg == PO_AGG_SUM)) { 1 4 5
&& (p->agg == PO_AGG_COUNT || p->agg == PO_AGG_SUM)) { 0 1 1
534 4 const double secs = (double)p->bucket_ns / 1e9;
535 4 ▶ if (secs > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (secs > 0) { 4 0 4
536 uint32_t i;
537 38 ▶ for (i = 0; i < x->res->ng; i++) x->res->g[i].value /= secs;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < x->res->ng; i++) x->res->g[i].value /= secs; 34 4 38
538 }
539 }
540 }
541 499 return PO_Q_DONE;
542 }
543  
544 /* Run to completion, counting the yields. Production drives the steps from a
545 * timer; this is for a caller that genuinely wants to block. */
546 481 static int po_qexec_run(po_qexec *x) {
547 int s;
548 524 ▶ do { s = po_qexec_step(x); } while (s == PO_Q_MORE);
Branch: 1 branch
BranchTrueFalseTotal
do { s = po_qexec_step(x); } while (s == PO_Q_MORE); 43 481 524
549 481 return s;
550 }
551  
552 #endif /* PO_QEXEC_H */
« include/punk_observe/po_labels.h ↑ Summary include/punk_observe/po_query.h »