Merge branch '2.2' into develop

This commit is contained in:
Esa Korhonen
2018-04-24 13:57:50 +03:00
6 changed files with 69 additions and 22 deletions

View File

@ -296,6 +296,10 @@ the above requirements. Rejoin does not obey `failcount` and will attempt to
rejoin any valid servers immediately. When activating rejoin manually, the
user-designated server must fulfill the same requirements.
The user can define files with SQL statements which are executed on any server
being demoted or promoted by cluster manipulation commands. See the sections on
`promotion_sql_file` and `demotion_sql_file` for more information.
### Limitations and requirements
Switchover and failover only understand simple topologies. They will not work if
@ -450,6 +454,41 @@ may fail if all valid promotion candidates are in the exclusion list.
servers_no_promotion=backup_dc_server1,backup_dc_server2
```
#### `promotion_sql_file` and `demotion_sql_file`
These optional settings are paths to text files with SQL statements in them.
During promotion or demotion, the contents are read line-by-line and executed on
the backend. Use these settings to execute custom statements on the servers to
complement the built-in operations.
Empty lines or lines starting with '#' are ignored. Any results returned by the
statements are ignored. All statements must succeed for the failover, switchover
or rejoin to continue. The monitor user may require additional privileges and
grants for the custom commands to succeed.
When promoting a slave to master during switchover or failover, the
`promotion_sql_file` is read and executed on the new master server after its
read-only flag is disabled. The commands are ran *before* starting replication
from an external master if any.
`demotion_sql_file` is ran on an old master during demotion to slave, before the
old master starts replicating from the new master. The file is also ran before
rejoining a standalone server to the cluster, as the standalone server is
typically a former master server. When redirecting a slave replicating from a
wrong master, the sql-file is not executed.
Since the queries in the files are ran during operations which modify
replication topology, care is required. If `promotion_sql_file` contains data
modification (DML) queries, the new master server may not be able to
successfully replicate from an external master. `demotion_sql_file` should never
contain DML queries, as these may not replicate to the slave servers before
slave threads are stopped, breaking replication.
```
promotion_sql_file=/home/root/scripts/promotion.sql
demotion_sql_file=/home/root/scripts/demotion.sql
```
### Manual activation
Failover, switchover and rejoin can be activated manually through the REST API

View File

@ -241,7 +241,7 @@ main(int argc, char **argv)
bool use_unix_socket = false;
int option_index = 0;
char c;
int c;
while ((c = getopt_long(argc, argv, "h:p::P:u:S:v?ei",
long_options, &option_index)) >= 0)
{

View File

@ -313,19 +313,21 @@ uint32_t MariaDBMonitor::do_rejoin(const ServerArray& joinable_servers, json_t**
{
MariaDBServer* joinable = *iter;
const char* name = joinable->name();
bool op_success = false;
if (joinable->n_slaves_configured == 0)
{
if (!m_demote_sql_file.empty() && !joinable->run_sql_from_file(m_demote_sql_file, output))
{
PRINT_MXS_JSON_ERROR(output, "%s execution failed when attempting to rejoin server '%s'.",
CN_DEMOTION_SQL_FILE, joinable->name());
}
else
{
bool op_success;
if (joinable->n_slaves_configured == 0)
{
MXS_NOTICE("Directing standalone server '%s' to replicate from '%s'.", name, master_name);
op_success = joinable->join_cluster(change_cmd);
}
}
else
{
MXS_NOTICE("Server '%s' is replicating from a server other than '%s', "
@ -339,7 +341,6 @@ uint32_t MariaDBMonitor::do_rejoin(const ServerArray& joinable_servers, json_t**
}
}
}
}
return servers_joined;
}

View File

@ -645,7 +645,7 @@ bool MariaDBServer::run_sql_from_file(const string& path, json_t** error_out)
std::ifstream sql_file(path);
if (sql_file.is_open())
{
MXS_NOTICE("Executing sql queries from file '%s'.", path.c_str());
MXS_NOTICE("Executing sql queries from file '%s' on server '%s'.", path.c_str(), name());
int lines_executed = 0;
while (!sql_file.eof() && !error)
@ -664,6 +664,12 @@ bool MariaDBServer::run_sql_from_file(const string& path, json_t** error_out)
if (mxs_mysql_query(conn, line.c_str()) == 0)
{
lines_executed++;
// Discard results if any.
MYSQL_RES* res = mysql_store_result(conn);
if (res != NULL)
{
mysql_free_result(res);
}
}
else
{

View File

@ -266,7 +266,8 @@ public:
bool failover_wait_relay_log(int seconds_remaining, json_t** err_out);
/**
* Read the file contents and send them as sql queries to the server. Queries should not return any data.
* Read the file contents and send them as sql queries to the server. Any data returned by the queries is
* discarded.
*
* @param server Server to send queries to
* @param path Text file path.

View File

@ -88,7 +88,7 @@ int main(int argc, char **argv)
char *key_file = NULL;
char *aes_algo = NULL;
int report_header = 0;
char c;
int c;
BINLOG_FILE_FIX binlog_file = {0, false, false};
#ifdef HAVE_GLIBC