r2937 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Fri Jul 11 22:41:40 CEST 2008


Author: phk
Date: 2008-07-11 22:41:39 +0200 (Fri, 11 Jul 2008)
New Revision: 2937

Added:
   trunk/varnish-cache/bin/varnishd/cache_backend_poll.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_backend.h
   trunk/varnish-cache/bin/varnishd/cache_backend_cfg.c
   trunk/varnish-cache/bin/varnishd/cache_cli.c
Log:
Add scaffold code for backend polling.

It doesn't actually do anything yet.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2008-07-11 19:49:20 UTC (rev 2936)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2008-07-11 20:41:39 UTC (rev 2937)
@@ -13,6 +13,7 @@
 	cache_acceptor_kqueue.c \
 	cache_backend.c \
 	cache_backend_cfg.c \
+	cache_backend_poll.c \
 	cache_ban.c \
 	cache_center.c \
 	cache_cli.c \

Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2008-07-11 19:49:20 UTC (rev 2936)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2008-07-11 20:41:39 UTC (rev 2937)
@@ -420,6 +420,7 @@
 void VBE_free_bereq(struct bereq *bereq);
 void VBE_UpdateHealth(const struct sess *sp, const struct vbe_conn *, int);
 void VBE_AddHostHeader(const struct sess *sp);
+void VBE_Poll(void);
 
 /* cache_backend_cfg.c */
 void VBE_Init(void);

Modified: trunk/varnish-cache/bin/varnishd/cache_backend.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend.h	2008-07-11 19:49:20 UTC (rev 2936)
+++ trunk/varnish-cache/bin/varnishd/cache_backend.h	2008-07-11 20:41:39 UTC (rev 2937)
@@ -68,6 +68,9 @@
  *
  */
 
+struct vbp_target;
+struct vrt_backend_probe;
+
 /* Backend indstance */
 struct backend {
 	unsigned		magic;
@@ -91,12 +94,17 @@
 
 	VTAILQ_HEAD(, vbe_conn)	connlist;
 
+	struct vbp_target	*probe;
 	int			health;
 };
 
+/* cache_backend.c */
+void VBE_ReleaseConn(struct vbe_conn *vc);
+
 /* cache_backend_cfg.c */
 extern MTX VBE_mtx;
 void VBE_DropRefLocked(struct backend *b);
 
-/* cache_backend.c */
-void VBE_ReleaseConn(struct vbe_conn *vc);
+/* cache_backend_poll.c */
+void VBP_Start(struct backend *b, struct vrt_backend_probe const *p);
+void VBP_Stop(struct backend *b);

Modified: trunk/varnish-cache/bin/varnishd/cache_backend_cfg.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend_cfg.c	2008-07-11 19:49:20 UTC (rev 2936)
+++ trunk/varnish-cache/bin/varnishd/cache_backend_cfg.c	2008-07-11 20:41:39 UTC (rev 2937)
@@ -71,6 +71,37 @@
 }
 
 /*--------------------------------------------------------------------
+ */
+
+static void
+VBE_Nuke(struct backend *b)
+{
+	VTAILQ_REMOVE(&backends, b, list);
+	free(b->ident);
+	free(b->hosthdr);
+	free(b->ipv4);
+	free(b->ipv6);
+	b->magic = 0;
+	free(b);
+	VSL_stats->n_backend--;
+}
+
+/*--------------------------------------------------------------------
+ */
+
+void
+VBE_Poll(void)
+{
+	struct backend *b, *b2;
+
+	ASSERT_CLI();
+	VTAILQ_FOREACH_SAFE(b, &backends, list, b2) {
+		if (b->refcount == 0 && b->probe == NULL)
+			VBE_Nuke(b);
+	}
+}
+
+/*--------------------------------------------------------------------
  * Drop a reference to a backend.
  * The last reference must come from the watcher in the CLI thread,
  * as only that thread is allowed to clean up the backend list.
@@ -91,20 +122,16 @@
 		return;
 
 	ASSERT_CLI();
-	VTAILQ_REMOVE(&backends, b, list);
 	VTAILQ_FOREACH_SAFE(vbe, &b->connlist, list, vbe2) {
 		VTAILQ_REMOVE(&b->connlist, vbe, list);
 		if (vbe->fd >= 0)
 			AZ(close(vbe->fd));
 		VBE_ReleaseConn(vbe);
 	}
-	free(b->ident);
-	free(b->hosthdr);
-	free(b->ipv4);
-	free(b->ipv6);
-	b->magic = 0;
-	free(b);
-	VSL_stats->n_backend--;
+	if (b->probe != NULL)
+		VBP_Stop(b);
+	else
+		VBE_Nuke(b);
 }
 
 void
@@ -206,6 +233,7 @@
 
 	assert(b->ipv4 != NULL || b->ipv6 != NULL);
 
+	VBP_Start(b, &vb->probe);
 	VTAILQ_INSERT_TAIL(&backends, b, list);
 	VSL_stats->n_backend++;
 	return (b);

Added: trunk/varnish-cache/bin/varnishd/cache_backend_poll.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend_poll.c	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishd/cache_backend_poll.c	2008-07-11 20:41:39 UTC (rev 2937)
@@ -0,0 +1,114 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2008 Linpro AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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.
+ *
+ * $Id: cache_backend_cfg.c 2905 2008-07-08 10:09:03Z phk $
+ *
+ * Poll backends for collection of health statistics
+ *
+ * We co-opt threads from the worker pool for probing the backends,
+ * but we want to avoid a potentially messy cleanup operation when we
+ * retire the backend, so the thread owns the health information, which
+ * the backend references, rather than the other way around.
+ *
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <poll.h>
+
+#include <sys/socket.h>
+
+#include "shmlog.h"
+#include "cache.h"
+#include "mgt_event.h"
+#include "vrt.h"
+#include "cache_backend.h"
+
+struct vbp_target {
+	unsigned			magic;
+#define VBP_TARGET_MAGIC		0x6b7cb656
+
+	struct backend			*backend;
+	struct workreq			wrq;
+	int				stop;
+};
+
+static void
+vbp_wrk_poll_backend(struct worker *w, void *priv)
+{
+	struct vbp_target *vt;
+
+	(void)w;
+	CAST_OBJ_NOTNULL(vt, priv, VBP_TARGET_MAGIC);
+	THR_Name("backend poll");
+
+	while (!vt->stop) {
+		printf("Poke backend %s\n", vt->backend->vcl_name);
+		sleep(1);
+	}
+	vt->backend->probe = NULL;
+	FREE_OBJ(vt);
+	THR_Name("cache-worker");
+}
+
+void
+VBP_Start(struct backend *b, struct vrt_backend_probe const *p)
+{
+	struct vbp_target *vt;
+
+	ASSERT_CLI();
+
+	/* Is probing even configured ? */
+	if (p->request == NULL)
+		return;
+
+	ALLOC_OBJ(vt, VBP_TARGET_MAGIC);
+	AN(vt);
+	vt->backend = b;
+	b->probe = vt;
+
+	vt->wrq.func = vbp_wrk_poll_backend;
+	vt->wrq.priv = vt;
+	if (WRK_Queue(&vt->wrq) == 0)
+		return;
+	assert(0 == __LINE__);
+	b->probe = NULL;
+	FREE_OBJ(vt);
+}
+
+void
+VBP_Stop(struct backend *b)
+{
+	if (b->probe == NULL)
+		return;
+	b->probe->stop = 1;
+}

Modified: trunk/varnish-cache/bin/varnishd/cache_cli.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_cli.c	2008-07-11 19:49:20 UTC (rev 2936)
+++ trunk/varnish-cache/bin/varnishd/cache_cli.c	2008-07-11 20:41:39 UTC (rev 2937)
@@ -103,6 +103,7 @@
 	cli = priv;
 	VSL(SLT_CLI, 0, "Rd %s", p);
 	VCL_Poll();
+	VBE_Poll();
 	vsb_clear(cli->sb);
 	LOCK(&cli_mtx);
 	cli_dispatch(cli, ccf_master_cli, p);




More information about the varnish-commit mailing list