[master] ec1e6f4 Introduce VSM_Start() which takes care of waiting around for varnishd if that is desired.

Poul-Henning Kamp phk at FreeBSD.org
Wed Jun 28 23:20:07 CEST 2017


commit ec1e6f408508df6cd28040ec9fd24204a9f13374
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Jun 28 21:18:30 2017 +0000

    Introduce VSM_Start() which takes care of waiting around for varnishd
    if that is desired.

diff --git a/bin/varnishadm/varnishadm.c b/bin/varnishadm/varnishadm.c
index 52a6267..43cab6f 100644
--- a/bin/varnishadm/varnishadm.c
+++ b/bin/varnishadm/varnishadm.c
@@ -405,7 +405,7 @@ n_arg_sock(const char *n_arg)
 		VSM_Destroy(&vsm);
 		return (-1);
 	}
-	if (VSM_Open(vsm)) {
+	if (VSM_Start(vsm, 0, -1)) {
 		fprintf(stderr, "%s\n", VSM_Error(vsm));
 		VSM_Destroy(&vsm);
 		return (-1);
diff --git a/bin/varnishstat/varnishstat.c b/bin/varnishstat/varnishstat.c
index 23056e7..1cb5f94 100644
--- a/bin/varnishstat/varnishstat.c
+++ b/bin/varnishstat/varnishstat.c
@@ -253,7 +253,7 @@ int
 main(int argc, char * const *argv)
 {
 	struct vsm *vd;
-	double t_arg = 5.0, t_start = NAN;
+	double t_arg = 5.0;
 	int once = 0, xml = 0, json = 0, f_list = 0, curses = 0;
 	signed char opt;
 	int i;
@@ -308,34 +308,12 @@ main(int argc, char * const *argv)
 	if (!(xml || json || once || f_list))
 		curses = 1;
 
-	while (1) {
-		i = VSM_Open(vd);
-		if (!i)
-			break;
-		if (isnan(t_start) && t_arg > 0.) {
-			fprintf(stderr, "Can't open log -"
-			    " retrying for %.0f seconds\n", t_arg);
-			t_start = VTIM_real();
-		}
-		if (t_arg <= 0.)
-			break;
-		if (VTIM_real() - t_start > t_arg)
-			break;
-		VSM_ResetError(vd);
-		VTIM_sleep(0.5);
-	}
-
-	if (curses) {
-		if (i && t_arg >= 0.)
-			VUT_Error(1, "%s", VSM_Error(vd));
-		do_curses(vd, 1.0);
-		exit(0);
-	}
-
-	if (i)
+	if (VSM_Start(vd, t_arg, STDERR_FILENO))
 		VUT_Error(1, "%s", VSM_Error(vd));
 
-	if (xml)
+	if (curses)
+		do_curses(vd, 1.0);
+	else if (xml)
 		do_xml(vd);
 	else if (json)
 		do_json(vd);
diff --git a/bin/varnishtest/tests/u00005.vtc b/bin/varnishtest/tests/u00005.vtc
index 47a036e..5d91958 100644
--- a/bin/varnishtest/tests/u00005.vtc
+++ b/bin/varnishtest/tests/u00005.vtc
@@ -41,7 +41,7 @@ shell -err -expect "-t: Range error" \
 	"varnishstat -t -1"
 shell -err -expect "-t: Syntax error" \
 	"varnishstat -t foo"
-shell -err -expect "Cannot open /nonexistent/_.vsm" \
+shell -err -expect "Could not get hold of varnishd" \
 	"varnishstat -n /nonexistent -t 1"
 shell -expect "MAIN.uptime" \
 	"varnishstat -n ${v1_name} -1"
diff --git a/bin/varnishtest/vtc_logexp.c b/bin/varnishtest/vtc_logexp.c
index 6804772..1c5a696 100644
--- a/bin/varnishtest/vtc_logexp.c
+++ b/bin/varnishtest/vtc_logexp.c
@@ -142,6 +142,7 @@ struct logexp {
 	VTAILQ_ENTRY(logexp)		list;
 
 	char				*name;
+	char				*vname;
 	struct vtclog			*vl;
 	char				run;
 	VTAILQ_HEAD(,logexp_test)	tests;
@@ -156,7 +157,6 @@ struct logexp {
 	char				*query;
 
 	struct vsm			*vsm;
-	struct vsb			*n_arg;
 	struct VSL_data			*vsl;
 	struct VSLQ			*vslq;
 	pthread_t			tp;
@@ -193,15 +193,14 @@ logexp_delete(struct logexp *le)
 	free(le->name);
 	free(le->query);
 	VSM_Destroy(&le->vsm);
-	if (le->n_arg)
-		VSB_destroy(&le->n_arg);
 	FREE_OBJ(le);
 }
 
 static struct logexp *
-logexp_new(const char *name)
+logexp_new(const char *name, const char *varg)
 {
 	struct logexp *le;
+	struct vsb *vsb, *n_arg;
 
 	ALLOC_OBJ(le, LOGEXP_MAGIC);
 	AN(le);
@@ -217,6 +216,21 @@ logexp_new(const char *name)
 	AN(le->vsl);
 
 	VTAILQ_INSERT_TAIL(&logexps, le, list);
+
+	REPLACE(le->vname, varg);
+
+	vsb = VSB_new_auto();
+	AN(vsb);
+	AZ(VSB_printf(vsb, "${tmpdir}/%s", varg));
+	AZ(VSB_finish(vsb));
+	n_arg = macro_expand(le->vl, VSB_data(vsb));
+	VSB_destroy(&vsb);
+	if (n_arg == NULL)
+		vtc_fatal(le->vl, "-v argument problems");
+	if (VSM_n_Arg(le->vsm, VSB_data(n_arg)) <= 0)
+		vtc_fatal(le->vl, "-v argument error: %s",
+		    VSM_Error(le->vsm));
+	VSB_destroy(&n_arg);
 	return (le);
 }
 
@@ -376,13 +390,8 @@ logexp_start(struct logexp *le)
 	AN(le->vsl);
 	AZ(le->vslq);
 
-	if (le->n_arg == NULL)
-		vtc_fatal(le->vl, "-v argument not given");
-	if (VSM_n_Arg(le->vsm, VSB_data(le->n_arg)) <= 0)
-		vtc_fatal(le->vl, "-v argument error: %s",
-		    VSM_Error(le->vsm));
-	if (VSM_Open(le->vsm))
-		vtc_fatal(le->vl, "VSM_Open: %s", VSM_Error(le->vsm));
+	if (VSM_Start(le->vsm, 0, -1))
+		vtc_fatal(le->vl, "VSM_Start: %s", VSM_Error(le->vsm));
 	AN(le->vsl);
 	c = VSL_CursorVSM(le->vsl, le->vsm,
 	    (le->d_arg ? 0 : VSL_COPT_TAIL) | VSL_COPT_BATCH);
@@ -504,8 +513,6 @@ void
 cmd_logexpect(CMD_ARGS)
 {
 	struct logexp *le, *le2;
-	const char tmpdir[] = "${tmpdir}";
-	struct vsb *vsb;
 
 	(void)priv;
 	(void)cmd;
@@ -532,8 +539,12 @@ cmd_logexpect(CMD_ARGS)
 		if (!strcmp(le->name, av[0]))
 			break;
 	}
-	if (le == NULL)
-		le = logexp_new(av[0]);
+	if (le == NULL) {
+		if (strcmp(av[1], "-v") || av[2] == NULL)
+			vtc_fatal(vl, "new logexp lacks -v");
+		le = logexp_new(av[0], av[2]);
+		av += 2;
+	}
 	av++;
 
 	for (; *av != NULL; av++) {
@@ -556,18 +567,8 @@ cmd_logexpect(CMD_ARGS)
 		AZ(le->run);
 
 		if (!strcmp(*av, "-v")) {
-			if (av[1] == NULL)
-				vtc_fatal(le->vl, "Missing -v argument");
-			if (le->n_arg != NULL)
-				VSB_destroy(&le->n_arg);
-			vsb = VSB_new_auto();
-			AN(vsb);
-			AZ(VSB_printf(vsb, "%s/%s", tmpdir, av[1]));
-			AZ(VSB_finish(vsb));
-			le->n_arg = macro_expand(le->vl, VSB_data(vsb));
-			VSB_destroy(&vsb);
-			if (le->n_arg == NULL)
-				return;
+			if (av[1] == NULL || strcmp(av[1], le->vname))
+				vtc_fatal(le->vl, "-v argument cannot change");
 			av++;
 			continue;
 		}
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index b31a16f..37aebdf 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -214,6 +214,9 @@ varnishlog_thread(void *priv)
 	AN(vsm);
 	(void)VSM_n_Arg(vsm, v->workdir);
 
+	if (VSM_Start(vsm, vtc_maxdur, -1))
+		vtc_fatal(v->vl, "vsm|%s", VSM_Error(vsm));
+
 	c = NULL;
 	opt = 0;
 	while (v->pid || c != NULL) {
diff --git a/include/vapi/vsm.h b/include/vapi/vsm.h
index 37d25ef..ce851b0 100644
--- a/include/vapi/vsm.h
+++ b/include/vapi/vsm.h
@@ -98,6 +98,8 @@ int VSM_n_Arg(struct vsm *vd, const char *n_arg);
 	 *	 <0 on failure, VSM_Error() returns diagnostic string
 	 */
 
+int VSM_Start(struct vsm *vd, double patience, int progress_fd);
+
 const char *VSM_Name(const struct vsm *vd);
 	/*
 	 * Return the instance name (-i argument to varnishd)
diff --git a/lib/libvarnishapi/libvarnishapi.map b/lib/libvarnishapi/libvarnishapi.map
index 4ac8f19..c860e7d 100644
--- a/lib/libvarnishapi/libvarnishapi.map
+++ b/lib/libvarnishapi/libvarnishapi.map
@@ -182,6 +182,7 @@ LIBVARNISHAPI_1.7 {
 	VSM_invalid;
 	VSM_valid;
 	VSM_similar;
+	VSM_Start;
 	VSM_Destroy;
 	VSM_Map;
 	VSM_Unmap;
diff --git a/lib/libvarnishapi/vsm.c b/lib/libvarnishapi/vsm.c
index e07f01b..e43bd9d 100644
--- a/lib/libvarnishapi/vsm.c
+++ b/lib/libvarnishapi/vsm.c
@@ -85,6 +85,8 @@ struct vsm {
 	uint64_t		age_ok;
 	double			t_ok;
 
+	int			started;
+
 	struct vsc		*vsc;
 };
 
@@ -179,6 +181,7 @@ VSM_n_Arg(struct vsm *vd, const char *arg)
 	struct vsb *vsb;
 
 	CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
+	AZ(vd->started);
 
 	if (vd->head)
 		return (vsm_diag(vd, "VSM_n_Arg: Already open"));
@@ -202,6 +205,37 @@ VSM_n_Arg(struct vsm *vd, const char *arg)
 
 /*--------------------------------------------------------------------*/
 
+int
+VSM_Start(struct vsm *vd, double patience, int progress)
+{
+	double t0;
+	int i, n = 0;
+
+	CHECK_OBJ_NOTNULL(vd, VSM_MAGIC);
+	AN(vd->dname);
+	t0 = VTIM_mono();
+	while (1) {
+		i = VSM_Open(vd);
+		if (patience <= 0. || i == 0) {
+			if (progress >= 0 && n > 4)
+				(void)write(progress, "\n", 1);
+			vd->started = 1;
+			return (i);
+		}
+		if (t0 + patience < VTIM_mono()) {
+			if (progress >= 0 && n > 4)
+				(void)write(progress, "\n", 1);
+			return (vsm_diag(vd,
+			    "Could not get hold of varnishd, is it running?"));
+		}
+		if (progress >= 0 && !(++n % 4))
+			(void)write(progress, ".", 1);
+		VTIM_sleep(.25);
+	}
+}
+
+/*--------------------------------------------------------------------*/
+
 const char *
 VSM_Name(const struct vsm *vd)
 {
diff --git a/lib/libvarnishapi/vut.c b/lib/libvarnishapi/vut.c
index 1d110f7..8954b92 100644
--- a/lib/libvarnishapi/vut.c
+++ b/lib/libvarnishapi/vut.c
@@ -227,8 +227,6 @@ void
 VUT_Setup(void)
 {
 	struct VSL_cursor *c;
-	double t_start;
-	int i;
 
 	AN(VUT.vsl);
 	AZ(VUT.vsm);
@@ -255,45 +253,16 @@ VUT_Setup(void)
 		AN(VUT.vsm);
 		if (VUT.n_arg && VSM_n_Arg(VUT.vsm, VUT.n_arg) <= 0)
 			VUT_Error(1, "%s", VSM_Error(VUT.vsm));
-		t_start = NAN;
-		c = NULL;
-		while (1) {
-			i = VSM_Open(VUT.vsm);
-			if (!i)
-				c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
-				    (VUT.d_opt ? VSL_COPT_TAILSTOP :
-					VSL_COPT_TAIL)
-				    | VSL_COPT_BATCH);
-			if (c)
-				break;
-
-			if (isnan(t_start) && VUT.t_arg > 0.) {
-				fprintf(stderr, "Cannot open log -"
-				    " retrying for %.0f seconds\n", VUT.t_arg);
-				t_start = VTIM_real();
-			}
-			VSM_Close(VUT.vsm);
-			if (VUT.t_arg <= 0.)
-				break;
-			if (VTIM_real() - t_start > VUT.t_arg)
-				break;
-
-			VSM_ResetError(VUT.vsm);
-			VSL_ResetError(VUT.vsl);
-			VTIM_sleep(0.5);
-		}
-
-		if (VUT.t_arg >= 0. && (i || !c)) {
-			if (i)
-				VUT_Error(1, "%s", VSM_Error(VUT.vsm));
-			else
-				VUT_Error(1, "%s", VSL_Error(VUT.vsl));
-		} else if (!isnan(t_start))
-			fprintf(stderr, "Log opened\n");
+		if (VSM_Start(VUT.vsm, VUT.t_arg, STDERR_FILENO))
+			VUT_Error(1, "VSM: %s", VSM_Error(VUT.vsm));
+		c = VSL_CursorVSM(VUT.vsl, VUT.vsm,
+		    (VUT.d_opt ? VSL_COPT_TAILSTOP : VSL_COPT_TAIL)
+		    | VSL_COPT_BATCH);
+		if (c == 0)
+			VUT_Error(1, "VSL: %s", VSL_Error(VUT.vsl));
 	}
 
-	if (c)
-		VSLQ_SetCursor(VUT.vslq, &c);
+	VSLQ_SetCursor(VUT.vslq, &c);
 	AZ(c);
 
 	/* Signal handlers */



More information about the varnish-commit mailing list