Skip to content

Commit cdb97cb

Browse files
committed
MEDIUM: server: split srv_init() in srv_preinit() + srv_postinit()
We actually need more granularity to split srv postparsing init tasks: Some of them are required to be run BEFORE the config is checked, and some of them AFTER the config is checked. Thus we push the logic from 368d013 ("MEDIUM: server: add and use srv_init() function") a little bit further and split the function in two distinct ones, one of them executed under check_config_validity() and the other one using REGISTER_POST_SERVER_CHECK() hook. SRV_F_CHECKED flag was removed because it is no longer needed, srv_preinit() is only called once, and so is srv_postinit().
1 parent 9736221 commit cdb97cb

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

include/haproxy/server-t.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ enum srv_init_state {
171171
#define SRV_F_DEFSRV_USE_SSL 0x4000 /* default-server uses SSL */
172172
#define SRV_F_DELETED 0x8000 /* srv is deleted but not yet purged */
173173
#define SRV_F_STRICT_MAXCONN 0x10000 /* maxconn is to be strictly enforced, as a limit of outbound connections */
174-
#define SRV_F_CHECKED 0x20000 /* set once server was postparsed */
174+
/* unused: 0x20000 */
175175

176176
/* configured server options for send-proxy (server->pp_opts) */
177177
#define SRV_PP_V1 0x0001 /* proxy protocol version 1 */

include/haproxy/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct server *new_server(struct proxy *proxy);
7474
void srv_take(struct server *srv);
7575
struct server *srv_drop(struct server *srv);
7676
void srv_free_params(struct server *srv);
77-
int srv_init(struct server *srv);
77+
int srv_preinit(struct server *srv);
7878
void srv_set_ssl(struct server *s, int use_ssl);
7979
const char *srv_adm_st_chg_cause(enum srv_adm_st_chg_cause cause);
8080
const char *srv_op_st_chg_cause(enum srv_op_st_chg_cause cause);

src/cfgparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2824,7 +2824,7 @@ int check_config_validity()
28242824
* as some of the fields may be accessed soon
28252825
*/
28262826
MT_LIST_FOR_EACH_ENTRY_LOCKED(newsrv, &servers_list, global_list, back) {
2827-
err_code |= srv_init(newsrv);
2827+
err_code |= srv_preinit(newsrv);
28282828
if (err_code & ERR_CODE)
28292829
goto out;
28302830
}

src/server.c

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,7 +3363,7 @@ static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
33633363
* This function is expected to be called after _srv_parse_init() initialization
33643364
* but only when the effective server's proxy mode is known, which is not always
33653365
* the case during parsing time, in which case the function will be called during
3366-
* postparsing thanks to the srv_init() below.
3366+
* postparsing thanks to the srv_postinit() below.
33673367
*
33683368
* Returns ERR_NONE on success else a combination or ERR_CODE.
33693369
*/
@@ -3427,7 +3427,27 @@ static int _srv_check_proxy_mode(struct server *srv, char postparse)
34273427

34283428
return err_code;
34293429
}
3430-
/* Finish initializing the server after parsing
3430+
3431+
/* Finish initializing the server after parsing and before config checks
3432+
*
3433+
* Returns ERR_NONE on success else a combination or ERR_CODE.
3434+
*/
3435+
static int srv_init_per_thr(struct server *srv);
3436+
int srv_preinit(struct server *srv)
3437+
{
3438+
int err_code = ERR_NONE;
3439+
3440+
if (srv_init_per_thr(srv) == -1) {
3441+
ha_alert("error during per-thread init for %s/%s server\n", srv->proxy->id, srv->id);
3442+
err_code |= ERR_ALERT | ERR_FATAL;
3443+
goto out;
3444+
}
3445+
3446+
out:
3447+
return err_code;
3448+
}
3449+
3450+
/* Finish initializing the server after parsing and config checks
34313451
*
34323452
* We must be careful that checks / postinits performed within this function
34333453
* don't depend or conflict with other postcheck functions that are registered
@@ -3437,14 +3457,10 @@ static int _srv_check_proxy_mode(struct server *srv, char postparse)
34373457
*/
34383458
static int init_srv_requeue(struct server *srv);
34393459
static int init_srv_slowstart(struct server *srv);
3440-
static int srv_init_per_thr(struct server *srv);
3441-
int srv_init(struct server *srv)
3460+
int srv_postinit(struct server *srv)
34423461
{
34433462
int err_code = ERR_NONE;
34443463

3445-
if (srv->flags & SRV_F_CHECKED)
3446-
return ERR_NONE; // nothing to do
3447-
34483464
err_code |= _srv_check_proxy_mode(srv, 1);
34493465

34503466
if (err_code & ERR_CODE)
@@ -3474,12 +3490,6 @@ int srv_init(struct server *srv)
34743490
if (err_code & ERR_CODE)
34753491
goto out;
34763492

3477-
if (srv_init_per_thr(srv) == -1) {
3478-
ha_alert("error during per-thread init for %s/%s server\n", srv->proxy->id, srv->id);
3479-
err_code |= ERR_ALERT | ERR_FATAL;
3480-
goto out;
3481-
}
3482-
34833493
/* initialize idle conns lists */
34843494
if (srv->max_idle_conns != 0) {
34853495
srv->curr_idle_thr = ha_aligned_zalloc(64, global.nbthread * sizeof(*srv->curr_idle_thr));
@@ -3492,11 +3502,9 @@ int srv_init(struct server *srv)
34923502
}
34933503

34943504
out:
3495-
if (!(err_code & ERR_CODE))
3496-
srv->flags |= SRV_F_CHECKED;
34973505
return err_code;
34983506
}
3499-
REGISTER_POST_SERVER_CHECK(srv_init);
3507+
REGISTER_POST_SERVER_CHECK(srv_postinit);
35003508

35013509
/* Allocate a new server pointed by <srv> and try to parse the first arguments
35023510
* in <args> as an address for a server or an address-range for a template or
@@ -6181,7 +6189,10 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
61816189
srv->agent.state &= ~CHK_ST_ENABLED;
61826190
}
61836191

6184-
errcode = srv_init(srv);
6192+
errcode = srv_preinit(srv);
6193+
if (errcode)
6194+
goto out;
6195+
errcode = srv_postinit(srv);
61856196
if (errcode)
61866197
goto out;
61876198

0 commit comments

Comments
 (0)