refactored tests (#147)

This commit is contained in:
Timofey Turenko
2017-12-08 12:23:04 +02:00
committed by GitHub
parent c6daf8c26b
commit 3c88bf1ec6
253 changed files with 5265 additions and 3649 deletions

View File

@ -17,9 +17,10 @@
#include <sstream>
#include <vector>
Mariadb_nodes::Mariadb_nodes(const char *pref, const char *test_cwd, bool verbose) :
v51(false), use_ipv6(false)
Mariadb_nodes::Mariadb_nodes(const char *pref, const char *test_cwd, bool verbose):
v51(false)
{
use_ipv6 = false;
strcpy(prefix, pref);
memset(this->nodes, 0, sizeof(this->nodes));
memset(blocked, 0, sizeof(blocked));
@ -79,21 +80,14 @@ void Mariadb_nodes::close_connections()
}
}
int Mariadb_nodes::read_env()
{
char * env;
char env_name[64];
sprintf(env_name, "%s_N", prefix);
env = getenv(env_name);
if (env != NULL)
{
sscanf(env, "%d", &N);
}
else
{
N = 0;
}
read_basic_env();
sprintf(env_name, "%s_user", prefix);
env = getenv(env_name);
@ -119,48 +113,15 @@ int Mariadb_nodes::read_env()
ssl = false;
sprintf(env_name, "%s_ssl", prefix);
env = getenv(env_name);
if ((env != NULL) && ((strcasecmp(env, "yes") == 0) || (strcasecmp(env, "true") == 0)))
if ((env != NULL) && ((strcasecmp(env, "yes") == 0) || (strcasecmp(env, "true") == 0) ))
{
ssl = true;
}
if ((N > 0) && (N < 255))
{
for (int i = 0; i < N; i++)
{
//reading IPs
sprintf(env_name, "%s_%03d_network", prefix, i);
env = getenv(env_name);
if (env != NULL)
{
sprintf(IP[i], "%s", env);
}
//reading private IPs
sprintf(env_name, "%s_%03d_private_ip", prefix, i);
env = getenv(env_name);
if (env != NULL)
{
sprintf(IP_private[i], "%s", env);
}
else
{
sprintf(IP_private[i], "%s", IP[i]);
}
//reading IPv6
sprintf(env_name, "%s_%03d_network6", prefix, i);
env = getenv(env_name);
if (env != NULL)
{
sprintf(IP6[i], "%s", env);
}
else
{
sprintf(IP6[i], "%s", IP[i]);
}
//reading ports
sprintf(env_name, "%s_%03d_port", prefix, i);
env = getenv(env_name);
@ -185,17 +146,7 @@ int Mariadb_nodes::read_env()
sprintf(socket[i], " ");
sprintf(socket_cmd[i], " ");
}
//reading sshkey
sprintf(env_name, "%s_%03d_keyfile", prefix, i);
env = getenv(env_name);
if (env != NULL)
{
sprintf(sshkey[i], "%s", env);
}
else
{
sprintf(sshkey[i], "vagrant.pem");
}
//reading start_db_command
sprintf(env_name, "%s_%03d_start_db_command", prefix, i);
@ -232,38 +183,6 @@ int Mariadb_nodes::read_env()
{
sprintf(cleanup_db_command[i], " ");
}
sprintf(env_name, "%s_%03d_whoami", prefix, i);
env = getenv(env_name);
if (env != NULL)
{
sprintf(access_user[i], "%s", env);
}
else
{
sprintf(access_user[i], "root");
}
sprintf(env_name, "%s_%03d_access_sudo", prefix, i);
env = getenv(env_name);
if (env != NULL)
{
sprintf(access_sudo[i], "%s", env);
}
else
{
sprintf(access_sudo[i], " ");
}
if (strcmp(access_user[i], "root") == 0)
{
sprintf(access_homedir[i], "/%s/", access_user[i]);
}
else
{
sprintf(access_homedir[i], "/home/%s/", access_user[i]);
}
}
}
}
@ -290,9 +209,9 @@ int Mariadb_nodes::find_master()
while ((found == 0) && (i < N))
{
if (find_field(
nodes[i], "show slave status;",
"Master_Host", &str[0]
) == 0)
nodes[i], (char *) "show slave status;",
(char *) "Master_Host", &str[0]
) == 0 )
{
found = 1;
strcpy(master_IP, str);
@ -328,13 +247,13 @@ int Mariadb_nodes::change_master(int NewMaster, int OldMaster)
{
if (i != OldMaster)
{
execute_query(nodes[i], "stop slave;");
execute_query(nodes[i], (char *) "stop slave;");
}
}
execute_query(nodes[NewMaster], create_repl_user);
execute_query(nodes[OldMaster], "reset master;");
find_field(nodes[NewMaster], "show master status", "File", &log_file[0]);
find_field(nodes[NewMaster], "show master status", "Position", &log_pos[0]);
execute_query(nodes[OldMaster], (char *) "reset master;");
find_field(nodes[NewMaster], (char *) "show master status", (char *) "File", &log_file[0]);
find_field(nodes[NewMaster], (char *) "show master status", (char *) "Position", &log_pos[0]);
for (i = 0; i < N; i++)
{
if (i != NewMaster)
@ -343,15 +262,15 @@ int Mariadb_nodes::change_master(int NewMaster, int OldMaster)
execute_query(nodes[i], str);
}
}
//for (i = 0; i < N; i++) {if (i != NewMaster) {execute_query(nodes[i], "start slave;"); }}
//for (i = 0; i < N; i++) {if (i != NewMaster) {execute_query(nodes[i], (char *) "start slave;"); }}
}
int Mariadb_nodes::stop_node(int node)
{
return ssh_node(node, true, stop_db_command[node]);
return ssh_node(node, stop_db_command[node], true);
}
int Mariadb_nodes::start_node(int node, const char* param)
int Mariadb_nodes::start_node(int node, char * param)
{
char cmd[1024];
if (v51)
@ -362,7 +281,7 @@ int Mariadb_nodes::start_node(int node, const char* param)
{
sprintf(cmd, "%s %s", start_db_command[node], param);
}
return ssh_node(node, true, cmd);
return ssh_node(node, cmd, true);
}
int Mariadb_nodes::stop_nodes()
@ -374,7 +293,7 @@ int Mariadb_nodes::stop_nodes()
{
printf("Stopping node %d\n", i);
fflush(stdout);
local_result += execute_query(nodes[i], "stop slave;");
local_result += execute_query(nodes[i], (char *) "stop slave;");
fflush(stdout);
local_result += stop_node(i);
fflush(stdout);
@ -391,7 +310,7 @@ int Mariadb_nodes::stop_slaves()
{
printf("Stopping slave %d\n", i);
fflush(stdout);
global_result += execute_query(nodes[i], "stop slave;");
global_result += execute_query(nodes[i], (char *) "stop slave;");
}
close_connections();
return global_result;
@ -399,7 +318,7 @@ int Mariadb_nodes::stop_slaves()
int Mariadb_nodes::cleanup_db_node(int node)
{
return ssh_node(node, true, cleanup_db_command[node]);
return ssh_node(node, cleanup_db_command[node], true);
}
int Mariadb_nodes::cleanup_db_nodes()
@ -426,41 +345,45 @@ int Mariadb_nodes::start_replication()
// Start all nodes
for (int i = 0; i < N; i++)
{
local_result += start_node(i, "");
ssh_node(i, true,
"mysql -u root %s -e \"STOP SLAVE; RESET SLAVE; RESET SLAVE ALL; RESET MASTER; SET GLOBAL read_only=OFF;\"",
socket_cmd[i]);
ssh_node(i, true, "sudo rm -f /etc/my.cnf.d/kerb.cnf");
ssh_node(i, true,
"for i in `mysql -ss -u root %s -e \"SHOW DATABASES\"|grep -iv 'mysql\\|information_schema\\|performance_schema'`; "
"do mysql -u root %s -e \"DROP DATABASE $i\";"
"done", socket_cmd[i], socket_cmd[i]);
local_result += start_node(i, (char *) "");
sprintf(str,
"mysql -u root %s -e \"STOP SLAVE; RESET SLAVE; RESET SLAVE ALL; RESET MASTER; SET GLOBAL read_only=OFF;\"",
socket_cmd[i]);
ssh_node(i, str, true);
}
sprintf(str, "%s/create_user.sh", test_dir);
sprintf(dtr, "%s", access_homedir[0]);
copy_to_node(str, dtr, 0);
ssh_node(0, false, "export node_user=\"%s\"; export node_password=\"%s\"; %s/create_user.sh %s",
user_name, password, access_homedir[0], socket_cmd[0]);
copy_to_node_legacy(str, dtr , 0);
sprintf(str, "export node_user=\"%s\"; export node_password=\"%s\"; %s/create_user.sh %s",
user_name, password, access_homedir[0], socket_cmd[0]);
printf("cmd: %s\n", str);
ssh_node(0, str, false);
// Create a database dump from the master and distribute it to the slaves
ssh_node(0, true, "mysql -u root %s -e \"CREATE DATABASE test\"; "
"mysqldump --all-databases --add-drop-database --flush-privileges --master-data=1 --gtid %s > /tmp/master_backup.sql",
socket_cmd[0], socket_cmd[0]);
sprintf(str,
"mysqldump --all-databases --add-drop-database --flush-privileges --master-data=1 --gtid %s > /tmp/master_backup.sql",
socket_cmd[0]);
ssh_node(0, str, true);
sprintf(str, "%s/master_backup.sql", test_dir);
copy_from_node("/tmp/master_backup.sql", str, 0);
copy_from_node_legacy("/tmp/master_backup.sql", str, 0);
for (int i = 1; i < N; i++)
{
// Reset all nodes by first loading the dump and then starting the replication
printf("Starting node %d\n", i);
fflush(stdout);
copy_to_node(str, "/tmp/master_backup.sql", i);
ssh_node(i, true, "mysql -u root %s < /tmp/master_backup.sql",
socket_cmd[i]);
ssh_node(i, true, "mysql -u root %s -e \"CHANGE MASTER TO MASTER_HOST=\\\"%s\\\", MASTER_PORT=%d, "
"MASTER_USER=\\\"repl\\\", MASTER_PASSWORD=\\\"repl\\\";"
"START SLAVE;\"", socket_cmd[i], IP_private[0], port[0]);
copy_to_node_legacy(str, "/tmp/master_backup.sql", i);
sprintf(dtr,
"mysql -u root %s < /tmp/master_backup.sql",
socket_cmd[i]);
ssh_node(i, dtr, true);
char query[512];
sprintf(query, "mysql -u root %s -e \"CHANGE MASTER TO MASTER_HOST=\\\"%s\\\", MASTER_PORT=%d, "
"MASTER_USER=\\\"repl\\\", MASTER_PASSWORD=\\\"repl\\\";"
"START SLAVE;\"", socket_cmd[i], IP_private[0], port[0]);
ssh_node(i, query, true);
}
return local_result;
@ -475,30 +398,40 @@ int Galera_nodes::start_galera()
local_result += stop_nodes();
// Remove the grastate.dat file
ssh_node(0, true, "rm -f /var/lib/mysql/grastate.dat");
ssh_node(0, "rm -f /var/lib/mysql/grastate.dat", true);
printf("Starting new Galera cluster\n");
fflush(stdout);
ssh_node(0, false, "echo [mysqld] > cluster_address.cnf");
ssh_node(0, false, "echo wsrep_cluster_address=gcomm:// >> cluster_address.cnf");
ssh_node(0, true, "cp cluster_address.cnf /etc/my.cnf.d/");
local_result += start_node(0, " --wsrep-cluster-address=gcomm://");
ssh_node(0, "echo [mysqld] > cluster_address.cnf", false);
ssh_node(0, "echo wsrep_cluster_address=gcomm:// >> cluster_address.cnf", false);
ssh_node(0, "cp cluster_address.cnf /etc/my.cnf.d/", true);
local_result += start_node(0, (char *) " --wsrep-cluster-address=gcomm://");
sprintf(str, "%s/create_user_galera.sh", test_dir);
copy_to_node(str, "~/", 0);
copy_to_node_legacy(str, "~/", 0);
ssh_node(0, false, "export galera_user=\"%s\"; export galera_password=\"%s\"; ./create_user_galera.sh %s",
user_name,
password, socket_cmd[0]);
sprintf(str, "export galera_user=\"%s\"; export galera_password=\"%s\"; ./create_user_galera.sh %s",
user_name,
password, socket_cmd[0]);
ssh_node(0, str, false);
for (i = 1; i < N; i++)
{
printf("Starting node %d\n", i);
ssh_node(i, true, "echo [mysqld] > cluster_address.cnf");
ssh_node(i, true, "echo wsrep_cluster_address=gcomm://%s >> cluster_address.cnf", IP_private[0]);
ssh_node(i, true, "cp cluster_address.cnf /etc/my.cnf.d/");
sprintf(str, " --wsrep-cluster-address=gcomm://%s", IP_private[0]);
local_result += start_node(i, str);
fflush(stdout);
ssh_node(i, "echo [mysqld] > cluster_address.cnf", true);
sprintf(str, "echo wsrep_cluster_address=gcomm://%s >> cluster_address.cnf", IP_private[0]);
ssh_node(i, str, true);
ssh_node(i, "cp cluster_address.cnf /etc/my.cnf.d/", true);
sprintf(&sys1[0], " --wsrep-cluster-address=gcomm://%s", IP_private[0]);
if (this->verbose)
{
printf("%s\n", sys1);
fflush(stdout);
}
local_result += start_node(i, sys1);
fflush(stdout);
}
sleep(5);
@ -511,30 +444,44 @@ int Galera_nodes::start_galera()
int Mariadb_nodes::clean_iptables(int node)
{
char sys1[1024];
int local_result = 0;
local_result += ssh_node(node, false, "echo \"#!/bin/bash\" > clean_iptables.sh");
local_result += ssh_node(node, false,
"echo \"while [ \\\"\\$(iptables -n -L INPUT 1|grep '%d')\\\" != \\\"\\\" ]; "
"do iptables -D INPUT 1; done\" >> clean_iptables.sh",
port[node]);
local_result += ssh_node(node, false,
"echo \"while [ \\\"\\$(ip6tables -n -L INPUT 1|grep '%d')\\\" != \\\"\\\" ]; "
"do ip6tables -D INPUT 1; done\" >> clean_iptables.sh",
port[node]);
local_result += ssh_node(node, (char *) "echo \"#!/bin/bash\" > clean_iptables.sh", false);
sprintf(sys1,
"echo \"while [ \\\"\\$(iptables -n -L INPUT 1|grep '%d')\\\" != \\\"\\\" ]; do iptables -D INPUT 1; done\" >> clean_iptables.sh",
port[node]);
local_result += ssh_node(node, (char *) sys1, false);
sprintf(sys1,
"echo \"while [ \\\"\\$(ip6tables -n -L INPUT 1|grep '%d')\\\" != \\\"\\\" ]; do ip6tables -D INPUT 1; done\" >> clean_iptables.sh",
port[node]);
local_result += ssh_node(node, (char *) sys1, false);
local_result += ssh_node(node, false, "chmod a+x clean_iptables.sh");
local_result += ssh_node(node, true, "./clean_iptables.sh");
local_result += ssh_node(node, (char *) "chmod a+x clean_iptables.sh", false);
local_result += ssh_node(node, (char *) "./clean_iptables.sh", true);
return local_result;
}
int Mariadb_nodes::block_node(int node)
{
char sys1[1024];
int local_result = 0;
local_result += clean_iptables(node);
sprintf(&sys1[0], "iptables -I INPUT -p tcp --dport %d -j REJECT", port[node]);
if (this->verbose)
{
printf("%s\n", sys1);
fflush(stdout);
}
local_result += ssh_node(node, sys1, true);
local_result += ssh_node(node, true, "iptables -I INPUT -p tcp --dport %d -j REJECT", port[node]);
local_result += ssh_node(node, true, "ip6tables -I INPUT -p tcp --dport %d -j REJECT", port[node]);
sprintf(&sys1[0], "ip6tables -I INPUT -p tcp --dport %d -j REJECT", port[node]);
if (this->verbose)
{
printf("%s\n", sys1);
fflush(stdout);
}
local_result += ssh_node(node, sys1, true);
blocked[node] = true;
return local_result;
@ -542,16 +489,29 @@ int Mariadb_nodes::block_node(int node)
int Mariadb_nodes::unblock_node(int node)
{
char sys1[1024];
int local_result = 0;
local_result += clean_iptables(node);
local_result += ssh_node(node, true, "iptables -I INPUT -p tcp --dport %d -j ACCEPT", port[node]);
local_result += ssh_node(node, true, "ip6tables -I INPUT -p tcp --dport %d -j ACCEPT", port[node]);
sprintf(&sys1[0], "iptables -I INPUT -p tcp --dport %d -j ACCEPT", port[node]);
if (this->verbose)
{
printf("%s\n", sys1);
fflush(stdout);
}
local_result += ssh_node(node, sys1, true);
sprintf(&sys1[0], "ip6tables -I INPUT -p tcp --dport %d -j ACCEPT", port[node]);
if (this->verbose)
{
printf("%s\n", sys1);
fflush(stdout);
}
local_result += ssh_node(node, sys1, true);
blocked[node] = false;
return local_result;
}
int Mariadb_nodes::unblock_all_nodes()
{
int rval = 0;
@ -562,36 +522,6 @@ int Mariadb_nodes::unblock_all_nodes()
return rval;
}
int Mariadb_nodes::check_node_ssh(int node)
{
int res = 0;
printf("Checking node %d\n", node);
fflush(stdout);
if (ssh_node(0, false, "ls > /dev/null") != 0)
{
printf("Node %d is not available\n", node);
fflush(stdout);
res = 1;
}
else
{
printf("Node %d is OK\n", node);
fflush(stdout);
}
return res;
}
int Mariadb_nodes::check_nodes()
{
int res = 0;
for (int i = 0; i < N; i++)
{
res += check_node_ssh(i);
}
return res;
}
bool Mariadb_nodes::check_master_node(MYSQL *conn)
{
bool rval = true;
@ -804,7 +734,7 @@ int Galera_nodes::check_galera()
{
char str[1024] = "";
if (find_field(conn, "SHOW STATUS WHERE Variable_name='wsrep_cluster_size';", "Value",
if (find_field(conn, (char *) "SHOW STATUS WHERE Variable_name='wsrep_cluster_size';", (char *) "Value",
str) != 0)
{
printf("wsrep_cluster_size is not found in SHOW STATUS LIKE 'wsrep%%' results\n");
@ -864,7 +794,7 @@ int Mariadb_nodes::get_server_id(int index)
int id = -1;
char str[1024];
if (find_field(this->nodes[index], "SELECT @@server_id", "@@server_id", (char*)str) == 0)
if (find_field(this->nodes[index], "SELECT @@server_id", "@@server_id", (char*) str) == 0)
{
id = atoi(str);
}
@ -883,123 +813,6 @@ std::string Mariadb_nodes::get_server_id_str(int index)
return ss.str();
}
void Mariadb_nodes::generate_ssh_cmd(char *cmd, int node, const char *ssh, bool sudo)
{
if (strcmp(IP[node], "127.0.0.1") == 0)
{
if (sudo)
{
sprintf(cmd, "%s %s",
access_sudo[node], ssh);
}
else
{
sprintf(cmd, "%s",
ssh);
}
}
else
{
if (sudo)
{
sprintf(cmd,
"ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet %s@%s '%s %s\'",
sshkey[node], access_user[node], IP[node], access_sudo[node], ssh);
}
else
{
sprintf(cmd,
"ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet %s@%s '%s'",
sshkey[node], access_user[node], IP[node], ssh);
}
}
}
char * Mariadb_nodes::ssh_node_output(int node, const char *ssh, bool sudo, int *exit_code)
{
char sys[strlen(ssh) + 1024];
generate_ssh_cmd(sys, node, ssh, sudo);
FILE *output = popen(sys, "r");
if (output == NULL)
{
printf("Error opening ssh %s\n", strerror(errno));
return NULL;
}
char buffer[1024];
size_t rsize = sizeof(buffer);
char* result = (char*)calloc(rsize, sizeof(char));
while (fgets(buffer, sizeof(buffer), output))
{
result = (char*)realloc(result, sizeof(buffer) + rsize);
rsize += sizeof(buffer);
strcat(result, buffer);
}
int code = pclose(output);
if (WIFEXITED(code))
{
* exit_code = WEXITSTATUS(code);
}
else
{
* exit_code = 256;
}
return result;
}
int Mariadb_nodes::ssh_node(int node, bool sudo, const char *format, ...)
{
va_list valist;
va_start(valist, format);
int message_len = vsnprintf(NULL, 0, format, valist);
va_end(valist);
if (message_len < 0)
{
return -1;
}
char *sys = (char*)malloc(message_len + 1);
va_start(valist, format);
vsnprintf(sys, message_len + 1, format, valist);
va_end(valist);
char *cmd = (char*)malloc(message_len + 1024);
if (strcmp(IP[node], "127.0.0.1") == 0)
{
sprintf(cmd, "bash");
}
else
{
sprintf(cmd,
"ssh -i %s -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet %s@%s > /dev/null",
sshkey[node], access_user[node], IP[node]);
}
int rc = 1;
FILE *in = popen(cmd, "w");
if (in)
{
if (sudo)
{
fprintf(in, "sudo su -\n");
fprintf(in, "cd /home/%s\n", access_user[node]);
}
fprintf(in, "%s\n", sys);
rc = pclose(in);
}
free(sys);
free(cmd);
return rc;
}
int Mariadb_nodes::flush_hosts()
{
@ -1083,7 +896,7 @@ int Mariadb_nodes::get_versions()
for (int i = 0; i < N; i++)
{
if ((local_result += find_field(nodes[i], "SELECT @@version", "@@version", version[i])))
if ((local_result += find_field(nodes[i], (char *) "SELECT @@version", (char *) "@@version", version[i])))
{
printf("Failed to get version: %s\n", mysql_error(nodes[i]));
}
@ -1094,11 +907,11 @@ int Mariadb_nodes::get_versions()
str[0] = 0;
}
strcpy(version_major[i], version_number[i]);
if (strstr(version_major[i], "5.") == version_major[i])
if (strstr(version_major[i], "5.") == version_major[i])
{
version_major[i][3] = 0;
}
if (strstr(version_major[i], "10.") == version_major[i])
if (strstr(version_major[i], "10.") == version_major[i])
{
version_major[i][4] = 0;
}
@ -1170,13 +983,13 @@ int Mariadb_nodes::configure_ssl(bool require)
printf("Node %d\n", i);
stop_node(i);
sprintf(str, "%s/ssl-cert", test_dir);
local_result += copy_to_node(str, "~/", i);
local_result += copy_to_node_legacy(str, (char *) "~/", i);
sprintf(str, "%s/ssl.cnf", test_dir);
local_result += copy_to_node(str, "~/", i);
local_result += ssh_node(i, true, "cp ~/ssl.cnf /etc/my.cnf.d/");
local_result += ssh_node(i, true, "cp -r ~/ssl-cert /etc/");
local_result += ssh_node(i, true, "chown mysql:mysql -R /etc/ssl-cert");
start_node(i, "");
local_result += copy_to_node_legacy(str, (char *) "~/", i);
local_result += ssh_node(i, (char *) "cp ~/ssl.cnf /etc/my.cnf.d/", true);
local_result += ssh_node(i, (char *) "cp -r ~/ssl-cert /etc/", true);
local_result += ssh_node(i, (char *) "chown mysql:mysql -R /etc/ssl-cert", true);
start_node(i, (char *) "");
}
if (require)
@ -1184,9 +997,14 @@ int Mariadb_nodes::configure_ssl(bool require)
// Create DB user on first node
printf("Set user to require ssl: %s\n", str);
sprintf(str, "%s/create_user_ssl.sh", test_dir);
copy_to_node(str, "~/", 0);
ssh_node(0, false, "export node_user=\"%s\"; export node_password=\"%s\"; "
"./create_user_ssl.sh %s", user_name, password, socket_cmd[0]);
copy_to_node_legacy(str, (char *) "~/", 0);
sprintf(str, "export node_user=\"%s\"; export node_password=\"%s\"; ./create_user_ssl.sh %s",
user_name,
password,
socket_cmd[0]);
printf("cmd: %s\n", str);
ssh_node(0, str, false);
}
return local_result;
@ -1200,77 +1018,23 @@ int Mariadb_nodes::disable_ssl()
local_result += connect();
sprintf(str, "DROP USER %s; grant all privileges on *.* to '%s'@'%%' identified by '%s';", user_name,
user_name, password);
local_result += execute_query(nodes[0], "");
local_result += execute_query(nodes[0], (char *) "");
close_connections();
for (int i = 0; i < N; i++)
{
stop_node(i);
local_result += ssh_node(i, true, "rm -f /etc/my.cnf.d/ssl.cnf");
start_node(i, "");
local_result += ssh_node(i, (char *) "rm -f /etc/my.cnf.d/ssl.cnf", true);
start_node(i, (char *) "");
}
return local_result;
}
int Mariadb_nodes::copy_to_node(const char* src, const char* dest, int i)
{
if (i >= N)
{
return 1;
}
char sys[strlen(src) + strlen(dest) + 1024];
if (strcmp(IP[i], "127.0.0.1") == 0)
{
sprintf(sys, "cp %s %s",
src, dest);
}
else
{
sprintf(sys, "scp -q -r -i %s -o UserKnownHostsFile=/dev/null "
"-o StrictHostKeyChecking=no -o LogLevel=quiet %s %s@%s:%s",
sshkey[i], src, access_user[i], IP[i], dest);
}
if (verbose)
{
printf("%s\n", sys);
}
return system(sys);
}
int Mariadb_nodes::copy_from_node(const char* src, const char* dest, int i)
{
if (i >= N)
{
return 1;
}
char sys[strlen(src) + strlen(dest) + 1024];
if (strcmp(IP[i], "127.0.0.1") == 0)
{
sprintf(sys, "cp %s %s",
src, dest);
}
else
{
sprintf(sys, "scp -q -r -i %s -o UserKnownHostsFile=/dev/null "
"-o StrictHostKeyChecking=no -o LogLevel=quiet %s@%s:%s %s",
sshkey[i], access_user[i], IP[i], src, dest);
}
if (verbose)
{
printf("%s\n", sys);
}
return system(sys);
}
static void wait_until_pos(MYSQL *mysql, int filenum, int pos)
{
int slave_filenum = 0;
int slave_pos = 0;
bool error = false;
do
{
@ -1285,36 +1049,29 @@ static void wait_until_pos(MYSQL *mysql, int filenum, int pos)
if (res)
{
MYSQL_ROW row = mysql_fetch_row(res);
error = true;
if (row && row[5] && row[21])
if (row && row[6] && row[21])
{
char *file_suffix = strchr(row[5], '.');
if (file_suffix)
{
file_suffix++;
slave_filenum = atoi(file_suffix);
slave_pos = atoi(row[21]);
error = false;
}
char *file_suffix = strchr(row[5], '.') + 1;
slave_filenum = atoi(file_suffix);
slave_pos = atoi(row[21]);
}
mysql_free_result(res);
}
}
while ((slave_filenum < filenum || slave_pos < pos) && !error);
while (slave_filenum < filenum || slave_pos < pos);
}
void Mariadb_nodes::sync_slaves(int node)
{
if (this->nodes[node] == NULL && this->connect())
if (this->nodes[node] == NULL)
{
printf("Failed to connect to all nodes.\n");
return;
this->connect();
}
if (mysql_query(this->nodes[node], "SHOW MASTER STATUS"))
{
printf("Failed to execute SHOW MASTER STATUS: %s\n", mysql_error(this->nodes[node]));
printf("Failed to execute SHOW MASTER STATUS: %s", mysql_error(this->nodes[node]));
}
else
{
@ -1323,26 +1080,18 @@ void Mariadb_nodes::sync_slaves(int node)
if (res)
{
MYSQL_ROW row = mysql_fetch_row(res);
if (row && row[0] && row[1])
if (row && row[node] && row[1])
{
const char* file_suffix = strchr(row[0], '.');
if (file_suffix)
{
file_suffix++;
int filenum = atoi(file_suffix);
int pos = atoi(row[1]);
const char* file_suffix = strchr(row[node], '.') + 1;
int filenum = atoi(file_suffix);
int pos = atoi(row[1]);
for (int i = 0; i < this->N; i++)
{
if (i != node)
{
wait_until_pos(this->nodes[i], filenum, pos);
}
}
}
else
for (int i = 0; i < this->N; i++)
{
printf("Cannot sync slaves, invalid binlog file name: %s\n", row[0]);
if (i != node)
{
wait_until_pos(this->nodes[i], filenum, pos);
}
}
}
mysql_free_result(res);
@ -1383,23 +1132,22 @@ void Mariadb_nodes::close_active_connections()
void Mariadb_nodes::stash_server_settings(int node)
{
ssh_node(node, true, "sudo mkdir /etc/my.cnf.d.backup");
ssh_node(node, true, "sudo cp -r /etc/my.cnf.d/* /etc/my.cnf.d.backup/");
ssh_node(node, "sudo mkdir /etc/my.cnf.d.backup", true);
ssh_node(node, "sudo cp -r /etc/my.cnf.d/* /etc/my.cnf.d.backup/", true);
}
void Mariadb_nodes::restore_server_settings(int node)
{
ssh_node(node, true, "sudo mv -f /etc/my.cnf.d.backup/* /etc/my.cnf.d/");
ssh_node(node, "sudo mv -f /etc/my.cnf.d.backup/* /etc/my.cnf.d/", true);
}
void Mariadb_nodes::disable_server_setting(int node, const char* setting)
{
ssh_node(node, true, "sudo sed -i 's/%s/#%s/' /etc/my.cnf.d/*", setting, setting);
ssh_node_f(node, true, "sudo sed -i 's/%s/#%s/' /etc/my.cnf.d/*", setting, setting);
}
void Mariadb_nodes::add_server_setting(int node, const char* setting)
{
ssh_node(node, true, "sudo sed -i '$a [server]' /etc/my.cnf.d/server.cnf", setting);
ssh_node(node, true, "sudo sed -i '$a %s' /etc/my.cnf.d/server.cnf", setting);
ssh_node_f(node, true, "sudo sed -i '$a [server]' /etc/my.cnf.d/server.cnf", setting);
ssh_node_f(node, true, "sudo sed -i '$a %s' /etc/my.cnf.d/server.cnf", setting);
}