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

phk at projects.linpro.no phk at projects.linpro.no
Wed Apr 19 08:34:11 CEST 2006


Author: phk
Date: 2006-04-19 08:34:10 +0200 (Wed, 19 Apr 2006)
New Revision: 142

Added:
   trunk/varnish-cache/bin/varnishd/cache_fetch.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_pool.c
   trunk/varnish-cache/bin/varnishd/hash_simple_list.c
   trunk/varnish-cache/bin/varnishlog/varnishlog.c
   trunk/varnish-cache/include/shmlog_tags.h
   trunk/varnish-cache/include/vcl_lang.h
   trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c
Log:
Implement enough of FetchSession and DeliverSession that we can actually
deliver a cached object.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2006-04-19 06:34:10 UTC (rev 142)
@@ -7,6 +7,7 @@
 varnishd_SOURCES = \
 	cache_acceptor.c \
 	cache_backend.c \
+	cache_fetch.c \
 	cache_httpd.c \
 	cache_main.c \
 	cache_pool.c \

Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2006-04-19 06:34:10 UTC (rev 142)
@@ -20,12 +20,14 @@
 
 typedef void hash_init_f(void);
 typedef struct object *hash_lookup_f(unsigned char *key, struct object *nobj);
+typedef void hash_deref_f(struct object *obj);
 typedef void hash_purge_f(struct object *obj);
 
 struct hash_slinger {
 	const char		*name;
 	hash_init_f		*init;
 	hash_lookup_f		*lookup;
+	hash_deref_f		*deref;
 	hash_purge_f		*purge;
 };
 
@@ -37,7 +39,7 @@
 
 struct storage {
 	TAILQ_ENTRY(storage)	list;
-	void			*ptr;
+	unsigned char		*ptr;
 	unsigned		len;
 	void			*priv;
 };
@@ -72,6 +74,8 @@
 void VBE_ClosedFd(void *ptr);
 void VBE_RecycleFd(void *ptr);
 
+/* cache_fetch.c */
+int FetchSession(struct worker *w, struct sess *sp);
 
 /* cache_httpd.c */
 void HttpdAnalyze(struct sess *sp, int rr);

Added: trunk/varnish-cache/bin/varnishd/cache_fetch.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_fetch.c	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/bin/varnishd/cache_fetch.c	2006-04-19 06:34:10 UTC (rev 142)
@@ -0,0 +1,125 @@
+/*
+ * $Id$
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <pthread.h>
+#include <sys/queue.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <sbuf.h>
+#include <event.h>
+
+#include "libvarnish.h"
+#include "shmlog.h"
+#include "vcl_lang.h"
+#include "cache.h"
+
+static void
+FetchReturn(struct sess *sp)
+{
+
+	/* do nothing */
+}
+
+/*--------------------------------------------------------------------*/
+int
+FetchSession(struct worker *w, struct sess *sp)
+{
+	int fd, i;
+	void *fd_token;
+	struct sess sp2;
+	off_t	cl;
+	struct storage *st;
+	unsigned char *p;
+
+	fd = VBE_GetFd(sp->backend, &fd_token);
+	assert(fd != -1);
+	VSL(SLT_HandlingFetch, sp->fd, "%d", fd);
+
+	HttpdBuildSbuf(0, 1, w->sb, sp);
+	i = write(fd, sbuf_data(w->sb), sbuf_len(w->sb));
+	assert(i == sbuf_len(w->sb));
+
+	/* XXX: copy any contents */
+
+	memset(&sp2, 0, sizeof sp2);
+	sp2.rd_e = &w->e1;
+	sp2.fd = fd;
+	/*
+	 * XXX: It might be cheaper to avoid the event_engine and simply
+	 * XXX: read(2) the header
+	 */
+	HttpdGetHead(&sp2, w->eb, FetchReturn);
+	event_base_loop(w->eb, 0);
+	HttpdAnalyze(&sp2, 2);
+
+	/* XXX: fill in object from headers */
+	sp->obj->valid = 1;
+	sp->obj->cacheable = 1;
+
+	/* XXX: unbusy, and kick other sessions into action */
+	sp->obj->busy = 0;
+
+	assert (sp2.http.H_Content_Length != NULL); /* XXX */
+
+	cl = strtoumax(sp2.http.H_Content_Length, NULL, 0);
+
+	sp->handling = HND_Unclass;
+	sp->vcl->fetch_func(sp);
+
+	st = stevedore->alloc(cl);
+	TAILQ_INSERT_TAIL(&sp->obj->store, st, list);
+	st->len = cl;
+	sp->obj->len = cl;
+	p = st->ptr;
+
+	i = fcntl(sp2.fd, F_GETFL);		/* XXX ? */
+	i &= ~O_NONBLOCK;
+	i = fcntl(sp2.fd, F_SETFL, i);
+
+	i = sp2.rcv_len - sp2.rcv_ptr;
+	if (i > 0) {
+		memcpy(p, sp2.rcv + sp2.rcv_ptr, i);
+		p += i;
+		cl -= i;
+	}
+	if (cl != 0) {
+		i = read(sp2.fd, p, cl);
+		VSL(SLT_Debug, 0, "R i %d cl %jd", i, cl);
+		assert(i == cl);
+	}
+
+	HttpdBuildSbuf(1, 1, w->sb, &sp2);
+	i = write(sp->fd, sbuf_data(w->sb), sbuf_len(w->sb));
+	assert(i == sbuf_len(w->sb));
+
+	i = write(sp->fd, st->ptr, st->len);
+	VSL(SLT_Debug, 0, "W i %d st->len %u", i, st->len);
+	assert(i == st->len);
+
+	hash->deref(sp->obj);
+
+	if (sp2.http.H_Connection != NULL &&
+	    !strcmp(sp2.http.H_Connection, "close")) {
+		close(fd);
+		VBE_ClosedFd(fd_token);
+	} else {
+		VBE_RecycleFd(fd_token);
+	}
+
+	/* XXX: this really belongs in the acceptor */
+	if (sp->rcv_len > sp->rcv_ptr)
+		memmove(sp->rcv, sp->rcv + sp->rcv_ptr,
+		    sp->rcv_len - sp->rcv_ptr);
+	sp->rcv_len -= sp->rcv_ptr;
+	sp->rcv_ptr = 0;
+	return (1);
+}


