Skip to content

Commit d496f00

Browse files
asadsa92nigoroll
authored andcommitted
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 b628c9b commit d496f00

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
@@ -334,9 +334,15 @@ vbe_dir_getfd(VRT_CTX, struct worker *wrk, VCL_BACKEND dir, struct backend *bp,
334334

335335
PFD_LocalName(pfd, abuf1, sizeof abuf1, pbuf1, sizeof pbuf1);
336336
PFD_RemoteName(pfd, abuf2, sizeof abuf2, pbuf2, sizeof pbuf2);
337-
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s %s",
338-
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1,
339-
PFD_State(pfd) == PFD_STATE_STOLEN ? "reuse" : "connect");
337+
if (PFD_State(pfd) != PFD_STATE_STOLEN) {
338+
VSLb(bo->vsl, SLT_BackendOpen, "%d %s %s %s %s %s connect",
339+
*fdp, VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1);
340+
} else {
341+
VSLb(bo->vsl, SLT_BackendOpen,
342+
"%d %s %s %s %s %s reuse %.6f %ju", *fdp,
343+
VRT_BACKEND_string(dir), abuf2, pbuf2, abuf1, pbuf1,
344+
PFD_Age(pfd), PFD_Reused(pfd));
345+
}
340346

341347
INIT_OBJ(bo->htc, HTTP_CONN_MAGIC);
342348
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
@@ -66,6 +66,9 @@ struct pfd {
6666
struct conn_pool *conn_pool;
6767

6868
pthread_cond_t *cond;
69+
70+
vtim_mono created;
71+
uint64_t reused;
6972
};
7073

7174
/*--------------------------------------------------------------------
@@ -139,6 +142,25 @@ PFD_Fd(struct pfd *p)
139142
return (&(p->fd));
140143
}
141144

145+
vtim_dur
146+
PFD_Age(const struct pfd *p)
147+
{
148+
vtim_mono t_mono;
149+
150+
CHECK_OBJ_NOTNULL(p, PFD_MAGIC);
151+
t_mono = VTIM_mono();
152+
assert(t_mono >= p->created);
153+
154+
return (t_mono - p->created);
155+
}
156+
157+
uint64_t
158+
PFD_Reused(const struct pfd *p)
159+
{
160+
CHECK_OBJ_NOTNULL(p, PFD_MAGIC);
161+
return (p->reused);
162+
}
163+
142164
void
143165
PFD_LocalName(const struct pfd *p, char *abuf, unsigned alen, char *pbuf,
144166
unsigned plen)
@@ -541,6 +563,7 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
541563
VSC_C_main->backend_reuse++;
542564
pfd->state = PFD_STATE_STOLEN;
543565
pfd->cond = &wrk->cond;
566+
pfd->reused++;
544567
}
545568
cp->n_used++; // Opening mostly works
546569
Lck_Unlock(&cp->mtx);
@@ -559,8 +582,10 @@ VCP_Get(struct conn_pool *cp, vtim_dur tmo, struct worker *wrk,
559582
Lck_Lock(&cp->mtx);
560583
cp->n_used--; // Nope, didn't work after all.
561584
Lck_Unlock(&cp->mtx);
562-
} else
585+
} else {
586+
pfd->created = VTIM_mono();
563587
VSC_C_main->backend_conn++;
588+
}
564589

565590
return (pfd);
566591
}

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_dur 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/b00098.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 [ %f %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)