[prev in list] [next in list] [prev in thread] [next in thread] 

List:       varnish-dev
Subject:    [PATCH] add a remove_backend() method to each of the directors in the VMOD
From:       Geoff Simmons <geoff () uplex ! de>
Date:       2015-12-06 14:01:28
Message-ID: 56643FB8.1070302 () uplex ! de
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


Hello all,

As agreed at VDD. This patch requires the previous one that I sent to
varnish-dev (using rwlocks in the directors VMOD).


Best,
Geoff
-- 
** * * UPLEX - Nils Goroll Systemoptimierung

Scheffelstraße 32
22301 Hamburg

Tel +49 40 2880 5731
Mob +49 176 636 90917
Fax +49 40 42949753

http://uplex.de

["0001-add-a-remove_backend-method-to-each-of-the-directors.patch" (text/x-patch)]

From 002e858f3ffe0e7c90e03f92bc30b6b8c1e085cd Mon Sep 17 00:00:00 2001
From: Geoff Simmons <geoff@uplex.de>
Date: Sun, 6 Dec 2015 14:56:18 +0100
Subject: [PATCH] add a remove_backend() method to each of the directors in the
 VMOD

---
 bin/varnishtest/tests/d00000.vtc    | 20 +++++++++++++++++
 bin/varnishtest/tests/d00001.vtc    | 16 ++++++++++++++
 bin/varnishtest/tests/d00002.vtc    | 43 +++++++++++++++++++++++++++++++++++++
 bin/varnishtest/tests/d00003.vtc    | 28 ++++++++++++++++++++++++
 lib/libvmod_directors/fall_back.c   |  9 ++++++++
 lib/libvmod_directors/hash.c        | 10 +++++++++
 lib/libvmod_directors/random.c      |  8 +++++++
 lib/libvmod_directors/round_robin.c |  9 ++++++++
 lib/libvmod_directors/vdir.c        | 26 ++++++++++++++++++++++
 lib/libvmod_directors/vdir.h        |  1 +
 lib/libvmod_directors/vmod.vcc      | 30 ++++++++++++++++++++++++++
 11 files changed, 200 insertions(+)

diff --git a/bin/varnishtest/tests/d00000.vtc b/bin/varnishtest/tests/d00000.vtc
index a88634f..2ec92ff 100644
--- a/bin/varnishtest/tests/d00000.vtc
+++ b/bin/varnishtest/tests/d00000.vtc
@@ -31,6 +31,15 @@ varnish v1 -vcl+backend {
 		rr.add_backend(s4);
 	}
 
+	sub vcl_recv {
+		if (req.method == "DELETE") {
+			rr.remove_backend(s1);
+			rr.remove_backend(s2);
+			rr.remove_backend(s3);
+			return(synth(204));
+		}
+	}
+
 	sub vcl_backend_fetch {
 		set bereq.backend = rr.backend();
 	}
@@ -64,3 +73,14 @@ client c2 {
 	rxresp
 	expect resp.bodylen == 2
 } -run
+
+server s4 -start
+
+client c3 {
+	txreq -req "DELETE"
+	rxresp
+	expect resp.status == 204
+	txreq -url "/foo31"
+	rxresp
+	expect resp.bodylen == 4
+} -run
diff --git a/bin/varnishtest/tests/d00001.vtc b/bin/varnishtest/tests/d00001.vtc
index c373b4d..2311b0a 100644
--- a/bin/varnishtest/tests/d00001.vtc
+++ b/bin/varnishtest/tests/d00001.vtc
@@ -26,6 +26,10 @@ varnish v1 -vcl+backend {
 	}
 
 	sub vcl_recv {
+		if (req.method == "DELETE") {
+			fb1.remove_backend(s2);
+			return(synth(204));
+		}
 		return (pass);
 	}
 
@@ -59,3 +63,15 @@ client c1 {
 	rxresp
 	expect resp.http.foo == "1"
 } -run
+
+varnish v1 -cliok "backend.set_health s1 sick"
+server s3 -start
+
+client c1 {
+	txreq -req "DELETE"
+	rxresp
+	expect resp.status == 204
+	txreq
+	rxresp
+	expect resp.http.foo == "3"
+} -run
diff --git a/bin/varnishtest/tests/d00002.vtc b/bin/varnishtest/tests/d00002.vtc
index b477176..0a0ba98 100644
--- a/bin/varnishtest/tests/d00002.vtc
+++ b/bin/varnishtest/tests/d00002.vtc
@@ -33,3 +33,46 @@ client c1 {
 	rxresp
 	expect resp.http.where == "foo-->s1"
 } -run
+
+server s1 -start
+server s2 {
+	loop 20 {
+		rxreq
+		txresp
+	}
+} -start
+
+varnish v1 -vcl+backend {
+	import ${vmod_directors};
+
+	sub vcl_init {
+		new foo = directors.random();
+		foo.add_backend(s1, 1);
+		foo.add_backend(s2, 1);
+	}
+
+	sub vcl_recv {
+		if (req.method == "DELETE") {
+			foo.remove_backend(s1);
+			return(synth(204));
+		}
+		set req.backend_hint = foo.backend();
+		return (pass);
+	}
+
+	sub vcl_backend_response {
+		set beresp.http.where = bereq.backend + "-->" + beresp.backend;
+	}
+}
+
+client c1 {
+	txreq -req "DELETE"
+	rxresp
+	expect resp.status == 204
+	loop 20 {
+		txreq
+		rxresp
+		expect resp.status == 200
+		expect resp.http.where == "foo-->s2"
+	}
+} -run
diff --git a/bin/varnishtest/tests/d00003.vtc b/bin/varnishtest/tests/d00003.vtc
index 56b3035..e6182d7 100644
--- a/bin/varnishtest/tests/d00003.vtc
+++ b/bin/varnishtest/tests/d00003.vtc
@@ -30,6 +30,10 @@ varnish v1 -vcl+backend {
 	}
 
 	sub vcl_recv {
+		if (req.method == "DELETE") {
+			h1.remove_backend(s1);
+			return(synth(204));
+		}
 		if (req.url == "/nohdr") {
 			set req.backend_hint = h1.backend(req.http.Void);
 		} else if (req.url == "/emptystring") {
@@ -74,3 +78,27 @@ client c1 {
 	rxresp
 	expect resp.http.foo == "9"
 } -run
+
+server s2 -start
+
+client c1 {
+	txreq -req "DELETE"
+	rxresp
+	expect resp.status == 204
+
+	txreq
+	rxresp
+	expect resp.http.foo == "2"
+
+	txreq
+	rxresp
+	expect resp.http.foo == "4"
+
+	txreq
+	rxresp
+	expect resp.http.foo == "6"
+
+	txreq
+	rxresp
+	expect resp.http.foo == "8"
+} -run
diff --git a/lib/libvmod_directors/fall_back.c b/lib/libvmod_directors/fall_back.c
index 241a675..a54807b 100644
--- a/lib/libvmod_directors/fall_back.c
+++ b/lib/libvmod_directors/fall_back.c
@@ -117,6 +117,15 @@ vmod_fallback_add_backend(VRT_CTX,
 	(void)vdir_add_backend(rr->vd, be, 0.0);
 }
 
+VCL_VOID __match_proto__()
+vmod_fallback_remove_backend(VRT_CTX,
+    struct vmod_directors_fallback *fb, VCL_BACKEND be)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(fb, VMOD_DIRECTORS_FALLBACK_MAGIC);
+	(void)vdir_remove_backend(fb->vd, be);
+}
+
 VCL_BACKEND __match_proto__()
 vmod_fallback_backend(VRT_CTX,
     struct vmod_directors_fallback *rr)
diff --git a/lib/libvmod_directors/hash.c b/lib/libvmod_directors/hash.c
index a858a09..b77c279 100644
--- a/lib/libvmod_directors/hash.c
+++ b/lib/libvmod_directors/hash.c
@@ -85,6 +85,16 @@ vmod_hash_add_backend(VRT_CTX,
 	(void)vdir_add_backend(rr->vd, be, w);
 }
 
+VCL_VOID __match_proto__()
+vmod_hash_remove_backend(VRT_CTX,
+    struct vmod_directors_hash *rr, VCL_BACKEND be)
+{
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
+	(void)vdir_remove_backend(rr->vd, be);
+}
+
 VCL_BACKEND __match_proto__()
 vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr,
     const char *arg, ...)
diff --git a/lib/libvmod_directors/random.c b/lib/libvmod_directors/random.c
index 2f29887..c79e7e3 100644
--- a/lib/libvmod_directors/random.c
+++ b/lib/libvmod_directors/random.c
@@ -113,6 +113,14 @@ vmod_random_add_backend(VRT_CTX,
 	(void)vdir_add_backend(rr->vd, be, w);
 }
 
+VCL_VOID vmod_random_remove_backend(VRT_CTX,
+    struct vmod_directors_random *rr, VCL_BACKEND be)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
+	(void)vdir_remove_backend(rr->vd, be);
+}
+
 VCL_BACKEND __match_proto__()
 vmod_random_backend(VRT_CTX, struct vmod_directors_random *rr)
 {
diff --git a/lib/libvmod_directors/round_robin.c b/lib/libvmod_directors/round_robin.c
index 75ca9e4..3aee3ad 100644
--- a/lib/libvmod_directors/round_robin.c
+++ b/lib/libvmod_directors/round_robin.c
@@ -120,6 +120,15 @@ vmod_round_robin_add_backend(VRT_CTX,
 	(void)vdir_add_backend(rr->vd, be, 0.0);
 }
 
+VCL_VOID __match_proto__()
+vmod_round_robin_remove_backend(VRT_CTX,
+    struct vmod_directors_round_robin *rr, VCL_BACKEND be)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
+	(void)vdir_remove_backend(rr->vd, be);
+}
+
 VCL_BACKEND __match_proto__()
 vmod_round_robin_backend(VRT_CTX,
     struct vmod_directors_round_robin *rr)
