MEDIUM: proxy: replace proxy->state with proxy->disabled

The remaining proxy states were only used to distinguish an enabled
proxy from a disabled one. Due to the initialization order, both
PR_STNEW and PR_STREADY were equivalent after startup, and they
would only differ from PR_STSTOPPED when the proxy is disabled or
shutdown (which is effectively another way to disable it).

Now we just have a "disabled" field which allows to distinguish them.
It's becoming obvious that start_proxies() is only used to print a
greeting message now, that we'd rather get rid of. Probably that
zombify_proxy() and stop_proxy() should be merged once their
differences move to the right place.
This commit is contained in:
Willy Tarreau 2020-09-24 08:39:22 +02:00
parent 1ad64acf6c
commit c3914d4fff
18 changed files with 46 additions and 58 deletions

View File

@ -1546,12 +1546,12 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
px = appctx->ctx.stats.obj1;
/* skip the disabled proxies, global frontend and non-networked ones */
if (px->state == PR_STSTOPPED || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
goto next_px;
switch (appctx->st2) {
case ST_F_STATUS:
metric = mkf_u32(FO_STATUS, px->state == PR_STREADY ? 1 : 0);
metric = mkf_u32(FO_STATUS, !px->disabled);
break;
case ST_F_SCUR:
metric = mkf_u32(0, px->feconn);
@ -1731,7 +1731,7 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
px = appctx->ctx.stats.obj1;
/* skip the disabled proxies, global frontend and non-networked ones */
if (px->state == PR_STSTOPPED || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
goto next_px;
switch (appctx->st2) {
@ -1976,7 +1976,7 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
px = appctx->ctx.stats.obj1;
/* skip the disabled proxies, global frontend and non-networked ones */
if (px->state == PR_STSTOPPED || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
goto next_px;
while (appctx->ctx.stats.obj2) {

View File

@ -52,7 +52,7 @@ int be_lastsession(const struct proxy *be);
/* Returns number of usable servers in backend */
static inline int be_usable_srv(struct proxy *be)
{
if (be->state == PR_STSTOPPED)
if (be->disabled)
return 0;
else if (be->srv_act)
return be->srv_act;

View File

@ -42,13 +42,6 @@
#include <haproxy/thread-t.h>
#include <haproxy/uri_auth-t.h>
/* values for proxy->state */
enum pr_state {
PR_STNEW = 0, /* proxy has not been initialized yet */
PR_STREADY, /* proxy has been initialized and is ready */
PR_STSTOPPED, /* proxy is stopped (end of a restart) */
} __attribute__((packed));
/* values for proxy->mode */
enum pr_mode {
PR_MODE_TCP = 0,
@ -252,7 +245,7 @@ struct error_snapshot {
struct proxy {
enum obj_type obj_type; /* object type == OBJ_TYPE_PROXY */
enum pr_state state; /* proxy state, one of PR_* */
char disabled; /* non-zero if disabled or shutdown */
enum pr_mode mode; /* mode = PR_MODE_TCP, PR_MODE_HTTP or PR_MODE_HEALTH */
char cap; /* supported capabilities (PR_CAP_*) */
unsigned int maxconn; /* max # of active streams on the frontend */

View File

@ -245,7 +245,7 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
memcpy(&curproxy->defsrv, &defproxy.defsrv, sizeof(curproxy->defsrv));
curproxy->defsrv.id = "default-server";
curproxy->state = defproxy.state;
curproxy->disabled = defproxy.disabled;
curproxy->options = defproxy.options;
curproxy->options2 = defproxy.options2;
curproxy->no_options = defproxy.no_options;
@ -783,12 +783,12 @@ int cfg_parse_listen(const char *file, int linenum, char **args, int kwm)
else if (!strcmp(args[0], "disabled")) { /* disables this proxy */
if (alertif_too_many_args(0, file, linenum, args, &err_code))
goto out;
curproxy->state = PR_STSTOPPED;
curproxy->disabled = 1;
}
else if (!strcmp(args[0], "enabled")) { /* enables this proxy (used to revert a disabled default) */
if (alertif_too_many_args(0, file, linenum, args, &err_code))
goto out;
curproxy->state = PR_STNEW;
curproxy->disabled = 0;
}
else if (!strcmp(args[0], "bind-process")) { /* enable this proxy only on some processes */
int cur_arg = 1;

View File

@ -414,7 +414,7 @@ void init_default_instance()
{
init_new_proxy(&defproxy);
defproxy.mode = PR_MODE_TCP;
defproxy.state = PR_STNEW;
defproxy.disabled = 0;
defproxy.maxconn = cfg_maxpconn;
defproxy.conn_retries = CONN_RETRIES;
defproxy.redispatch_after = 0;
@ -2117,7 +2117,7 @@ void propagate_processes(struct proxy *from, struct proxy *to)
if (!(from->cap & PR_CAP_FE))
return;
if (from->state == PR_STSTOPPED)
if (from->disabled)
return;
/* default_backend */
@ -2234,7 +2234,7 @@ int check_config_validity()
next_pxid++;
if (curproxy->state == PR_STSTOPPED) {
if (curproxy->disabled) {
/* ensure we don't keep listeners uselessly bound. We
* can't disable their listeners yet (fdtab not
* allocated yet) but let's skip them.
@ -3949,7 +3949,7 @@ out_uri_auth_compat:
* other proxies.
*/
for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
if (curproxy->state == PR_STSTOPPED || !curproxy->table)
if (curproxy->disabled || !curproxy->table)
continue;
if (!stktable_init(curproxy->table)) {

View File

@ -859,7 +859,7 @@ static struct task *process_chk_conn(struct task *t, void *context, unsigned sho
* is disabled.
*/
if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
proxy->state == PR_STSTOPPED)
proxy->disabled)
goto reschedule;
/* we'll initiate a new check */

View File

@ -2428,7 +2428,6 @@ int mworker_cli_proxy_create()
proxies_list = mworker_proxy;
mworker_proxy->id = strdup("MASTER");
mworker_proxy->mode = PR_MODE_CLI;
mworker_proxy->state = PR_STNEW;
mworker_proxy->last_change = now.tv_sec;
mworker_proxy->cap = PR_CAP_LISTEN; /* this is a listen section */
mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */

View File

@ -495,7 +495,7 @@ struct task *process_chk_proc(struct task *t, void *context, unsigned short stat
* is disabled.
*/
if (((check->state & (CHK_ST_ENABLED | CHK_ST_PAUSED)) != CHK_ST_ENABLED) ||
s->proxy->state == PR_STSTOPPED)
s->proxy->disabled)
goto reschedule;
/* we'll initiate a new check */

View File

@ -2010,7 +2010,7 @@ static void init(int argc, char **argv)
break;
for (px = proxies_list; px; px = px->next)
if (px->state != PR_STSTOPPED && px->li_all)
if (!px->disabled && px->li_all)
break;
if (pr || px) {
@ -2340,7 +2340,7 @@ static void init(int argc, char **argv)
/* stop disabled proxies */
for (px = proxies_list; px; px = px->next) {
if (px->state == PR_STSTOPPED)
if (px->disabled)
stop_proxy(px);
}
@ -3511,7 +3511,7 @@ int main(int argc, char **argv)
/* we might have to unbind some proxies from some processes */
px = proxies_list;
while (px != NULL) {
if (px->bind_proc && px->state != PR_STSTOPPED) {
if (px->bind_proc && !px->disabled) {
if (!(px->bind_proc & (1UL << proc))) {
if (global.tune.options & GTUNE_SOCKET_TRANSFER)
zombify_proxy(px);
@ -3525,7 +3525,7 @@ int main(int argc, char **argv)
/* we might have to unbind some log forward proxies from some processes */
px = cfg_log_forward;
while (px != NULL) {
if (px->bind_proc && px->state != PR_STSTOPPED) {
if (px->bind_proc && !px->disabled) {
if (!(px->bind_proc & (1UL << proc))) {
if (global.tune.options & GTUNE_SOCKET_TRANSFER)
zombify_proxy(px);

View File

@ -3001,7 +3001,7 @@ static int fcgi_process(struct fcgi_conn *fconn)
}
fcgi_send(fconn);
if (unlikely(fconn->proxy->state == PR_STSTOPPED)) {
if (unlikely(fconn->proxy->disabled)) {
/* frontend is stopping, reload likely in progress, let's try
* to announce a graceful shutdown if not yet done. We don't
* care if it fails, it will be tried again later.

View File

@ -888,7 +888,7 @@ static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m)
}
/* If KAL, check if the frontend is stopping. If yes, switch in CLO mode */
if (h1s->flags & H1S_F_WANT_KAL && fe->state == PR_STSTOPPED) {
if (h1s->flags & H1S_F_WANT_KAL && fe->disabled) {
h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
}
@ -952,7 +952,7 @@ static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m)
}
/* If KAL, check if the backend is stopping. If yes, switch in CLO mode */
if (h1s->flags & H1S_F_WANT_KAL && be->state == PR_STSTOPPED) {
if (h1s->flags & H1S_F_WANT_KAL && be->disabled) {
h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO;
TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s);
}

View File

@ -3609,7 +3609,7 @@ static int h2_process(struct h2c *h2c)
}
h2_send(h2c);
if (unlikely(h2c->proxy->state == PR_STSTOPPED)) {
if (unlikely(h2c->proxy->disabled)) {
/* frontend is stopping, reload likely in progress, let's try
* to announce a graceful shutdown if not yet done. We don't
* care if it fails, it will be tried again later.

View File

@ -437,7 +437,7 @@ void mworker_cleanlisteners()
}
/* if the proxy shouldn't be in the master, we stop it */
if (!listen_in_master)
curproxy->state = PR_STSTOPPED;
curproxy->disabled = 1;
}
}

View File

@ -1052,10 +1052,8 @@ void start_proxies(void)
struct proxy *curproxy;
for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
if (curproxy->state != PR_STNEW)
continue; /* already initialized */
curproxy->state = PR_STREADY;
if (curproxy->disabled)
continue;
send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
}
}
@ -1078,7 +1076,7 @@ struct task *manage_proxy(struct task *t, void *context, unsigned short state)
*/
/* first, let's check if we need to stop the proxy */
if (unlikely(stopping && p->state != PR_STSTOPPED)) {
if (unlikely(stopping && !p->disabled)) {
int t;
t = tick_remain(now_ms, p->stop_time);
if (t == 0) {
@ -1102,7 +1100,7 @@ struct task *manage_proxy(struct task *t, void *context, unsigned short state)
* be in neither list. Any entry being dumped will have ref_cnt > 0.
* However we protect tables that are being synced to peers.
*/
if (unlikely(stopping && p->state == PR_STSTOPPED && p->table && p->table->current)) {
if (unlikely(stopping && p->disabled && p->table && p->table->current)) {
if (!p->table->syncing) {
stktable_trash_oldest(p->table, p->table->current);
pool_gc(NULL);
@ -1230,7 +1228,7 @@ void soft_stop(void)
p = proxies_list;
tv_update_date(0,1); /* else, the old time before select will be used */
while (p) {
if (p->state != PR_STSTOPPED) {
if (!p->disabled) {
ha_warning("Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
send_log(p, LOG_WARNING, "Stopping %s %s in %d ms.\n", proxy_cap_str(p->cap), p->id, p->grace);
p->stop_time = tick_add(now_ms, p->grace);
@ -1257,7 +1255,7 @@ void soft_stop(void)
p = cfg_log_forward;
while (p) {
/* Zombie proxy, let's close the file descriptors */
if (p->state == PR_STSTOPPED &&
if (p->disabled &&
!LIST_ISEMPTY(&p->conf.listeners) &&
LIST_ELEM(p->conf.listeners.n,
struct listener *, by_fe)->state > LI_ASSIGNED) {
@ -1269,7 +1267,7 @@ void soft_stop(void)
}
}
if (p->state != PR_STSTOPPED) {
if (!p->disabled) {
stop_proxy(p);
}
p = p->next;
@ -1288,7 +1286,7 @@ int pause_proxy(struct proxy *p)
{
struct listener *l;
if (!(p->cap & PR_CAP_FE) || p->state == PR_STSTOPPED || !p->li_ready)
if (!(p->cap & PR_CAP_FE) || p->disabled || !p->li_ready)
return 1;
ha_warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
@ -1318,7 +1316,7 @@ void zombify_proxy(struct proxy *p)
if (l->state >= LI_ASSIGNED)
delete_listener(l);
}
p->state = PR_STSTOPPED;
p->disabled = 1;
}
/*
@ -1352,7 +1350,7 @@ void stop_proxy(struct proxy *p)
}
}
if (!nostop)
p->state = PR_STSTOPPED;
p->disabled = 1;
HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
}
@ -1367,7 +1365,7 @@ int resume_proxy(struct proxy *p)
struct listener *l;
int fail;
if (p->state == PR_STSTOPPED || !p->li_paused)
if (p->disabled || !p->li_paused)
return 1;
ha_warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
@ -1685,7 +1683,7 @@ void proxy_adjust_all_maxconn()
struct switching_rule *swrule1, *swrule2;
for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
if (curproxy->state == PR_STSTOPPED)
if (curproxy->disabled)
continue;
if (!(curproxy->cap & PR_CAP_FE))
@ -1729,7 +1727,7 @@ void proxy_adjust_all_maxconn()
* loop above because cross-references are not yet fully resolved.
*/
for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
if (curproxy->state == PR_STSTOPPED)
if (curproxy->disabled)
continue;
/* If <fullconn> is not set, let's set it to 10% of the sum of
@ -2187,7 +2185,7 @@ static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx
if (!px)
return 1;
if (px->state == PR_STSTOPPED)
if (px->disabled)
return cli_msg(appctx, LOG_NOTICE, "Frontend was already shut down.\n");
ha_warning("Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
@ -2215,7 +2213,7 @@ static int cli_parse_disable_frontend(char **args, char *payload, struct appctx
if (!px)
return 1;
if (px->state == PR_STSTOPPED)
if (px->disabled)
return cli_msg(appctx, LOG_NOTICE, "Frontend was previously shut down, cannot disable.\n");
if (!px->li_ready)
@ -2247,7 +2245,7 @@ static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *
if (!px)
return 1;
if (px->state == PR_STSTOPPED)
if (px->disabled)
return cli_err(appctx, "Frontend was previously shut down, cannot enable.\n");
if (px->li_ready == px->li_all)

View File

@ -4246,7 +4246,7 @@ struct server *cli_find_server(struct appctx *appctx, char *arg)
return NULL;
}
if (px->state == PR_STSTOPPED) {
if (px->disabled) {
cli_err(appctx, "Proxy is disabled.\n");
return NULL;
}

View File

@ -1629,7 +1629,7 @@ int stats_fill_fe_stats(struct proxy *px, struct field *stats, int len)
stats[ST_F_EREQ] = mkf_u64(FN_COUNTER, px->fe_counters.failed_req);
stats[ST_F_DCON] = mkf_u64(FN_COUNTER, px->fe_counters.denied_conn);
stats[ST_F_DSES] = mkf_u64(FN_COUNTER, px->fe_counters.denied_sess);
stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->state == PR_STREADY ? "OPEN" : "STOP");
stats[ST_F_STATUS] = mkf_str(FO_STATUS, px->disabled ? "STOP" : "OPEN");
stats[ST_F_PID] = mkf_u32(FO_KEY, relative_pid);
stats[ST_F_IID] = mkf_u32(FO_KEY|FS_SERVICE, px->uuid);
stats[ST_F_SID] = mkf_u32(FO_KEY|FS_SERVICE, 0);
@ -3044,9 +3044,7 @@ static int stats_dump_proxies(struct stream_interface *si,
px = appctx->ctx.stats.obj1;
/* skip the disabled proxies, global frontend and non-networked ones */
if (px->state != PR_STSTOPPED && px->uuid > 0
&& (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
if (!px->disabled && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE))) {
if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
return 0;
}
@ -3458,7 +3456,7 @@ static int stats_process_http_post(struct stream_interface *si)
total_servers++;
break;
case ST_ADM_ACTION_SHUTDOWN:
if (px->state != PR_STSTOPPED) {
if (!px->disabled) {
srv_shutdown_streams(sv, SF_ERR_KILLED);
altered_servers++;
total_servers++;

View File

@ -640,7 +640,7 @@ int stktable_init(struct stktable *t)
t->exp_task->process = process_table_expire;
t->exp_task->context = (void *)t;
}
if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
if (t->peers.p && t->peers.p->peers_fe && !t->peers.p->peers_fe->disabled) {
peers_register_table(t->peers.p, t);
}

View File

@ -702,7 +702,7 @@ static void stream_free(struct stream *s)
pool_free(pool_head_stream, s);
/* We may want to free the maximum amount of pools if the proxy is stopping */
if (fe && unlikely(fe->state == PR_STSTOPPED)) {
if (fe && unlikely(fe->disabled)) {
pool_flush(pool_head_buffer);
pool_flush(pool_head_http_txn);
pool_flush(pool_head_requri);