[master] 615298547 remove the temperature lock which serves no purpose

Nils Goroll nils.goroll at uplex.de
Mon Nov 18 15:05:08 UTC 2019


commit 615298547adfb06aa785b42552062bc391f56ecd
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Nov 13 17:30:35 2019 +0100

    remove the temperature lock which serves no purpose
    
    we ensure that only backends on the director_list receive events.
    Because events happen in the cli thread, we do not need another level of
    serialization.
    
    There is no explicit vtc, the test case has been added to vmod_debug and
    runs with every invocation of that vmod.
    
    Fixes #3094

diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index bdff0e489..c320d6273 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -264,9 +264,7 @@ vcl_get(struct vcl **vcc, struct vcl *vcl)
 	AZ((*vcc)->discard);
 	(*vcc)->busy++;
 	Lck_Unlock(&vcl_mtx);
-	AZ(errno=pthread_rwlock_rdlock(&(*vcc)->temp_rwl));
 	assert(VCL_WARM((*vcc)->temp));
-	AZ(errno=pthread_rwlock_unlock(&(*vcc)->temp_rwl));
 }
 
 /*--------------------------------------------------------------------*/
@@ -415,7 +413,6 @@ VCL_Open(const char *fn, struct vsb *msg)
 	}
 	ALLOC_OBJ(vcl, VCL_MAGIC);
 	AN(vcl);
-	AZ(errno=pthread_rwlock_init(&vcl->temp_rwl, NULL));
 	vcl->dlh = dlh;
 	vcl->conf = cnf;
 	return (vcl);
@@ -432,7 +429,6 @@ VCL_Close(struct vcl **vclp)
 	assert(VTAILQ_EMPTY(&vcl->vfps));
 	assert(VTAILQ_EMPTY(&vcl->vdps));
 	AZ(dlclose(vcl->dlh));
-	AZ(errno=pthread_rwlock_destroy(&vcl->temp_rwl));
 	FREE_OBJ(vcl);
 }
 
@@ -496,7 +492,6 @@ vcl_set_state(VRT_CTX, const char *state)
 	assert(ctx->msg != NULL || *state == '0');
 
 	vcl = ctx->vcl;
-	AZ(errno=pthread_rwlock_wrlock(&vcl->temp_rwl));
 	AN(vcl->temp);
 
 	switch (state[0]) {
@@ -543,7 +538,6 @@ vcl_set_state(VRT_CTX, const char *state)
 	}
 	if (i == 0 && state[1])
 		bprintf(vcl->state, "%s", state + 1);
-	AZ(errno=pthread_rwlock_unlock(&vcl->temp_rwl));
 
 	return (i);
 }
@@ -811,7 +805,6 @@ vcl_cli_discard(struct cli *cli, const char * const *av, void *priv)
 	if (!strcmp(vcl->state, VCL_TEMP_LABEL)) {
 		VTAILQ_REMOVE(&vcl_head, vcl, list);
 		free(vcl->loaded_name);
-		AZ(errno=pthread_rwlock_destroy(&vcl->temp_rwl));
 		FREE_OBJ(vcl);
 	} else if (vcl->temp == VCL_TEMP_COLD) {
 		VCL_Poll();
@@ -836,7 +829,6 @@ vcl_cli_label(struct cli *cli, const char * const *av, void *priv)
 		bprintf(lbl->state, "%s", VCL_TEMP_LABEL);
 		lbl->temp = VCL_TEMP_WARM;
 		REPLACE(lbl->loaded_name, av[2]);
-		AZ(errno=pthread_rwlock_init(&lbl->temp_rwl, NULL));
 		VTAILQ_INSERT_TAIL(&vcl_head, lbl, list);
 	}
 	if (lbl->label != NULL)