diff --git a/lib/libvmod_directors/vdir.c b/lib/libvmod_directors/vdir.c
index 09d904a..c430288 100644
--- a/lib/libvmod_directors/vdir.c
+++ b/lib/libvmod_directors/vdir.c
@@ -138,6 +138,32 @@ vdir_add_backend(struct vdir *vd, VCL_BACKEND be, double weight)
 }
 
 unsigned
+vdir_remove_backend(struct vdir *vd, VCL_BACKEND be)
+{
+        unsigned u, n;
+
+        CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC);
+        if (be == NULL)
+                return vd->n_backend;
+        CHECK_OBJ(be, DIRECTOR_MAGIC);
+        vdir_wrlock(vd);
+        for (u = 0; u < vd->n_backend; u++)
+                if (vd->backend[u] == be)
+                        break;
+        if (u == vd->n_backend) {
+                vdir_unlock(vd);
+                return vd->n_backend;
+        }
+        vd->total_weight -= vd->weight[u];
+        n = vd->n_backend - u - 1;
+        memmove(&vd->backend[u], &vd->backend[u+1], n * sizeof(vd->backend[0]));
+        memmove(&vd->weight[u], &vd->weight[u+1], n * sizeof(vd->weight[0]));
+        vd->n_backend--;
+        vdir_unlock(vd);
+        return vd->n_backend;
+}
+
+unsigned
 vdir_any_healthy(struct vdir *vd, const struct busyobj *bo, double *changed)
 {
 	unsigned retval = 0;
diff --git a/lib/libvmod_directors/vdir.h b/lib/libvmod_directors/vdir.h
index 81cc3fa..76dbd0a 100644
--- a/lib/libvmod_directors/vdir.h
+++ b/lib/libvmod_directors/vdir.h
@@ -48,6 +48,7 @@ void vdir_rdlock(struct vdir *vd);
 void vdir_wrlock(struct vdir *vd);
 void vdir_unlock(struct vdir *vd);
 unsigned vdir_add_backend(struct vdir *, VCL_BACKEND be, double weight);
+unsigned vdir_remove_backend(struct vdir *, VCL_BACKEND be);
 unsigned vdir_any_healthy(struct vdir *, const struct busyobj *,
     double *changed);
 VCL_BACKEND vdir_pick_be(struct vdir *, double w);
diff --git a/lib/libvmod_directors/vmod.vcc b/lib/libvmod_directors/vmod.vcc
index 8522920..529db05 100644
--- a/lib/libvmod_directors/vmod.vcc
+++ b/lib/libvmod_directors/vmod.vcc
@@ -72,6 +72,14 @@ Example
 	vdir.add_backend(backend1);
 	vdir.add_backend(backend2);
 
+$Method VOID .remove_backend(BACKEND)
+
+Description
+	Remove a backend from the round-robin director.
+Example
+	vdir.remove_backend(backend1);
+	vdir.remove_backend(backend2);
+
 $Method BACKEND .backend()
 
 Description
@@ -103,6 +111,14 @@ Example
 	vdir.add_backend(backend1);
 	vdir.add_backend(backend2);
 
+$Method VOID .remove_backend(BACKEND)
+
+Description
+	Remove a backend from the director.
+Example
+	vdir.remove_backend(backend1);
+	vdir.remove_backend(backend2);
+
 $Method BACKEND .backend()
 
 Description
@@ -136,6 +152,13 @@ Example
 	vdir.add_backend(backend1, 10.0);
 	vdir.add_backend(backend2, 5.0);
 
+$Method VOID .remove_backend(BACKEND)
+
+Description
+	Remove a backend from the director.
+Example
+	vdir.remove_backend(backend1);
+	vdir.remove_backend(backend2);
 
 $Method BACKEND .backend()
 
@@ -170,6 +193,13 @@ Example
 	vdir.add_backend(backend1, 1.0);
 	vdir.add_backend(backend2, 1.0);
 
+$Method VOID .remove_backend(BACKEND)
+
+Description
+	Remove a backend from the director.
+Example
+	vdir.remove_backend(backend1);
+	vdir.remove_backend(backend2);
 
 $Method BACKEND .backend(STRING_LIST)
 
-- 
2.1.4


["signature.asc" (application/pgp-signature)]

_______________________________________________
varnish-dev mailing list
varnish-dev@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic