MEDIUM: server: add server-states version 2

Even if it is possibly too much work for the current usage, it makes
sure we don't break states file from v2.3 to v2.4; indeed, since v2.3,
we introduced two new fields, so we put them aside to guarantee we can
easily reload from a version 1.
The diff seems huge but there is no specific change apart from:
- introduce v2 where it is needed (parsing, update)
- move away from switch/case in update to be able to reuse code
- move srv lock to the whole function to make it easier

this patch confirm how painful it is to maintain this functionality.

Signed-off-by: William Dauchy <wdauchy@gmail.com>
This commit is contained in:
William Dauchy 2021-02-11 22:51:25 +01:00 committed by Christopher Faulet
parent 7cabc06da6
commit 63e6cba12a
2 changed files with 384 additions and 398 deletions

View File

@ -101,9 +101,9 @@ enum srv_initaddr {
} __attribute__((packed));
/* server-state-file version */
#define SRV_STATE_FILE_VERSION 1
#define SRV_STATE_FILE_VERSION 2
#define SRV_STATE_FILE_VERSION_MIN 1
#define SRV_STATE_FILE_VERSION_MAX 1
#define SRV_STATE_FILE_VERSION_MAX 2
#define SRV_STATE_FILE_FIELD_NAMES \
"be_id " \
"be_name " \
@ -129,7 +129,8 @@ enum srv_initaddr {
"srv_check_port"
#define SRV_STATE_FILE_MAX_FIELDS 22
#define SRV_STATE_FILE_NB_FIELDS_VERSION_1 22
#define SRV_STATE_FILE_NB_FIELDS_VERSION_1 20
#define SRV_STATE_FILE_NB_FIELDS_VERSION_2 22
#define SRV_STATE_LINE_MAXLEN 512
/* server flags -- 32 bits */

View File

@ -2632,11 +2632,10 @@ static void srv_update_state(struct server *srv, int version, char **params)
fqdn = NULL;
port_svc = port_check = 0;
msg = get_trash_chunk();
switch (version) {
case 1:
/*
* now we can proceed with server's state update:
* srv_addr: params[0]
HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
if (version >= 1) {
/* srv_addr: params[0]
* srv_op_state: params[1]
* srv_admin_state: params[2]
* srv_uweight: params[3]
@ -2652,8 +2651,6 @@ static void srv_update_state(struct server *srv, int version, char **params)
* srv_fqdn: params[13]
* srv_port: params[14]
* srvrecord: params[15]
* srv_use_ssl: params[16]
* srv_check_port: params[17]
*/
/* validating srv_op_state */
@ -2785,14 +2782,6 @@ static void srv_update_state(struct server *srv, int version, char **params)
port_svc_st = NULL;
}
}
port_check_st = params[17];
if (port_check_st) {
port_check = strl2uic(port_check_st, strlen(port_check_st));
if (port_check > USHRT_MAX) {
chunk_appendf(msg, ", invalid srv_port value '%s'", port_check_st);
port_check_st = NULL;
}
}
/* SRV record
* NOTE: in HAProxy, SRV records must start with an underscore '_'
@ -2801,15 +2790,10 @@ static void srv_update_state(struct server *srv, int version, char **params)
if (srvrecord && *srvrecord != '_')
srvrecord = NULL;
#ifdef USE_OPENSSL
use_ssl = strtol(params[16], &p, 10);
#endif
/* don't apply anything if one error has been detected */
if (msg->data)
goto out;
HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
/* recover operational state and apply it to this server
* and all servers tracking this one */
srv->check.health = srv_check_health;
@ -2968,7 +2952,6 @@ static void srv_update_state(struct server *srv, int version, char **params)
/* we can't apply previous state if SRV record has changed */
if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id);
HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
goto out;
}
@ -2977,7 +2960,6 @@ static void srv_update_state(struct server *srv, int version, char **params)
srv->srvrq = new_dns_srvrq(srv, srvrecord);
if (srv->srvrq == NULL) {
chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
goto out;
}
@ -2985,7 +2967,6 @@ static void srv_update_state(struct server *srv, int version, char **params)
res = srv_prepare_for_resolution(srv, fqdn);
if (res == -1) {
chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
goto out;
}
@ -3000,23 +2981,34 @@ static void srv_update_state(struct server *srv, int version, char **params)
if (port_svc_st)
srv->svc_port = port_svc;
if (!srv->check.port)
srv->check.port = port_check;
}
if (version >= 2) {
/* srv_use_ssl: params[16]
* srv_check_port: params[17]
*/
#ifdef USE_OPENSSL
use_ssl = strtol(params[16], &p, 10);
#endif
port_check_st = params[17];
if (port_check_st) {
port_check = strl2uic(port_check_st, strlen(port_check_st));
if (port_check > USHRT_MAX)
chunk_appendf(msg, ", invalid srv_port value '%s'", port_check_st);
}
#ifdef USE_OPENSSL
/* configure ssl if connection has been initiated at startup */
if (srv->ssl_ctx.ctx != NULL)
ssl_sock_set_srv(srv, use_ssl);
#endif
if (!srv->check.port)
srv->check.port = port_check;
HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
break;
default:
chunk_appendf(msg, ", version '%d' not supported", version);
}
out:
HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
if (msg->data) {
chunk_appendf(msg, "\n");
ha_warning("server-state application failed for server '%s/%s'%s",
@ -3098,9 +3090,8 @@ static void srv_state_parse_line(char *buf, const int version, char **params, ch
++cur;
while (isspace((unsigned char)*cur))
++cur;
switch (version) {
case 1:
/*
/* v1
* srv_addr: params[4] => srv_params[0]
* srv_op_state: params[5] => srv_params[1]
* srv_admin_state: params[6] => srv_params[2]
@ -3117,15 +3108,15 @@ static void srv_state_parse_line(char *buf, const int version, char **params, ch
* srv_fqdn: params[17] => srv_params[13]
* srv_port: params[18] => srv_params[14]
* srvrecord: params[19] => srv_params[15]
* v2
* srv_use_ssl: params[20] => srv_params[16]
* srv_check_port: params[21] => srv_params[17]
*/
if (arg >= 4) {
if ((version == 1 && arg >= 4 && arg <= 19) ||
(version == 2 && arg >= 4)) {
srv_params[srv_arg] = cur;
++srv_arg;
}
break;
}
params[arg] = cur;
++arg;
@ -3137,16 +3128,9 @@ static void srv_state_parse_line(char *buf, const int version, char **params, ch
/* if line is incomplete line, then ignore it.
* otherwise, update useful flags */
switch (version) {
case 1:
if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
if ((version == 1 && arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) ||
(version == 2 && arg < SRV_STATE_FILE_NB_FIELDS_VERSION_2))
params[0] = NULL;
return;
}
break;
}
return;
}
@ -3433,6 +3417,7 @@ void apply_server_state(void)
* otherwise, update useful flags */
switch (version) {
case 1:
case 2:
bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
check_id = (atoi(params[0]) == curproxy->uuid);
check_name = (strcmp(curproxy->id, params[1]) == 0);