[master] b16a9d9a8 Retire (struct director).sick and VRT_SetHealth()

Nils Goroll nils.goroll at uplex.de
Tue Mar 5 11:09:03 UTC 2019


commit b16a9d9a8d000a0c9fcac2245ec0e6e0bd5cbecb
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Mar 4 17:14:57 2019 +0100

    Retire (struct director).sick and VRT_SetHealth()
    
    The sick state of the director and the healthy callback are the same
    thing coming from different directions: Either we query the status
    dynamically or we already have it.
    
    For layering directors, the health state is determined by their
    backends, so having a director-layer sick state does not make much sense
    and duplicates logic.
    
    Also, the sick field duplicates admin_health to some extend (see
    cache_director.c do_set_health).
    
    This is also relevant in the context of streamlining the backend.list
    output: admin_health "probe" only makes sense if backends actually do
    have a probe (= some dynamically determined health state). It appears
    streaight forward that the presense of a vdi_healthy_f callback is the
    signal for dynamically determined health state.
    
    So we move the sick field into VBE and retire VRT_SetHealth().
    
    We also remove the ctx argument from VRT_SetChanged() because I
    previously overlooked that it is required, for example, in probe code
    where we got no ctx.
    
    Ref #2896

diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 3ff3c9ac7..d553d0317 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -122,7 +122,7 @@ vbe_dir_getfd(struct worker *wrk, struct backend *bp, struct busyobj *bo,
 	CHECK_OBJ_NOTNULL(bp, BACKEND_MAGIC);
 	AN(bp->vsc);
 
-	if (bp->director->sick) {
+	if (bp->sick) {
 		VSLb(bo->vsl, SLT_FetchError,
 		     "backend %s: unhealthy", VRT_BACKEND_string(bp->director));
 		bp->vsc->unhealthy++;
@@ -461,14 +461,33 @@ vbe_list(VRT_CTX, const struct director *d, struct vsb *vsb, int pflag,
 	else if (jflag && pflag)
 		VSB_printf(vsb, "{},\n");
 	else if (jflag)
-		VSB_printf(vsb, "\"%s\"", d->sick ? "sick" : "healthy");
+		VSB_printf(vsb, "\"%s\"", bp->sick ? "sick" : "healthy");
 	else if (pflag)
 		return;
 	else
 		VSB_printf(vsb, "%s",
-		    d->sick ? "sick" : "healthy");
+		    bp->sick ? "sick" : "healthy");
 }
 
+/*--------------------------------------------------------------------
+ */
+
+static VCL_BOOL v_matchproto_(vdi_healthy_f)
+vbe_healthy(VRT_CTX, VCL_BACKEND d, VCL_TIME *t)
+{
+	struct backend *bp;
+
+	(void)ctx;
+	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
+	CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC);
+
+	if (t != NULL)
+		*t = bp->changed;
+
+	return (! bp->sick);
+}
+
+
 /*--------------------------------------------------------------------
  */
 
@@ -483,6 +502,20 @@ static const struct vdi_methods vbe_methods[1] = {{
 	.destroy =		vbe_destroy,
 	.panic =		vbe_panic,
 	.list =			vbe_list,
+	.healthy =		vbe_healthy
+}};
+
+static const struct vdi_methods vbe_methods_noprobe[1] = {{
+	.magic =		VDI_METHODS_MAGIC,
+	.type =			"backend",
+	.http1pipe =		vbe_dir_http1pipe,
+	.gethdrs =		vbe_dir_gethdrs,
+	.getip =		vbe_dir_getip,
+	.finish =		vbe_dir_finish,
+	.event =		vbe_dir_event,
+	.destroy =		vbe_destroy,
+	.panic =		vbe_panic,
+	.list =			vbe_list
 }};
 
 /*--------------------------------------------------------------------
@@ -542,8 +575,11 @@ VRT_new_backend_clustered(VRT_CTX, struct vsmw_cluster *vc,
 
 	if (vbp != NULL)
 		VBP_Insert(be, vbp, be->tcp_pool);
+	else
+		be->sick = 0;
 
-	be->director = VRT_AddDirector(ctx, vbe_methods, be,
+	be->director = VRT_AddDirector(ctx,
+	    vbp != NULL ? vbe_methods : vbe_methods_noprobe, be,
 	    "%s", vrt->vcl_name);
 
 	if (be->director != NULL) {
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index 796be9b32..c46e10365 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -57,6 +57,9 @@ struct backend {
 
 	VRT_BACKEND_FIELDS()
 
+	unsigned		sick;
+	vtim_real		changed;
+
 	struct vbp_target	*probe;
 
 	struct vsc_seg		*vsc_seg;
diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index c7dbb2247..ba8316162 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -152,9 +152,8 @@ vbp_has_poked(struct vbp_target *vt)
 void
 VBP_Update_Backend(struct vbp_target *vt)
 {
-	unsigned i = 0;
+	unsigned i = 0, chg;
 	char bits[10];
-	const char *logmsg;
 
 	CHECK_OBJ_NOTNULL(vt, VBP_TARGET_MAGIC);
 
@@ -175,27 +174,21 @@ VBP_Update_Backend(struct vbp_target *vt)
 		return;
 	}
 
-	if (vt->good >= vt->threshold) {
-		if (vt->backend->director->sick) {
-			logmsg = "Back healthy";
-			VRT_SetHealth(vt->backend->director, 1);
-		} else {
-			logmsg = "Still healthy";
-		}
-	} else {
-		if (vt->backend->director->sick) {
-			logmsg = "Still sick";
-		} else {
-			logmsg = "Went sick";
-			VRT_SetHealth(vt->backend->director, 0);
-		}
-	}
-	VSL(SLT_Backend_health, 0, "%s %s %s %u %u %u %.6f %.6f %s",
-	    vt->backend->director->vcl_name, logmsg, bits,
+	i = (vt->good < vt->threshold);
+	chg = (i != vt->backend->sick);
+	vt->backend->sick = i;
+
+	VSL(SLT_Backend_health, 0, "%s %s %s %s %u %u %u %.6f %.6f %s",
+	    vt->backend->director->vcl_name, chg ? "Went" : "Still",
+	    i ? "sick" : "healthy", bits,
 	    vt->good, vt->threshold, vt->window,
 	    vt->last, vt->avg, vt->resp_buf);
 	VBE_SetHappy(vt->backend, vt->happy);
 
+	if (chg) {
+		vt->backend->changed = VTIM_real();
+		VRT_SetChanged(vt->backend->director, vt->backend->changed);
+	}
 	Lck_Unlock(&vbp_mtx);
 }
 
@@ -524,10 +517,10 @@ VBP_Status(struct vsb *vsb, const struct backend *be, int details, int json)
 		if (json)
 			VSB_printf(vsb, "[%u, %u, \"%s\"]",
 			    vt->good, vt->window,
-			    vt->backend->director->sick ? "sick" : "healthy");
+			    vt->backend->sick ? "sick" : "healthy");
 		else
 			VSB_printf(vsb, "%u/%u %s", vt->good, vt->window,
-			    vt->backend->director->sick ? "sick" : "healthy");
+			    vt->backend->sick ? "sick" : "healthy");
 		return;
 	}
 
@@ -690,7 +683,7 @@ VBP_Remove(struct backend *be)
 	CHECK_OBJ_NOTNULL(vt, VBP_TARGET_MAGIC);
 
 	Lck_Lock(&vbp_mtx);
-	VRT_SetHealth(be->director, 1);
+	be->sick = 1;
 	be->probe = NULL;
 	vt->backend = NULL;
 	if (vt->running) {
diff --git a/bin/varnishd/cache/cache_director.c b/bin/varnishd/cache/cache_director.c
index b78e6c863..064ac546b 100644
--- a/bin/varnishd/cache/cache_director.c
+++ b/bin/varnishd/cache/cache_director.c
@@ -229,21 +229,18 @@ VRT_Healthy(VRT_CTX, VCL_BACKEND d, VCL_TIME *changed)
 	if (d->vdir->methods->healthy == NULL) {
 		if (changed != NULL)
 			*changed = d->vdir->health_changed;
-		return (!d->sick);
+		return (1);
 	}
 
 	return (d->vdir->methods->healthy(ctx, d, changed));
 }
 
 /*--------------------------------------------------------------------
- * Update health_changed. This is for load balancing directors
- * to update their health_changed time based on their backends.
+ * Update health_changed.
  */
 VCL_VOID
-VRT_SetChanged(VRT_CTX, VCL_BACKEND d, VCL_TIME changed)
+VRT_SetChanged(VCL_BACKEND d, VCL_TIME changed)
 {
-	(void)ctx;
-
 	if (d == NULL)
 		return;
 
@@ -278,7 +275,6 @@ VDI_Panic(const struct director *d, struct vsb *vsb, const char *nm)
 	VSB_printf(vsb, "%s = %p {\n", nm, d);
 	VSB_indent(vsb, 2);
 	VSB_printf(vsb, "cli_name = %s,\n", d->vdir->cli_name);
-	VSB_printf(vsb, "health = %s,\n", d->sick ?  "sick" : "healthy");
 	VSB_printf(vsb, "admin_health = %s, changed = %f,\n",
 	    VDI_Ahealth(d), d->vdir->health_changed);
 	VSB_printf(vsb, "type = %s {\n", d->vdir->methods->type);
@@ -460,8 +456,6 @@ do_set_health(struct cli *cli, struct director *d, void *priv)
 	if (d->vdir->admin_health != sh->ah) {
 		d->vdir->health_changed = VTIM_real();
 		d->vdir->admin_health = sh->ah;
-		d->sick &= ~0x02;
-		d->sick |= sh->ah->health ? 0 : 0x02;
 	}
 	return (0);
 }
diff --git a/bin/varnishd/cache/cache_vrt_vcl.c b/bin/varnishd/cache/cache_vrt_vcl.c
index 3374f80f4..b70430edd 100644
--- a/bin/varnishd/cache/cache_vrt_vcl.c
+++ b/bin/varnishd/cache/cache_vrt_vcl.c
@@ -186,7 +186,6 @@ VRT_AddDirector(VRT_CTX, const struct vdi_methods *m, void *priv,
 	d->vcl_name = vdir->cli_name + i;
 
 	vdir->vcl = vcl;
-	d->sick = 0;
 	vdir->admin_health = VDI_AH_PROBE;
 	vdir->health_changed = VTIM_real();
 
@@ -231,22 +230,6 @@ VRT_DelDirector(VCL_BACKEND *bp)
 	FREE_OBJ(vdir);
 }
 
-void
-VRT_SetHealth(VCL_BACKEND d, int health)
-{
-	struct vcldir *vdir;
-
-	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
-	vdir = d->vdir;
-	CHECK_OBJ_NOTNULL(vdir, VCLDIR_MAGIC);
-
-	if (health)
-		vdir->dir->sick &= ~0x01;
-	else
-		vdir->dir->sick |= 0x01;
-	vdir->health_changed = VTIM_real();
-}
-
 void
 VRT_DisableDirector(VCL_BACKEND d)
 {
@@ -257,7 +240,6 @@ VRT_DisableDirector(VCL_BACKEND d)
 	CHECK_OBJ_NOTNULL(vdir, VCLDIR_MAGIC);
 
 	vdir->admin_health = VDI_AH_DELETED;
-	vdir->dir->sick |= 0x04;
 	vdir->health_changed = VTIM_real();
 }
 
diff --git a/include/vrt.h b/include/vrt.h
index ca821d591..0d4b339a3 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -64,6 +64,7 @@
  *	struct vdi_methods .list callback signature changed
  *	VRT_LookupDirector() added
  *	VRT_SetChanged() added
+ *	VRT_SetHealth() removed
  * 8.0 (2018-09-15)
  *	VRT_Strands() added
  *	VRT_StrandsWS() added
@@ -518,17 +519,15 @@ struct vcldir;
 struct director {
 	unsigned			magic;
 #define DIRECTOR_MAGIC			0x3336351d
-	unsigned			sick;
 	void				*priv;
 	char				*vcl_name;
 	struct vcldir			*vdir;
 };
 
 VCL_BOOL VRT_Healthy(VRT_CTX, VCL_BACKEND, VCL_TIME *);
-VCL_VOID VRT_SetChanged(VRT_CTX, VCL_BACKEND, VCL_TIME);
+VCL_VOID VRT_SetChanged(VCL_BACKEND, VCL_TIME);
 VCL_BACKEND VRT_AddDirector(VRT_CTX, const struct vdi_methods *,
     void *, const char *, ...) v_printflike_(4, 5);
-void VRT_SetHealth(VCL_BACKEND d, int health);
 void VRT_DisableDirector(VCL_BACKEND);
 VCL_BACKEND VRT_LookupDirector(VRT_CTX, VCL_STRING);
 void VRT_DelDirector(VCL_BACKEND *);
diff --git a/lib/libvmod_directors/vdir.c b/lib/libvmod_directors/vdir.c
index 865ee1979..58b8b8dcc 100644
--- a/lib/libvmod_directors/vdir.c
+++ b/lib/libvmod_directors/vdir.c
@@ -238,7 +238,7 @@ vdir_list(VRT_CTX, struct vdir *vd, struct vsb *vsb, int pflag, int jflag)
 	}
 	vdir_unlock(vd);
 
-	VRT_SetChanged(ctx, vd->dir, changed);
+	VRT_SetChanged(vd->dir, changed);
 
 	if (jflag && (pflag)) {
 		VSB_printf(vsb, "\n");


More information about the varnish-commit mailing list