Hi,

Last day of training, and probably the last patch set regarding
dynamic backends operations (only during vcl.* commands).

I'm almost satisfied with this batch, and it's basically all non
disruptive changes I could do to make dynamic backends work
to some extent.

Cheers,
Dridi

On Wed, Apr 15, 2015 at 12:23 AM, Dridi Boukelmoune <[email protected]> wrote:
> Hi,
>
> It turns out my trainees were so quiet during exercises that I could
> explore this a bit more.
>
> I have come to the point where you don't need a backend declaration in
> your VCL, as long as you have a director that will dynamically create
> one. Once backend check was deferred to *after* vcl_init, it made very
> easy to allow a director to become the default backend.
>
> See the test d00007.vtc for a pure dynamic backend:
>
> varnish v1 -vcl {
>         import ${vmod_debug};
>
>         sub vcl_init {
>                 new s1 = debug.dyn("${s1_addr}", "${s1_port}");
>         }
> } -start
>
> See the test d00008.vtc for a director as the default:
>
> varnish v1 -vcl+backend {
>         import ${vmod_directors};
>
>         # no need for `set bereq.backend = default;`
>         sub vcl_init {
>                 new default = directors.round_robin();
>                 default.add_backend(s1);
>                 default.add_backend(s2);
>         }
> } -start
>
> There are still caveats with the current implementation, and it's still not
> enough to implement a new dns director. Dynamically created backend
> won't receive events, which is not a problem *so far* since we can't pass
> probes to VMODs yet. Instead of emitting per-backend code for events,
> it could rely on backend_find() to fire events on both static and dynamic
> backends.
>
> Comments?
>
> Cheers,
> Dridi
From 665e49b7f28bdcce7ff0577e089014e986f5d34c Mon Sep 17 00:00:00 2001
From: Dridi Boukelmoune <[email protected]>
Date: Wed, 15 Apr 2015 08:52:54 +0200
Subject: [PATCH 08/10] Fix comment in a test case

---
 bin/varnishtest/tests/d00008.vtc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bin/varnishtest/tests/d00008.vtc b/bin/varnishtest/tests/d00008.vtc
