[master] 579a1df Add a "list" method to directors, to participate in CLI::backend.list

Poul-Henning Kamp phk at FreeBSD.org
Tue Apr 24 20:53:17 UTC 2018


commit 579a1df977693effa2fe545a667f60eea960185b
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Apr 24 20:51:18 2018 +0000

    Add a "list" method to directors, to participate in CLI::backend.list
    
    Add a new -v flag, just because we can.

diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 40b36e7..0235194 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -417,6 +417,23 @@ vbe_panic(const struct director *d, struct vsb *vsb)
 }
 
 /*--------------------------------------------------------------------
+ */
+
+static void
+vbe_list(const struct director *d, struct vsb *vsb, int vflag, int pflag)
+{
+	struct backend *bp;
+
+	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
+	CAST_OBJ_NOTNULL(bp, d->priv, BACKEND_MAGIC);
+
+	if (bp->probe != NULL)
+		VBP_Status(vsb, bp, vflag | pflag);
+	else
+		VSB_printf(vsb, "%-10s", d->health ? "healthy" : "sick");
+}
+
+/*--------------------------------------------------------------------
  * Create a new static or dynamic director::backend instance.
  */
 
@@ -473,6 +490,7 @@ VRT_new_backend_clustered(VRT_CTX, struct vsmw_cluster *vc,
 	d->finish = vbe_dir_finish;
 	d->event = vbe_dir_event;
 	d->panic = vbe_panic;
+	d->list = vbe_list;
 	d->destroy = vbe_destroy;
 
 	d->health = 1;
diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index b0a0442..5f8eca1 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -81,4 +81,4 @@ void VBP_Insert(struct backend *b, struct vrt_backend_probe const *p,
     struct tcp_pool *);
 void VBP_Remove(struct backend *b);
 void VBP_Control(const struct backend *b, int stop);
-void VBP_Status(struct cli *cli, const struct backend *, int details);
+void VBP_Status(struct vsb *, const struct backend *, int details);
diff --git a/bin/varnishd/cache/cache_backend_probe.c b/bin/varnishd/cache/cache_backend_probe.c
index d180101..caf3673 100644
--- a/bin/varnishd/cache/cache_backend_probe.c
+++ b/bin/varnishd/cache/cache_backend_probe.c
@@ -168,9 +168,9 @@ vbp_update_backend(struct vbp_target *vt)
 		assert(i < sizeof bits);
 
 		if (vt->good >= vt->threshold) {
-			if (vt->backend->director->health)
+			if (vt->backend->director->health) {
 				logmsg = "Still healthy";
-			else {
+			} else {
 				logmsg = "Back healthy";
 				vt->backend->director->health_changed =
 				     VTIM_real();
@@ -181,8 +181,9 @@ vbp_update_backend(struct vbp_target *vt)
 				logmsg = "Went sick";
 				vt->backend->director->health_changed =
 				     VTIM_real();
-			} else
+			} else {
 				logmsg = "Still sick";
+			}
 			vt->backend->director->health = 0;
 		}
 		VSL(SLT_Backend_health, 0, "%s %s %s %u %u %u %.6f %.6f %s",
@@ -458,58 +459,56 @@ vbp_thread(struct worker *wrk, void *priv)
  */
 
 static void
-vbp_bitmap(struct cli *cli, char c, uint64_t map, const char *lbl)
+vbp_bitmap(struct vsb *vsb, char c, uint64_t map, const char *lbl)
 {
 	int i;
 	uint64_t u = (1ULL << 63);
 
-	VCLI_Out(cli, "  ");
+	VSB_printf(vsb, "  ");
 	for (i = 0; i < 64; i++) {
 		if (map & u)
-			VCLI_Out(cli, "%c", c);
+			VSB_putc(vsb, c);
 		else
-			VCLI_Out(cli, "-");
+			VSB_putc(vsb, '-');
 		map <<= 1;
 	}
-	VCLI_Out(cli, " %s\n", lbl);
+	VSB_printf(vsb, " %s\n", lbl);
 }
 
 /*lint -e{506} constant value boolean */
 /*lint -e{774} constant value boolean */
-static void
-vbp_health_one(struct cli *cli, const struct vbp_target *vt)
+void
+VBP_Status(struct vsb *vsb, const struct backend *be, int details)
 {
+	struct vbp_target *vt;
+	char buf[12];
+
+	CHECK_OBJ_NOTNULL(be, BACKEND_MAGIC);
+	vt = be->probe;
+	CHECK_OBJ_NOTNULL(vt, VBP_TARGET_MAGIC);
+
+	if (!details) {
+		bprintf(buf, "%d/%d %s", vt->good, vt->window,
+		    vt->backend->director->health ? "good" : "bad");
+		VSB_printf(vsb, "%-10s", buf);
+		return;
+	}
 
-	VCLI_Out(cli,
-	    "  Current states  good: %2u threshold: %2u window: %2u\n",
+	VSB_printf(vsb,
+	    "\nCurrent states  good: %2u threshold: %2u window: %2u\n",
 	    vt->good, vt->threshold, vt->window);
-	VCLI_Out(cli,
+	VSB_printf(vsb,
 	    "  Average response time of good probes: %.6f\n", vt->avg);
-	VCLI_Out(cli,
+	VSB_printf(vsb,
 	    "  Oldest ======================"
 	    "============================ Newest\n");
 
 #define BITMAP(n, c, t, b)					\
 		if ((vt->n != 0) || (b))			\
-			vbp_bitmap(cli, (c), vt->n, (t));
+			vbp_bitmap(vsb, (c), vt->n, (t));
 #include "tbl/backend_poll.h"
 }
 
-void
-VBP_Status(struct cli *cli, const struct backend *be, int details)
-{
-	struct vbp_target *vt;
-
-	CHECK_OBJ_NOTNULL(be, BACKEND_MAGIC);
-	vt = be->probe;
-	CHECK_OBJ_NOTNULL(vt, VBP_TARGET_MAGIC);
-	VCLI_Out(cli, "%d/%d", vt->good, vt->window);
-	if (details) {
-		VCLI_Out(cli, "\n");
-		vbp_health_one(cli, vt);
-	}
-}
-
 /*--------------------------------------------------------------------
  * Build request from probe spec
  */
diff --git a/bin/varnishd/cache/cache_director.c b/bin/varnishd/cache/cache_director.c
index 3ea2710..cd99f1a 100644
--- a/bin/varnishd/cache/cache_director.c
+++ b/bin/varnishd/cache/cache_director.c
@@ -38,7 +38,6 @@
 #include "cache_varnishd.h"
 
 #include "cache_director.h"
-#include "cache_backend.h"
 
 #include "vcli_serve.h"
 #include "vtim.h"
@@ -279,62 +278,69 @@ VDI_Healthy(const struct director *d, double *changed)
 
 /*---------------------------------------------------------------------*/
 
+struct list_args {
+	unsigned	magic;
+#define LIST_ARGS_MAGIC	0x7e7cefeb
+	int		p;
+	int		v;
+};
+
 static int v_matchproto_(vcl_be_func)
 do_list(struct cli *cli, struct director *d, void *priv)
 {
-	int *probes;
 	char time_str[VTIM_FORMAT_SIZE];
-	struct backend *be;
+	struct list_args *la;
 
-	AN(priv);
-	probes = priv;
+	CAST_OBJ_NOTNULL(la, priv, LIST_ARGS_MAGIC);
 	CHECK_OBJ_NOTNULL(d, DIRECTOR_MAGIC);
-	CAST_OBJ_NOTNULL(be, d->priv, BACKEND_MAGIC);
+
 	if (d->admin_health == VDI_AH_DELETED)
 		return (0);
 
-	VCLI_Out(cli, "\n%-30s", d->cli_name);
-
-	VCLI_Out(cli, " %-10s", VDI_Ahealth(d));
+	VCLI_Out(cli, "\n%-30s %-7s ", d->cli_name, VDI_Ahealth(d));
 
-	if (be->probe == NULL)
-		VCLI_Out(cli, " %-20s", "Healthy (no probe)");
-	else {
-		if (d->health)
-			VCLI_Out(cli, " %-20s", "Healthy ");
-		else
-			VCLI_Out(cli, " %-20s", "Sick ");
-		VBP_Status(cli, be, *probes);
-	}
+	if (d->list != NULL)
+		d->list(d, cli->sb, 0, 0);
+	else
+		VCLI_Out(cli, "%-10s", d->health ? "healthy" : "sick");
 
 	VTIM_format(d->health_changed, time_str);
 	VCLI_Out(cli, " %s", time_str);
-
+	if (la->p || la->v)
+		d->list(d, cli->sb, la->p, la->v);
 	return (0);
 }
 
 static void v_matchproto_(cli_func_t)
 cli_backend_list(struct cli *cli, const char * const *av, void *priv)
 {
-	int probes = 0;
+	const char *p;
+	struct list_args la[1];
 
 	(void)priv;
 	ASSERT_CLI();
-	if (av[2] != NULL && !strcmp(av[2], "-p")) {
+	INIT_OBJ(la, LIST_ARGS_MAGIC);
+	while (av[2] != NULL && av[2][0] == '-') {
+		for(p = av[2] + 1; *p; p++) {
+			switch(*p) {
+			case 'p': la->p = !la->p; break;
+			case 'v': la->p = !la->p; break;
+			default:
+				VCLI_Out(cli, "Invalid flag %c", *p);
+				VCLI_SetResult(cli, CLIS_PARAM);
+				return;
+			}
+		}
 		av++;
-		probes = 1;
-	} else if (av[2] != NULL && av[2][0] == '-') {
-		VCLI_Out(cli, "Invalid flags %s", av[2]);
-		VCLI_SetResult(cli, CLIS_PARAM);
-		return;
-	} else if (av[3] != NULL) {
+	}
+	if (av[3] != NULL) {
 		VCLI_Out(cli, "Too many arguments");
 		VCLI_SetResult(cli, CLIS_PARAM);
 		return;
 	}
-	VCLI_Out(cli, "%-30s %-10s %-20s %s", "Backend name", "Admin",
-	    "Probe", "Last updated");
-	(void)VCL_IterDirector(cli, av[2], do_list, &probes);
+	VCLI_Out(cli, "%-30s %-7s %-10s %s",
+	    "Backend name", "Admin", "Probe", "Last change");
+	(void)VCL_IterDirector(cli, av[2], do_list, la);
 }
 
 /*---------------------------------------------------------------------*/
diff --git a/bin/varnishd/cache/cache_director.h b/bin/varnishd/cache/cache_director.h
index c65118b..5a9cb60 100644
--- a/bin/varnishd/cache/cache_director.h
+++ b/bin/varnishd/cache/cache_director.h
@@ -60,6 +60,8 @@ typedef void vdi_destroy_f(const struct director *);
 
 typedef void vdi_panic_f(const struct director *, struct vsb *);
 
+typedef void vdi_list_f(const struct director *, struct vsb *, int, int);
+
 struct director {
 	unsigned			magic;
 #define DIRECTOR_MAGIC			0x3336351d
@@ -75,6 +77,8 @@ struct director {
 	vdi_event_f			*event;
 	vdi_destroy_f			*destroy;
 	vdi_panic_f			*panic;
+	vdi_list_f			*list;
+
 	void				*priv;
 	const void			*priv2;
 
diff --git a/bin/varnishtest/tests/o00004.vtc b/bin/varnishtest/tests/o00004.vtc
index f51bcc6..6aa345c 100644
--- a/bin/varnishtest/tests/o00004.vtc
+++ b/bin/varnishtest/tests/o00004.vtc
@@ -49,5 +49,5 @@ varnish v2 -vcl {
 server s1 -wait
 
 delay 1
-varnish v2 -cliexpect "vcl1.bp1[ ]+probe[ ]+Healthy[ ]+1/1" backend.list
-varnish v2 -cliexpect "vcl1.bp2[ ]+probe[ ]+Healthy[ ]+1/1" backend.list
+varnish v2 -cliexpect "vcl1.bp1[ ]+probe[ ]+1/1[ ]+good" backend.list
+varnish v2 -cliexpect "vcl1.bp2[ ]+probe[ ]+1/1[ ]+good" backend.list


More information about the varnish-commit mailing list