[master] 0aa8782 Get rid of the array of backend/directors in the compiled VCL.
Poul-Henning Kamp
phk at FreeBSD.org
Mon Jun 1 11:33:24 CEST 2015
commit 0aa8782e582cb84284845975a340497f716dece8
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Mon Jun 1 09:32:59 2015 +0000
Get rid of the array of backend/directors in the compiled VCL.
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index 653a0cd..c4d3ab4 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -649,7 +649,7 @@ cnt_recv(struct worker *wrk, struct req *req)
/* By default we use the first backend */
AZ(req->director_hint);
- req->director_hint = req->vcl->director[0];
+ req->director_hint = *req->vcl->default_director;
AN(req->director_hint);
req->d_ttl = -1;
diff --git a/bin/varnishtest/tests/r00916.vtc b/bin/varnishtest/tests/r00916.vtc
index 04aa7c6..8057f1a 100644
--- a/bin/varnishtest/tests/r00916.vtc
+++ b/bin/varnishtest/tests/r00916.vtc
@@ -5,7 +5,7 @@ server s1 {
txresp -body "FOO"
} -start
-varnish v1 -errvcl {Symbol not found: 's-1' (expected type BACKEND)} {
+varnish v1 -errvcl {Backend not found: 's-1'} {
backend b { .host = "127.0.0.1"; }
sub s1 {
}
diff --git a/bin/varnishtest/tests/v00016.vtc b/bin/varnishtest/tests/v00016.vtc
index 3198e89..990f39e 100644
--- a/bin/varnishtest/tests/v00016.vtc
+++ b/bin/varnishtest/tests/v00016.vtc
@@ -118,3 +118,12 @@ varnish v1 -errvcl {'bereq.between_bytes_timeout': cannot be set} {
set bereq.between_bytes_timeout = 10s;
}
}
+
+varnish v1 -errvcl {Backend not found: 'c'} {
+ backend b { .host = "127.0.0.1"; }
+ sub vcl_backend_response {
+ if (beresp.backend == c) {
+ set beresp.ttl = 1h;
+ }
+ }
+}
diff --git a/lib/libvcc/generate.py b/lib/libvcc/generate.py
index f695dc7..54560bf 100755
--- a/lib/libvcc/generate.py
+++ b/lib/libvcc/generate.py
@@ -1060,8 +1060,7 @@ struct VCL_conf {
char *loaded_name;
- struct director **director;
- unsigned ndirector;
+ struct director **default_director;
struct vrt_ref *ref;
unsigned nref;
unsigned busy;
diff --git a/lib/libvcc/vcc_backend.c b/lib/libvcc/vcc_backend.c
index b937404..d788d28 100644
--- a/lib/libvcc/vcc_backend.c
+++ b/lib/libvcc/vcc_backend.c
@@ -262,7 +262,7 @@ vcc_ParseProbe(struct vcc *tl)
*/
static void
-vcc_ParseHostDef(struct vcc *tl, const struct token *t_be)
+vcc_ParseHostDef(struct vcc *tl, const struct token *t_be, const char *vgcname)
{
struct token *t_field;
struct token *t_host = NULL;
@@ -273,11 +273,6 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be)
struct vsb *vsb;
unsigned u;
double t;
- char vgcname[MAX_BACKEND_NAME + 8];
-
- sprintf(vgcname, "_%.*s", PF(t_be));
-
- Fh(tl, 1, "\n#define VGC_backend_%s %d\n", vgcname, tl->ndirector);
fs = vcc_FldSpec(tl,
"!host",
@@ -415,15 +410,14 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be)
ifp = New_IniFin(tl);
VSB_printf(ifp->ini,
- "\tVRT_init_vbe(ctx, &VGCDIR(%s), &vgc_dir_priv_%s);",
+ "\tVRT_init_vbe(ctx, &%s, &vgc_dir_priv_%s);",
vgcname, vgcname);
VSB_printf(ifp->fin,
- "\tVRT_fini_vbe(ctx, &VGCDIR(%s), &vgc_dir_priv_%s);",
+ "\tVRT_fini_vbe(ctx, &%s, &vgc_dir_priv_%s);",
vgcname, vgcname);
VSB_printf(ifp->event,
- "\tVRT_event_vbe(ctx, ev, VGCDIR(%s), &vgc_dir_priv_%s);",
+ "\tVRT_event_vbe(ctx, ev, %s, &vgc_dir_priv_%s);",
vgcname, vgcname);
- tl->ndirector++;
}
/*--------------------------------------------------------------------
@@ -434,8 +428,8 @@ void
vcc_ParseBackend(struct vcc *tl)
{
struct token *t_first, *t_be;
- int isfirst;
struct symbol *sym;
+ char vgcname[MAX_BACKEND_NAME + 20];
t_first = tl->t;
vcc_NextToken(tl); /* ID: backend */
@@ -455,7 +449,8 @@ vcc_ParseBackend(struct vcc *tl)
t_be = tl->t;
vcc_NextToken(tl);
- isfirst = tl->ndirector;
+ sprintf(vgcname, "vgc_backend_%.*s", PF(t_be));
+ Fh(tl, 0, "static struct director *%s;\n", vgcname);
sym = VCC_GetSymbolTok(tl, t_be, SYM_BACKEND);
AN(sym);
@@ -466,10 +461,11 @@ vcc_ParseBackend(struct vcc *tl)
}
sym->fmt = BACKEND;
sym->eval = vcc_Eval_Backend;
+ sym->eval_priv = TlDup(tl, vgcname);
sym->ndef++;
ERRCHK(tl);
- vcc_ParseHostDef(tl, t_be);
+ vcc_ParseHostDef(tl, t_be, vgcname);
ERRCHK(tl);
if (tl->err) {
@@ -479,8 +475,8 @@ vcc_ParseBackend(struct vcc *tl)
return;
}
- if (isfirst == 1 || vcc_IdIs(t_be, "default")) {
- tl->defaultdir = tl->ndirector - 1;
- tl->t_defaultdir = t_be;
+ if (tl->default_director == NULL || vcc_IdIs(t_be, "default")) {
+ tl->default_director = sym->eval_priv;
+ tl->t_default_director = t_be;
}
}
diff --git a/lib/libvcc/vcc_compile.c b/lib/libvcc/vcc_compile.c
index 4717148..06f3a3e 100644
--- a/lib/libvcc/vcc_compile.c
+++ b/lib/libvcc/vcc_compile.c
@@ -387,14 +387,10 @@ EmitStruct(const struct vcc *tl)
}
Fc(tl, 0, "};\n");
- Fc(tl, 0, "\nstatic struct director\t*directors[%d];\n",
- tl->ndirector);
-
Fc(tl, 0, "\nconst struct VCL_conf VCL_conf = {\n");
Fc(tl, 0, "\t.magic = VCL_CONF_MAGIC,\n");
Fc(tl, 0, "\t.event_vcl = VGC_Event,\n");
- Fc(tl, 0, "\t.ndirector = %d,\n", tl->ndirector);
- Fc(tl, 0, "\t.director = directors,\n");
+ Fc(tl, 0, "\t.default_director = &%s,\n", tl->default_director);
Fc(tl, 0, "\t.ref = VGC_ref,\n");
Fc(tl, 0, "\t.nref = VGC_NREFS,\n");
Fc(tl, 0, "\t.nsrc = %u,\n", tl->nsources);
@@ -535,7 +531,6 @@ vcc_NewVcc(const struct vcc *tl0)
VTAILQ_INIT(&tl->sources);
tl->nsources = 0;
- tl->ndirector = 1;
/* General C code */
tl->fc = VSB_new_auto();
@@ -600,7 +595,6 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
struct vcc *tl;
struct symbol *sym;
const struct var *v;
- struct inifin *ifp;
char *of;
int i;
@@ -629,9 +623,6 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
Fh(tl, 0, "/* ---===### VCC generated code ###===---*/\n");
Fh(tl, 0, "\nextern const struct VCL_conf VCL_conf;\n");
- /* Macro for accessing directors */
- Fh(tl, 0, "#define VGCDIR(n) VCL_conf.director[VGC_backend_##n]\n");
-
/* Register and lex the main source */
VTAILQ_INSERT_TAIL(&tl->sources, sp, list);
sp->idx = tl->nsources++;
@@ -665,7 +656,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
return (vcc_DestroyTokenList(tl, NULL));
/* Check if we have any backends at all */
- if (tl->ndirector == 1) {
+ if (tl->default_director == NULL) {
VSB_printf(tl->sb,
"No backends or directors found in VCL program, "
"at least one is necessary.\n");
@@ -674,11 +665,7 @@ vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
}
/* Configure the default director */
- ifp = New_IniFin(tl);
- VSB_printf(ifp->ini,
- "\tVCL_conf.director[0] = VCL_conf.director[%d];",
- tl->defaultdir);
- vcc_AddRef(tl, tl->t_defaultdir, SYM_BACKEND);
+ vcc_AddRef(tl, tl->t_default_director, SYM_BACKEND);
/* Check for orphans */
if (vcc_CheckReferences(tl))
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index b2e15e3..a222b9b 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -189,7 +189,6 @@ struct vcc {
struct vsb *fm[VCL_MET_MAX]; /* Method bodies */
struct vsb *sb;
int err;
- int ndirector;
struct proc *curproc;
struct proc *mprocs[VCL_MET_MAX];
@@ -197,8 +196,8 @@ struct vcc {
int nprobe;
- int defaultdir;
- struct token *t_defaultdir;
+ const char *default_director;
+ struct token *t_default_director;
unsigned unique;
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index 6a03bff..d3ad332 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -508,7 +508,7 @@ vcc_Eval_Backend(struct vcc *tl, struct expr **e, const struct symbol *sym)
vcc_ExpectCid(tl);
vcc_AddRef(tl, tl->t, SYM_BACKEND);
- *e = vcc_mk_expr(BACKEND, "VGCDIR(_%.*s)", PF(tl->t));
+ *e = vcc_mk_expr(BACKEND, "%s", sym->eval_priv);
(*e)->constant = EXPR_VAR; /* XXX ? */
vcc_NextToken(tl);
}
@@ -1129,6 +1129,7 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, enum var_type fmt)
const char *re;
const char *not;
struct token *tk;
+ struct symbol *sym;
*e = NULL;
@@ -1188,10 +1189,19 @@ vcc_expr_cmp(struct vcc *tl, struct expr **e, enum var_type fmt)
}
if ((*e)->fmt == BACKEND &&
(tl->t->tok == T_EQ || tl->t->tok == T_NEQ)) {
+ // XXX: just ask for a BACKEND expression instead ?
vcc_NextToken(tl);
ExpectErr(tl, ID);
+ sym = VCC_FindSymbol(tl, tl->t, SYM_BACKEND);
+ if (sym == NULL) {
+ VSB_printf(tl->sb, "Backend not found: ");
+ vcc_ErrToken(tl, tl->t);
+ VSB_printf(tl->sb, "\n");
+ vcc_ErrWhere(tl, tl->t);
+ return;
+ }
vcc_AddRef(tl, tl->t, SYM_BACKEND);
- bprintf(buf, "(\v1 %.*s VGCDIR(_%.*s))", PF(tk), PF(tl->t));
+ bprintf(buf, "(\v1 %.*s %s)", PF(tk), sym->eval_priv);
vcc_NextToken(tl);
*e = vcc_expr_edit(BOOL, buf, *e, NULL);
return;
More information about the varnish-commit
mailing list