index 9419387..25c578a 100644
--- a/bin/varnishtest/tests/d00008.vtc
+++ b/bin/varnishtest/tests/d00008.vtc
@@ -13,7 +13,7 @@ server s2 {
 varnish v1 -vcl+backend {
 	import ${vmod_directors};
 
-	# no need for `set bereq.backend = default;`
+	# no need for `set bereq.backend = default.backend();`
 	sub vcl_init {
 		new default = directors.round_robin();
 		default.add_backend(s1);
-- 
2.1.0

From a1fcd986ee88b162f16b000f9d4bf4eedad4d7bd Mon Sep 17 00:00:00 2001
From: Dridi Boukelmoune <[email protected]>
Date: Wed, 15 Apr 2015 14:27:12 +0200
Subject: [PATCH 09/10] Rename backend_find to VBE_Find and make it public

---
 bin/varnishd/cache/cache_backend.h     |  5 +++++
 bin/varnishd/cache/cache_backend_cfg.c | 10 ++++------
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index 48dffa4..c0c7893 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -109,6 +109,11 @@ unsigned VBE_Healthy(const struct backend *b, double *changed);
 struct backend *VBE_AddBackend(const char *vcl, const struct vrt_backend *vb);
 void VBE_DeleteBackend(struct backend *);
 
+typedef int vbe_find_func(struct cli *cli, struct backend *b, void *priv);
+int VBE_Find(struct cli *cli, const char *matcher, vbe_find_func *func,
+    void *priv);
+
+
 /* cache_backend_poll.c */
 void VBP_Insert(struct backend *b, struct vrt_backend_probe const *p,
     const char *hosthdr);
diff --git a/bin/varnishd/cache/cache_backend_cfg.c b/bin/varnishd/cache/cache_backend_cfg.c
index 8058b6c..46713e9 100644
--- a/bin/varnishd/cache/cache_backend_cfg.c
+++ b/bin/varnishd/cache/cache_backend_cfg.c
@@ -154,10 +154,8 @@ vbe_str2adminhealth(const char *wstate)
  * Otherwise we return the number of matches.
  */
 
-typedef int bf_func(struct cli *cli, struct backend *b, void *priv);
-
-static int
-backend_find(struct cli *cli, const char *matcher, bf_func *func, void *priv)
+int
+VBE_Find(struct cli *cli, const char *matcher, vbe_find_func *func, void *priv)
 {
 	int i, found = 0;
 	struct vsb *vsb;
@@ -250,7 +248,7 @@ cli_backend_list(struct cli *cli, const char * const *av, void *priv)
 		return;
 	}
 	VCLI_Out(cli, "%-30s %-10s %s", "Backend name", "Admin", "Probe");
-	(void)backend_find(cli, av[2], do_list, &probes);
+	(void)VBE_Find(cli, av[2], do_list, &probes);
 }
 
 /*---------------------------------------------------------------------*/
@@ -289,7 +287,7 @@ cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
 		VCLI_SetResult(cli, CLIS_PARAM);
 		return;
 	}
-	n = backend_find(cli, av[2], do_set_health, &state);
+	n = VBE_Find(cli, av[2], do_set_health, &state);
 	if (n == 0) {
 		VCLI_Out(cli, "No Backends matches");
 		VCLI_SetResult(cli, CLIS_PARAM);
-- 
2.1.0

From d7d1af70b1d8fa0eb5504f0db787a83a034d8c8c Mon Sep 17 00:00:00 2001
From: Dridi Boukelmoune <[email protected]>
Date: Wed, 15 Apr 2015 16:49:19 +0200
Subject: [PATCH 10/10] Fire events to all the backends of the active VCL

Event propagation was generated during the VCL-to-C compilation, which
would not take dynamic backends into account. Instead of relying on
static code, events are propagated to all vcl backends with VBE_Find.
---
 bin/varnishd/cache/cache_backend.c     | 31 ++++++++++++++++++++-----------
 bin/varnishd/cache/cache_backend.h     |  4 ++--
 bin/varnishd/cache/cache_backend_cfg.c | 14 +++++++++-----
 bin/varnishd/cache/cache_vcl.c         |  4 ++++
 include/vrt.h                          |  3 +--
 lib/libvcc/vcc_backend.c               |  3 ---
 lib/libvcc/vcc_compile.c               |  7 +------
 lib/libvmod_debug/vmod_debug_dyn.c     |  2 --
 8 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 35c9103..7c9540d 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -318,19 +318,16 @@ VRT_init_vbe(VRT_CTX, struct director **dp, const struct vrt_backend *vrt)
 	*dp = d;
 }
 
-void
-VRT_event_vbe(enum vcl_event_e ev, const struct director *d,
-    const struct vrt_backend *vrt)
+static int __match_proto__()
+fire_event(struct cli *cli, struct backend *be, void *priv)
 {
-	struct backend *be;
+	enum vcl_event_e ev;
 
-	ASSERT_CLI();
-	(void)ev;
-	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
-	CHECK_OBJ_NOTNULL(vrt, VRT_BACKEND_MAGIC);
-	assert(d->priv2 == vrt);
+	AN(priv);
+	CHECK_OBJ_NOTNULL(be, BACKEND_MAGIC);
+
+	ev = *(enum vcl_event_e*)priv;
 
-	CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC);
 	if (ev == VCL_EVENT_WARM) {
 		be->vsc = VSM_Alloc(sizeof *be->vsc,
 		    VSC_CLASS, VSC_type_vbe, be->display_name);
@@ -347,6 +344,19 @@ VRT_event_vbe(enum vcl_event_e ev, const struct director *d,
 		VSM_Free(be->vsc);
 		be->vsc = NULL;
 	}
+
+	return (0);
+}
+
+void
+VRT_event_vbe(VRT_CTX, enum vcl_event_e ev)
+{
+	ASSERT_CLI();
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(ctx->vcl, VCL_CONF_MAGIC);
+
+	VSL(SLT_Debug, 0, "VRT_event_vbe: %d", ev);
+	assert(VBE_Find(ctx->cli, ctx->vcl, NULL, fire_event, &ev) > 0);
 }
 
 void
@@ -387,7 +397,6 @@ VRT_event_vdi(VRT_CTX, struct director *dir)
 
 	if (ctx->vcl->director[0] == NULL ||
 	    !strcmp("default", dir->vcl_name)) {
-		VSL(SLT_Debug, 0, "Registered director %s", dir->vcl_name);
 		ctx->vcl->director[0] = dir;
 	}
 
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index c0c7893..015e6c4 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -110,8 +110,8 @@ struct backend *VBE_AddBackend(const char *vcl, const struct vrt_backend *vb);
 void VBE_DeleteBackend(struct backend *);
 
 typedef int vbe_find_func(struct cli *cli, struct backend *b, void *priv);
-int VBE_Find(struct cli *cli, const char *matcher, vbe_find_func *func,
-    void *priv);
+int VBE_Find(struct cli *cli, struct VCL_conf *vcc, const char *matcher,
+    vbe_find_func *func, void *priv);
 
 
 /* cache_backend_poll.c */
diff --git a/bin/varnishd/cache/cache_backend_cfg.c b/bin/varnishd/cache/cache_backend_cfg.c
index 46713e9..a4e3a70 100644
--- a/bin/varnishd/cache/cache_backend_cfg.c
+++ b/bin/varnishd/cache/cache_backend_cfg.c
@@ -155,14 +155,18 @@ vbe_str2adminhealth(const char *wstate)
  */
 
 int
-VBE_Find(struct cli *cli, const char *matcher, vbe_find_func *func, void *priv)
+VBE_Find(struct cli *cli, struct VCL_conf *vcc, const char *matcher,
+    vbe_find_func *func, void *priv)
 {
 	int i, found = 0;
 	struct vsb *vsb;
-	struct VCL_conf *vcc = NULL;
 	struct backend *b;
 
-	VCL_Refresh(&vcc);
+	if (vcc == NULL) {
+		AN(cli);
+		VCL_Refresh(&vcc);
+	}
+
 	AN(vcc);
 	vsb = VSB_new_auto();
 	AN(vsb);
@@ -248,7 +252,7 @@ cli_backend_list(struct cli *cli, const char * const *av, void *priv)
 		return;
 	}
 	VCLI_Out(cli, "%-30s %-10s %s", "Backend name", "Admin", "Probe");
-	(void)VBE_Find(cli, av[2], do_list, &probes);
+	(void)VBE_Find(cli, NULL, av[2], do_list, &probes);
 }
 
 /*---------------------------------------------------------------------*/
