From: Simon Horman Date: Tue, 12 Feb 2013 01:45:51 +0000 (+0900) Subject: MEDIUM: server: Break out set weight processing code X-Git-Tag: v1.5-dev18~96 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d09b9a4df37ef06153df6869a89f11cbd75bc8c;p=thirdparty%2Fhaproxy.git MEDIUM: server: Break out set weight processing code Break out set weight processing code. This is in preparation for reusing the code. Also, remove duplicate check in nested if clauses. {px->lbprm.algo & BE_LB_PROP_DYN) is checked by the immediate outer if clause, so there is no need to check it a second time. Signed-off-by: Simon Horman --- diff --git a/include/proto/server.h b/include/proto/server.h index 408427b2bc..ded640af46 100644 --- a/include/proto/server.h +++ b/include/proto/server.h @@ -58,6 +58,13 @@ struct srv_kw *srv_find_kw(const char *kw); /* Dumps all registered "server" keywords to the string pointer. */ void srv_dump_kws(char **out); +/* + * Parses weight_str and configures sv accordingly. + * Returns NULL on success, error message string otherwise. + */ +const char *server_parse_weight_change_request(struct server *sv, + const char *weight_str); + /* * Local variables: * c-indent-level: 8 diff --git a/src/dumpstats.c b/src/dumpstats.c index 5954e8c2a9..0c3c2aeeb9 100644 --- a/src/dumpstats.c +++ b/src/dumpstats.c @@ -1089,72 +1089,18 @@ static int stats_sock_parse_request(struct stream_interface *si, char *line) } else if (strcmp(args[0], "set") == 0) { if (strcmp(args[1], "weight") == 0) { - struct proxy *px; struct server *sv; - int w; + const char *warning; sv = expect_server_admin(s, si, args[2]); if (!sv) return 1; - px = sv->proxy; - - /* if the weight is terminated with '%', it is set relative to - * the initial weight, otherwise it is absolute. - */ - if (!*args[3]) { - si->applet.ctx.cli.msg = "Require or .\n"; - si->applet.st0 = STAT_CLI_PRINT; - return 1; - } - - w = atoi(args[3]); - if (strchr(args[3], '%') != NULL) { - if (w < 0 || w > 100) { - si->applet.ctx.cli.msg = "Relative weight can only be set between 0 and 100% inclusive.\n"; - si->applet.st0 = STAT_CLI_PRINT; - return 1; - } - w = sv->iweight * w / 100; - } - else { - if (w < 0 || w > 256) { - si->applet.ctx.cli.msg = "Absolute weight can only be between 0 and 256 inclusive.\n"; - si->applet.st0 = STAT_CLI_PRINT; - return 1; - } - } - if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) { - si->applet.ctx.cli.msg = "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; + warning = server_parse_weight_change_request(sv, args[3]); + if (warning) { + si->applet.ctx.cli.msg = warning; si->applet.st0 = STAT_CLI_PRINT; - return 1; } - - sv->uweight = w; - - if (px->lbprm.algo & BE_LB_PROP_DYN) { - /* we must take care of not pushing the server to full throttle during slow starts */ - if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) - sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; - else - sv->eweight = BE_WEIGHT_SCALE; - sv->eweight *= sv->uweight; - } else { - sv->eweight = sv->uweight; - } - - /* static LB algorithms are a bit harder to update */ - if (px->lbprm.update_server_eweight) - px->lbprm.update_server_eweight(sv); - else if (sv->eweight) { - if (px->lbprm.set_server_status_up) - px->lbprm.set_server_status_up(sv); - } - else { - if (px->lbprm.set_server_status_down) - px->lbprm.set_server_status_down(sv); - } - return 1; } else if (strcmp(args[1], "timeout") == 0) { diff --git a/src/server.c b/src/server.c index 568d042206..7a2774c47a 100644 --- a/src/server.c +++ b/src/server.c @@ -156,6 +156,64 @@ static void __listener_init(void) srv_register_keywords(&srv_kws); } +/* + * Parses weight_str and configures sv accordingly. + * Returns NULL on success, error message string otherwise. + */ +const char *server_parse_weight_change_request(struct server *sv, + const char *weight_str) +{ + struct proxy *px; + int w; + + px = sv->proxy; + + /* if the weight is terminated with '%', it is set relative to + * the initial weight, otherwise it is absolute. + */ + if (!*weight_str) + return "Require or .\n"; + + w = atoi(weight_str); + if (strchr(weight_str, '%') != NULL) { + if (w < 0 || w > 100) + return "Relative weight must be positive.\n"; + w = sv->iweight * w / 100; + } + else if (w < 0 || w > 256) + return "Absolute weight can only be between 0 and 256 inclusive.\n"; + + if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN)) + return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n"; + + sv->uweight = w; + + if (px->lbprm.algo & BE_LB_PROP_DYN) { + /* we must take care of not pushing the server to full throttle during slow starts */ + if ((sv->state & SRV_WARMINGUP)) + sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; + else + sv->eweight = BE_WEIGHT_SCALE; + sv->eweight *= sv->uweight; + } else { + sv->eweight = sv->uweight; + } + + /* static LB algorithms are a bit harder to update */ + if (px->lbprm.update_server_eweight) + px->lbprm.update_server_eweight(sv); + else if (sv->eweight) { + if (px->lbprm.set_server_status_up) + px->lbprm.set_server_status_up(sv); + } + else { + if (px->lbprm.set_server_status_down) + px->lbprm.set_server_status_down(sv); + } + + return NULL; +} + /* * Local variables: * c-indent-level: 8