From: phk Date: Thu, 28 Aug 2008 09:11:41 +0000 (+0000) Subject: Make it possible to configure the number of retries the random director X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0627a35d5c7f27cf0ce13ad2d28907e4aa33c020;p=varnish Make it possible to configure the number of retries the random director will make at getting a backend connection. By default it tries as many times as it has members. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3138 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache_dir_random.c b/varnish-cache/bin/varnishd/cache_dir_random.c index dcb09ce8..dcdb9133 100644 --- a/varnish-cache/bin/varnishd/cache_dir_random.c +++ b/varnish-cache/bin/varnishd/cache_dir_random.c @@ -57,6 +57,8 @@ struct vdi_random { unsigned magic; #define VDI_RANDOM_MAGIC 0x3771ae23 struct director dir; + + unsigned retries; struct vdi_random_host *hosts; unsigned nhosts; }; @@ -74,7 +76,7 @@ vdi_random_getfd(struct sess *sp) CAST_OBJ_NOTNULL(vs, sp->director->priv, VDI_RANDOM_MAGIC); k = 0; - for (k = 0; k < 4; ) { /* XXX: 4 is arbitrary */ + for (k = 0; k < vs->retries; ) { r = random() / 2147483648.0; /* 2^31 */ assert(r >= 0.0 && r < 1.0); @@ -143,7 +145,6 @@ VRT_init_dir_random(struct cli *cli, struct director **bp, const struct vrt_dir_ struct vdi_random *vs; const struct vrt_dir_random_entry *te; struct vdi_random_host *vh; - double s; int i; (void)cli; @@ -159,7 +160,9 @@ VRT_init_dir_random(struct cli *cli, struct director **bp, const struct vrt_dir_ vs->dir.getfd = vdi_random_getfd; vs->dir.fini = vdi_random_fini; - s = 0; + vs->retries = t->retries; + if (vs->retries == 0) + vs->retries = t->nmember; vh = vs->hosts; te = t->members; for (i = 0; i < t->nmember; i++, vh++, te++) { diff --git a/varnish-cache/include/vrt.h b/varnish-cache/include/vrt.h index 243c5d65..9b356826 100644 --- a/varnish-cache/include/vrt.h +++ b/varnish-cache/include/vrt.h @@ -93,6 +93,7 @@ struct vrt_dir_random_entry { struct vrt_dir_random { const char *name; + unsigned retries; unsigned nmember; const struct vrt_dir_random_entry *members; }; diff --git a/varnish-cache/lib/libvcl/vcc_dir_random.c b/varnish-cache/lib/libvcl/vcc_dir_random.c index 8f817199..bd681c42 100644 --- a/varnish-cache/lib/libvcl/vcc_dir_random.c +++ b/varnish-cache/lib/libvcl/vcc_dir_random.c @@ -54,11 +54,29 @@ vcc_ParseRandomDirector(struct tokenlist *tl, const struct token *t_policy, cons { struct token *t_field, *t_be; int nbh, nelem; - struct fld_spec *fs; - unsigned u; + struct fld_spec *fs, *mfs; + unsigned u, retries; const char *first; - fs = vcc_FldSpec(tl, "!backend", "!weight", NULL); + fs = vcc_FldSpec(tl, "?retries", NULL); + + retries = 0; + while (tl->t->tok != '{') { + vcc_IsField(tl, &t_field, fs); + ERRCHK(tl); + if (vcc_IdIs(t_field, "retries")) { + ExpectErr(tl, CNUM); + retries = vcc_UintVal(tl); + ERRCHK(tl); + vcc_NextToken(tl); + ExpectErr(tl, ';'); + vcc_NextToken(tl); + } else { + ErrInternal(tl); + } + } + + mfs = vcc_FldSpec(tl, "!backend", "!weight", NULL); Fc(tl, 0, "\nstatic const struct vrt_dir_random_entry vdre_%.*s[] = {\n", @@ -67,7 +85,7 @@ vcc_ParseRandomDirector(struct tokenlist *tl, const struct token *t_policy, cons for (nelem = 0; tl->t->tok != '}'; nelem++) { /* List of members */ first = ""; t_be = tl->t; - vcc_ResetFldSpec(fs); + vcc_ResetFldSpec(mfs); nbh = -1; ExpectErr(tl, '{'); @@ -75,7 +93,7 @@ vcc_ParseRandomDirector(struct tokenlist *tl, const struct token *t_policy, cons Fc(tl, 0, "\t{"); while (tl->t->tok != '}') { /* Member fields */ - vcc_IsField(tl, &t_field, fs); + vcc_IsField(tl, &t_field, mfs); ERRCHK(tl); if (vcc_IdIs(t_field, "backend")) { vcc_ParseBackendHost(tl, &nbh, @@ -85,6 +103,7 @@ vcc_ParseRandomDirector(struct tokenlist *tl, const struct token *t_policy, cons } else if (vcc_IdIs(t_field, "weight")) { ExpectErr(tl, CNUM); u = vcc_UintVal(tl); + ERRCHK(tl); if (u == 0) { vsb_printf(tl->sb, "The .weight must be higher " @@ -103,7 +122,7 @@ vcc_ParseRandomDirector(struct tokenlist *tl, const struct token *t_policy, cons } first = ", "; } - vcc_FieldsOk(tl, fs); + vcc_FieldsOk(tl, mfs); if (tl->err) { vsb_printf(tl->sb, "\nIn member host specfication starting at:\n"); @@ -118,6 +137,7 @@ vcc_ParseRandomDirector(struct tokenlist *tl, const struct token *t_policy, cons "\nstatic const struct vrt_dir_random vdr_%.*s = {\n", PF(t_dir)); Fc(tl, 0, "\t.name = \"%.*s\",\n", PF(t_dir)); + Fc(tl, 0, "\t.retries = %u,\n", retries); Fc(tl, 0, "\t.nmember = %d,\n", nelem); Fc(tl, 0, "\t.members = vdre_%.*s,\n", PF(t_dir)); Fc(tl, 0, "};\n"); diff --git a/varnish-cache/lib/libvcl/vcc_fixed_token.c b/varnish-cache/lib/libvcl/vcc_fixed_token.c index 3ee45b25..3d5510c1 100644 --- a/varnish-cache/lib/libvcl/vcc_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcc_fixed_token.c @@ -373,6 +373,7 @@ vcl_output_lang_h(struct vsb *sb) vsb_cat(sb, "\n"); vsb_cat(sb, "struct vrt_dir_random {\n"); vsb_cat(sb, " const char *name;\n"); + vsb_cat(sb, " unsigned retries;\n"); vsb_cat(sb, " unsigned nmember;\n"); vsb_cat(sb, " const struct vrt_dir_random_entry *members;\n"); vsb_cat(sb, "};\n");