From 5e2854104ef0950f27bf46bb49d5b3cf80753dc0 Mon Sep 17 00:00:00 2001 From: Nicola Strappazzon C Date: Thu, 1 Feb 2024 22:31:30 +0100 Subject: [PATCH] feat - add connnection on mysql and proxysql --- mysql/mysql.go | 16 +++++++++------- proxysql/main.go | 1 + proxysql/server.go | 13 ++++++++++++- proxysql/stats.go | 4 ++-- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/mysql/mysql.go b/mysql/mysql.go index bb371a7..5e8d262 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -5,13 +5,15 @@ import ( ) type MySQL struct { - Host string `json:"host" yaml:"host"` - Port uint16 `json:"port" yaml:"port"` - Username string `json:"username" yaml:"username"` - Password string `json:"password" yaml:"password"` - Schema string `json:"schema" yaml:"schema"` - Status string `json:"status" yaml:"status"` - Timeout uint8 `json:"timeout" yaml:"timeout"` + Connection *Connection + Host string `json:"host" yaml:"host"` + Name string `json:"name" yaml:"name"` + Password string `json:"password" yaml:"password"` + Port uint16 `json:"port" yaml:"port"` + Schema string `json:"schema" yaml:"schema"` + Status string `json:"status" yaml:"status"` + Timeout uint8 `json:"timeout" yaml:"timeout"` + Username string `json:"username" yaml:"username"` } func (m *MySQL) DSN() string { diff --git a/proxysql/main.go b/proxysql/main.go index ff584bd..17b874b 100644 --- a/proxysql/main.go +++ b/proxysql/main.go @@ -26,5 +26,6 @@ func (p *ProxySQL) AddServer(s Server) { func (p *ProxySQL) Link() { for i, _ := range p.Servers { p.Servers[i].Connection = p.Connection + p.Servers[i].Stats.Connection = p.Connection } } diff --git a/proxysql/server.go b/proxysql/server.go index 5e5151f..d4a5c9e 100644 --- a/proxysql/server.go +++ b/proxysql/server.go @@ -37,7 +37,7 @@ func (s *Server) Update() error { } func (s *Server) Fetcher() error { - return s.Connection.Instance.QueryRow(s.QuerySelect()).Scan( + err := s.Connection.Instance.QueryRow(s.QuerySelect()).Scan( &s.HostgroupID, &s.Hostname, &s.Port, @@ -45,6 +45,13 @@ func (s *Server) Fetcher() error { &s.Weight, &s.MaxConnections, &s.MaxReplicationLag) + + // c.ProxySQL.Servers.First().Stats.Connection = c.ProxySQL.Connection + // c.ProxySQL.Servers.First().Stats.HostgroupID = c.ProxySQL.Servers.First().HostgroupID + // c.ProxySQL.Servers.First().Stats.Hostname = c.AWS.RDS.Replica.Instance.Endpoint + // c.ProxySQL.Servers.First().Stats.Fetcher() + + return err } func (s *Server) Delete() error { @@ -56,6 +63,10 @@ func (s *Server) Delete() error { return nil } +func (s *Server) IsOnLine() bool { + return s.Status == ONLINE && s.Stats.Status == ONLINE +} + func (s *Server) QueryInsert() string { return fmt.Sprintf( "INSERT INTO mysql_servers (hostgroup_id, hostname, port, status, weight, max_connections, max_replication_lag) VALUES (%d, '%s', %d, '%s', %d, %d, %d);", diff --git a/proxysql/stats.go b/proxysql/stats.go index d6b1062..29bf5c1 100644 --- a/proxysql/stats.go +++ b/proxysql/stats.go @@ -46,8 +46,8 @@ func (p *Stats) Fetcher() error { func (p *Stats) QuerySelect() string { return fmt.Sprintf( - "SELECT hostgroup, srv_host, srv_port, status, ConnUsed, ConnFree, ConnOK, ConnERR, MaxConnUsed, Queries, Queries_GTID_sync, Bytes_data_sent, Bytes_data_recv, Latency_us " + - "FROM stats_mysql_connection_pool WHERE hostgroup = %d AND srv_host = '%s' LIMIT 1;", + "SELECT hostgroup, srv_host, srv_port, status, ConnUsed, ConnFree, ConnOK, ConnERR, MaxConnUsed, Queries, Queries_GTID_sync, Bytes_data_sent, Bytes_data_recv, Latency_us "+ + "FROM stats_mysql_connection_pool WHERE hostgroup = %d AND srv_host = '%s' LIMIT 1;", p.HostgroupID, p.Hostname, )