[master] 1ccba69 Move also the fall-back director to the VMOD

Poul-Henning Kamp phk at varnish-cache.org
Mon Mar 11 12:47:05 CET 2013


commit 1ccba69ce528d5923b0c4fb62fd38412e9332bb1
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Mar 11 11:46:49 2013 +0000

    Move also the fall-back director to the VMOD

diff --git a/bin/varnishtest/tests/b00016.vtc b/bin/varnishtest/tests/b00016.vtc
index db68663..17e38f4 100644
--- a/bin/varnishtest/tests/b00016.vtc
+++ b/bin/varnishtest/tests/b00016.vtc
@@ -46,13 +46,17 @@ client c1 {
 } -run
 
 varnish v1 -vcl { 
-	director baz round-robin {
-		 { .backend = {
-		 	.host = "${bad_ip}"; .port = "9080";
-		} }
+	import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so" ;
+	backend b1 {
+		.host = "${bad_ip}"; .port = "9080";
+	}
+	sub vcl_init {
+		new baz = directors.round_robin();
+		baz.add_backend(b1);
 	}
 
 	sub vcl_recv {
+	    set req.backend = baz.backend();
 	    error 200 "ok";
 	}
 
diff --git a/bin/varnishtest/tests/v00009.vtc b/bin/varnishtest/tests/v00009.vtc
index 5a5d769..53e569b 100644
--- a/bin/varnishtest/tests/v00009.vtc
+++ b/bin/varnishtest/tests/v00009.vtc
@@ -23,15 +23,18 @@ server s4 {
 
 
 varnish v1 -vcl+backend {
-	director batman round-robin {
-		{ .backend = s1; }
-		{ .backend = s2; }
-		{ .backend = s3; }
-		{ .backend = s4; }
+	import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so" ;
+
+	sub vcl_init {
+		new batman = directors.round_robin();
+		batman.add_backend(s1);
+		batman.add_backend(s2);
+		batman.add_backend(s3);
+		batman.add_backend(s4);
 	}
 
 	sub vcl_recv {
-		set req.backend = batman;
+		set req.backend = batman.backend();
 	}
 } -start
 
diff --git a/bin/varnishtest/tests/v00036.vtc b/bin/varnishtest/tests/v00036.vtc
index 323f80c..d38498c 100644
--- a/bin/varnishtest/tests/v00036.vtc
+++ b/bin/varnishtest/tests/v00036.vtc
@@ -15,32 +15,25 @@ server s3 {
         txresp -hdr "Foo: 3"
 } -start
 
-varnish v1 -vcl {
-        backend b1 {
-                .host = "${s1_addr}";
-                .port = "${s1_port}";
-        }
-        backend b2 {
-                .host = "${s2_addr}";
-                .port = "${s2_port}";
-        }
-        backend b3 {
-                .host = "${s3_addr}";
-                .port = "${s3_port}";
-        }
-        director f1 fallback {
-                { .backend = b1; }
-                { .backend = b2; }
-                { .backend = b3; }
-        }
+varnish v1 -vcl+backend {
+	import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so" ;
+
+	sub vcl_init {
+		new fb1 = directors.fall_back();
+		fb1.add_backend(s1);
+		fb1.add_backend(s2);
+		fb1.add_backend(s3);
+	}
+
         sub vcl_recv {
-                set req.backend = f1;
+                set req.backend = fb1.backend();
                 return (pass);
         }
+
 } -start
 
-varnish v1 -cliok "backend.set_health b1 sick"
-varnish v1 -cliok "backend.set_health b2 sick"
+varnish v1 -cliok "backend.set_health s1 sick"
+varnish v1 -cliok "backend.set_health s2 sick"
 
 client c1 {
         # s1 & s2 are both sick, expect response from s3
@@ -49,7 +42,7 @@ client c1 {
         expect resp.http.foo == "3"
 } -run
 
-varnish v1 -cliok "backend.set_health b2 healthy"
+varnish v1 -cliok "backend.set_health s2 healthy"
 
 client c1 {
         txreq
@@ -57,7 +50,7 @@ client c1 {
         expect resp.http.foo == "2"
 } -run
 
-varnish v1 -cliok "backend.set_health b1 healthy"
+varnish v1 -cliok "backend.set_health s1 healthy"
 
 client c1 {
         txreq
diff --git a/lib/libvmod_directors/Makefile.am b/lib/libvmod_directors/Makefile.am
index 971885a..e28ba46 100644
--- a/lib/libvmod_directors/Makefile.am
+++ b/lib/libvmod_directors/Makefile.am
@@ -16,6 +16,7 @@ libvmod_directors_la_LDFLAGS = $(AM_LDFLAGS) -module -export-dynamic -avoid-vers
 libvmod_directors_la_SOURCES = \
 	vdir.c \
 	vdir.h \
+	fall_back.c \
 	round_robin.c
 
 nodist_libvmod_directors_la_SOURCES = \
diff --git a/lib/libvmod_directors/fall_back.c b/lib/libvmod_directors/fall_back.c
new file mode 100644
index 0000000..64b53fe
--- /dev/null
+++ b/lib/libvmod_directors/fall_back.c
@@ -0,0 +1,121 @@
+/*-
+ * Copyright (c) 2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+
+#include "cache/cache.h"
+#include "cache/cache_backend.h"
+
+#include "vrt.h"
+#include "vcc_if.h"
+
+#include "vdir.h"
+
+struct vmod_directors_fall_back {
+	unsigned				magic;
+#define VMOD_DIRECTORS_FALL_BACK_MAGIC		0xad4e26ba
+	struct vdir				*vd;
+};
+
+static unsigned __match_proto__(vdi_healthy)
+vmod_rr_healthy(const struct director *dir, const struct req *req)
+{
+	struct vmod_directors_fall_back *rr;
+
+	CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_FALL_BACK_MAGIC);
+	return (vdir_any_healthy(rr->vd, req));
+}
+
+static struct vbc * __match_proto__(vdi_getfd_f)
+vmod_rr_getfd(const struct director *dir, struct req *req)
+{
+	struct vmod_directors_fall_back *rr;
+	unsigned u;
+	VCL_BACKEND be = NULL;
+
+	CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_FALL_BACK_MAGIC);
+	vdir_lock(rr->vd);
+	for (u = 0; u < rr->vd->n_backend; u++) {
+		be = rr->vd->backend[u];
+		CHECK_OBJ_NOTNULL(be, DIRECTOR_MAGIC);
+		if (be->healthy(be, req))
+			break;
+	}
+	vdir_unlock(rr->vd);
+	if (u == rr->vd->n_backend || be == NULL)
+		return (NULL);
+	return (be->getfd(be, req));
+}
+
+VCL_VOID __match_proto__()
+vmod_fall_back__init(struct req *req, struct vmod_directors_fall_back **rrp,
+    const char *vcl_name)
+{
+	struct vmod_directors_fall_back *rr;
+
+	AZ(req);
+	AN(rrp);
+	AZ(*rrp);
+	ALLOC_OBJ(rr, VMOD_DIRECTORS_FALL_BACK_MAGIC);
+	AN(rr);
+	*rrp = rr;
+	vdir_new(&rr->vd, vcl_name, vmod_rr_healthy, vmod_rr_getfd, rr);
+}
+
+VCL_VOID __match_proto__()
+vmod_fall_back__fini(struct req *req, struct vmod_directors_fall_back **rrp)
+{
+	struct vmod_directors_fall_back *rr;
+
+	AZ(req);
+	rr = *rrp;
+	*rrp = NULL;
+	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_FALL_BACK_MAGIC);
+	vdir_delete(&rr->vd);
+	FREE_OBJ(rr);
+}
+
+VCL_VOID __match_proto__()
+vmod_fall_back_add_backend(struct req *req,
+    struct vmod_directors_fall_back *rr, VCL_BACKEND be)
+{
+
+	(void)req;
+	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_FALL_BACK_MAGIC);
+	(void)vdir_add_backend(rr->vd, be, 0.0);
+}
+
+VCL_BACKEND __match_proto__()
+vmod_fall_back_backend(struct req *req, struct vmod_directors_fall_back *rr)
+{
+	(void)req;
+	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_FALL_BACK_MAGIC);
+	return (rr->vd->dir);
+}
diff --git a/lib/libvmod_directors/round_robin.c b/lib/libvmod_directors/round_robin.c
index 240bcb7..98df225 100644
--- a/lib/libvmod_directors/round_robin.c
+++ b/lib/libvmod_directors/round_robin.c
@@ -77,7 +77,7 @@ vmod_rr_getfd(const struct director *dir, struct req *req)
 	return (be->getfd(be, req));
 }
 
-VCL_VOID
+VCL_VOID __match_proto__()
 vmod_round_robin__init(struct req *req, struct vmod_directors_round_robin **rrp,
     const char *vcl_name)
 {
@@ -92,7 +92,7 @@ vmod_round_robin__init(struct req *req, struct vmod_directors_round_robin **rrp,
 	vdir_new(&rr->vd, vcl_name, vmod_rr_healthy, vmod_rr_getfd, rr);
 }
 
-VCL_VOID
+VCL_VOID __match_proto__()
 vmod_round_robin__fini(struct req *req, struct vmod_directors_round_robin **rrp)
 {
 	struct vmod_directors_round_robin *rr;
@@ -105,14 +105,14 @@ vmod_round_robin__fini(struct req *req, struct vmod_directors_round_robin **rrp)
 	FREE_OBJ(rr);
 }
 
-VCL_VOID
+VCL_VOID __match_proto__()
 vmod_round_robin_add_backend(struct req *req,
     struct vmod_directors_round_robin *rr, VCL_BACKEND be)
 {
 
 	(void)req;
 	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
-	vdir_add_backend(rr->vd, be, 0.0);
+	(void)vdir_add_backend(rr->vd, be, 0.0);
 }
 
 VCL_BACKEND __match_proto__()
diff --git a/lib/libvmod_directors/vmod.vcc b/lib/libvmod_directors/vmod.vcc
index 0cf0251..df85fc8 100644
--- a/lib/libvmod_directors/vmod.vcc
+++ b/lib/libvmod_directors/vmod.vcc
@@ -31,3 +31,8 @@ Object round_robin() {
 	Method VOID .add_backend(BACKEND)
 	Method BACKEND .backend()
 }
+
+Object fall_back() {
+	Method VOID .add_backend(BACKEND)
+	Method BACKEND .backend()
+}



More information about the varnish-commit mailing list