Property changes on: trunk/varnish-cache/bin/varnishd/cache_fetch.c
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: trunk/varnish-cache/bin/varnishd/cache_pool.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_pool.c	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/bin/varnishd/cache_pool.c	2006-04-19 06:34:10 UTC (rev 142)
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <assert.h>
 #include <pthread.h>
+#include <unistd.h>
 #include <queue.h>
 #include <sys/time.h>
 #include <sbuf.h>
@@ -40,8 +41,12 @@
 	MD5Update(&ctx, sp->http.url, strlen(sp->http.url));
 	MD5Final(key, &ctx);
 	o = hash->lookup(key, w->nobj);
-	if (o == w->nobj)
+	if (o == w->nobj) {
+		VSL(SLT_Debug, 0, "Lookup new %p %s", o, sp->http.url);
 		w->nobj = NULL;
+	} else {
+		VSL(SLT_Debug, 0, "Lookup found %p %s", o, sp->http.url);
+	}
 	/*
 	 * XXX: if obj is busy, park session on it
 	 */
@@ -59,10 +64,26 @@
 }
 
 static int
-FetchSession(struct worker *w, struct sess *sp)
+DeliverSession(struct worker *w, struct sess *sp)
 {
+	char buf[BUFSIZ];
+	int i, j;
+	struct storage *st;
 
-	assert(w == NULL);
+	sprintf(buf,
+	    "HTTP/1.1 200 OK\r\n"
+	    "Server: Varnish\r\n"
+	    "Content-Length: %u\r\n"
+	    "\r\n", sp->obj->len);
+
+	j = strlen(buf);
+	i = write(sp->fd, buf, j);
+	assert(i == j);
+	TAILQ_FOREACH(st, &sp->obj->store, list) {
+		i = write(sp->fd, st->ptr, st->len);
+		assert(i == st->len);
+	}
+	return (1);
 }
 
 static void *
@@ -112,6 +133,9 @@
 			case HND_Fetch:
 				done = FetchSession(&w, sp);
 				break;
+			case HND_Deliver:
+				done = DeliverSession(&w, sp);
+				break;
 			case HND_Pipe:
 				PipeSession(&w, sp);
 				done = 1;
@@ -121,15 +145,22 @@
 				done = 1;
 				break;
 			case HND_Unclass:
-			case HND_Deliver:
 				assert(sp->handling == HND_Unclass);
 			}
 		}
+		if (sp->http.H_Connection != NULL &&
+		    !strcmp(sp->http.H_Connection, "close")) {
+			close(sp->fd);
+			sp->fd = -1;
+		}
 
 		AZ(pthread_mutex_lock(&sessmtx));
 		RelVCL(sp->vcl);
 		sp->vcl = NULL;
