Commit 305fb429 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Move the top-of-director APIs from cache_backend.c to cache_dir.c and

give them the VTLA "VDI"



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@5090 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent d7a611a2
......@@ -20,6 +20,7 @@ varnishd_SOURCES = \
cache_ban.c \
cache_center.c \
cache_cli.c \
cache_dir.c \
cache_dir_random.c \
cache_dir_dns.c \
cache_dir_round_robin.c \
......
......@@ -473,12 +473,12 @@ extern pthread_t VCA_thread;
/* cache_backend.c */
struct vbc *VBE_GetFd(const struct director *, struct sess *sp);
int VBE_Healthy(double now, const struct director *, uintptr_t target);
int VBE_Healthy_sp(const struct sess *sp, const struct director *);
void VBE_CloseFd(struct sess *sp);
void VBE_RecycleFd(struct sess *sp);
void VBE_AddHostHeader(const struct sess *sp);
struct vbc *VDI_GetFd(const struct director *, struct sess *sp);
int VDI_Healthy(double now, const struct director *, uintptr_t target);
int VDI_Healthy_sp(const struct sess *sp, const struct director *);
void VDI_CloseFd(struct sess *sp);
void VDI_RecycleFd(struct sess *sp);
void VDI_AddHostHeader(const struct sess *sp);
void VBE_Poll(void);
/* cache_backend_cfg.c */
......
......@@ -53,20 +53,6 @@ SVNID("$Id$")
*/
static VTAILQ_HEAD(,vbc) vbcs = VTAILQ_HEAD_INITIALIZER(vbcs);
/*--------------------------------------------------------------------
* Create default Host: header for backend request
*/
void
VBE_AddHostHeader(const struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(sp->wrk->bereq, HTTP_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc->backend, BACKEND_MAGIC);
http_PrintfHeader(sp->wrk, sp->fd, sp->wrk->bereq,
"Host: %s", sp->vbc->backend->hosthdr);
}
/* Private interface from backend_cfg.c */
void
......@@ -342,7 +328,7 @@ vbe_GetVbe(struct sess *sp, struct backend *bp)
}
VSC_main->backend_toolate++;
sp->vbc = vc;
VBE_CloseFd(sp);
VDI_CloseFd(sp);
}
if (!vbe_Healthy(sp->t_req, (uintptr_t)sp->objhead, bp)) {
......@@ -371,90 +357,6 @@ vbe_GetVbe(struct sess *sp, struct backend *bp)
return (vc);
}
/* Close a connection ------------------------------------------------*/
void
VBE_CloseFd(struct sess *sp)
{
struct backend *bp;
CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc->backend, BACKEND_MAGIC);
assert(sp->vbc->fd >= 0);
bp = sp->vbc->backend;
WSL(sp->wrk, SLT_BackendClose, sp->vbc->fd, "%s", bp->vcl_name);
TCP_close(&sp->vbc->fd);
VBE_DropRefConn(bp);
sp->vbc->backend = NULL;
VBE_ReleaseConn(sp->vbc);
sp->vbc = NULL;
}
/* Recycle a connection ----------------------------------------------*/
void
VBE_RecycleFd(struct sess *sp)
{
struct backend *bp;
CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc->backend, BACKEND_MAGIC);
assert(sp->vbc->fd >= 0);
bp = sp->vbc->backend;
WSL(sp->wrk, SLT_BackendReuse, sp->vbc->fd, "%s", bp->vcl_name);
/*
* Flush the shmlog, so that another session reusing this backend
* will log chronologically later than our use of it.
*/
WSL_Flush(sp->wrk, 0);
Lck_Lock(&bp->mtx);
VSC_main->backend_recycle++;
VTAILQ_INSERT_HEAD(&bp->connlist, sp->vbc, list);
sp->vbc = NULL;
VBE_DropRefLocked(bp);
}
/* Get a connection --------------------------------------------------*/
struct vbc *
VBE_GetFd(const struct director *d, struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
if (d == NULL)
d = sp->director;
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
return (d->getfd(d, sp));
}
/* Check health ------------------------------------------------------
*
* The target is really an objhead pointer, but since it can not be
* dereferenced during health-checks, we pass it as uintptr_t, which
* hopefully will make people investigate, before mucking about with it.
*/
int
VBE_Healthy_sp(const struct sess *sp, const struct director *d)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
return (d->healthy(sp->t_req, d, (uintptr_t)sp->objhead));
}
int
VBE_Healthy(double now, const struct director *d, uintptr_t target)
{
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
return (d->healthy(now, d, target));
}
/*--------------------------------------------------------------------
* The "simple" director really isn't, since thats where all the actual
* connections happen. Nontheless, pretend it is simple by sequestering
......
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2010 Redpill Linpro AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@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.
*
* Handle backend connections and backend request structures.
*
*/
#include "config.h"
#include "svnid.h"
SVNID("$Id: cache_backend.c 5089 2010-08-11 12:12:47Z phk $")
#include "cache.h"
#include "cache_backend.h"
/*--------------------------------------------------------------------
* Create default Host: header for backend request
*/
void
VDI_AddHostHeader(const struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(sp->wrk->bereq, HTTP_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc->backend, BACKEND_MAGIC);
http_PrintfHeader(sp->wrk, sp->fd, sp->wrk->bereq,
"Host: %s", sp->vbc->backend->hosthdr);
}
/* Close a connection ------------------------------------------------*/
void
VDI_CloseFd(struct sess *sp)
{
struct backend *bp;
CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc->backend, BACKEND_MAGIC);
assert(sp->vbc->fd >= 0);
bp = sp->vbc->backend;
WSL(sp->wrk, SLT_BackendClose, sp->vbc->fd, "%s", bp->vcl_name);
TCP_close(&sp->vbc->fd);
VBE_DropRefConn(bp);
sp->vbc->backend = NULL;
VBE_ReleaseConn(sp->vbc);
sp->vbc = NULL;
}
/* Recycle a connection ----------------------------------------------*/
void
VDI_RecycleFd(struct sess *sp)
{
struct backend *bp;
CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
CHECK_OBJ_NOTNULL(sp->vbc->backend, BACKEND_MAGIC);
assert(sp->vbc->fd >= 0);
bp = sp->vbc->backend;
WSL(sp->wrk, SLT_BackendReuse, sp->vbc->fd, "%s", bp->vcl_name);
/*
* Flush the shmlog, so that another session reusing this backend
* will log chronologically later than our use of it.
*/
WSL_Flush(sp->wrk, 0);
Lck_Lock(&bp->mtx);
VSC_main->backend_recycle++;
VTAILQ_INSERT_HEAD(&bp->connlist, sp->vbc, list);
sp->vbc = NULL;
VBE_DropRefLocked(bp);
}
/* Get a connection --------------------------------------------------*/
struct vbc *
VDI_GetFd(const struct director *d, struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
if (d == NULL)
d = sp->director;
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
return (d->getfd(d, sp));
}
/* Check health ------------------------------------------------------
*
* The target is really an objhead pointer, but since it can not be
* dereferenced during health-checks, we pass it as uintptr_t, which
* hopefully will make people investigate, before mucking about with it.
*/
int
VDI_Healthy_sp(const struct sess *sp, const struct director *d)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
return (d->healthy(sp->t_req, d, (uintptr_t)sp->objhead));
}
int
VDI_Healthy(double now, const struct director *d, uintptr_t target)
{
CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
return (d->healthy(now, d, target));
}
......@@ -164,7 +164,7 @@ vdi_dns_pick_host(const struct sess *sp, struct vdi_dns_hostgroup *group) {
current = i + initial - nhosts;
else
current = i + initial;
if (VBE_Healthy_sp(sp, group->hosts[current])) {
if (VDI_Healthy_sp(sp, group->hosts[current])) {
group->next_host = current+1;
return group->hosts[current];
}
......@@ -395,10 +395,10 @@ vdi_dns_getfd(const struct director *director, struct sess *sp)
CAST_OBJ_NOTNULL(vs, director->priv, VDI_DNS_MAGIC);
dir = vdi_dns_find_backend(sp, vs);
if (!dir || !VBE_Healthy_sp(sp, dir))
if (!dir || !VDI_Healthy_sp(sp, dir))
return (NULL);
vbe = VBE_GetFd(dir, sp);
vbe = VDI_GetFd(dir, sp);
return (vbe);
}
......
......@@ -138,9 +138,9 @@ vdi_random_getfd(const struct director *d, struct sess *sp)
if (r >= s1)
continue;
d2 = vs->hosts[i].backend;
if (!VBE_Healthy_sp(sp, d2))
if (!VDI_Healthy_sp(sp, d2))
break;
vbe = VBE_GetFd(d2, sp);
vbe = VDI_GetFd(d2, sp);
if (vbe != NULL)
return (vbe);
break;
......@@ -153,7 +153,7 @@ vdi_random_getfd(const struct director *d, struct sess *sp)
for (i = 0; i < vs->nhosts; i++) {
d2 = vs->hosts[i].backend;
/* XXX: cache result of healty to avoid double work */
if (VBE_Healthy_sp(sp, d2))
if (VDI_Healthy_sp(sp, d2))
s1 += vs->hosts[i].weight;
}
......@@ -172,12 +172,12 @@ vdi_random_getfd(const struct director *d, struct sess *sp)
s1 = 0.0;
for (i = 0; i < vs->nhosts; i++) {
d2 = vs->hosts[i].backend;
if (!VBE_Healthy_sp(sp, d2))
if (!VDI_Healthy_sp(sp, d2))
continue;
s1 += vs->hosts[i].weight;
if (r >= s1)
continue;
vbe = VBE_GetFd(d2, sp);
vbe = VDI_GetFd(d2, sp);
if (vbe != NULL)
return (vbe);
break;
......@@ -197,7 +197,7 @@ vdi_random_healthy(double now, const struct director *d, uintptr_t target)
CAST_OBJ_NOTNULL(vs, d->priv, VDI_RANDOM_MAGIC);
for (i = 0; i < vs->nhosts; i++) {
if (VBE_Healthy(now, vs->hosts[i].backend, target))
if (VDI_Healthy(now, vs->hosts[i].backend, target))
return 1;
}
return 0;
......
......@@ -74,9 +74,9 @@ vdi_round_robin_getfd(const struct director *d, struct sess *sp)
for (i = 0; i < vs->nhosts; i++) {
backend = vs->hosts[vs->next_host].backend;
vs->next_host = (vs->next_host + 1) % vs->nhosts;
if (!VBE_Healthy_sp(sp, backend))
if (!VDI_Healthy_sp(sp, backend))
continue;
vbe = VBE_GetFd(backend, sp);
vbe = VDI_GetFd(backend, sp);
if (vbe != NULL)
return (vbe);
}
......@@ -96,7 +96,7 @@ vdi_round_robin_healthy(double now, const struct director *d, uintptr_t target)
for (i = 0; i < vs->nhosts; i++) {
backend = vs->hosts[i].backend;
if (VBE_Healthy(now, backend, target))
if (VDI_Healthy(now, backend, target))
return 1;
}
return 0;
......
......@@ -351,7 +351,7 @@ FetchHdr(struct sess *sp)
w = sp->wrk;
hp = sp->wrk->bereq;
sp->vbc = VBE_GetFd(NULL, sp);
sp->vbc = VDI_GetFd(NULL, sp);
if (sp->vbc == NULL) {
WSP(sp, SLT_FetchError, "no backend connection");
return (__LINE__);
......@@ -364,7 +364,7 @@ FetchHdr(struct sess *sp)
* XXX: This possibly ought to go into the default VCL
*/
if (!http_GetHdr(hp, H_Host, &b))
VBE_AddHostHeader(sp);
VDI_AddHostHeader(sp);
(void)TCP_blocking(vc->fd); /* XXX: we should timeout instead */
WRW_Reserve(w, &vc->fd);
......@@ -374,7 +374,7 @@ FetchHdr(struct sess *sp)
i = FetchReqBody(sp);
if (WRW_FlushRelease(w) || i > 0) {
WSP(sp, SLT_FetchError, "backend write error: %d", errno);
VBE_CloseFd(sp);
VDI_CloseFd(sp);
/* XXX: other cleanup ? */
return (__LINE__);
}
......@@ -397,7 +397,7 @@ FetchHdr(struct sess *sp)
if (i < 0) {
WSP(sp, SLT_FetchError,
"http read error: %d", errno);
VBE_CloseFd(sp);
VDI_CloseFd(sp);
/* XXX: other cleanup ? */
return (__LINE__);
}
......@@ -412,7 +412,7 @@ FetchHdr(struct sess *sp)
if (http_DissectResponse(sp->wrk, sp->wrk->htc, hp)) {
WSP(sp, SLT_FetchError, "http format error");
VBE_CloseFd(sp);
VDI_CloseFd(sp);
/* XXX: other cleanup ? */
return (__LINE__);
}
......@@ -466,7 +466,7 @@ FetchBody(struct sess *sp)
sp->wrk->stats.fetch_bad++;
/* XXX: AUGH! */
WSL(sp->wrk, SLT_Debug, vc->fd, "Invalid Transfer-Encoding");
VBE_CloseFd(sp);
VDI_CloseFd(sp);
return (__LINE__);
} else if (http_HdrIs(hp, H_Connection, "keep-alive")) {
/*
......@@ -515,7 +515,7 @@ FetchBody(struct sess *sp)
VTAILQ_REMOVE(&sp->obj->store, st, list);
STV_free(st);
}
VBE_CloseFd(sp);
VDI_CloseFd(sp);
sp->obj->len = 0;
return (__LINE__);
}
......@@ -540,9 +540,9 @@ FetchBody(struct sess *sp)
cls = 1;
if (cls)
VBE_CloseFd(sp);
VDI_CloseFd(sp);
else
VBE_RecycleFd(sp);
VDI_RecycleFd(sp);
return (0);
}
......
......@@ -402,7 +402,7 @@ HSH_Lookup(struct sess *sp, struct objhead **poh)
if (oc == NULL /* We found no live object */
&& grace_oc != NULL /* There is a grace candidate */
&& (busy_oc != NULL /* Somebody else is already busy */
|| !VBE_Healthy(sp->t_req, sp->director, (uintptr_t)oh))) {
|| !VDI_Healthy(sp->t_req, sp->director, (uintptr_t)oh))) {
/* Or it is impossible to fetch: */
o = grace_oc->obj;
CHECK_OBJ_NOTNULL(o, OBJECT_MAGIC);
......
......@@ -74,7 +74,7 @@ PipeSession(struct sess *sp)
CHECK_OBJ_NOTNULL(sp->wrk, WORKER_MAGIC);
w = sp->wrk;
sp->vbc = VBE_GetFd(NULL, sp);
sp->vbc = VDI_GetFd(NULL, sp);
if (sp->vbc == NULL)
return;
vc = sp->vbc;
......@@ -91,7 +91,7 @@ PipeSession(struct sess *sp)
if (i) {
vca_close_session(sp, "pipe");
VBE_CloseFd(sp);
VDI_CloseFd(sp);
return;
}
......@@ -131,5 +131,5 @@ PipeSession(struct sess *sp)
}
}
vca_close_session(sp, "pipe");
VBE_CloseFd(sp);
VDI_CloseFd(sp);
}
......@@ -819,7 +819,7 @@ VRT_r_req_backend_healthy(const struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC);
return (VBE_Healthy_sp(sp, sp->director));
return (VDI_Healthy_sp(sp, sp->director));
}
/*--------------------------------------------------------------------*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment