[master] 81b988c Create a new vmod "directors" and move the demo-rr director into it, it is no longer a demo, it is going to replace the "built-in" rr director.
Poul-Henning Kamp
phk at varnish-cache.org
Thu Mar 7 10:52:09 CET 2013
commit 81b988c15ba74505c4aa859c81b886a76345879e
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Thu Mar 7 09:51:10 2013 +0000
Create a new vmod "directors" and move the demo-rr director into it,
it is no longer a demo, it is going to replace the "built-in" rr
director.
Other directors will follow as time permits.
diff --git a/bin/varnishd/flint.sh b/bin/varnishd/flint.sh
index c32c01b..0694b78 100755
--- a/bin/varnishd/flint.sh
+++ b/bin/varnishd/flint.sh
@@ -26,6 +26,7 @@ flexelint \
../../lib/libvcl/*.c \
../../lib/libvmod_std/*.c \
../../lib/libvmod_debug/*.c \
+ ../../lib/libvmod_directors/*.c \
2>&1 | tee _.fl
if [ -f _.fl.old ] ; then
diff --git a/bin/varnishtest/tests/m00009.vtc b/bin/varnishtest/tests/m00009.vtc
index c56f2ef..cad9b96 100644
--- a/bin/varnishtest/tests/m00009.vtc
+++ b/bin/varnishtest/tests/m00009.vtc
@@ -1,4 +1,4 @@
-varnishtest "Test vmod.debug round robin director"
+varnishtest "Test vmod.directors round robin director"
server s1 {
@@ -25,9 +25,9 @@ server s4 {
varnish v1 -vcl+backend {
- import debug from "${topbuild}/lib/libvmod_debug/.libs/libvmod_debug.so" ;
+ import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so" ;
sub vcl_init {
- new rr = debug.rr();
+ new rr = directors.round_robin();
rr.add_backend(s1);
rr.add_backend(s2);
rr.add_backend(s3);
diff --git a/bin/varnishtest/tests/m00010.vtc b/bin/varnishtest/tests/m00010.vtc
index 332d16e..63ba40d 100644
--- a/bin/varnishtest/tests/m00010.vtc
+++ b/bin/varnishtest/tests/m00010.vtc
@@ -1,4 +1,4 @@
-varnishtest "Test vmod.debug round robin director"
+varnishtest "Test vmod.directors round robin director in stacked fashion"
server s1 {
@@ -25,17 +25,17 @@ server s4 {
varnish v1 -vcl+backend {
- import debug from "${topbuild}/lib/libvmod_debug/.libs/libvmod_debug.so" ;
+ import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so" ;
sub vcl_init {
- new rr1 = debug.rr();
+ new rr1 = directors.round_robin();
rr1.add_backend(s1);
rr1.add_backend(s3);
- new rr2 = debug.rr();
+ new rr2 = directors.round_robin();
rr2.add_backend(s2);
rr2.add_backend(s4);
- new rr3 = debug.rr();
+ new rr3 = directors.round_robin();
rr3.add_backend(rr1.backend());
rr3.add_backend(rr2.backend());
}
diff --git a/configure.ac b/configure.ac
index 597ae29..e918ad7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -607,6 +607,7 @@ AC_CONFIG_FILES([
lib/libvgz/Makefile
lib/libvmod_debug/Makefile
lib/libvmod_std/Makefile
+ lib/libvmod_directors/Makefile
lib/libjemalloc/Makefile
man/Makefile
redhat/Makefile
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4fcc0ff..080f1b7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -8,6 +8,7 @@ SUBDIRS = \
libvgz \
libvmod_debug \
libvmod_std \
+ libvmod_directors \
@JEMALLOC_SUBDIR@
DIST_SUBDIRS = \
diff --git a/lib/libvmod_debug/Makefile.am b/lib/libvmod_debug/Makefile.am
index a4f9fb7..7e2c849 100644
--- a/lib/libvmod_debug/Makefile.am
+++ b/lib/libvmod_debug/Makefile.am
@@ -15,8 +15,7 @@ libvmod_debug_la_LDFLAGS = $(AM_LDFLAGS) -module -export-dynamic -avoid-version
libvmod_debug_la_SOURCES = \
vmod_debug.c \
- vmod_debug_obj.c \
- vmod_debug_rr.c
+ vmod_debug_obj.c
nodist_libvmod_debug_la_SOURCES = \
vcc_if.c \
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 8451c6c..57d8721 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -35,8 +35,3 @@ Object obj(STRING) {
Method STRING .foo(STRING why)
Method TIME .date()
}
-
-Object rr() {
- Method VOID .add_backend(BACKEND)
- Method BACKEND .backend()
-}
diff --git a/lib/libvmod_debug/vmod_debug_rr.c b/lib/libvmod_debug/vmod_debug_rr.c
deleted file mode 100644
index a326051..0000000
--- a/lib/libvmod_debug/vmod_debug_rr.c
+++ /dev/null
@@ -1,161 +0,0 @@
-/*-
- * 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"
-
-struct vmod_debug_rr_entry {
- unsigned magic;
-#define VMOD_DEBUG_RR_ENTRY_MAGIC 0xa80970cf
- VTAILQ_ENTRY(vmod_debug_rr_entry) list;
- VCL_BACKEND be;
-};
-
-struct vmod_debug_rr {
- unsigned magic;
-#define VMOD_DEBUG_RR_MAGIC 0x99f4b726
- VTAILQ_HEAD(, vmod_debug_rr_entry) listhead;
- int nbe;
- pthread_mutex_t mtx;
- struct director *dir;
-};
-
-static unsigned
-vmod_rr_healthy(const struct director *dir, const struct req *req)
-{
- struct vmod_debug_rr_entry *ep;
- struct vmod_debug_rr *rr;
- unsigned retval = 0;
-
- CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DEBUG_RR_MAGIC);
- AZ(pthread_mutex_lock(&rr->mtx));
- VTAILQ_FOREACH(ep, &rr->listhead, list) {
- if (ep->be->healthy(ep->be, req)) {
- retval = 1;
- break;
- }
- }
- AZ(pthread_mutex_unlock(&rr->mtx));
- return (retval);
-}
-
-static struct vbc *
-vmod_rr_getfd(const struct director *dir, struct req *req)
-{
- struct vmod_debug_rr_entry *ep = NULL;
- struct vmod_debug_rr *rr;
- int i;
-
- CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DEBUG_RR_MAGIC);
- AZ(pthread_mutex_lock(&rr->mtx));
- for (i = 0; i < rr->nbe; i++) {
- ep = VTAILQ_FIRST(&rr->listhead);
- VTAILQ_REMOVE(&rr->listhead, ep, list);
- VTAILQ_INSERT_TAIL(&rr->listhead, ep, list);
- if (ep->be->healthy(ep->be, req))
- break;
- }
- AZ(pthread_mutex_unlock(&rr->mtx));
- if (i == rr->nbe || ep == NULL)
- return (NULL);
- return (ep->be->getfd(ep->be, req));
-}
-
-VCL_VOID
-vmod_rr__init(struct req *req, struct vmod_debug_rr **rrp, const char *vcl_name)
-{
- struct vmod_debug_rr *rr;
-
- (void)req;
-
- AN(rrp);
- AZ(*rrp);
- ALLOC_OBJ(rr, VMOD_DEBUG_RR_MAGIC);
- AN(rr);
- *rrp = rr;
- AZ(pthread_mutex_init(&rr->mtx, NULL));
- VTAILQ_INIT(&rr->listhead);
- ALLOC_OBJ(rr->dir, DIRECTOR_MAGIC);
- AN(rr->dir);
- REPLACE(rr->dir->vcl_name, vcl_name);
- rr->dir->priv = rr;
- rr->dir->healthy = vmod_rr_healthy;
- rr->dir->getfd = vmod_rr_getfd;
-}
-
-VCL_VOID
-vmod_rr__fini(struct req *req, struct vmod_debug_rr **rrp)
-{
- struct vmod_debug_rr *rr;
- struct vmod_debug_rr_entry *ep;
-
- (void)req;
-
- rr = *rrp;
- *rrp = NULL;
- CHECK_OBJ_NOTNULL(rr, VMOD_DEBUG_RR_MAGIC);
-
- AZ(pthread_mutex_destroy(&rr->mtx));
- while (!VTAILQ_EMPTY(&rr->listhead)) {
- ep = VTAILQ_FIRST(&rr->listhead);
- VTAILQ_REMOVE(&rr->listhead, ep, list);
- FREE_OBJ(ep);
- }
- REPLACE(rr->dir->vcl_name, NULL);
- FREE_OBJ(rr->dir);
- FREE_OBJ(rr);
-}
-
-VCL_VOID
-vmod_rr_add_backend(struct req *req, struct vmod_debug_rr * rr, VCL_BACKEND be)
-{
- struct vmod_debug_rr_entry *ep;
- (void)req;
-
- ALLOC_OBJ(ep, VMOD_DEBUG_RR_ENTRY_MAGIC);
- AN(ep);
- ep->be = be;
- AZ(pthread_mutex_lock(&rr->mtx));
- VTAILQ_INSERT_TAIL(&rr->listhead, ep, list);
- rr->nbe++;
- AZ(pthread_mutex_unlock(&rr->mtx));
-}
-
-VCL_BACKEND __match_proto__()
-vmod_rr_backend(struct req *req, struct vmod_debug_rr *rr)
-{
- (void)req;
- return (rr->dir);
-}
diff --git a/lib/libvmod_directors/Makefile.am b/lib/libvmod_directors/Makefile.am
new file mode 100644
index 0000000..1e6ee13
--- /dev/null
+++ b/lib/libvmod_directors/Makefile.am
@@ -0,0 +1,31 @@
+#
+AM_LDFLAGS = $(AM_LT_LDFLAGS)
+
+AM_CPPFLAGS = \
+ -I$(top_srcdir)/include \
+ -I$(top_srcdir)/bin/varnishd \
+ -I$(top_builddir)/include
+
+vmoddir = $(pkglibdir)/vmods
+vmod_srcdir = $(top_srcdir)/lib/libvmod_directors
+vmodtool = $(top_srcdir)/lib/libvcl/vmodtool.py
+noinst_LTLIBRARIES = libvmod_directors.la
+
+libvmod_directors_la_LDFLAGS = $(AM_LDFLAGS) -module -export-dynamic -avoid-version -shared -rpath /nowhere
+
+libvmod_directors_la_SOURCES = \
+ round_robin.c
+
+nodist_libvmod_directors_la_SOURCES = \
+ vcc_if.c \
+ vcc_if.h
+
+# BUILT_SOURCES is only a hack and dependency tracking does not help for the first build
+vmod_directors.lo: vcc_if.h
+
+vcc_if.c vcc_if.h: $(vmodtool) $(vmod_srcdir)/vmod.vcc
+ @PYTHON@ $(vmodtool) $(vmod_srcdir)/vmod.vcc
+
+EXTRA_DIST = vmod.vcc
+
+CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h
diff --git a/lib/libvmod_directors/round_robin.c b/lib/libvmod_directors/round_robin.c
new file mode 100644
index 0000000..ce89039
--- /dev/null
+++ b/lib/libvmod_directors/round_robin.c
@@ -0,0 +1,163 @@
+/*-
+ * 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"
+
+struct rr_entry {
+ unsigned magic;
+#define RR_ENTRY_MAGIC 0xa80970cf
+ VTAILQ_ENTRY(rr_entry) list;
+ VCL_BACKEND be;
+};
+
+struct vmod_directors_round_robin {
+ unsigned magic;
+#define VMOD_DEBUG_RR_MAGIC 0x99f4b726
+ VTAILQ_HEAD(, rr_entry) listhead;
+ int nbe;
+ pthread_mutex_t mtx;
+ struct director *dir;
+};
+
+static unsigned
+vmod_rr_healthy(const struct director *dir, const struct req *req)
+{
+ struct rr_entry *ep;
+ struct vmod_directors_round_robin *rr;
+ unsigned retval = 0;
+
+ CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DEBUG_RR_MAGIC);
+ AZ(pthread_mutex_lock(&rr->mtx));
+ VTAILQ_FOREACH(ep, &rr->listhead, list) {
+ if (ep->be->healthy(ep->be, req)) {
+ retval = 1;
+ break;
+ }
+ }
+ AZ(pthread_mutex_unlock(&rr->mtx));
+ return (retval);
+}
+
+static struct vbc *
+vmod_rr_getfd(const struct director *dir, struct req *req)
+{
+ struct rr_entry *ep = NULL;
+ struct vmod_directors_round_robin *rr;
+ int i;
+
+ CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DEBUG_RR_MAGIC);
+ AZ(pthread_mutex_lock(&rr->mtx));
+ for (i = 0; i < rr->nbe; i++) {
+ ep = VTAILQ_FIRST(&rr->listhead);
+ VTAILQ_REMOVE(&rr->listhead, ep, list);
+ VTAILQ_INSERT_TAIL(&rr->listhead, ep, list);
+ if (ep->be->healthy(ep->be, req))
+ break;
+ }
+ AZ(pthread_mutex_unlock(&rr->mtx));
+ if (i == rr->nbe || ep == NULL)
+ return (NULL);
+ return (ep->be->getfd(ep->be, req));
+}
+
+VCL_VOID
+vmod_round_robin__init(struct req *req, struct vmod_directors_round_robin **rrp,
+ const char *vcl_name)
+{
+ struct vmod_directors_round_robin *rr;
+
+ (void)req;
+
+ AN(rrp);
+ AZ(*rrp);
+ ALLOC_OBJ(rr, VMOD_DEBUG_RR_MAGIC);
+ AN(rr);
+ *rrp = rr;
+ AZ(pthread_mutex_init(&rr->mtx, NULL));
+ VTAILQ_INIT(&rr->listhead);
+ ALLOC_OBJ(rr->dir, DIRECTOR_MAGIC);
+ AN(rr->dir);
+ REPLACE(rr->dir->vcl_name, vcl_name);
+ rr->dir->priv = rr;
+ rr->dir->healthy = vmod_rr_healthy;
+ rr->dir->getfd = vmod_rr_getfd;
+}
+
+VCL_VOID
+vmod_round_robin__fini(struct req *req, struct vmod_directors_round_robin **rrp)
+{
+ struct vmod_directors_round_robin *rr;
+ struct rr_entry *ep;
+
+ (void)req;
+
+ rr = *rrp;
+ *rrp = NULL;
+ CHECK_OBJ_NOTNULL(rr, VMOD_DEBUG_RR_MAGIC);
+
+ AZ(pthread_mutex_destroy(&rr->mtx));
+ while (!VTAILQ_EMPTY(&rr->listhead)) {
+ ep = VTAILQ_FIRST(&rr->listhead);
+ VTAILQ_REMOVE(&rr->listhead, ep, list);
+ FREE_OBJ(ep);
+ }
+ REPLACE(rr->dir->vcl_name, NULL);
+ FREE_OBJ(rr->dir);
+ FREE_OBJ(rr);
+}
+
+VCL_VOID
+vmod_round_robin_add_backend(struct req *req, struct vmod_directors_round_robin * rr,
+ VCL_BACKEND be)
+{
+ struct rr_entry *ep;
+ (void)req;
+
+ ALLOC_OBJ(ep, RR_ENTRY_MAGIC);
+ AN(ep);
+ ep->be = be;
+ AZ(pthread_mutex_lock(&rr->mtx));
+ VTAILQ_INSERT_TAIL(&rr->listhead, ep, list);
+ rr->nbe++;
+ AZ(pthread_mutex_unlock(&rr->mtx));
+}
+
+VCL_BACKEND __match_proto__()
+vmod_round_robin_backend(struct req *req, struct vmod_directors_round_robin *rr)
+{
+ (void)req;
+ return (rr->dir);
+}
diff --git a/lib/libvmod_directors/vmod.vcc b/lib/libvmod_directors/vmod.vcc
new file mode 100644
index 0000000..0cf0251
--- /dev/null
+++ b/lib/libvmod_directors/vmod.vcc
@@ -0,0 +1,33 @@
+#-
+# 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.
+
+Module directors
+
+Object round_robin() {
+ Method VOID .add_backend(BACKEND)
+ Method BACKEND .backend()
+}
More information about the varnish-commit
mailing list