From 1bea86581157f28ce8c92e6550e54a30dc223f09 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Fri, 10 Jul 2020 16:03:45 +0200 Subject: [PATCH] MINOR: backend: Add sample fetches to get the server's weight The following sample fetches have been added : * srv_iweight : returns the initial server's weight * srv_uweight : returns the user-visible server's weight * srv_weight : returns the current (or effetctive) server's weight The requested server must be passed as argument, evnetually preceded by the backend name. For instance : srv_weight(back-http/www1) --- doc/configuration.txt | 15 +++++++++++++++ src/backend.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/doc/configuration.txt b/doc/configuration.txt index d040af6ba..b7c3b7e8d 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -16160,6 +16160,21 @@ srv_sess_rate([/]) : integer acl srv2_full srv_sess_rate(be1/srv2) gt 50 use_backend be2 if srv1_full or srv2_full +srv_iweight([/]): integer + Returns an integer corresponding to the server's initial weight. If + is omitted, then the server is looked up in the current backend. See also + "srv_weight" and "srv_uweight". + +srv_uweight([/]): integer + Returns an integer corresponding to the user visible server's weight. If + is omitted, then the server is looked up in the current + backend. See also "srv_weight" and "srv_iweight". + +srv_weight([/]): integer + Returns an integer corresponding to the current (or effective) server's + weight. If is omitted, then the server is looked up in the current + backend. See also "srv_iweight" and "srv_uweight". + stopping : boolean Returns TRUE if the process calling the function is currently stopping. This can be useful for logging, or for relaxing certain checks or helping close diff --git a/src/backend.c b/src/backend.c index 372389aae..6ec45d265 100644 --- a/src/backend.c +++ b/src/backend.c @@ -2810,6 +2810,48 @@ smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char * return 1; } +/* set temp integer to the server weight. + * Accepts exactly 1 argument. Argument is a server, other types will lead to + * undefined behaviour. + */ +static int +smp_fetch_srv_weight(const struct arg *args, struct sample *smp, const char *kw, void *private) +{ + struct server *srv = args->data.srv; + struct proxy *px = srv->proxy; + + smp->flags = SMP_F_VOL_TEST; + smp->data.type = SMP_T_SINT; + smp->data.u.sint = (srv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv; + return 1; +} + +/* set temp integer to the server initial weight. + * Accepts exactly 1 argument. Argument is a server, other types will lead to + * undefined behaviour. + */ +static int +smp_fetch_srv_iweight(const struct arg *args, struct sample *smp, const char *kw, void *private) +{ + smp->flags = SMP_F_VOL_TEST; + smp->data.type = SMP_T_SINT; + smp->data.u.sint = args->data.srv->iweight; + return 1; +} + +/* set temp integer to the server user-specified weight. + * Accepts exactly 1 argument. Argument is a server, other types will lead to + * undefined behaviour. + */ +static int +smp_fetch_srv_uweight(const struct arg *args, struct sample *smp, const char *kw, void *private) +{ + smp->flags = SMP_F_VOL_TEST; + smp->data.type = SMP_T_SINT; + smp->data.u.sint = args->data.srv->uweight; + return 1; +} + static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private) { @@ -2881,6 +2923,9 @@ static struct sample_fetch_kw_list smp_kws = {ILH, { { "srv_name", smp_fetch_srv_name, 0, NULL, SMP_T_STR, SMP_USE_SERVR, }, { "srv_queue", smp_fetch_srv_queue, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, }, { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, }, + { "srv_weight", smp_fetch_srv_weight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, }, + { "srv_iweight", smp_fetch_srv_iweight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, }, + { "srv_uweight", smp_fetch_srv_uweight, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, }, { /* END */ }, }};