only one rw node allow

This commit is contained in:
tsthght 2018-09-05 21:23:51 +08:00
parent 8e8f10009a
commit 7bb654a530
4 changed files with 85 additions and 10 deletions

View File

@ -953,11 +953,32 @@ static backend_state_t backend_state(const char* str)
void admin_insert_backend(network_mysqld_con* con, char *addr, char *type, char *state)
{
chassis_private *g = con->srv->priv;
int affected = network_backends_add(g->backends, addr,
int ret = network_backends_add(g->backends, addr,
backend_type(type),
backend_state(state), con->srv);
network_mysqld_con_send_ok_full(con->client, affected==0?1:0, 0,
SERVER_STATUS_AUTOCOMMIT, 0);
switch (ret) {
case BACKEND_OPERATE_SUCCESS:
{
network_mysqld_con_send_ok_full(con->client, 1, 0,
SERVER_STATUS_AUTOCOMMIT, 0);
break;
}
case BACKEND_OPERATE_NETERR:
{
network_mysqld_con_send_error(con->client, C("get network address failed"));
break;
}
case BACKEND_OPERATE_DUPLICATE:
{
network_mysqld_con_send_error(con->client, C("backend is already known"));
break;
}
case BACKEND_OPERATE_2MASTER:
{
network_mysqld_con_send_error(con->client, C("rw node is already existsonly one rw node is allowed"));
break;
}
}
}
void admin_update_backend(network_mysqld_con* con, GList* equations,
@ -997,6 +1018,17 @@ void admin_update_backend(network_mysqld_con* con, GList* equations,
network_mysqld_con_send_error(con->client, C("no such backend"));
return;
}
if (type_str && backend_type(type_str) == BACKEND_TYPE_RW && network_backend_check_available_rw(g->backends, bk->server_group)) {
if (backend_type(type_str) == bk->type) {
network_mysqld_con_send_ok_full(con->client, 0, 0,
SERVER_STATUS_AUTOCOMMIT, 0);
} else {
network_mysqld_con_send_error(con->client, C("rw node is already existsonly one rw node is allowed"));
}
return;
}
int type = type_str ? backend_type(type_str) : bk->type;
int state = state_str ? backend_state(state_str) : bk->state;
if (type == ERROR_PARAM || state == ERROR_PARAM) {

View File

@ -215,7 +215,7 @@ network_backends_add(network_backends_t *bs, const gchar *address,
if (0 != network_address_set_address(new_backend->addr, new_backend->address->str)) {
network_backend_free(new_backend);
return -1;
return BACKEND_OPERATE_NETERR;
}
guint i;
@ -227,10 +227,14 @@ network_backends_add(network_backends_t *bs, const gchar *address,
network_backend_free(new_backend);
g_critical("backend %s is already known!", address);
return -1;
return BACKEND_OPERATE_DUPLICATE;
}
}
if (type == BACKEND_TYPE_RW && network_backend_check_available_rw(bs, new_backend->server_group)) {
return BACKEND_OPERATE_2MASTER;
}
g_ptr_array_add(bs->backends, new_backend);
if (type == BACKEND_TYPE_RO) {
bs->ro_server_num += 1;
@ -241,7 +245,7 @@ network_backends_add(network_backends_t *bs, const gchar *address,
network_backends_into_group(bs, new_backend);
g_message("added %s backend: %s, state: %s", backend_type_t_str[type], address, backend_state_t_str[state]);
return 0;
return BACKEND_OPERATE_SUCCESS;
}
/**
@ -643,3 +647,31 @@ network_backends_used_conns(network_backends_t *bs)
}
return sum;
}
int
network_backend_check_available_rw(network_backends_t *bs, GString *name)
{
if (!name || name->len == 0) {
int i = 0;
int count = network_backends_count(bs);
for (i = 0; i < count; i++) {
network_backend_t *backend = network_backends_get(bs, i);
if ((BACKEND_TYPE_RW == backend->type) &&
backend->state != BACKEND_STATE_MAINTAINING && backend->state != BACKEND_STATE_DELETED) {
break;
}
}
return i < count ? 1 : 0;
} else {
network_group_t *group = network_backends_get_group(bs, name);
if (group == NULL) {
return 0;
}
network_backend_t *rw = group->master;
if (rw && rw->state != BACKEND_STATE_MAINTAINING && rw->state != BACKEND_STATE_DELETED) {
return 1;
} else {
return 0;
}
}
}

View File

@ -48,6 +48,13 @@ typedef enum {
BACKEND_TYPE_RO,
} backend_type_t;
typedef enum {
BACKEND_OPERATE_SUCCESS,
BACKEND_OPERATE_NETERR,
BACKEND_OPERATE_DUPLICATE,
BACKEND_OPERATE_2MASTER
} backend_operate_t;
typedef struct backend_config {
GString *default_username;
GString *default_db;
@ -144,4 +151,6 @@ int network_backends_get_rw_ndx(network_backends_t *);
int network_backends_idle_conns(network_backends_t *);
int network_backends_used_conns(network_backends_t *);
int network_backend_check_available_rw(network_backends_t *, GString *);
#endif /* _BACKEND_H_ */

View File

@ -446,16 +446,18 @@ plugin_add_backends(chassis *chas, gchar **backend_addresses, gchar **read_only_
GPtrArray *backends_arr = g->backends->backends;
for (i = 0; backend_addresses[i]; i++) {
if (-1 == network_backends_add(g->backends, backend_addresses[i], BACKEND_TYPE_RW, BACKEND_STATE_UNKNOWN, chas)) {
return -1;
if (BACKEND_OPERATE_SUCCESS != network_backends_add(g->backends, backend_addresses[i], BACKEND_TYPE_RW, BACKEND_STATE_UNKNOWN, chas)) {
g_critical("add rw node: %s failed.", backend_addresses[i]);
continue;
}
network_backend_init_extra(backends_arr->pdata[backends_arr->len - 1], chas);
}
for (i = 0; read_only_backend_addresses && read_only_backend_addresses[i]; i++) {
if (-1 == network_backends_add(g->backends,
if (BACKEND_OPERATE_SUCCESS != network_backends_add(g->backends,
read_only_backend_addresses[i], BACKEND_TYPE_RO, BACKEND_STATE_UNKNOWN, chas)) {
return -1;
g_critical("add ro node: %s failed.", read_only_backend_addresses[i]);
continue;
}
/* set conn-pool config */
network_backend_init_extra(backends_arr->pdata[backends_arr->len - 1], chas);