diff --git a/proxysql/main.go b/proxysql/main.go index 3cc248a..ff584bd 100644 --- a/proxysql/main.go +++ b/proxysql/main.go @@ -16,7 +16,6 @@ type ProxySQL struct { mysql.MySQL Connection *mysql.Connection Servers Servers - Stats Stats } func (p *ProxySQL) AddServer(s Server) { @@ -28,7 +27,4 @@ func (p *ProxySQL) Link() { for i, _ := range p.Servers { p.Servers[i].Connection = p.Connection } - - p.Stats.Connection = p.Connection - p.Stats.ConnectionPool.Connection = p.Connection } diff --git a/proxysql/main_test.go b/proxysql/main_test.go index a13f9da..c5c55e2 100644 --- a/proxysql/main_test.go +++ b/proxysql/main_test.go @@ -89,6 +89,4 @@ func TestLink(t *testing.T) { assert.Equal(t, z.Servers.Count(), 2) assert.NotEmpty(t, z.Connection, nil) assert.NotEmpty(t, z.Servers.First(), nil) - assert.NotEmpty(t, z.Stats.Connection, nil) - assert.NotEmpty(t, z.Stats.ConnectionPool.Connection, nil) } diff --git a/proxysql/server.go b/proxysql/server.go index 7f95379..5e5151f 100644 --- a/proxysql/server.go +++ b/proxysql/server.go @@ -15,6 +15,7 @@ type Server struct { Port uint16 `yaml:"port"` Status string `yaml:"status"` Weight uint16 `yaml:"weight"` + Stats Stats } func (s *Server) Insert() error { diff --git a/proxysql/stats.go b/proxysql/stats.go index b844f23..d6b1062 100644 --- a/proxysql/stats.go +++ b/proxysql/stats.go @@ -1,18 +1,14 @@ package proxysql import ( + "fmt" + "github.com/debeando/go-common/mysql" ) -type Stats struct { - Connection *mysql.Connection - ConnectionPool ConnectionPool -} - -const QueryConnectionPool = "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;" const QueryConnectionPoolReset = "SELECT * FROM stats_mysql_connection_pool_reset;" -type ConnectionPool struct { +type Stats struct { Connection *mysql.Connection `db:"-"` HostgroupID uint8 `db:"hostgroup"` Hostname string `db:"srv_host"` @@ -30,8 +26,8 @@ type ConnectionPool struct { Latency uint64 `db:"Latency_us"` } -func (p *ConnectionPool) Fetcher() error { - return p.Connection.Instance.QueryRow(QueryConnectionPool).Scan( +func (p *Stats) Fetcher() error { + return p.Connection.Instance.QueryRow(p.QuerySelect()).Scan( &p.HostgroupID, &p.Hostname, &p.Port, @@ -48,6 +44,15 @@ func (p *ConnectionPool) Fetcher() error { &p.Latency) } -func (p *ConnectionPool) Reset() { +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;", + p.HostgroupID, + p.Hostname, + ) +} + +func (p *Stats) Reset() { p.Connection.Query(QueryConnectionPoolReset) } diff --git a/proxysql/stats_test.go b/proxysql/stats_test.go index 667e841..0e87d2a 100644 --- a/proxysql/stats_test.go +++ b/proxysql/stats_test.go @@ -11,7 +11,7 @@ import ( func TestStatsConnectionPoolFetcher(t *testing.T) { p.Servers.Reset() p.AddServer(proxysql.Server{ - HostgroupID: uint8(10), + HostgroupID: uint8(11), Hostname: "127.0.0.1", MaxConnections: uint16(100), MaxReplicationLag: uint16(60), @@ -23,14 +23,15 @@ func TestStatsConnectionPoolFetcher(t *testing.T) { p.ServersLoadToRunTime() p.ServersSaveToDisk() - p.Stats.Connection = p.Connection - p.Stats.ConnectionPool.Connection = p.Connection - p.Stats.ConnectionPool.Fetcher() + p.Servers.First().Stats.Connection = p.Connection + p.Servers.First().Stats.HostgroupID = p.Servers.First().HostgroupID + p.Servers.First().Stats.Hostname = p.Servers.First().Hostname + p.Servers.First().Stats.Fetcher() - assert.Equal(t, p.Stats.ConnectionPool.HostgroupID, uint8(10)) - assert.Equal(t, p.Stats.ConnectionPool.Hostname, "127.0.0.1") - assert.Equal(t, p.Stats.ConnectionPool.Port, uint16(3307)) - assert.Equal(t, p.Stats.ConnectionPool.Status, proxysql.OFFLINE_SOFT) + assert.Equal(t, p.Servers.First().Stats.HostgroupID, uint8(11)) + assert.Equal(t, p.Servers.First().Stats.Hostname, "127.0.0.1") + assert.Equal(t, p.Servers.First().Stats.Port, uint16(3307)) + assert.Equal(t, p.Servers.First().Stats.Status, proxysql.OFFLINE_SOFT) p.Servers.First().Delete() p.ServersLoadToRunTime()