[master] 1f80653 Allow multiple VCL labels to point to the same "real" VCL.

Poul-Henning Kamp phk at FreeBSD.org
Sat Sep 10 22:39:07 CEST 2016


commit 1f806539dc0b5c4780fe020355342c0266e50f3d
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat Sep 10 20:07:55 2016 +0000

    Allow multiple VCL labels to point to the same "real" VCL.

diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index 621711c..6995612 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -74,6 +74,7 @@ struct vcl {
 	VTAILQ_HEAD(,backend)	backend_list;
 	VTAILQ_HEAD(,vclref)	ref_list;
 	struct vcl		*label;
+	int			nlabels;
 };
 
 struct vclref {
@@ -258,12 +259,13 @@ vcl_get(struct vcl **vcc, struct vcl *vcl)
 	AZ(errno=pthread_rwlock_unlock(&vcl->temp_rwl));
 	Lck_Lock(&vcl_mtx);
 	AN(vcl);
-	if (vcl->label == NULL)
+	if (vcl->label == NULL) {
+		AN(strcmp(vcl->state, VCL_TEMP_LABEL));
 		*vcc = vcl;
-	else if (strcmp(vcl->state, VCL_TEMP_LABEL))
-		*vcc = vcl;
-	else
+	} else {
+		AZ(strcmp(vcl->state, VCL_TEMP_LABEL));
 		*vcc = vcl->label;
+	}
 	AN(*vcc);
 	AZ((*vcc)->discard);
 	(*vcc)->busy++;
@@ -804,6 +806,8 @@ vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
 	struct vcl *vcl;
 	const char *flg;
 
+	/* NB: Shall generate same output as mcf_vcl_list() */
+
 	(void)av;
 	(void)priv;
 	ASSERT_CLI();
@@ -816,11 +820,12 @@ vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
 			flg = "available";
 		VCLI_Out(cli, "%-10s %5s/%-8s %6u %s",
 		    flg, vcl->state, vcl->temp, vcl->busy, vcl->loaded_name);
-		if (vcl->label != NULL) {
-			VCLI_Out(cli, " %s %s",
-			    strcmp(vcl->state, VCL_TEMP_LABEL) ?
-			    "<-" : "->", vcl->label->loaded_name);
-		}
+		if (vcl->label != NULL)
+			VCLI_Out(cli, " -> %s", vcl->label->loaded_name);
+		else if (vcl->nlabels > 1)
+			VCLI_Out(cli, " (%d labels)", vcl->nlabels);
+		else if (vcl->nlabels > 0)
+			VCLI_Out(cli, " (%d label)", vcl->nlabels);
 		VCLI_Out(cli, "\n");
 	}
 }
@@ -874,12 +879,13 @@ vcl_cli_discard(struct cli *cli, const char * const *av, void *priv)
 	AN(vcl);			// MGT ensures this
 	Lck_Lock(&vcl_mtx);
 	assert (vcl != vcl_active);	// MGT ensures this
+	AZ(vcl->nlabels);		// MGT ensures this
 	VSC_C_main->n_vcl_discard++;
 	VSC_C_main->n_vcl_avail--;
 	vcl->discard = 1;
 	if (vcl->label != NULL) {
 		AZ(strcmp(vcl->state, VCL_TEMP_LABEL));
-		vcl->label->label = NULL;
+		vcl->label->nlabels--;
 		vcl->label= NULL;
 	}
 	Lck_Unlock(&vcl_mtx);
@@ -901,21 +907,20 @@ vcl_cli_label(struct cli *cli, const char * const *av, void *priv)
 	(void)cli;
 	(void)priv;
 	vcl = vcl_find(av[3]);
-	AN(vcl);
+	AN(vcl);				// MGT ensures this
 	lbl = vcl_find(av[2]);
 	if (lbl == NULL) {
 		ALLOC_OBJ(lbl, VCL_MAGIC);
 		AN(lbl);
 		bprintf(lbl->state, "%s", VCL_TEMP_LABEL);
 		lbl->temp = VCL_TEMP_WARM;
-		lbl->loaded_name = strdup(av[2]);
-		AN(lbl->loaded_name);
+		REPLACE(lbl->loaded_name, av[2]);
 		VTAILQ_INSERT_TAIL(&vcl_head, lbl, list);
 	}
 	if (lbl->label != NULL)
