Skip to content

Commit 227df30

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 3eec7e8 commit 227df30

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

bin/varnishd/cache/cache_backend.c

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

326326
PFD_LocalName(pfd, abuf1, sizeof abuf1, pbuf1, sizeof pbuf1);
327327
PFD_RemoteName(pfd, abuf2, sizeof abuf2, pbuf2, sizeof pbuf2);
328-
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s %s",
329-
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1,
330-
PFD_State(pfd) == PFD_STATE_STOLEN ? "reuse" : "connect");
328+
if (PFD_State(pfd) != PFD_STATE_STOLEN) {
329+
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s connect",
330+
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1);
331+
} else {
332+
VSLb(bo->vsl, SLT_BackendOpen,
333+
"%d %s %s %s %s %s reuse %.6f %ld", *fdp,
334+
VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1,
335+
PFD_Age(pfd, VTIM_real()), PFD_Reused(pfd));
336+
}
331337

332338
INIT_OBJ(bo->htc, HTTP_CONN_MAGIC);
333339
bo->htc->priv = pfd;

bin/varnishd/cache/cache_conn_pool.c

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

6565
pthread_cond_t *cond;
66+
67+
vtim_real created;
68+
uint64_t reused;
6669
};
6770

6871
/*--------------------------------------------------------------------
@@ -126,6 +129,21 @@ PFD_Fd(struct pfd *p)
126129
return (&(p->fd));
127130
}
128131

132+
vtim_real
133+
PFD_Age(const struct pfd *p, vtim_real now)
134+
{
135+
CHECK_OBJ_NOTNULL(p, PFD_MAGIC);
136+
assert(now >= p->created);
137+
return (now - p->created);
138+
}
139+
140+
uint64_t
141+
PFD_Reused(const struct pfd *p)
142+
{
143+
CHECK_OBJ_NOTNULL(p, PFD_MAGIC);
144+
return (p->reused);
145+
}
146+
129147
void
130148
PFD_LocalName(const struct pfd *p, char *abuf, unsigned alen, char *pbuf,
131149
unsigned plen)
@@ -455,6 +473,7 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
455473
VSC_C_main->backend_reuse++;
456474
pfd->state = PFD_STATE_STOLEN;
457475
pfd->cond = &wrk->cond;
476+
pfd->reused++;
458477
}
459478
cp->n_used++; // Opening mostly works
460479
Lck_Unlock(&cp->mtx);
@@ -473,8 +492,10 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
473492
Lck_Lock(&cp->mtx);
474493
cp->n_used--; // Nope, didn't work after all.
475494
Lck_Unlock(&cp->mtx);
476-
} else
495+
} else {
496+
pfd->created = VTIM_real();
477497
VSC_C_main->backend_conn++;
498+
}
478499

479500
return (pfd);
480501
}

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 *, vtim_real);
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/b00087.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 uses\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)