[master] b0c0eaeaa refactor the vcl temperature into a struct

Nils Goroll nils.goroll at uplex.de
Mon Nov 18 15:32:06 UTC 2019


commit b0c0eaeaaa1aa156feeaba2e46d339b4212a5221
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Nov 18 16:30:57 2019 +0100

    refactor the vcl temperature into a struct
    
    as suggested by @Dridi with reference to @bsdphk in
    https://github.com/varnishcache/varnish-cache/pull/3130#pullrequestreview-316916213

diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 402f9f6f4..a2c437416 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -604,7 +604,7 @@ VRT_new_backend_clustered(VRT_CTX, struct vsmw_cluster *vc,
 
 	if (be->director != NULL) {
 		/* for cold VCL, update initial director state */
-		if (be->probe != NULL && ! VCL_WARM(vcl->temp))
+		if (be->probe != NULL && ! vcl->temp->is_warm)
 			VBP_Update_Backend(be->probe);
 
 		Lck_Lock(&backends_mtx);
diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index c320d6273..6b365cc56 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -45,14 +45,14 @@
 #include "vcli_serve.h"
 #include "vtim.h"
 
-const char * const VCL_TEMP_INIT = "init";
-const char * const VCL_TEMP_COLD = "cold";
-const char * const VCL_TEMP_WARM = "warm";
-const char * const VCL_TEMP_BUSY = "busy";
-const char * const VCL_TEMP_COOLING = "cooling";
+const struct vcltemp VCL_TEMP_INIT[1] = {{ .name = "init", .is_cold = 1 }};
+const struct vcltemp VCL_TEMP_COLD[1] = {{ .name = "cold", .is_cold = 1 }};
+const struct vcltemp VCL_TEMP_WARM[1] = {{ .name = "warm", .is_warm = 1 }};
+const struct vcltemp VCL_TEMP_BUSY[1] = {{ .name = "busy", .is_warm = 1 }};
+const struct vcltemp VCL_TEMP_COOLING[1] = {{ .name = "cooling" }};
 
 // not really a state
-static const char * const VCL_TEMP_LABEL = "label";
+const struct vcltemp VCL_TEMP_LABEL[1] = {{ .name = "label" }};
 
 /*
  * XXX: Presently all modifications to this list happen from the
@@ -254,17 +254,17 @@ vcl_get(struct vcl **vcc, struct vcl *vcl)
 				   * race */
 	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
 	if (vcl->label == NULL) {
-		AN(strcmp(vcl->state, VCL_TEMP_LABEL));
+		AN(strcmp(vcl->state, VCL_TEMP_LABEL->name));
 		*vcc = vcl;
 	} else {
-		AZ(strcmp(vcl->state, VCL_TEMP_LABEL));
+		AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
 		*vcc = vcl->label;
 	}
 	CHECK_OBJ_NOTNULL(*vcc, VCL_MAGIC);
 	AZ((*vcc)->discard);
 	(*vcc)->busy++;
 	Lck_Unlock(&vcl_mtx);
-	assert(VCL_WARM((*vcc)->temp));
+	assert((*vcc)->temp->is_warm);
 }
 
 /*--------------------------------------------------------------------*/
@@ -498,7 +498,7 @@ vcl_set_state(VRT_CTX, const char *state)
 	case '0':
 		if (vcl->temp == VCL_TEMP_COLD)
 			break;
-		if (vcl->busy == 0 && VCL_WARM(vcl->temp)) {
+		if (vcl->busy == 0 && vcl->temp->is_warm) {
 			vcl->temp = VTAILQ_EMPTY(&vcl->ref_list) ?
 			    VCL_TEMP_COLD : VCL_TEMP_COOLING;
 			AZ(vcl_send_event(ctx, VCL_EVENT_COLD));
@@ -682,8 +682,8 @@ vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
 			flg = "discarded";
 		} else
 			flg = "available";
-		VSB_printf(vsb, "%s\t%s\t%s\t%6u\t%s",
-		    flg, vcl->state, vcl->temp, vcl->busy, vcl->loaded_name);
+		VSB_printf(vsb, "%s\t%s\t%s\t%6u\t%s", flg, vcl->state,
+		    vcl->temp->name, vcl->busy, vcl->loaded_name);
 		if (vcl->label != NULL) {
 			VSB_printf(vsb, "\t->\t%s", vcl->label->loaded_name);
 			if (vcl->nrefs)
@@ -718,7 +718,7 @@ vcl_cli_list_json(struct cli *cli, const char * const *av, void *priv)
 		} else
 			VCLI_Out(cli, "\"available\",\n");
 		VCLI_Out(cli, "\"state\": \"%s\",\n", vcl->state);
-		VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp);
+		VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp->name);
 		VCLI_Out(cli, "\"busy\": %u,\n", vcl->busy);
 		VCLI_Out(cli, "\"name\": \"%s\"", vcl->loaded_name);
 		if (vcl->label != NULL) {
@@ -796,13 +796,13 @@ vcl_cli_discard(struct cli *cli, const char * const *av, void *priv)
 	VSC_C_main->n_vcl_avail--;
 	vcl->discard = 1;
 	if (vcl->label != NULL) {
-		AZ(strcmp(vcl->state, VCL_TEMP_LABEL));
+		AZ(strcmp(vcl->state, VCL_TEMP_LABEL->name));
 		vcl->label->nlabels--;
 		vcl->label= NULL;
 	}
 	Lck_Unlock(&vcl_mtx);
 
-	if (!strcmp(vcl->state, VCL_TEMP_LABEL)) {
+	if (!strcmp(vcl->state, VCL_TEMP_LABEL->name)) {
 		VTAILQ_REMOVE(&vcl_head, vcl, list);
 		free(vcl->loaded_name);
 		FREE_OBJ(vcl);
@@ -826,7 +826,7 @@ vcl_cli_label(struct cli *cli, const char * const *av, void *priv)
 	if (lbl == NULL) {
 		ALLOC_OBJ(lbl, VCL_MAGIC);
 		AN(lbl);
-		bprintf(lbl->state, "%s", VCL_TEMP_LABEL);
+		bprintf(lbl->state, "%s", VCL_TEMP_LABEL->name);
 		lbl->temp = VCL_TEMP_WARM;
 		REPLACE(lbl->loaded_name, av[2]);
 		VTAILQ_INSERT_TAIL(&vcl_head, lbl, list);
diff --git a/bin/varnishd/cache/cache_vcl.h b/bin/varnishd/cache/cache_vcl.h
index df45e9bf3..e6ac097c1 100644
--- a/bin/varnishd/cache/cache_vcl.h
+++ b/bin/varnishd/cache/cache_vcl.h
@@ -32,6 +32,7 @@
  */
 
 struct vfilter;
+struct vcltemp;;
 
 VTAILQ_HEAD(vfilter_head, vfilter);
 
@@ -46,7 +47,7 @@ struct vcl {
 	char			*loaded_name;
 	unsigned		busy;
 	unsigned		discard;
-	const char		*temp;
+	const struct vcltemp	*temp;
 	VTAILQ_HEAD(,vcldir)	director_list;
 	VTAILQ_HEAD(,vclref)	ref_list;
 	int			nrefs;
@@ -69,15 +70,18 @@ extern struct vcl		*vcl_active; /* protected by vcl_mtx */
 struct vcl *vcl_find(const char *);
 void vcl_get(struct vcl **, struct vcl *);
 
-extern const char * const VCL_TEMP_INIT;
-extern const char * const VCL_TEMP_COLD;
-extern const char * const VCL_TEMP_WARM;
-extern const char * const VCL_TEMP_BUSY;
-extern const char * const VCL_TEMP_COOLING;
+struct vcltemp {
+	const char * const	name;
+	unsigned		is_warm;
+	unsigned		is_cold;
+};
 
 /*
  * NB: The COOLING temperature is neither COLD nor WARM.
  * And LABEL is not a temperature, it's a different kind of VCL.
  */
-#define VCL_WARM(t) ((t) == VCL_TEMP_WARM || (t) == VCL_TEMP_BUSY)
-#define VCL_COLD(t) ((t) == VCL_TEMP_INIT || (t) == VCL_TEMP_COLD)
+extern const struct vcltemp VCL_TEMP_INIT[1];
+extern const struct vcltemp VCL_TEMP_COLD[1];
+extern const struct vcltemp VCL_TEMP_WARM[1];
+extern const struct vcltemp VCL_TEMP_BUSY[1];
+extern const struct vcltemp VCL_TEMP_COOLING[1];
diff --git a/bin/varnishd/cache/cache_vrt_vcl.c b/bin/varnishd/cache/cache_vrt_vcl.c
index a8ddee7ac..42bbf3b9b 100644
--- a/bin/varnishd/cache/cache_vrt_vcl.c
+++ b/bin/varnishd/cache/cache_vrt_vcl.c
@@ -113,7 +113,7 @@ VCL_Ref(struct vcl *vcl)
 {
 
 	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
-	assert(!VCL_COLD(vcl->temp));
+	assert(!vcl->temp->is_cold);
 	Lck_Lock(&vcl_mtx);
 	assert(vcl->busy > 0);
 	vcl->busy++;
@@ -150,7 +150,7 @@ VRT_AddDirector(VRT_CTX, const struct vdi_methods *m, void *priv,
 	struct vcl *vcl;
 	struct vcldir *vdir;
 	struct director *d;
-	const char *temp;
+	const struct vcltemp *temp;
 	va_list ap;
 	int i;
 
@@ -193,7 +193,7 @@ VRT_AddDirector(VRT_CTX, const struct vdi_methods *m, void *priv,
 	temp = vcl->temp;
 	if (temp != VCL_TEMP_COOLING)
 		VTAILQ_INSERT_TAIL(&vcl->director_list, vdir, list);
-	if (VCL_WARM(temp))
+	if (temp->is_warm)
 		VDI_Event(d, VCL_EVENT_WARM);
 	Lck_Unlock(&vcl_mtx);
 
@@ -201,7 +201,7 @@ VRT_AddDirector(VRT_CTX, const struct vdi_methods *m, void *priv,
 		deldirector(vdir);
 		return (NULL);
 	}
-	if (!VCL_WARM(temp) && temp != VCL_TEMP_INIT)
+	if (!temp->is_warm && temp != VCL_TEMP_INIT)
 		WRONG("Dynamic Backends can only be added to warm VCLs");
 
 	return (d);
@@ -222,7 +222,7 @@ VRT_DelDirector(VCL_BACKEND *bp)
 {
 	struct vcl *vcl;
 	struct vcldir *vdir;
-	const char *temp;
+	const struct vcltemp *temp;
 	VCL_BACKEND d;
 
 	TAKE_OBJ_NOTNULL(d, bp, DIRECTOR_MAGIC);
@@ -236,7 +236,7 @@ VRT_DelDirector(VCL_BACKEND *bp)
 	VTAILQ_REMOVE(&vcl->director_list, vdir, list);
 	Lck_Unlock(&vcl_mtx);
 
-	if (VCL_WARM(temp))
+	if (temp->is_warm)
 		VDI_Event(d, VCL_EVENT_COLD);
 	assert (d == vdir->dir);
 	deldirector(vdir);
@@ -374,7 +374,7 @@ VRT_VCL_Prevent_Discard(VRT_CTX, const char *desc)
 
 	vcl = ctx->vcl;
 	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
-	assert(VCL_WARM(vcl->temp));
+	assert(vcl->temp->is_warm);
 
 	ALLOC_OBJ(ref, VCLREF_MAGIC);
 	AN(ref);


More information about the varnish-commit mailing list