-		lbl->label->label = NULL;
+		lbl->label->nlabels--;
 	lbl->label = vcl;
-	vcl->label = lbl;
+	vcl->nlabels++;
 	return;
 }
 
diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 612499f..ea06216 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -73,6 +73,7 @@ struct vclprog {
 	double			go_cold;
 	VTAILQ_HEAD(, vcldep)	dfrom;
 	VTAILQ_HEAD(, vcldep)	dto;
+	int			nto;
 };
 
 static VTAILQ_HEAD(, vclprog)	vclhead = VTAILQ_HEAD_INITIALIZER(vclhead);
@@ -171,6 +172,7 @@ mgt_vcl_dep_add(struct vclprog *vp_from, struct vclprog *vp_to)
 	VTAILQ_INSERT_TAIL(&vp_from->dfrom, vd, lfrom);
 	vd->to = vp_to;
 	VTAILQ_INSERT_TAIL(&vp_to->dto, vd, lto);
+	vp_to->nto++;
 }
 
 static void
@@ -180,6 +182,7 @@ mgt_vcl_dep_del(struct vcldep *vd)
 	CHECK_OBJ_NOTNULL(vd, VCLDEP_MAGIC);
 	VTAILQ_REMOVE(&vd->from->dfrom, vd, lfrom);
 	VTAILQ_REMOVE(&vd->to->dto, vd, lto);
+	vd->to->nto--;
 	FREE_OBJ(vd);
 }
 
@@ -614,27 +617,15 @@ mcf_vcl_discard(struct cli *cli, const char * const *av, void *priv)
 	mgt_vcl_del(vp);
 }
 
-static void
-mcf_list_labels(struct cli *cli, const struct vclprog *vp)
-{
-	int n = 0;
-	struct vcldep *vd;
-
-	VTAILQ_FOREACH(vd, &vp->dto, lto) {
-		if (n++ == 5) {
-			VCLI_Out(cli, " [...]");
-			break;
-		}
-		VCLI_Out(cli, " %s", vd->from->name);
-	}
-}
-
 static void __match_proto__(cli_func_t)
 mcf_vcl_list(struct cli *cli, const char * const *av, void *priv)
 {
 	unsigned status;
 	char *p;
 	struct vclprog *vp;
+	struct vcldep *vd;
+
+	/* NB: Shall generate same output as vcl_cli_list() */
 
 	(void)av;
 	(void)priv;
@@ -652,7 +643,15 @@ mcf_vcl_list(struct cli *cli, const char * const *av, void *priv)
 			VCLI_Out(cli, "/%-8s", vp->warm ?
 			    VCL_STATE_WARM : VCL_STATE_COLD);
 			VCLI_Out(cli, " %6s %s", "", vp->name);
-			mcf_list_labels(cli, vp);
+			if (mcf_is_label(vp)) {
+				vd = VTAILQ_FIRST(&vp->dfrom);
+				AN(vd);
+				VCLI_Out(cli, " -> %s", vd->to->name);
+			} else if (vp->nto > 1) {
+				VCLI_Out(cli, " (%d labels)", vp->nto);
+			} else if (vp->nto > 0) {
+				VCLI_Out(cli, " (%d label)", vp->nto);
+			}
 			VCLI_Out(cli, "\n");
 		}
 	}
@@ -680,13 +679,6 @@ mcf_vcl_label(struct cli *cli, const char * const *av, void *priv)
 		VCLI_Out(cli, "VCL labels cannot point to labels");
 		return;
 	}
-	if (!VTAILQ_EMPTY(&vpt->dto)) {
-		VCLI_SetResult(cli, CLIS_CANT);
-		VCLI_Out(cli, "VCL already labeled with");
-		mcf_list_labels(cli, vpt);
-		VCLI_Out(cli, "\n");
-		return;
-	}
 	vpl = mcf_vcl_byname(av[2]);
 	if (vpl != NULL) {
 		if (!mcf_is_label(vpl)) {



More information about the varnish-commit mailing list