diff --git a/bin/varnishd/cache/cache_vcl.h b/bin/varnishd/cache/cache_vcl.h
index cfe489956..df45e9bf3 100644
--- a/bin/varnishd/cache/cache_vcl.h
+++ b/bin/varnishd/cache/cache_vcl.h
@@ -47,7 +47,6 @@ struct vcl {
 	unsigned		busy;
 	unsigned		discard;
 	const char		*temp;
-	pthread_rwlock_t	temp_rwl;
 	VTAILQ_HEAD(,vcldir)	director_list;
 	VTAILQ_HEAD(,vclref)	ref_list;
 	int			nrefs;
diff --git a/bin/varnishd/cache/cache_vrt_vcl.c b/bin/varnishd/cache/cache_vrt_vcl.c
index c1ee2c057..a8ddee7ac 100644
--- a/bin/varnishd/cache/cache_vrt_vcl.c
+++ b/bin/varnishd/cache/cache_vrt_vcl.c
@@ -113,9 +113,7 @@ VCL_Ref(struct vcl *vcl)
 {
 
 	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
-	AZ(errno=pthread_rwlock_rdlock(&vcl->temp_rwl));
 	assert(!VCL_COLD(vcl->temp));
-	AZ(errno=pthread_rwlock_unlock(&vcl->temp_rwl));
 	Lck_Lock(&vcl_mtx);
 	assert(vcl->busy > 0);
 	vcl->busy++;
@@ -152,6 +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;
 	va_list ap;
 	int i;
 
@@ -160,11 +159,10 @@ VRT_AddDirector(VRT_CTX, const struct vdi_methods *m, void *priv,
 	AN(fmt);
 	vcl = ctx->vcl;
 	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
-	AZ(errno=pthread_rwlock_rdlock(&vcl->temp_rwl));
-	if (vcl->temp == VCL_TEMP_COOLING) {
-		AZ(errno=pthread_rwlock_unlock(&vcl->temp_rwl));
+
+	// opportunistic, re-checked again under lock
+	if (vcl->temp == VCL_TEMP_COOLING)
 		return (NULL);
-	}
 
 	ALLOC_OBJ(d, DIRECTOR_MAGIC);
 	AN(d);
@@ -192,15 +190,19 @@ VRT_AddDirector(VRT_CTX, const struct vdi_methods *m, void *priv,
 	vdir->health_changed = VTIM_real();
 
 	Lck_Lock(&vcl_mtx);
-	VTAILQ_INSERT_TAIL(&vcl->director_list, vdir, list);
+	temp = vcl->temp;
+	if (temp != VCL_TEMP_COOLING)
+		VTAILQ_INSERT_TAIL(&vcl->director_list, vdir, list);
+	if (VCL_WARM(temp))
+		VDI_Event(d, VCL_EVENT_WARM);
 	Lck_Unlock(&vcl_mtx);
 
-	if (VCL_WARM(vcl->temp))
-		/* Only when adding backend to already warm VCL */
-		VDI_Event(d, VCL_EVENT_WARM);
-	else if (vcl->temp != VCL_TEMP_INIT)
+	if (temp == VCL_TEMP_COOLING) {
+		deldirector(vdir);
+		return (NULL);
+	}
+	if (!VCL_WARM(temp) && temp != VCL_TEMP_INIT)
 		WRONG("Dynamic Backends can only be added to warm VCLs");
-	AZ(errno=pthread_rwlock_unlock(&vcl->temp_rwl));
 
 	return (d);
 }
@@ -220,6 +222,7 @@ VRT_DelDirector(VCL_BACKEND *bp)
 {
 	struct vcl *vcl;
 	struct vcldir *vdir;
+	const char *temp;
 	VCL_BACKEND d;
 
 	TAKE_OBJ_NOTNULL(d, bp, DIRECTOR_MAGIC);
@@ -227,14 +230,14 @@ VRT_DelDirector(VCL_BACKEND *bp)
 	CHECK_OBJ_NOTNULL(vdir, VCLDIR_MAGIC);
 	vcl = vdir->vcl;
 	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
+
 	Lck_Lock(&vcl_mtx);
+	temp = vcl->temp;
 	VTAILQ_REMOVE(&vcl->director_list, vdir, list);
 	Lck_Unlock(&vcl_mtx);
 
-	AZ(errno=pthread_rwlock_rdlock(&vcl->temp_rwl));
-	if (VCL_WARM(vcl->temp))
+	if (VCL_WARM(temp))
 		VDI_Event(d, VCL_EVENT_COLD);
-	AZ(errno=pthread_rwlock_unlock(&vcl->temp_rwl));
 	assert (d == vdir->dir);
 	deldirector(vdir);
 }
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index fd408f767..1a547e172 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -50,6 +50,7 @@ struct priv_vcl {
 	struct vclref		*vclref_discard;
 	struct vclref		*vclref_cold;
 	VCL_DURATION		vcl_discard_delay;
+	VCL_BACKEND		be;
 };
 
 
@@ -403,6 +404,10 @@ xyzzy_vcl_allow_cold(VRT_CTX, struct vmod_priv *priv)
 	VRT_VCL_Allow_Cold(&priv_vcl->vclref_cold);
 }
 
+static const struct vdi_methods empty_methods[1] = {{
+	.magic =	VDI_METHODS_MAGIC,
+	.type =	"debug.dummy"
+}};
 
 static int
 event_warm(VRT_CTX, const struct vmod_priv *priv)
@@ -423,6 +428,11 @@ event_warm(VRT_CTX, const struct vmod_priv *priv)
 
 	bprintf(buf, "vmod-debug ref on %s", VCL_Name(ctx->vcl));
 	priv_vcl->vclref_discard = VRT_VCL_Prevent_Discard(ctx, buf);
+
+	AZ(priv_vcl->be);
+	priv_vcl->be = VRT_AddDirector(ctx, empty_methods,
+	    NULL, "%s", "dir_warmcold");
+
 	return (0);
 }
 
@@ -450,6 +460,8 @@ event_cold(VRT_CTX, const struct vmod_priv *priv)
 
 	VSL(SLT_Debug, 0, "%s: VCL_EVENT_COLD", VCL_Name(ctx->vcl));
 
+	VRT_DelDirector(&priv_vcl->be);
+
 	if (priv_vcl->vcl_discard_delay == 0.0) {
 		VRT_VCL_Allow_Discard(&priv_vcl->vclref_discard);
 		return (0);


More information about the varnish-commit mailing list