[master] 552607abb shard director lifetime is not identical to the vcl object

Nils Goroll nils.goroll at uplex.de
Tue Jun 26 08:42:10 UTC 2018


commit 552607abb2de6a6bfe514fa3d9ee8217f67e50e1
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Tue Jun 26 10:31:13 2018 +0200

    shard director lifetime is not identical to the vcl object
    
    Prepare for use of the shard director as a dynamic backend, where the
    VCL_BACKEND can outlast the vmod object:
    
    * the director priv now points to the struct sharddir instead of
      the vmod object
    
    * move the default parameters pointer to struct sharddir
    
    * index the PRIV_TASK by the struct sharddir pointer

diff --git a/lib/libvmod_directors/shard_dir.c b/lib/libvmod_directors/shard_dir.c
index e0a876c2e..61a14bf75 100644
--- a/lib/libvmod_directors/shard_dir.c
+++ b/lib/libvmod_directors/shard_dir.c
@@ -221,7 +221,8 @@ shard_next(struct shard_state *state, VCL_INT skip, VCL_BOOL healthy)
 }
 
 void
-sharddir_new(struct sharddir **sharddp, const char *vcl_name)
+sharddir_new(struct sharddir **sharddp, const char *vcl_name,
+    const struct vmod_directors_shard_param *param)
 {
 	struct sharddir *shardd;
 
@@ -232,9 +233,18 @@ sharddir_new(struct sharddir **sharddp, const char *vcl_name)
 	AN(shardd);
 	*sharddp = shardd;
 	shardd->name = vcl_name;
+	shardd->param = param;
 	AZ(pthread_rwlock_init(&shardd->mtx, NULL));
 }
 
