From 5d5c8c080653cbf824e166ba07f9616c6f7c2d1c Mon Sep 17 00:00:00 2001 From: tfheen Date: Sun, 7 Sep 2008 07:47:39 +0000 Subject: [PATCH] Make backend.healthy available to VCL (Fixes: #312) It'll typically be used in a manner similar to: if (! backend.healthy) { error 500 "Backend sick!"; } git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3164 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache_backend.h | 2 ++ varnish-cache/bin/varnishd/cache_dir_random.c | 18 ++++++++++++++++++ .../bin/varnishd/cache_dir_round_robin.c | 18 ++++++++++++++++++ varnish-cache/bin/varnishd/cache_dir_simple.c | 12 ++++++++++++ varnish-cache/bin/varnishd/cache_vrt.c | 15 +++++---------- varnish-cache/include/vrt_obj.h | 2 +- varnish-cache/lib/libvcl/vcc_fixed_token.c | 2 +- varnish-cache/lib/libvcl/vcc_gen_obj.tcl | 2 +- varnish-cache/lib/libvcl/vcc_obj.c | 5 ++--- 9 files changed, 60 insertions(+), 16 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache_backend.h b/varnish-cache/bin/varnishd/cache_backend.h index bbfc1e4b..780fdc4c 100644 --- a/varnish-cache/bin/varnishd/cache_backend.h +++ b/varnish-cache/bin/varnishd/cache_backend.h @@ -79,6 +79,7 @@ struct vrt_backend_probe; typedef struct vbe_conn *vdi_getfd_f(struct sess *sp); typedef void vdi_fini_f(struct director *d); +typedef unsigned vdi_healthy(const struct sess *sp); struct director { unsigned magic; @@ -86,6 +87,7 @@ struct director { const char *name; vdi_getfd_f *getfd; vdi_fini_f *fini; + vdi_healthy *healthy; void *priv; }; diff --git a/varnish-cache/bin/varnishd/cache_dir_random.c b/varnish-cache/bin/varnishd/cache_dir_random.c index dcdb9133..9687bf26 100644 --- a/varnish-cache/bin/varnishd/cache_dir_random.c +++ b/varnish-cache/bin/varnishd/cache_dir_random.c @@ -120,6 +120,23 @@ vdi_random_getfd(struct sess *sp) return (NULL); } +static unsigned * +vdi_random_healthy(const struct sess *sp) +{ + struct vdi_random *vs; + int i; + + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC); + CAST_OBJ_NOTNULL(vs, sp->director->priv, VDI_RANDOM_MAGIC); + + for (i = 0; i < vs->nhosts; i++) { + if (vs->hosts[i].backend->healthy) + return 1; + } + return 0; +} + /*lint -e{818} not const-able */ static void vdi_random_fini(struct director *d) @@ -159,6 +176,7 @@ VRT_init_dir_random(struct cli *cli, struct director **bp, const struct vrt_dir_ vs->dir.name = "random"; vs->dir.getfd = vdi_random_getfd; vs->dir.fini = vdi_random_fini; + vs->dir.healthy = vdi_random_healthy; vs->retries = t->retries; if (vs->retries == 0) diff --git a/varnish-cache/bin/varnishd/cache_dir_round_robin.c b/varnish-cache/bin/varnishd/cache_dir_round_robin.c index 6b468827..275c8a33 100644 --- a/varnish-cache/bin/varnishd/cache_dir_round_robin.c +++ b/varnish-cache/bin/varnishd/cache_dir_round_robin.c @@ -84,6 +84,23 @@ vdi_round_robin_getfd(struct sess *sp) return (NULL); } +static unsigned * +vdi_round_robin_healthy(const struct sess *sp) +{ + struct vdi_round_robin *vs; + int i; + + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC); + CAST_OBJ_NOTNULL(vs, sp->director->priv, VDI_ROUND_ROBIN_MAGIC); + + for (i = 0; i < vs->nhosts; i++) { + if (vs->hosts[i].backend->healthy) + return 1; + } + return 0; +} + /*lint -e{818} not const-able */ static void vdi_round_robin_fini(struct director *d) @@ -124,6 +141,7 @@ VRT_init_dir_round_robin(struct cli *cli, struct director **bp, const struct vrt vs->dir.name = "round_robin"; vs->dir.getfd = vdi_round_robin_getfd; vs->dir.fini = vdi_round_robin_fini; + vs->dir.healthy = vdi_round_robin_healthy; vh = vs->hosts; te = t->members; diff --git a/varnish-cache/bin/varnishd/cache_dir_simple.c b/varnish-cache/bin/varnishd/cache_dir_simple.c index ef7d4ae4..d7b1e3ff 100644 --- a/varnish-cache/bin/varnishd/cache_dir_simple.c +++ b/varnish-cache/bin/varnishd/cache_dir_simple.c @@ -66,6 +66,17 @@ vdi_simple_getfd(struct sess *sp) return (VBE_GetVbe(sp, vs->backend)); } +static unsigned * +vdi_simple_healthy(const struct sess *sp) +{ + struct vdi_simple *vs; + + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC); + CAST_OBJ_NOTNULL(vs, sp->director->priv, VDI_SIMPLE_MAGIC); + return vs->backend->healthy; +} + /*lint -e{818} not const-able */ static void vdi_simple_fini(struct director *d) @@ -94,6 +105,7 @@ VRT_init_dir_simple(struct cli *cli, struct director **bp, const struct vrt_dir_ vs->dir.name = "simple"; vs->dir.getfd = vdi_simple_getfd; vs->dir.fini = vdi_simple_fini; + vs->dir.healthy = vdi_simple_healthy; vs->backend = VBE_AddBackend(cli, t->host); diff --git a/varnish-cache/bin/varnishd/cache_vrt.c b/varnish-cache/bin/varnishd/cache_vrt.c index ddd982d7..e3738863 100644 --- a/varnish-cache/bin/varnishd/cache_vrt.c +++ b/varnish-cache/bin/varnishd/cache_vrt.c @@ -50,7 +50,7 @@ #include "vrt_obj.h" #include "vcl.h" #include "cache.h" - +#include "cache_backend.h" void *vrt_magic_string_end = &vrt_magic_string_end; @@ -575,17 +575,12 @@ VRT_r_obj_hash(const struct sess *sp) return (sp->obj->objhead->hash); } -int -VRT_r_backend_health(const struct sess *sp) +unsigned +VRT_r_backend_healthy(const struct sess *sp) { CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); -#if 0 - CHECK_OBJ_NOTNULL(sp->backend, BACKEND_MAGIC); - return (sp->backend->health); -#else - INCOMPL(); - return (0); -#endif + CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC); + return (sp->director->healthy(sp)); } /*--------------------------------------------------------------------*/ diff --git a/varnish-cache/include/vrt_obj.h b/varnish-cache/include/vrt_obj.h index eae751b4..35f1f804 100644 --- a/varnish-cache/include/vrt_obj.h +++ b/varnish-cache/include/vrt_obj.h @@ -51,4 +51,4 @@ void VRT_l_resp_status(const struct sess *, int); const char * VRT_r_resp_response(const struct sess *); void VRT_l_resp_response(const struct sess *, const char *, ...); double VRT_r_now(const struct sess *); -int VRT_r_backend_health(const struct sess *); +unsigned VRT_r_backend_healthy(const struct sess *); diff --git a/varnish-cache/lib/libvcl/vcc_fixed_token.c b/varnish-cache/lib/libvcl/vcc_fixed_token.c index 3d5510c1..04b1784c 100644 --- a/varnish-cache/lib/libvcl/vcc_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcc_fixed_token.c @@ -509,5 +509,5 @@ vcl_output_lang_h(struct vsb *sb) vsb_cat(sb, "const char * VRT_r_resp_response(const struct sess *);\n"); vsb_cat(sb, "void VRT_l_resp_response(const struct sess *, const char *, ...);\n"); vsb_cat(sb, "double VRT_r_now(const struct sess *);\n"); - vsb_cat(sb, "int VRT_r_backend_health(const struct sess *);\n"); + vsb_cat(sb, "unsigned VRT_r_backend_healthy(const struct sess *);\n"); } diff --git a/varnish-cache/lib/libvcl/vcc_gen_obj.tcl b/varnish-cache/lib/libvcl/vcc_gen_obj.tcl index 18f354fa..e26d3fa2 100755 --- a/varnish-cache/lib/libvcl/vcc_gen_obj.tcl +++ b/varnish-cache/lib/libvcl/vcc_gen_obj.tcl @@ -211,7 +211,7 @@ set spobj { {recv pipe pass hash miss hit fetch deliver discard timeout} "const struct sess *" } - { backend.health RO INT + { backend.healthy RO BOOL {recv pipe pass hash miss hit fetch deliver discard timeout} "const struct sess *" } diff --git a/varnish-cache/lib/libvcl/vcc_obj.c b/varnish-cache/lib/libvcl/vcc_obj.c index 6cf25cfa..2aa6a80b 100644 --- a/varnish-cache/lib/libvcl/vcc_obj.c +++ b/varnish-cache/lib/libvcl/vcc_obj.c @@ -8,7 +8,6 @@ #include "config.h" #include -#include "config.h" #include "vcc_compile.h" struct var vcc_vars[] = { @@ -229,8 +228,8 @@ struct var vcc_vars[] = { 0, VCL_MET_RECV | VCL_MET_PIPE | VCL_MET_PASS | VCL_MET_HASH | VCL_MET_MISS | VCL_MET_HIT | VCL_MET_FETCH | VCL_MET_DELIVER | VCL_MET_DISCARD | VCL_MET_TIMEOUT }, - { "backend.health", INT, 14, - "VRT_r_backend_health(sp)", + { "backend.healthy", BOOL, 15, + "VRT_r_backend_healthy(sp)", NULL, V_RO, 0, -- 2.39.5