r2007 - in trunk/varnish-cache: bin/varnishd include lib/libvcl

phk at projects.linpro.no phk at projects.linpro.no
Mon Sep 24 13:25:16 CEST 2007


Author: phk
Date: 2007-09-24 13:25:15 +0200 (Mon, 24 Sep 2007)
New Revision: 2007

Modified:
   trunk/varnish-cache/bin/varnishd/cache_backend_simple.c
   trunk/varnish-cache/include/vrt.h
   trunk/varnish-cache/lib/libvcl/vcc_backend.c
   trunk/varnish-cache/lib/libvcl/vcc_fixed_token.c
Log:
Add code to VCC to output an identifier for each simple backend which
varnishd can use to decide if a backend state can be shared between
multiple VCL programs.
 
Originally the idea was to calculate a strong hash over the tokens 
which define the backend.  

Considering the relatively short lengths of backend declarations
and the infrequency of comparisons, we have opted for an extremely
weak hash instead:  We simply output the space separted tokens as
a string.
 
The net change in code for simple backends is marginal, but for
complex backends this will be a lot simpler to implement.



Modified: trunk/varnish-cache/bin/varnishd/cache_backend_simple.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend_simple.c	2007-09-24 08:23:34 UTC (rev 2006)
+++ trunk/varnish-cache/bin/varnishd/cache_backend_simple.c	2007-09-24 11:25:15 UTC (rev 2007)
@@ -49,6 +49,7 @@
 #define BES_MAGIC		0x015e17ac
 	char			*hostname;
 	char			*portname;
+	char			*ident;
 	struct addrinfo		*addr;
 	struct addrinfo		*last_addr;
 	double			dnsttl;
@@ -369,13 +370,9 @@
 		CHECK_OBJ_NOTNULL(b, BACKEND_MAGIC);
 		if (b->method != &backend_method_simple)
 			continue;
-		if (strcmp(b->vcl_name, t->name))
-			continue;
 		CAST_OBJ_NOTNULL(bes, b->priv, BES_MAGIC);
-		if (strcmp(bes->portname, t->port))
+		if (strcmp(bes->ident, t->ident))
 			continue;
-		if (strcmp(bes->hostname, t->host))
-			continue;
 		b->refcount++;
 		*bp = b;
 		return;
@@ -391,6 +388,9 @@
 
 	bes->dnsttl = 300;
 
+	AN(t->ident);
+	REPLACE(bes->ident, t->ident);
+
 	AN(t->name);
 	REPLACE(b->vcl_name, t->name);
 

Modified: trunk/varnish-cache/include/vrt.h
===================================================================
--- trunk/varnish-cache/include/vrt.h	2007-09-24 08:23:34 UTC (rev 2006)
+++ trunk/varnish-cache/include/vrt.h	2007-09-24 11:25:15 UTC (rev 2007)
@@ -41,6 +41,7 @@
 struct sockaddr;
 
 struct vrt_simple_backend {
+	const char	*ident;
 	const char	*name;
 	const char	*port;
 	const char	*host;

Modified: trunk/varnish-cache/lib/libvcl/vcc_backend.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_backend.c	2007-09-24 08:23:34 UTC (rev 2006)
+++ trunk/varnish-cache/lib/libvcl/vcc_backend.c	2007-09-24 11:25:15 UTC (rev 2007)
@@ -58,6 +58,32 @@
 	return (NULL);
 }
 
+/*--------------------------------------------------------------------
+ * When a new VCL is loaded, it is likely to contain backend declarations
+ * identical to other loaded VCL programs, and we want to reuse the state
+ * of those in order to not have to relearn statistics, DNS etc.
+ *
+ * This function emits a space separated text-string of the tokens which
+ * define a given backend which can be used to determine "identical backend"
+ * in that context.
+ */
+
+static void
+vcc_EmitBeIdent(struct tokenlist *tl, struct token *first, struct token *last)
+{
+
+	Fc(tl, 0, "\t.ident =");
+	while (first != last) {
+		if (first->dec != NULL)
+			Fc(tl, 0, "\n\t    \"\\\"\" %.*s \"\\\" \"",
+			    PF(first));
+		else
+			Fc(tl, 0, "\n\t    \"%.*s \"", PF(first));
+		first = TAILQ_NEXT(first, list);
+	}
+	Fc(tl, 0, ",\n");
+}
+
 void
 vcc_ParseSimpleBackend(struct tokenlist *tl)
 {
@@ -65,8 +91,10 @@
 	struct token *t_be = NULL;
 	struct token *t_host = NULL;
 	struct token *t_port = NULL;
+	struct token *t_first;
 	const char *ep;
 
+	t_first = tl->t;
 	vcc_NextToken(tl);
 	ExpectErr(tl, ID);
 	t_be = tl->t;
@@ -152,6 +180,7 @@
 	else
 		Fc(tl, 0, "\t.port = \"http\",\n");
 	Fc(tl, 0, "\t.host = %.*s,\n", PF(t_host));
+	vcc_EmitBeIdent(tl, t_first, tl->t);
 	Fc(tl, 0, "};\n");
 	Fi(tl, 0, "\tVRT_init_simple_backend(&VGC_backend_%.*s , &sbe_%.*s);\n",
 	    PF(t_be), PF(t_be));

Modified: trunk/varnish-cache/lib/libvcl/vcc_fixed_token.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcc_fixed_token.c	2007-09-24 08:23:34 UTC (rev 2006)
+++ trunk/varnish-cache/lib/libvcl/vcc_fixed_token.c	2007-09-24 11:25:15 UTC (rev 2007)
@@ -419,6 +419,7 @@
 	vsb_cat(sb, "struct sockaddr;\n");
 	vsb_cat(sb, "\n");
 	vsb_cat(sb, "struct vrt_simple_backend {\n");
+	vsb_cat(sb, "	const char	*ident;\n");
 	vsb_cat(sb, "	const char	*name;\n");
 	vsb_cat(sb, "	const char	*port;\n");
 	vsb_cat(sb, "	const char	*host;\n");




More information about the varnish-commit mailing list