Skip to content

Commit ef0ed20

Browse files
committed
[vcl] Better debug
Detecting if the response is a hit usingthe x-varnish header is flaky and notably doesn't work in a multi-tier setup. On top of this, HIT/MISS is a false dichotomy (https://info.varnish-software.com/blog/using-obj-hits) and while I'd like something more detailed like https://docs.varnish-software.com/tutorials/hit-miss-logging/ (tech version of the blog), I recognize that it's "a lot" of VCL, so this commit only differentiates between hits, misses and passes, the latter being pretty useful to identify while debugging.
1 parent 87d2563 commit ef0ed20

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

app/code/Magento/PageCache/etc/varnish6.vcl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ sub vcl_backend_response {
219219
}
220220

221221
sub vcl_deliver {
222-
if (resp.http.x-varnish ~ " ") {
222+
if (obj.uncacheable) {
223+
set resp.http.X-Magento-Cache-Debug = "UNCACHEABLE";
224+
} else if (obj.hits) {
223225
set resp.http.X-Magento-Cache-Debug = "HIT";
224226
set resp.http.Grace = req.http.grace;
225227
} else {

dev/tests/varnish/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Files in this directory are Varnish Test Cases (VTC) and check the behavior of the VCL file shipped by `magento2`.
2+
3+
Varnish needs to be installed, but then the test scenarios can be run individually or all at once:
4+
5+
``` shell
6+
varnishtest *.vtc
7+
```
8+
9+
Documentation:
10+
- varnishtest itself: https://varnish-cache.org/docs/trunk/reference/varnishtest.html
11+
- VTC syntax: https://varnish-cache.org/docs/trunk/reference/vtc.html

dev/tests/varnish/debug.vtc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
varnishtest "X-Magento-Cache-Debug header"
2+
3+
server s1 {
4+
# first request will be the probe, handle it and be on our way
5+
rxreq
6+
expect req.url == "/health_check.php"
7+
txresp
8+
9+
# the probe expects the connection to close
10+
close
11+
accept
12+
13+
rxreq
14+
txresp -hdr "answer-to: POST"
15+
16+
rxreq
17+
txresp -hdr "answer-to: GET"
18+
} -start
19+
20+
# generate usable VCL pointing towards s1
21+
# mostly, we replace the place-holders, but we also jack up the probe
22+
# interval to avoid further interference
23+
shell {
24+
# testdir is automatically set to the directory containing the present vtc
25+
sed ${testdir}/../../../app/code/Magento/PageCache/etc/varnish6.vcl > ${tmpdir}/output.vcl \
26+
-e 's@\.interval = 5s;@.interval = 5m; .initial = 10;@' \
27+
-e 's@/\* {{ host }} \*/@${s1_addr}@' \
28+
-e 's@/\* {{ port }} \*/@${s1_port}@' \
29+
-e 's@/\* {{ ssl_offloaded_header }} \*/@unused@' \
30+
-e 's@/\* {{ grace_period }} \*/@0@'
31+
}
32+
33+
varnish v1 -arg "-f" -arg "${tmpdir}/output.vcl" -start
34+
35+
# make surethe probe request fired
36+
delay 1
37+
38+
client c1 {
39+
txreq -method "POST"
40+
rxresp
41+
expect resp.http.answer-to == "POST"
42+
expect resp.http.X-Magento-Cache-Debug == "UNCACHEABLE"
43+
44+
txreq
45+
rxresp
46+
expect resp.http.answer-to == "GET"
47+
expect resp.http.X-Magento-Cache-Debug == "MISS"
48+
49+
txreq
50+
rxresp
51+
expect resp.http.answer-to == "GET"
52+
expect resp.http.X-Magento-Cache-Debug == "HIT"
53+
} -run

0 commit comments

Comments
 (0)