-		vca_recycle_session(sp);
+		if (sp->fd < 0) 
+			vca_retire_session(sp);
+		else
+			vca_recycle_session(sp);
 	}
 }
 

Modified: trunk/varnish-cache/bin/varnishd/hash_simple_list.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/hash_simple_list.c	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/bin/varnishd/hash_simple_list.c	2006-04-19 06:34:10 UTC (rev 142)
@@ -55,6 +55,7 @@
 	assert(he2 != NULL);
 	he2->obj = nobj;
 	nobj->refcnt++;
+	memcpy(nobj->hash, key, sizeof nobj->hash);
 	if (he != NULL)
 		TAILQ_INSERT_BEFORE(he, he2, list);
 	else
@@ -64,6 +65,15 @@
 }
 
 static void
+hsl_deref(struct object *obj)
+{
+
+	AZ(pthread_mutex_lock(&hsl_mutex));
+	obj->refcnt--;
+	AZ(pthread_mutex_unlock(&hsl_mutex));
+}
+
+static void
 hsl_purge(struct object *obj)
 {
 	struct hsl_entry *he;
@@ -85,5 +95,6 @@
 	"simple_list",
 	hsl_init,
 	hsl_lookup,
+	hsl_deref,
 	hsl_purge
 };

Modified: trunk/varnish-cache/bin/varnishlog/varnishlog.c
===================================================================
--- trunk/varnish-cache/bin/varnishlog/varnishlog.c	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/bin/varnishlog/varnishlog.c	2006-04-19 06:34:10 UTC (rev 142)
@@ -40,6 +40,7 @@
 	int fd;
 	int i;
 	unsigned u;
+	unsigned startup;
 	struct shmloghead slh;
 	unsigned char *p;
 
@@ -74,6 +75,7 @@
 	for (i = 0; stagnames[i].tag != SLT_ENDMARKER; i++)
 		tagnames[stagnames[i].tag] = stagnames[i].name;
 
+	startup = 1;
 	while (1) {
 		p = logstart;
 		while (1) {
@@ -82,13 +84,16 @@
 			while (*p == SLT_ENDMARKER) {
 				fflush(stdout);
 				sleep(1);
+				startup = 0;
 			}
 			u = (p[2] << 8) | p[3];
-			printf("%02x %02d %4d %-12s <",
-			    p[0], p[1], u, tagnames[p[0]]);
-			if (p[1] > 0)
-				fwrite(p + 4, p[1], 1, stdout);
-			printf(">\n");
+			if (!startup) {
+				printf("%02x %02d %4d %-12s <",
+				    p[0], p[1], u, tagnames[p[0]]);
+				if (p[1] > 0)
+					fwrite(p + 4, p[1], 1, stdout);
+				printf(">\n");
+			}
 			p += p[1] + 4;
 		}
 	}

Modified: trunk/varnish-cache/include/shmlog_tags.h
===================================================================
--- trunk/varnish-cache/include/shmlog_tags.h	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/include/shmlog_tags.h	2006-04-19 06:34:10 UTC (rev 142)
@@ -12,6 +12,7 @@
 SLTM(SessionReuse)
 SLTM(SessionClose)
 SLTM(ClientAddr)
+SLTM(HandlingFetch)
 SLTM(HandlingPass)
 SLTM(HandlingPipe)
 SLTM(Request)

Modified: trunk/varnish-cache/include/vcl_lang.h
===================================================================
--- trunk/varnish-cache/include/vcl_lang.h	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/include/vcl_lang.h	2006-04-19 06:34:10 UTC (rev 142)
@@ -47,6 +47,7 @@
 	unsigned		cacheable;
 
 	unsigned		busy;
+	unsigned		len;
 
 	TAILQ_HEAD(, storage)	store;
 };

Modified: trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c
===================================================================
--- trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c	2006-04-18 08:23:44 UTC (rev 141)
+++ trunk/varnish-cache/lib/libvcl/vcl_fixed_token.c	2006-04-19 06:34:10 UTC (rev 142)
@@ -485,6 +485,7 @@
 	fputs("	unsigned		cacheable;\n", f);
 	fputs("\n", f);
 	fputs("	unsigned		busy;\n", f);
+	fputs("	unsigned		len;\n", f);
 	fputs("\n", f);
 	fputs("	TAILQ_HEAD(, storage)	store;\n", f);
 	fputs("};\n", f);




More information about the varnish-commit mailing list