-
-
Notifications
You must be signed in to change notification settings - Fork 39
Prioritize backends using an ACL during selection #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,68 +252,70 @@ dom_wait_active(struct dynamic_domain *dom) | |
| } | ||
|
|
||
| /* find a healthy dynamic_ref */ | ||
|
|
||
| static struct dynamic_ref * | ||
| dom_find(VRT_CTX, struct dynamic_domain *dom, struct dynamic_ref *start, | ||
| VCL_BOOL *healthy, VCL_TIME *changed, unsigned wait) | ||
| { | ||
| struct dynamic_ref *next, *alt; | ||
| dom_find_v2(VRT_CTX, struct dynamic_domain *dom, VCL_BOOL *healthy, VCL_TIME *changed, unsigned wait) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be renamed to |
||
| struct dynamic_ref *r, *r_alt; | ||
| VCL_TIME c, cc; | ||
| VCL_BOOL h; | ||
|
|
||
| CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); | ||
| CHECK_OBJ_NOTNULL(dom, DYNAMIC_DOMAIN_MAGIC); | ||
| CHECK_OBJ_ORNULL(start, DYNAMIC_REF_MAGIC); | ||
|
|
||
| dom_wait_active(dom); | ||
|
|
||
| if (dom->status > DYNAMIC_ST_ACTIVE) | ||
| return (NULL); | ||
|
|
||
| if (start == NULL) | ||
| start = VTAILQ_FIRST(&dom->refs); | ||
|
|
||
| h = 0; | ||
| cc = dom->changed_cached; | ||
| next = start; | ||
| alt = NULL; | ||
|
|
||
| //lint -e{506} Constant value boolean | ||
| do { | ||
| CHECK_OBJ_ORNULL(next, DYNAMIC_REF_MAGIC); | ||
| if (next != NULL) | ||
| next = VTAILQ_NEXT(next, list); | ||
| if (next == NULL) | ||
| next = VTAILQ_FIRST(&dom->refs); | ||
| if (next == NULL) | ||
| break; | ||
| if (next->dir != creating && next->dir != NULL) { | ||
| h = VRT_Healthy(ctx, next->dir, &c); | ||
| if (c > cc) | ||
| cc = c; | ||
| if (h) | ||
| break; | ||
| } | ||
| /* if we do not find a healthy backend, use one with a director | ||
| * or, alternatively, whatever we can get | ||
| */ | ||
| if (alt == NULL || | ||
| (alt->dir == creating && next->dir != creating)) | ||
| alt = next; | ||
| if (next != start) | ||
| continue; | ||
|
|
||
| // we have iterated the list once | ||
|
|
||
| if (alt->dir != creating) { | ||
| next = alt; | ||
| break; | ||
| } | ||
| if (wait == 0) | ||
| break; | ||
|
|
||
| assert(alt->dir == creating); | ||
| if (VTAILQ_EMPTY(&dom->refs)) | ||
| return (NULL); | ||
|
|
||
| r = dom->current == NULL ? VTAILQ_FIRST(&dom->refs) : VTAILQ_NEXT(dom->current, list); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| r_alt = NULL; | ||
|
|
||
| cc = dom->changed_cached; | ||
| h = 0; | ||
|
|
||
| // 1st pass: find a healthy backend | ||
| VTAILQ_FOREACH_FROM(r, &dom->refs, list) { | ||
| if (r->dir != NULL && r->dir != creating) { | ||
| h = VRT_Healthy(ctx, r->dir, &c); | ||
| if(h) { | ||
| dom->current = r; | ||
|
|
||
| if (c > cc) | ||
| cc = c; | ||
|
|
||
| LOG(ctx, SLT_Error, dom, "Selecting healthy backend %s", dom->current->dir->vcl_name); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 2nd pass: find a healthy backend AND a preferred one | ||
| if (r == NULL) { | ||
| goto done; | ||
| } | ||
|
|
||
| VTAILQ_FOREACH_FROM(r_alt, &dom->refs, list) { | ||
| if (r_alt == r) { | ||
| continue; | ||
| } | ||
|
|
||
| if (r_alt->dir != NULL && r_alt->dir != creating) { | ||
| h = VRT_Healthy(ctx, r_alt->dir, NULL); | ||
| if(h && r_alt->preferred > r->preferred) { | ||
| dom->current = r_alt; | ||
| LOG(ctx, SLT_Error, dom, "Selecting preferred backend %s", dom->current->dir->vcl_name); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| done: | ||
| if (wait && dom->current->dir == creating) { | ||
| AZ(Lck_CondWait(&dom->resolve, &dom->mtx)); | ||
| } while (1); | ||
| } | ||
|
|
||
| dom->healthy_cached = h; | ||
| dom->changed_cached = cc; | ||
|
|
@@ -323,14 +325,14 @@ dom_find(VRT_CTX, struct dynamic_domain *dom, struct dynamic_ref *start, | |
| if (changed) | ||
| *changed = cc; | ||
|
|
||
| return (next); | ||
| return dom->current; | ||
| } | ||
|
|
||
| static VCL_BACKEND v_matchproto_(vdi_resolve_f) | ||
| dom_resolve(VRT_CTX, VCL_BACKEND d) | ||
| { | ||
| struct dynamic_domain *dom; | ||
| struct dynamic_ref *r; | ||
| struct dynamic_ref *r = NULL; | ||
| VCL_BACKEND n = NULL; | ||
|
|
||
| CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); | ||
|
|
@@ -348,8 +350,7 @@ dom_resolve(VRT_CTX, VCL_BACKEND d) | |
| dynamic_gc_expired(dom->obj); | ||
|
|
||
| Lck_Lock(&dom->mtx); | ||
| r = dom_find(ctx, dom, dom->current, NULL, NULL, 1); | ||
| dom->current = r; | ||
| r = dom_find_v2(ctx, dom, NULL, NULL, 1); | ||
| if (r != NULL) | ||
| VRT_Assign_Backend(&n, r->dir); | ||
| Lck_Unlock(&dom->mtx); | ||
|
|
@@ -388,7 +389,7 @@ dom_healthy(VRT_CTX, VCL_BACKEND d, VCL_TIME *changed) | |
| return (dom->healthy_cached); | ||
| } | ||
|
|
||
| (void) dom_find(ctx, dom, NULL, &retval, changed, IS_CLI() ? 0 : 1); | ||
| (void) dom_find_v2(ctx, dom, &retval, changed, IS_CLI() ? 0 : 1); | ||
| Lck_Unlock(&dom->mtx); | ||
|
|
||
| return (retval); | ||
|
|
@@ -443,7 +444,9 @@ dom_list(VRT_CTX, VCL_BACKEND dir, struct vsb *vsb, int pflag, int jflag) | |
| VSB_indent(vsb, 2); | ||
| VSB_printf(vsb, "\"health\": \"%s\"\n", | ||
| h ? "healthy" : "sick"); | ||
| VSB_indent(vsb, -2); | ||
| VSB_printf(vsb, "\"preferred\": %d\n", | ||
| r->preferred); | ||
| VSB_indent(vsb, -2); | ||
| VSB_cat(vsb, "}"); | ||
| } | ||
| else if (pflag) { | ||
|
|
@@ -775,6 +778,9 @@ dom_update(struct dynamic_domain *dom, const struct res_cb *res, | |
| r->sa = VSA_Clone(info->sa); | ||
| AZ(r->dir); | ||
| r->dir = creating; | ||
| if (r->dom->obj->prefer != NULL) { | ||
| r->preferred = VRT_acl_match(ctx, r->dom->obj->prefer, info->sa); | ||
| } | ||
| VTAILQ_INSERT_TAIL(&dom->refs, r, list); | ||
| } | ||
|
|
||
|
|
@@ -1318,6 +1324,7 @@ vmod_director__init(VRT_CTX, | |
| VCL_ENUM share_arg, | ||
| VCL_PROBE probe, | ||
| VCL_ACL whitelist, | ||
| VCL_ACL prefer, | ||
| VCL_DURATION ttl, | ||
| VCL_DURATION connect_timeout, | ||
| VCL_DURATION first_byte_timeout, | ||
|
|
@@ -1396,6 +1403,7 @@ vmod_director__init(VRT_CTX, | |
| obj->share = dynamic_share_parse(share_arg); | ||
| obj->probe = probe; | ||
| obj->whitelist = whitelist; | ||
| obj->prefer = prefer; | ||
| obj->ttl = ttl; | ||
| obj->retry_after = retry_after; | ||
| obj->connect_tmo = connect_timeout; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,7 @@ struct dynamic_ref { | |
| unsigned magic; | ||
| #define DYNAMIC_REF_MAGIC 0x79a19d81 | ||
| unsigned keep; | ||
| unsigned preferred; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be extended to something like |
||
| VTAILQ_ENTRY(dynamic_ref) list; | ||
| struct dynamic_domain *dom; | ||
| VCL_BACKEND dir; | ||
|
|
@@ -174,6 +175,7 @@ struct vmod_dynamic_director { | |
| enum dynamic_share_e share; | ||
| VCL_PROBE probe; | ||
| VCL_ACL whitelist; | ||
| VCL_ACL prefer; | ||
| VCL_DURATION ttl; | ||
| VCL_DURATION retry_after; | ||
| VCL_DURATION connect_tmo; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I tried to keep the function as is but did not find a good way to easily integrate backend selection flow for
preferredwith the existing implementation. If there is a better way, I am open to refactor!