Flush hosts in parallel

The Mariadb_nodes class now performs the node flushing in parallel. This
should speed up startup. Also increased the max_connect_errors.
This commit is contained in:
Markus Mäkelä
2018-06-18 22:09:57 +03:00
parent 8f71e803c1
commit 5350817790

View File

@ -17,6 +17,7 @@
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <future>
namespace namespace
{ {
@ -978,32 +979,29 @@ std::string Mariadb_nodes::get_server_id_str(int index)
return ss.str(); return ss.str();
} }
int Mariadb_nodes::flush_hosts() bool do_flush_hosts(MYSQL* conn)
{ {
if (this->nodes[0] == NULL && this->connect())
{
return 1;
}
int local_result = 0; int local_result = 0;
for (int i = 0; i < N; i++) if (mysql_query(conn, "FLUSH HOSTS"))
{
if (mysql_query(nodes[i], "FLUSH HOSTS"))
{ {
local_result++; local_result++;
} }
if (mysql_query(nodes[i], "SET GLOBAL max_connections=10000")) if (mysql_query(conn, "SET GLOBAL max_connections=10000"))
{ {
local_result++; local_result++;
} }
if (mysql_query(nodes[i], if (mysql_query(conn, "SET GLOBAL max_connect_errors=10000000"))
{
local_result++;
}
if (mysql_query(conn,
"SELECT CONCAT('\\'', user, '\\'@\\'', host, '\\'') FROM mysql.user WHERE user = ''") == 0) "SELECT CONCAT('\\'', user, '\\'@\\'', host, '\\'') FROM mysql.user WHERE user = ''") == 0)
{ {
MYSQL_RES *res = mysql_store_result(nodes[i]); MYSQL_RES *res = mysql_store_result(conn);
if (res) if (res)
{ {
@ -1026,19 +1024,48 @@ int Mariadb_nodes::flush_hosts()
std::string query = "DROP USER "; std::string query = "DROP USER ";
query += s; query += s;
printf("%s\n", query.c_str()); printf("%s\n", query.c_str());
mysql_query(nodes[i], query.c_str()); mysql_query(conn, query.c_str());
} }
} }
} }
} }
else else
{ {
printf("Failed to query for anonymous users: %s\n", mysql_error(nodes[i])); printf("Failed to query for anonymous users: %s\n", mysql_error(conn));
local_result++; local_result++;
} }
return local_result == 0;
}
int Mariadb_nodes::flush_hosts()
{
if (this->nodes[0] == NULL && this->connect())
{
return 1;
}
bool all_ok = true;
std::vector<std::future<bool>> futures;
for (int i = 0; i < N; i++)
{
std::packaged_task<bool (MYSQL*)> task(do_flush_hosts);
futures.push_back(task.get_future());
std::thread(std::move(task), nodes[i]).detach();
}
for (auto& f: futures)
{
f.wait();
if (!f.get())
{
all_ok = false;
}
} }
return local_result; return all_ok;
} }
int Mariadb_nodes::execute_query_all_nodes(const char* sql) int Mariadb_nodes::execute_query_all_nodes(const char* sql)