xs/exec.xs

filter: Statementstmt 100% 162 / 162 Branchbran 75.8% 94 / 124 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.5% 256 / 286 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_result.h ↑ Summary include/punk_observe/po_labels.h »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Exec PREFIX = poe_
2  
3 # Execute a query over seeded rows.
4 #
5 # Rows come from Perl here; in production the scan operator reads them from an
6 # mmap'd segment. Nothing above the scan changes, which is the point of the
7 # one-row-shape decision in phase 0.
8 void
9 poe_run(SV *query, SV *rows, SV *opts)
10 PPCODE:
11 {
12 po_query q;
13 po_plan plan;
14 po_budget budget;
15 po_result res;
16 po_qexec x;
17 499 po_row *rv = NULL;
18 499 SV **keep = NULL;
19 499 IV nrows = 0;
20 STRLEN qlen;
21 const char *qp;
22 499 HV *o = NULL;
23 499 po_u64 step_budget = 0, hard_max = 0;
24 int status;
25  
26 499 qp = SvPV(query, qlen);
27 499 ▶ if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) o = (HV *)SvRV(opts);
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) o = (HV *)SvRV(opts); 499 0 499
if (SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) o = (HV *)SvRV(opts); 499 0 499
28  
29 499 memset(&budget, 0, sizeof(budget));
30 499 ▶ if (o) {
Branch: 1 branch
BranchTrueFalseTotal
if (o) { 499 0 499
31 SV **f;
32 499 ▶ if ((f = hv_fetchs(o, "max_rows", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "max_rows", 0))) 360 139 499
33 360 (void)po_sv_to_u64(aTHX_ *f, &budget.max_rows);
34 499 ▶ if ((f = hv_fetchs(o, "rows_available", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "rows_available", 0))) 361 138 499
35 361 (void)po_sv_to_u64(aTHX_ *f, &budget.rows_available);
36 499 ▶ if ((f = hv_fetchs(o, "step", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "step", 0))) 357 142 499
37 357 (void)po_sv_to_u64(aTHX_ *f, &step_budget);
38 499 ▶ if ((f = hv_fetchs(o, "hard_max", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(o, "hard_max", 0))) 357 142 499
39 357 (void)po_sv_to_u64(aTHX_ *f, &hard_max);
40 }
41  
42 499 ▶ if (!po_parse(&q, qp, (size_t)qlen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_parse(&q, qp, (size_t)qlen)) { 2 497 499
43 2 HV *r = newHV();
44 2 hv_stores(r, "ok", newSViv(0));
45 2 hv_stores(r, "error", newSVpv(q.err, 0));
46 2 hv_stores(r, "stage", newSVpvs("parse"));
47 2 po_query_free(&q);
48 2 ▶ mXPUSHs(newRV_noinc((SV *)r));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)r)); 0 2 2
49 18 XSRETURN(1);
50 }
51  
52 497 ▶ if (!po_plan_build(&plan, &q, &budget)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_plan_build(&plan, &q, &budget)) { 16 481 497
53 16 HV *r = newHV();
54 16 hv_stores(r, "ok", newSViv(0));
55 16 hv_stores(r, "error", newSVpv(plan.err, 0));
56 16 hv_stores(r, "stage", newSVpvs("plan"));
57 16 po_query_free(&q);
58 16 ▶ mXPUSHs(newRV_noinc((SV *)r));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)r)); 0 16 16
59 16 XSRETURN(1);
60 }
61  
62 481 nrows = poe_load_rows(aTHX_ rows, &rv, &keep);
63 481 ▶ if (nrows < 0) { po_query_free(&q); croak("bad rows"); }
Branch: 1 branch
BranchTrueFalseTotal
if (nrows < 0) { po_query_free(&q); croak("bad rows"); } 0 481 481
64  
65 481 po_result_init(&res);
66 481 po_qexec_init(&x, &plan, rv, (po_u64)nrows, &res,
67 step_budget, hard_max);
68 481 status = po_qexec_run(&x);
69  
70 {
71 481 HV *r = newHV();
72 481 HV *meta = newHV();
73  
74 /* A REFUSAL, NOT A SHORT ANSWER. A bucketed chart that simply
75 * stops has a shape somebody reads as "the service went
76 * quiet", so this reports failure and says what to change.
77 *
78 * It falls through to the shared cleanup at the end of the
79 * block rather than returning here: the data is skipped, the
80 * metadata is not, because a caller asking why it was refused
81 * wants to know how much was scanned before it was. */
82 481 ▶ if (res.too_many_groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (res.too_many_groups) { 1 480 481
83 1 hv_stores(r, "ok", newSViv(0));
84 1 hv_stores(r, "error", newSVpvs(
85 "that query produces more series than can be answered"
86 " - try a wider bucket, a shorter range, or fewer"
87 " fields after `by`"));
88 1 hv_stores(r, "stage", newSVpvs("plan"));
89 }
90 else {
91  
92 480 hv_stores(r, "ok", newSViv(status == PO_Q_DONE ? 1 : 0));
93 480 ▶ hv_stores(r, "shape",
Branch: 3 branches
BranchTrueFalseTotal
hv_stores(r, "shape", 320 160 480
hv_stores(r, "shape", 66 254 320
hv_stores(r, "shape", 49 17 66
94 newSVpv(res.shape == PO_RES_ROWS ? "rows"
95 : res.bucket_ns ? "buckets"
96 : res.shape == PO_RES_SERIES ? "series" : "scalar", 0));
97  
98 480 ▶ if (res.shape == PO_RES_ROWS) {
Branch: 1 branch
BranchTrueFalseTotal
if (res.shape == PO_RES_ROWS) { 160 320 480
99 160 AV *out = newAV();
100 uint32_t i;
101 320 po_u64 lim = plan.limit ? plan.limit
102 160 ▶ : (plan.slowest ? plan.slowest : 0);
Branch: 1 branch
BranchTrueFalseTotal
: (plan.slowest ? plan.slowest : 0); 8 152 160
103 160 uint32_t n = res.nrow;
104 160 ▶ if (lim && (po_u64)n > lim) n = (uint32_t)lim;
Branch: 2 branches
BranchTrueFalseTotal
if (lim && (po_u64)n > lim) n = (uint32_t)lim; 12 148 160
if (lim && (po_u64)n > lim) n = (uint32_t)lim; 6 6 12
105 7658 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7498 160 7658
106 7498 HV *h = newHV();
107 7498 const po_row *w = &res.row[i];
108 7498 hv_stores(h, "t", po_u64_to_sv(w->t));
109 7498 ▶ if (w->body)
Branch: 1 branch
BranchTrueFalseTotal
if (w->body) 7498 0 7498
110 7498 hv_stores(h, "body", newSVpvn(w->body, w->body_len));
111 7498 ▶ if (w->service)
Branch: 1 branch
BranchTrueFalseTotal
if (w->service) 7498 0 7498
112 7498 hv_stores(h, "service", newSVpvn(w->service, w->service_len));
113 7498 ▶ if (w->kind == PO_LOG)
Branch: 1 branch
BranchTrueFalseTotal
if (w->kind == PO_LOG) 2839 4659 7498
114 2839 hv_stores(h, "severity", newSVuv((UV)w->severity));
115 7498 ▶ if (w->kind == PO_SPAN)
Branch: 1 branch
BranchTrueFalseTotal
if (w->kind == PO_SPAN) 337 7161 7498
116 337 hv_stores(h, "duration", po_u64_to_sv(w->duration));
117 7498 ▶ if (w->kind == PO_METRIC)
Branch: 1 branch
BranchTrueFalseTotal
if (w->kind == PO_METRIC) 4322 3176 7498
118 4322 hv_stores(h, "value", newSVnv((NV)w->value));
119 7498 ▶ if (w->trace_hi || w->trace_lo) {
Branch: 2 branches
BranchTrueFalseTotal
if (w->trace_hi || w->trace_lo) { 2896 4602 7498
if (w->trace_hi || w->trace_lo) { 7 2889 2896
120 4609 hv_stores(h, "trace_hi", po_u64_to_sv(w->trace_hi));
121 4609 hv_stores(h, "trace_lo", po_u64_to_sv(w->trace_lo));
122 }
123 7498 av_push(out, newRV_noinc((SV *)h));
124 }
125 160 hv_stores(r, "rows", newRV_noinc((SV *)out));
126 }
127 320 ▶ else if (res.bucket_ns) {
Branch: 1 branch
BranchTrueFalseTotal
else if (res.bucket_ns) { 254 66 320
128 254 hv_stores(r, "series", po_buckets_sv(aTHX_ &res));
129 254 hv_stores(r, "bucket_ns", po_u64_to_sv(res.bucket_ns));
130 }
131 else {
132 66 AV *out = newAV();
133 uint32_t i;
134 260 ▶ for (i = 0; i < res.ng; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < res.ng; i++) { 194 66 260
135 194 HV *h = newHV();
136 194 hv_stores(h, "key", newSVpvn(res.g[i].key, res.g[i].key_len));
137 194 hv_stores(h, "value", newSVnv((NV)res.g[i].value));
138 194 hv_stores(h, "count", po_u64_to_sv(res.g[i].count));
139 194 av_push(out, newRV_noinc((SV *)h));
140 }
141 66 hv_stores(r, "groups", newRV_noinc((SV *)out));
142 }
143  
144 } /* end of the answered case */
145  
146 /* meta is NEVER optional */
147 481 hv_stores(meta, "scanned_rows", po_u64_to_sv(res.scanned_rows));
148 481 hv_stores(meta, "scanned_bytes", po_u64_to_sv(res.scanned_bytes));
149 481 hv_stores(meta, "truncated", newSViv(res.truncated));
150 481 hv_stores(meta, "degraded", newSViv(res.degraded));
151 481 hv_stores(meta, "exact", newSViv(res.exact));
152 481 hv_stores(meta, "steps", newSViv(res.steps));
153 481 hv_stores(r, "meta", newRV_noinc((SV *)meta));
154  
155 /* THE IDS THE JOIN WILL RE-KEY ON, handed out separately from
156 * the rows because after an aggregate there are no rows. The
157 * store runs the second pass; the executor only says which
158 * traces survived its filters. */
159 481 ▶ if (res.nex || res.ex_overflow) {
Branch: 2 branches
BranchTrueFalseTotal
if (res.nex || res.ex_overflow) { 475 6 481
if (res.nex || res.ex_overflow) { 0 475 475
160 6 AV *xh = newAV(), *xl = newAV();
161 uint32_t i;
162 4112 ▶ for (i = 0; i < res.nex; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < res.nex; i++) { 4106 6 4112
163 4106 av_push(xh, po_u64_to_sv(res.ex_hi[i]));
164 4106 av_push(xl, po_u64_to_sv(res.ex_lo[i]));
165 }
166 6 hv_stores(r, "rekey_hi", newRV_noinc((SV *)xh));
167 6 hv_stores(r, "rekey_lo", newRV_noinc((SV *)xl));
168 6 ▶ if (res.ex_overflow)
Branch: 1 branch
BranchTrueFalseTotal
if (res.ex_overflow) 1 5 6
169 1 hv_stores(r, "rekey_overflow", newSViv(1));
170 }
171  
172 481 ▶ mXPUSHs(newRV_noinc((SV *)r));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)r)); 0 481 481
173 }
174  
175 481 po_result_free(&res);
176 481 Safefree(rv);
177 481 Safefree(keep);
178 481 po_query_free(&q);
179 }
180  
181 # Drive the executor one step at a time, reporting the cursor after each, so
182 # a test can assert that it actually YIELDS rather than running to completion
183 # in one call.
184 void
185 poe_steps(SV *query, SV *rows, SV *step)
186 PPCODE:
187 {
188 po_query q;
189 po_plan plan;
190 po_result res;
191 po_qexec x;
192 18 po_row *rv = NULL;
193 18 SV **keep = NULL;
194 IV nrows;
195 STRLEN qlen;
196 18 const char *qp = SvPV(query, qlen);
197 18 po_u64 sb = 0;
198 18 AV *cursors = newAV();
199 int s;
200  
201 18 (void)po_sv_to_u64(aTHX_ step, &sb);
202 18 ▶ if (!po_parse(&q, qp, (size_t)qlen)) { po_query_free(&q); croak("parse"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_parse(&q, qp, (size_t)qlen)) { po_query_free(&q); croak("parse"); } 0 18 18
203 18 ▶ if (!po_plan_build(&plan, &q, NULL)) { po_query_free(&q); croak("plan"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!po_plan_build(&plan, &q, NULL)) { po_query_free(&q); croak("plan"); } 0 18 18
204 18 nrows = poe_load_rows(aTHX_ rows, &rv, &keep);
205 18 ▶ if (nrows < 0) { po_query_free(&q); croak("rows"); }
Branch: 1 branch
BranchTrueFalseTotal
if (nrows < 0) { po_query_free(&q); croak("rows"); } 0 18 18
206  
207 18 po_result_init(&res);
208 18 po_qexec_init(&x, &plan, rv, (po_u64)nrows, &res, sb, 0);
209 do {
210 2333 s = po_qexec_step(&x);
211 2333 av_push(cursors, po_u64_to_sv(res.scanned_rows));
212 2333 ▶ } while (s == PO_Q_MORE);
Branch: 1 branch
BranchTrueFalseTotal
} while (s == PO_Q_MORE); 2315 18 2333
213  
214 {
215 18 HV *r = newHV();
216 18 hv_stores(r, "cursors", newRV_noinc((SV *)cursors));
217 18 hv_stores(r, "steps", newSViv(res.steps));
218 18 hv_stores(r, "scanned", po_u64_to_sv(res.scanned_rows));
219 18 hv_stores(r, "rows", newSVuv((UV)res.nrow));
220 18 ▶ mXPUSHs(newRV_noinc((SV *)r));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)r)); 0 18 18
221 }
222 18 po_result_free(&res);
223 18 Safefree(rv);
224 18 Safefree(keep);
225 18 po_query_free(&q);
226 }
227  
228 # --- the cross-signal edges -------------------------------------------------
229 #
230 # NOT a general relational join. Exactly three named edges, and they are the
231 # three that physically exist in the data:
232 #
233 # metric -> traces the exemplar's trace_id
234 # traces -> logs LogRecord.trace_id
235 # traces -> spans the same trace, already contiguous
236 #
237 # A general join across three columnar stores with no shared key would need a
238 # planner nobody can predict and a cost model nobody can debug. Refusing it is
239 # the design.
240 void
241 poe_join(SV *left, SV *right, SV *edge)
242 PPCODE:
243 {
244 AV *L, *R;
245 SSize_t i, j, ln, rn;
246 STRLEN elen;
247 14 const char *e = SvPV(edge, elen);
248 14 AV *out = newAV();
249 14 HV *seen = newHV();
250  
251 14 ▶ if (!SvROK(left) || !SvROK(right)) croak("arrayrefs required");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(left) || !SvROK(right)) croak("arrayrefs required"); 14 0 14
if (!SvROK(left) || !SvROK(right)) croak("arrayrefs required"); 0 14 14
252 14 L = (AV *)SvRV(left); R = (AV *)SvRV(right);
253 14 ln = av_len(L) + 1; rn = av_len(R) + 1;
254  
255 14 ▶ if (!(elen == 6 && memcmp(e, "traces", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!(elen == 6 && memcmp(e, "traces", 6) == 0) 7 7 14
if (!(elen == 6 && memcmp(e, "traces", 6) == 0) 0 7 7
256 7 ▶ && !(elen == 4 && memcmp(e, "logs", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
&& !(elen == 4 && memcmp(e, "logs", 4) == 0) 5 2 7
&& !(elen == 4 && memcmp(e, "logs", 4) == 0) 0 5 5
257 2 ▶ && !(elen == 5 && memcmp(e, "spans", 5) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(elen == 5 && memcmp(e, "spans", 5) == 0)) { 1 1 2
&& !(elen == 5 && memcmp(e, "spans", 5) == 0)) { 0 1 1
258 1 SvREFCNT_dec((SV *)seen);
259 1 SvREFCNT_dec((SV *)out);
260 1 croak("unknown edge '%s': only traces, logs and spans exist", e);
261 }
262  
263 /* Collect the left side's trace keys. */
264 36 ▶ for (i = 0; i < ln; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ln; i++) { 23 13 36
265 23 SV **el = av_fetch(L, i, 0);
266 HV *h; SV **a, **b;
267 char key[64];
268 int n;
269 23 ▶ if (!el || !SvROK(*el)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!el || !SvROK(*el)) continue; 23 0 23
if (!el || !SvROK(*el)) continue; 0 23 23
270 23 h = (HV *)SvRV(*el);
271 23 a = hv_fetchs(h, "trace_hi", 0);
272 23 b = hv_fetchs(h, "trace_lo", 0);
273 23 ▶ if (!a || !b) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) continue; 23 0 23
if (!a || !b) continue; 0 23 23
274 69 ▶ n = my_snprintf(key, sizeof(key), "%s/%s",
Branch: 1 branch
BranchTrueFalseTotal
n = my_snprintf(key, sizeof(key), "%s/%s", 23 0 23
275 SvPV_nolen(*a), SvPV_nolen(*b));
276 23 hv_store(seen, key, n, newSViv(1), 0);
277 }
278  
279 55 ▶ for (j = 0; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < rn; j++) { 42 13 55
280 42 SV **el = av_fetch(R, j, 0);
281 HV *h; SV **a, **b;
282 char key[64];
283 int n;
284 42 ▶ if (!el || !SvROK(*el)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!el || !SvROK(*el)) continue; 42 0 42
if (!el || !SvROK(*el)) continue; 0 42 42
285 42 h = (HV *)SvRV(*el);
286 42 a = hv_fetchs(h, "trace_hi", 0);
287 42 b = hv_fetchs(h, "trace_lo", 0);
288 42 ▶ if (!a || !b) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !b) continue; 42 0 42
if (!a || !b) continue; 0 42 42
289 126 ▶ n = my_snprintf(key, sizeof(key), "%s/%s",
Branch: 1 branch
BranchTrueFalseTotal
n = my_snprintf(key, sizeof(key), "%s/%s", 42 0 42
290 SvPV_nolen(*a), SvPV_nolen(*b));
291 42 ▶ if (hv_exists(seen, key, n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(seen, key, n)) { 24 18 42
292 24 SvREFCNT_inc(*el);
293 24 av_push(out, *el);
294 }
295 }
296 13 SvREFCNT_dec((SV *)seen);
297 13 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 13 13
298 }
« include/punk_observe/po_result.h ↑ Summary include/punk_observe/po_labels.h »