+void
+sharddir_set_param(struct sharddir *shardd,
+    const struct vmod_directors_shard_param *param)
+{
+	CHECK_OBJ_NOTNULL(shardd, SHARDDIR_MAGIC);
+	shardd->param = param;
+}
+
 void
 sharddir_delete(struct sharddir **sharddp)
 {
diff --git a/lib/libvmod_directors/shard_dir.h b/lib/libvmod_directors/shard_dir.h
index 7bf5d756c..62bc2df53 100644
--- a/lib/libvmod_directors/shard_dir.h
+++ b/lib/libvmod_directors/shard_dir.h
@@ -65,6 +65,8 @@ struct shard_backend {
 	uint32_t		canon_point;
 };
 
+struct vmod_directors_shard_param;
+
 #define	SHDBG_LOOKUP	 1
 #define	SHDBG_CIRCLE	(1<<1)
 #define	SHDBG_RAMPWARM	(1<<2)
@@ -76,13 +78,13 @@ struct sharddir {
 
 	pthread_rwlock_t			mtx;
 
-	const char				*name;
-
 	unsigned				n_backend;
 	unsigned				l_backend;
 	struct shard_backend			*backend;
 
+	const char				*name;
 	struct shard_circlepoint		*hashcircle;
+	const struct vmod_directors_shard_param	*param;
 
 	VCL_DURATION				rampup_duration;
 	VCL_REAL				warmup;
@@ -127,7 +129,10 @@ void sharddir_debug(struct sharddir *shardd, const uint32_t flags);
 void sharddir_err(VRT_CTX, enum VSL_tag_e tag,  const char *fmt, ...);
 uint32_t sharddir_sha256v(const char *s, va_list ap);
 uint32_t sharddir_sha256(const char *s, ...);
-void sharddir_new(struct sharddir **sharddp, const char *vcl_name);
+void sharddir_new(struct sharddir **sharddp, const char *vcl_name,
+    const struct vmod_directors_shard_param *param);
+void sharddir_set_param(struct sharddir *shardd,
+    const struct vmod_directors_shard_param *param);
 void sharddir_delete(struct sharddir **sharddp);
 void sharddir_wrlock(struct sharddir *shardd);
 void sharddir_unlock(struct sharddir *shardd);
diff --git a/lib/libvmod_directors/vmod_shard.c b/lib/libvmod_directors/vmod_shard.c
index 5e03f8a27..6a28721b1 100644
--- a/lib/libvmod_directors/vmod_shard.c
+++ b/lib/libvmod_directors/vmod_shard.c
@@ -175,7 +175,6 @@ struct vmod_directors_shard {
 #define VMOD_SHARD_SHARD_MAGIC			0x6e63e1bf
 	struct sharddir				*shardd;
 	VCL_BACKEND				dir;
-	const struct vmod_directors_shard_param	*param;
 };
 
 static enum by_e
@@ -250,11 +249,11 @@ vmod_shard__init(VRT_CTX, struct vmod_directors_shard **vshardp,
 	AN(vshard);
 
 	*vshardp = vshard;
-	sharddir_new(&vshard->shardd, vcl_name);
+	sharddir_new(&vshard->shardd, vcl_name, &shard_param_default);
 
-	vshard->param = &shard_param_default;
 	vshard->dir =
-	    VRT_AddDirector(ctx, vmod_shard_methods, vshard, "%s", vcl_name);
+	    VRT_AddDirector(ctx, vmod_shard_methods, vshard->shardd,
+			    "%s", vcl_name);
 }
 
 VCL_VOID v_matchproto_(td_directors_shard__fini)
@@ -315,7 +314,8 @@ vmod_shard_associate(VRT_CTX,
 	CHECK_OBJ_NOTNULL(vshard, VMOD_SHARD_SHARD_MAGIC);
 
 	if (b == NULL) {
-		vshard->param = &shard_param_default;
+		sharddir_set_param(vshard->shardd,
+				   &shard_param_default);
 		return;
 	}
 
@@ -326,7 +326,8 @@ vmod_shard_associate(VRT_CTX,
 		return;
 	}
 
-	vshard->param = ppt;
+	sharddir_set_param(vshard->shardd,
+			   ppt);
 }
 
 VCL_BOOL v_matchproto_(td_directors_shard_add_backend)
@@ -663,7 +664,8 @@ vmod_shard_backend(VRT_CTX, struct vmod_directors_shard *vshard,
 
 		assert(ctx->method & VCL_MET_TASK_B);
 
-		pp = shard_param_task(ctx, vshard, vshard->param);
+		pp = shard_param_task(ctx, vshard->shardd,
+				      vshard->shardd->param);
 		if (pp == NULL)
 			return (NULL);
 		pp->vcl_name = vshard->shardd->name;
@@ -675,7 +677,7 @@ vmod_shard_backend(VRT_CTX, struct vmod_directors_shard *vshard,
 				 "used in vcl_init{}/vcl_fini{}");
 			return (NULL);
 		}
-		pp = shard_param_stack(&pstk, vshard->param,
+		pp = shard_param_stack(&pstk, vshard->shardd->param,
 				       vshard->shardd->name);
 		break;
 	default:
@@ -712,31 +714,31 @@ vmod_shard_backend(VRT_CTX, struct vmod_directors_shard *vshard,
 static VCL_BOOL v_matchproto_(vdi_healthy)
 vmod_shard_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
 {
-	struct vmod_directors_shard *vshard;
+	struct sharddir *shardd;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
-	CAST_OBJ_NOTNULL(vshard, dir->priv, VMOD_SHARD_SHARD_MAGIC);
-	return (sharddir_any_healthy(ctx, vshard->shardd, changed));
+	CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
+	return (sharddir_any_healthy(ctx, shardd, changed));
 }
 
 static VCL_BACKEND v_matchproto_(vdi_resolve_f)
 vmod_shard_resolve(VRT_CTX, VCL_BACKEND dir)
 {
-	struct vmod_directors_shard *vshard;
+	struct sharddir *shardd;
 	struct vmod_directors_shard_param pstk[1];
 	const struct vmod_directors_shard_param *pp;
 
 	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
 	CHECK_OBJ_NOTNULL(dir, DIRECTOR_MAGIC);
-	CAST_OBJ_NOTNULL(vshard, dir->priv, VMOD_SHARD_SHARD_MAGIC);
+	CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
 
-	pp = vmod_shard_param_read(ctx, vshard,
-				   vshard->param, pstk, "shard_resolve");
+	pp = vmod_shard_param_read(ctx, shardd, shardd->param,
+				   pstk, "shard_resolve");
 	if (pp == NULL)
 		return (NULL);
 
-	return (sharddir_pick_be(ctx, vshard->shardd,
+	return (sharddir_pick_be(ctx, shardd,
 				 shard_get_key(ctx, pp), pp->alt, pp->warmup,
 				 pp->rampup, pp->healthy));
 }


More information about the varnish-commit mailing list