Skip to content

Commit

Permalink
mysql_replication_hostgroups in configfile , #648
Browse files Browse the repository at this point in the history
Now Admin supports the configuration of mysql_replication_hostgroups from configfile.
That means that also LOAD MYSQL SERVERS FROM CONFIG is responsible for configuring mysql_replication_hostgroups.
This commit also allows comments in config file for `mysql_servers` and `mysql_replication_hostgroups` , related to #643
  • Loading branch information
renecannao committed Aug 17, 2016
1 parent 6439ced commit 66626c1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 41 deletions.
113 changes: 72 additions & 41 deletions lib/ProxySQL_Admin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4404,50 +4404,81 @@ int ProxySQL_Admin::Read_MySQL_Query_Rules_from_configfile() {

int ProxySQL_Admin::Read_MySQL_Servers_from_configfile() {
const Setting& root = GloVars.confFile->cfg->getRoot();
if (root.exists("mysql_servers")==false) return 0;
const Setting &mysql_servers = root["mysql_servers"];
int count = mysql_servers.getLength();
//fprintf(stderr, "Found %d servers\n",count);
int i;
int rows=0;
admindb->execute("PRAGMA foreign_keys = OFF");
char *q=(char *)"INSERT OR REPLACE INTO mysql_servers (hostname, port, hostgroup_id, compression, weight, status, max_connections, max_replication_lag, use_ssl, max_latency_ms) VALUES (\"%s\", %d, %d, %d, %d, \"%s\", %d, %d, %d, %d)";
for (i=0; i< count; i++) {
const Setting &server = mysql_servers[i];
std::string address;
std::string status="ONLINE";
int port;
int hostgroup;
int weight=1;
int compression=0;
int max_connections=1000; // default
int max_replication_lag=0; // default
int use_ssl=0;
int max_latency_ms=0;
if (server.lookupValue("address", address)==false) continue;
if (server.lookupValue("port", port)==false) continue;
if (server.lookupValue("hostgroup", hostgroup)==false) continue;
server.lookupValue("status", status);
if (
(strcasecmp(status.c_str(),(char *)"ONLINE"))
&& (strcasecmp(status.c_str(),(char *)"SHUNNED"))
&& (strcasecmp(status.c_str(),(char *)"OFFLINE_SOFT"))
&& (strcasecmp(status.c_str(),(char *)"OFFLINE_HARD"))
) {
status="ONLINE";
}
server.lookupValue("compression", compression);
server.lookupValue("weight", weight);
server.lookupValue("max_connections", max_connections);
server.lookupValue("max_replication_lag", max_replication_lag);
server.lookupValue("use_ssl", use_ssl);
server.lookupValue("max_latency_ms", max_latency_ms);
char *query=(char *)malloc(strlen(q)+strlen(status.c_str())+strlen(address.c_str())+128);
sprintf(query,q, address.c_str(), port, hostgroup, compression, weight, status.c_str(), max_connections, max_replication_lag, use_ssl, max_latency_ms);
//fprintf(stderr, "%s\n", query);
admindb->execute(query);
free(query);
rows++;
if (root.exists("mysql_servers")==true) {
const Setting &mysql_servers = root["mysql_servers"];
int count = mysql_servers.getLength();
//fprintf(stderr, "Found %d servers\n",count);
char *q=(char *)"INSERT OR REPLACE INTO mysql_servers (hostname, port, hostgroup_id, compression, weight, status, max_connections, max_replication_lag, use_ssl, max_latency_ms, comment) VALUES (\"%s\", %d, %d, %d, %d, \"%s\", %d, %d, %d, %d, '%s')";
for (i=0; i< count; i++) {
const Setting &server = mysql_servers[i];
std::string address;
std::string status="ONLINE";
int port;
int hostgroup;
int weight=1;
int compression=0;
int max_connections=1000; // default
int max_replication_lag=0; // default
int use_ssl=0;
int max_latency_ms=0;
std::string comment="";
if (server.lookupValue("address", address)==false) continue;
if (server.lookupValue("port", port)==false) continue;
if (server.lookupValue("hostgroup", hostgroup)==false) continue;
server.lookupValue("status", status);
if (
(strcasecmp(status.c_str(),(char *)"ONLINE"))
&& (strcasecmp(status.c_str(),(char *)"SHUNNED"))
&& (strcasecmp(status.c_str(),(char *)"OFFLINE_SOFT"))
&& (strcasecmp(status.c_str(),(char *)"OFFLINE_HARD"))
) {
status="ONLINE";
}
server.lookupValue("compression", compression);
server.lookupValue("weight", weight);
server.lookupValue("max_connections", max_connections);
server.lookupValue("max_replication_lag", max_replication_lag);
server.lookupValue("use_ssl", use_ssl);
server.lookupValue("max_latency_ms", max_latency_ms);
server.lookupValue("comment", comment);
char *o1=strdup(comment.c_str());
char *o=escape_string_single_quotes(o1, false);
char *query=(char *)malloc(strlen(q)+strlen(status.c_str())+strlen(address.c_str())+strlen(o)+128);
sprintf(query,q, address.c_str(), port, hostgroup, compression, weight, status.c_str(), max_connections, max_replication_lag, use_ssl, max_latency_ms, o);
//fprintf(stderr, "%s\n", query);
admindb->execute(query);
if (o!=o1) free(o);
free(o1);
free(query);
rows++;
}
}
if (root.exists("mysql_replication_hostgroups")==true) {
const Setting &mysql_replication_hostgroups = root["mysql_replication_hostgroups"];
int count = mysql_replication_hostgroups.getLength();
char *q=(char *)"INSERT OR REPLACE INTO mysql_replication_hostgroups (writer_hostgroup, reader_hostgroup, comment) VALUES (%d, %d, '%s')";
for (i=0; i< count; i++) {
const Setting &line = mysql_replication_hostgroups[i];
int writer_hostgroup;
int reader_hostgroup;
std::string comment="";
if (line.lookupValue("writer_hostgroup", writer_hostgroup)==false) continue;
if (line.lookupValue("reader_hostgroup", reader_hostgroup)==false) continue;
line.lookupValue("comment", comment);
char *o1=strdup(comment.c_str());
char *o=escape_string_single_quotes(o1, false);
char *query=(char *)malloc(strlen(q)+strlen(o)+32);
sprintf(query,q, writer_hostgroup, reader_hostgroup, o);
//fprintf(stderr, "%s\n", query);
admindb->execute(query);
if (o!=o1) free(o);
free(o1);
free(query);
rows++;
}
}
admindb->execute("PRAGMA foreign_keys = ON");
return rows;
Expand Down
15 changes: 15 additions & 0 deletions src/proxysql.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,24 @@ mysql_servers =
port=3306
hostgroup=0
max_connections=200
comment="test server"
}
)

mysql_replication_hostgroups=
(
{
writer_hostgroup=30
reader_hostgroup=40
comment="test repl 1"
},
{
writer_hostgroup=50
reader_hostgroup=60
comment="test repl 2"
}
)

mysql_users:
(
{
Expand Down

0 comments on commit 66626c1

Please sign in to comment.