[master] 03f1a2148 Output correct director health in CLI

Nils Goroll nils.goroll at uplex.de
Mon Feb 4 14:11:09 UTC 2019


commit 03f1a214828c7c250fa4a4edfe9011bcfc864860
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Fri Jan 25 17:47:20 2019 +0100

    Output correct director health in CLI
    
    As VRT_Healthy needs a VRT_CTX, we move vcl_get_ctx and vcl_rel_ctx
    to cache_varnishd.h scope as VCL_Get_CliCtx and VCL_Rel_CliCtx.
    
    It has been discussed before if all varnishd cli commands should have
    a VRT_CTX, so the natural solution would seem  to add one in or close
    to VCLS_Poll(), yet CLI commands need specific ctxes (e.g. vcl.load
    needs VCL_MET_INIT), so (for now) it appears sensible to limit the
    ctx' scope to individual backend method calls.
    
    Fixes #2887

diff --git a/bin/varnishd/cache/cache_director.c b/bin/varnishd/cache/cache_director.c
index ed1309db3..9bb13a444 100644
--- a/bin/varnishd/cache/cache_director.c
+++ b/bin/varnishd/cache/cache_director.c
@@ -277,6 +277,20 @@ struct list_args {
 	const char	*jsep;
 };
 
+static const char *
+cli_health(struct director *d)
+{
+	struct vrt_ctx *ctx;
+	VCL_BOOL healthy;
+
+	ctx = VCL_Get_CliCtx(0,0);
+	healthy = VRT_Healthy(ctx, d, NULL);
+	VCL_Rel_CliCtx(&ctx);
+	AZ(ctx);
+
+	return (healthy ? "healthy" : "sick");
+}
+
 static int v_matchproto_(vcl_be_func)
 do_list(struct cli *cli, struct director *d, void *priv)
 {
@@ -295,7 +309,7 @@ do_list(struct cli *cli, struct director *d, void *priv)
 	if (d->vdir->methods->list != NULL)
 		d->vdir->methods->list(d, cli->sb, 0, 0, 0);
 	else
-		VCLI_Out(cli, "%-10s", d->sick ? "sick" : "healthy");
+		VCLI_Out(cli, "%-10s", cli_health(d));
 
 	VTIM_format(d->vdir->health_changed, time_str);
 	VCLI_Out(cli, " %s", time_str);
@@ -327,7 +341,7 @@ do_list_json(struct cli *cli, struct director *d, void *priv)
 	if (d->vdir->methods->list != NULL)
 		d->vdir->methods->list(d, cli->sb, 0, 0, 1);
 	else
-		VCLI_Out(cli, "\"%s\"", d->sick ? "sick" : "healthy");
+		VCLI_Out(cli, "\"%s\"", cli_health(d));
 	VCLI_Out(cli, ",\n");
 
 	if ((la->p || la->v) && d->vdir->methods->list != NULL) {
diff --git a/bin/varnishd/cache/cache_varnishd.h b/bin/varnishd/cache/cache_varnishd.h
index 38ca918a7..07fd45969 100644
--- a/bin/varnishd/cache/cache_varnishd.h
+++ b/bin/varnishd/cache/cache_varnishd.h
@@ -389,6 +389,8 @@ const char *VCL_Return_Name(unsigned);
 const char *VCL_Method_Name(unsigned);
 void VCL_Bo2Ctx(struct vrt_ctx *, struct busyobj *);
 void VCL_Req2Ctx(struct vrt_ctx *, struct req *);
+struct vrt_ctx *VCL_Get_CliCtx(unsigned, int);
+void VCL_Rel_CliCtx(struct vrt_ctx **);
 
 #define VCL_MET_MAC(l,u,t,b) \
     void VCL_##l##_method(struct vcl *, struct worker *, struct req *, \
diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index 270452132..ed89868d7 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -106,8 +106,8 @@ VCL_Req2Ctx(struct vrt_ctx *ctx, struct req *req)
 
 /*--------------------------------------------------------------------*/
 
-static struct vrt_ctx *
-vcl_get_ctx(unsigned method, int msg)
+struct vrt_ctx *
+VCL_Get_CliCtx(unsigned method, int msg)
 {
 
 	ASSERT_CLI();
@@ -126,8 +126,8 @@ vcl_get_ctx(unsigned method, int msg)
 	return (&ctx_cli);
 }
 
-static void
-vcl_rel_ctx(struct vrt_ctx **ctx)
+void
+VCL_Rel_CliCtx(struct vrt_ctx **ctx)
 {
 
 	ASSERT_CLI();
@@ -614,7 +614,7 @@ VCL_Poll(void)
 	struct vcl *vcl, *vcl2;
 
 	ASSERT_CLI();
-	ctx = vcl_get_ctx(0, 0);
+	ctx = VCL_Get_CliCtx(0, 0);
 	VTAILQ_FOREACH_SAFE(vcl, &vcl_head, list, vcl2) {
 		if (vcl->temp == VCL_TEMP_BUSY ||
 		    vcl->temp == VCL_TEMP_COOLING) {
@@ -639,7 +639,7 @@ VCL_Poll(void)
 			VSC_C_main->n_vcl_discard--;
 		}
 	}
-	vcl_rel_ctx(&ctx);
+	VCL_Rel_CliCtx(&ctx);
 }
 
 /*--------------------------------------------------------------------*/
@@ -730,9 +730,9 @@ vcl_cli_load(struct cli *cli, const char * const *av, void *priv)
 
 	AZ(priv);
 	ASSERT_CLI();
-	ctx = vcl_get_ctx(VCL_MET_INIT, 1);
+	ctx = VCL_Get_CliCtx(VCL_MET_INIT, 1);
 	vcl_load(cli, ctx, av[2], av[3], av[4]);
-	vcl_rel_ctx(&ctx);
+	VCL_Rel_CliCtx(&ctx);
 }
 
 static void v_matchproto_(cli_func_t)
@@ -744,7 +744,7 @@ vcl_cli_state(struct cli *cli, const char * const *av, void *priv)
 	ASSERT_CLI();
 	AN(av[2]);
 	AN(av[3]);
-	ctx = vcl_get_ctx(0, 1);
+	ctx = VCL_Get_CliCtx(0, 1);
 	ctx->vcl = vcl_find(av[2]);
 	AN(ctx->vcl);			// MGT ensures this
 	if (vcl_set_state(ctx, av[3])) {
@@ -755,7 +755,7 @@ vcl_cli_state(struct cli *cli, const char * const *av, void *priv)
 		if (VSB_len(ctx->msg))
 			VCLI_Out(cli, "\nMessage:\n\t%s", VSB_data(ctx->msg));
 	}
-	vcl_rel_ctx(&ctx);
+	VCL_Rel_CliCtx(&ctx);
 }
 
 static void v_matchproto_(cli_func_t)


More information about the varnish-commit mailing list