Skip to content

Commit e244e96

Browse files
committed
varnishd: add backend age & reuses to Open & Close
This extra information comes handy when trubleshooting idle timeout from the backend. Signed-off-by: Asad Sajjad Ahmed <[email protected]>
1 parent febf5a0 commit e244e96

File tree

5 files changed

+76
-13
lines changed

5 files changed

+76
-13
lines changed

bin/varnishd/cache/cache_backend.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,15 @@ vbe_dir_getfd(VRT_CTX, struct worker *wrk, VCL_BACKEND dir, struct backend *bp,
328328

329329
PFD_LocalName(pfd, abuf1, sizeof abuf1, pbuf1, sizeof pbuf1);
330330
PFD_RemoteName(pfd, abuf2, sizeof abuf2, pbuf2, sizeof pbuf2);
331-
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s %s",
332-
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1,
333-
PFD_State(pfd) == PFD_STATE_STOLEN ? "reuse" : "connect");
331+
if (PFD_State(pfd) != PFD_STATE_STOLEN) {
332+
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s connect",
333+
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1);
334+
} else {
335+
VSLb(bo->vsl, SLT_BackendOpen,
336+
"%d %s %s %s %s %s reuse %.6f %ju", *fdp,
337+
VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1,
338+
PFD_Age(pfd), PFD_Reused(pfd));
339+
}
334340

335341
INIT_OBJ(bo->htc, HTTP_CONN_MAGIC);
336342
bo->htc->priv = pfd;

bin/varnishd/cache/cache_conn_pool.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ struct pfd {
6464
struct conn_pool *conn_pool;
6565

6666
pthread_cond_t *cond;
67+
68+
vtim_mono created;
69+
uint64_t reused;
6770
};
6871

6972
/*--------------------------------------------------------------------
@@ -131,6 +134,25 @@ PFD_Fd(struct pfd *p)
131134
return (&(p->fd));
132135
}
133136

137+
vtim_mono
138+
PFD_Age(const struct pfd *p)
139+
{
140+
vtim_mono now;
141+
142+
CHECK_OBJ_NOTNULL(p, PFD_MAGIC);
143+
now = VTIM_mono();
144+
assert(now >= p->created);
145+
146+
return (now - p->created);
147+
}
148+
149+
uint64_t
150+
PFD_Reused(const struct pfd *p)
151+
{
152+
CHECK_OBJ_NOTNULL(p, PFD_MAGIC);
153+
return (p->reused);
154+
}
155+
134156
void
135157
PFD_LocalName(const struct pfd *p, char *abuf, unsigned alen, char *pbuf,
136158
unsigned plen)
@@ -469,6 +491,7 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
469491
VSC_C_main->backend_reuse++;
470492
pfd->state = PFD_STATE_STOLEN;
471493
pfd->cond = &wrk->cond;
494+
pfd->reused++;
472495
}
473496
cp->n_used++; // Opening mostly works
474497
Lck_Unlock(&cp->mtx);
@@ -487,8 +510,10 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
487510
Lck_Lock(&cp->mtx);
488511
cp->n_used--; // Nope, didn't work after all.
489512
Lck_Unlock(&cp->mtx);
490-
} else
513+
} else {
514+
pfd->created = VTIM_mono();
491515
VSC_C_main->backend_conn++;
516+
}
492517

493518
return (pfd);
494519
}

bin/varnishd/cache/cache_conn_pool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct pfd;
4545

4646
unsigned PFD_State(const struct pfd *);
4747
int *PFD_Fd(struct pfd *);
48+
vtim_real PFD_Age(const struct pfd *);
49+
uint64_t PFD_Reused(const struct pfd *);
4850
void PFD_LocalName(const struct pfd *, char *, unsigned, char *, unsigned);
4951
void PFD_RemoteName(const struct pfd *, char *, unsigned, char *, unsigned);
5052

bin/varnishtest/tests/b00092.vtc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
varnishtest "VSL tags backend age & reuse"
2+
3+
server s1 {
4+
rxreq
5+
txresp
6+
rxreq
7+
txresp
8+
} -start
9+
10+
varnish v1 -vcl+backend {} -start
11+
varnish v1 -cliok "param.set debug +syncvsl"
12+
13+
client c1 {
14+
txreq
15+
rxresp
16+
expect resp.status == 200
17+
txreq -url /2
18+
rxresp
19+
expect resp.status == 200
20+
} -run
21+
22+
logexpect l1 -v v1 -d 1 -g session {
23+
expect * * Begin ^bereq
24+
expect * = BackendOpen "connect$"
25+
expect * = BackendClose "s1 recycle$"
26+
expect * * BackendOpen "reuse \\d+\\.\\d\\d\\d\\d\\d\\d 1$"
27+
expect * = BackendClose "s1 recycle$"
28+
} -run

include/tbl/vsl_tags.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,17 @@ SLTM(SessClose, 0, "Client connection closed",
138138
SLTM(BackendOpen, 0, "Backend connection opened",
139139
"Logged when a new backend connection is opened.\n\n"
140140
"The format is::\n\n"
141-
"\t%d %s %s %s %s %s %s\n"
142-
"\t| | | | | | |\n"
143-
"\t| | | | | | +- \"connect\" or \"reuse\"\n"
144-
"\t| | | | | +---- Local port\n"
145-
"\t| | | | +------- Local address\n"
146-
"\t| | | +---------- Remote port\n"
147-
"\t| | +------------- Remote address\n"
148-
"\t| +---------------- Backend display name\n"
149-
"\t+------------------- Connection file descriptor\n"
141+
"\t%d %s %s %s %s %s %s [ %.6f %ld ]\n"
142+
"\t| | | | | | | | |\n"
143+
"\t| | | | | | | | +- Connection reuses\n"
144+
"\t| | | | | | | +------ Connection age\n"
145+
"\t| | | | | | +----------- \"connect\" or \"reuse\"\n"
146+
"\t| | | | | +-------------- Local port\n"
147+
"\t| | | | +----------------- Local address\n"
148+
"\t| | | +-------------------- Remote port\n"
149+
"\t| | +----------------------- Remote address\n"
150+
"\t| +-------------------------- Backend display name\n"
151+
"\t+----------------------------- Connection file descriptor\n"
150152
"\n"
151153
)
152154

0 commit comments

Comments
 (0)