@@ -287,7 +291,7 @@ cli_backend_set_health(struct cli *cli, const char * const *av, void *priv)
 		VCLI_SetResult(cli, CLIS_PARAM);
 		return;
 	}
-	n = VBE_Find(cli, av[2], do_set_health, &state);
+	n = VBE_Find(cli, NULL, av[2], do_set_health, &state);
 	if (n == 0) {
 		VCLI_Out(cli, "No Backends matches");
 		VCLI_SetResult(cli, CLIS_PARAM);
diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index f7a3f82..ee41d28 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -186,6 +186,8 @@ vcl_set_state(struct vcls *vcl, const char *state)
 
 	INIT_OBJ(&ctx, VRT_CTX_MAGIC);
 	ctx.handling = &hand;
+	ctx.vcl = vcl->conf;
+	AN(ctx.vcl);
 	(void)vcl->conf->event_vcl(&ctx,
 	    vcl->warm ? VCL_EVENT_WARM : VCL_EVENT_COLD);
 }
@@ -414,6 +416,8 @@ ccf_config_use(struct cli *cli, const char * const *av, void *priv)
 	INIT_OBJ(&ctx, VRT_CTX_MAGIC);
 	ctx.handling = &hand;
 	ctx.cli = cli;
+	ctx.vcl = vcl->conf;
+	AN(ctx.vcl);
 	if (vcl->conf->event_vcl(&ctx, VCL_EVENT_USE)) {
 		VCLI_Out(cli, "VCL \"%s\" Failed to activate", av[2]);
 		VCLI_SetResult(cli, CLIS_CANT);
diff --git a/include/vrt.h b/include/vrt.h
index a0fc083..f925957 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -234,8 +234,7 @@ void VRT_synth_page(VRT_CTX, const char *, ...);
 /* Backend related */
 void VRT_init_vbe(VRT_CTX, struct director **, const struct vrt_backend *);
 #ifdef VCL_RET_MAX
-void VRT_event_vbe(enum vcl_event_e, const struct director *,
-    const struct vrt_backend *);
+void VRT_event_vbe(VRT_CTX, enum vcl_event_e);
 #endif
 void VRT_fini_vbe(struct director **, const struct vrt_backend *);
 void VRT_event_vdi(VRT_CTX, struct director *dir);
diff --git a/lib/libvcc/vcc_backend.c b/lib/libvcc/vcc_backend.c
index d475dc6..1a0ad8e 100644
--- a/lib/libvcc/vcc_backend.c
+++ b/lib/libvcc/vcc_backend.c
@@ -420,9 +420,6 @@ vcc_ParseHostDef(struct vcc *tl, const struct token *t_be)
 	VSB_printf(ifp->fin,
 	    "\tVRT_fini_vbe(&VGCDIR(%s), &vgc_dir_priv_%s);",
 	    vgcname, vgcname);
-	VSB_printf(ifp->event,
-	    "\tVRT_event_vbe(ev, VGCDIR(%s), &vgc_dir_priv_%s);",
-	    vgcname, vgcname);
 	tl->ndirector++;
 }
 
diff --git a/lib/libvcc/vcc_compile.c b/lib/libvcc/vcc_compile.c
index c1a9299..3d649cb 100644
--- a/lib/libvcc/vcc_compile.c
+++ b/lib/libvcc/vcc_compile.c
@@ -349,12 +349,7 @@ EmitInitFini(const struct vcc *tl)
 	Fc(tl, 0, "\tif (ev == VCL_EVENT_DISCARD)\n");
 	Fc(tl, 0, "\t\treturn(VGC_Discard(ctx));\n");
 	Fc(tl, 0, "\t\n");
-	VTAILQ_FOREACH(p, &tl->inifin, list) {
-		AZ(VSB_finish(p->event));
-		if (VSB_len(p->event))
-			Fc(tl, 0, "\t/* %u */\n%s\n", p->n, VSB_data(p->event));
-		VSB_delete(p->event);
-	}
+	Fc(tl, 0, "\tVRT_event_vbe(ctx, ev);\n");
 	Fc(tl, 0, "\treturn (0);\n");
 	Fc(tl, 0, "}\n");
 }
diff --git a/lib/libvmod_debug/vmod_debug_dyn.c b/lib/libvmod_debug/vmod_debug_dyn.c
index 106b568..c91577b 100644
--- a/lib/libvmod_debug/vmod_debug_dyn.c
+++ b/lib/libvmod_debug/vmod_debug_dyn.c
@@ -86,7 +86,6 @@ vmod_dyn__init(VRT_CTX, struct vmod_debug_dyn **op,
 	if (o->dir)
 		*op = o;
 
-	VRT_event_vbe(VCL_EVENT_WARM, o->dir, &o->vrt);
 	VRT_event_vdi(ctx, o->dir);
 }
 
@@ -99,7 +98,6 @@ vmod_dyn__fini(struct vmod_debug_dyn **op)
 	CAST_OBJ_NOTNULL(o, *op, VMOD_DEBUG_DYN_MAGIC);
 	CHECK_OBJ_NOTNULL(&o->vrt, VRT_BACKEND_MAGIC);
 	AN(o->dir);
-	VRT_event_vbe(VCL_EVENT_COLD, o->dir, &o->vrt);
 	VRT_fini_vbe(&o->dir, &o->vrt);
 	AZ(o->dir);
 	free(o->addr);
-- 
2.1.0

_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to