diff --git a/proxysql/main.go b/proxysql/main.go index 8b9e7e6..7560dab 100644 --- a/proxysql/main.go +++ b/proxysql/main.go @@ -1,7 +1,11 @@ package proxysql import ( + // "fmt" + // "reflect" + "github.com/debeando/go-common/mysql" + // "gopkg.in/yaml.v3" ) const ( @@ -20,6 +24,27 @@ type ProxySQL struct { } func (p *ProxySQL) AddServer(s Server) { - s.ProxySQL = p + s.Connection = p.Connection p.Servers.Add(s) } + +// func (p *ProxySQL) UnmarshalYAML(node *yaml.Node) error { +// type nodeProxySQL ProxySQL +// var subProxySQL *nodeProxySQL + +// subProxySQL = (*nodeProxySQL)(p) + +// if err := node.Decode(&subProxySQL); err != nil { +// return err +// } + +// // subProxySQL = p + +// // fmt.Println(fmt.Sprintf("%#v", subProxySQL.Connection)) + +// // for i := 0 ; i < reflect.TypeOf(subProxySQL).NumField(); i++ { +// // fmt.Println(reflect.TypeOf(subProxySQL).Field(i)) +// // } + +// return nil +// } diff --git a/proxysql/main_test.go b/proxysql/main_test.go index 7c041dd..20698b1 100644 --- a/proxysql/main_test.go +++ b/proxysql/main_test.go @@ -7,6 +7,8 @@ import ( "github.com/debeando/go-common/proxysql" "github.com/stretchr/testify/assert" + + "gopkg.in/yaml.v3" ) var p = proxysql.ProxySQL{} @@ -34,7 +36,51 @@ func TestAddServer(t *testing.T) { }) assert.Equal(t, p.Servers.Count(), 1) - assert.NotEmpty(t, p.Servers.First().ProxySQL) + assert.NotEmpty(t, p.Servers.First().Connection) p.Servers.Reset() } + +/* TODO: + * - Defaults + * - Heredar + */ + +func TestUnmarshal(t *testing.T) { + pT := proxysql.ProxySQL{} + cT := ` +--- +mysql: + host: 127.0.0.1 + port: 6032 + username: radmin + password: radmin +servers: + - hostgroup_id: 20 + hostname: "127.0.0.1" + port: 3306 + status: ONLINE + weight: 1 + max_connections: 0 + max_replication_lag: 0 +` + + err := yaml.Unmarshal([]byte(cT), &pT) + assert.NoError(t, err) + assert.Equal(t, pT.Servers.Count(), 1) + + pT.Connection = mysql.New("proxysql", pT.MySQL.DSN()) + assert.NoError(t, pT.Connection.Connect()) + + pT.Servers.First().Connection = pT.Connection + + // t.Log(c) + // t.Log(pT.MySQL) + + t.Log(pT.Servers.First().HostgroupID) + t.Log(pT.Servers.First().Hostname) + t.Log(pT.Servers.First().Port) + t.Log(pT.Servers.First().Insert()) + t.Log(pT.Servers.First().Fetcher()) + t.Log(pT.Servers.First().Delete()) +} diff --git a/proxysql/server.go b/proxysql/server.go index 972b92e..7f95379 100644 --- a/proxysql/server.go +++ b/proxysql/server.go @@ -2,21 +2,23 @@ package proxysql import ( "fmt" + + "github.com/debeando/go-common/mysql" ) type Server struct { - ProxySQL *ProxySQL `yaml:"-"` - HostgroupID uint8 `yaml:"hostgroup_id"` - Hostname string `yaml:"hostname"` - MaxConnections uint16 `yaml:"max_connections"` - MaxReplicationLag uint16 `yaml:"max_replication_lag"` - Port uint16 `yaml:"port"` - Status string `yaml:"status"` - Weight uint16 `yaml:"weight"` + Connection *mysql.Connection `yaml:"-"` + HostgroupID uint8 `yaml:"hostgroup_id"` + Hostname string `yaml:"hostname"` + MaxConnections uint16 `yaml:"max_connections"` + MaxReplicationLag uint16 `yaml:"max_replication_lag"` + Port uint16 `yaml:"port"` + Status string `yaml:"status"` + Weight uint16 `yaml:"weight"` } -func (s *Server) Save() error { - _, err := s.ProxySQL.Connection.Instance.Query(s.QueryInsert()) +func (s *Server) Insert() error { + _, err := s.Connection.Instance.Query(s.QueryInsert()) if err != nil { return err } @@ -25,7 +27,7 @@ func (s *Server) Save() error { } func (s *Server) Update() error { - _, err := s.ProxySQL.Connection.Instance.Query(s.QueryUpdate()) + _, err := s.Connection.Instance.Query(s.QueryUpdate()) if err != nil { return err } @@ -34,7 +36,7 @@ func (s *Server) Update() error { } func (s *Server) Fetcher() error { - return s.ProxySQL.Connection.Instance.QueryRow(s.QuerySelect()).Scan( + return s.Connection.Instance.QueryRow(s.QuerySelect()).Scan( &s.HostgroupID, &s.Hostname, &s.Port, @@ -45,7 +47,7 @@ func (s *Server) Fetcher() error { } func (s *Server) Delete() error { - _, err := s.ProxySQL.Connection.Instance.Query(s.QueryDelete()) + _, err := s.Connection.Instance.Query(s.QueryDelete()) if err != nil { return err } diff --git a/proxysql/server_test.go b/proxysql/server_test.go index 956850c..a845a3d 100644 --- a/proxysql/server_test.go +++ b/proxysql/server_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestServerSave(t *testing.T) { +func TestServerInsert(t *testing.T) { s := proxysql.Server{ HostgroupID: uint8(30), Hostname: "127.0.0.1", @@ -18,8 +18,8 @@ func TestServerSave(t *testing.T) { Status: proxysql.ONLINE, Weight: uint16(1), } - s.ProxySQL = &p - assert.NoError(t, s.Save()) + s.Connection = p.Connection + assert.NoError(t, s.Insert()) } func TestServerUpdate(t *testing.T) { @@ -32,7 +32,7 @@ func TestServerUpdate(t *testing.T) { Status: proxysql.OFFLINE_SOFT, Weight: uint16(1), } - s.ProxySQL = &p + s.Connection = p.Connection assert.NoError(t, s.Update()) } @@ -58,7 +58,7 @@ func TestServerDelete(t *testing.T) { HostgroupID: uint8(30), Hostname: "127.0.0.1", } - s.ProxySQL = &p + s.Connection = p.Connection assert.NoError(t, s.Delete()) } diff --git a/proxysql/stats.go b/proxysql/stats.go index 02bc000..b844f23 100644 --- a/proxysql/stats.go +++ b/proxysql/stats.go @@ -1,7 +1,11 @@ package proxysql +import ( + "github.com/debeando/go-common/mysql" +) + type Stats struct { - ProxySQL *ProxySQL + Connection *mysql.Connection ConnectionPool ConnectionPool } @@ -9,25 +13,25 @@ const QueryConnectionPool = "SELECT hostgroup, srv_host, srv_port, status, ConnU const QueryConnectionPoolReset = "SELECT * FROM stats_mysql_connection_pool_reset;" type ConnectionPool struct { - ProxySQL *ProxySQL `db:"-"` - HostgroupID uint8 `db:"hostgroup"` - Hostname string `db:"srv_host"` - Port uint16 `db:"srv_port"` - Status string `db:"status"` - ConnUsed uint64 `db:"ConnUsed"` - ConnFree uint64 `db:"ConnFree"` - ConnOK uint64 `db:"ConnOK"` - ConnERR uint64 `db:"ConnERR"` - MaxConnUsed uint64 `db:"MaxConnUsed"` - Queries uint64 `db:"Queries"` - QueriesGTIDSync uint64 `db:"Queries_GTID_sync"` - BytesDataSent uint64 `db:"Bytes_data_sent"` - BytesDataRecv uint64 `db:"Bytes_data_recv"` - Latency uint64 `db:"Latency_us"` + Connection *mysql.Connection `db:"-"` + HostgroupID uint8 `db:"hostgroup"` + Hostname string `db:"srv_host"` + Port uint16 `db:"srv_port"` + Status string `db:"status"` + ConnUsed uint64 `db:"ConnUsed"` + ConnFree uint64 `db:"ConnFree"` + ConnOK uint64 `db:"ConnOK"` + ConnERR uint64 `db:"ConnERR"` + MaxConnUsed uint64 `db:"MaxConnUsed"` + Queries uint64 `db:"Queries"` + QueriesGTIDSync uint64 `db:"Queries_GTID_sync"` + BytesDataSent uint64 `db:"Bytes_data_sent"` + BytesDataRecv uint64 `db:"Bytes_data_recv"` + Latency uint64 `db:"Latency_us"` } func (p *ConnectionPool) Fetcher() error { - return p.ProxySQL.Connection.Instance.QueryRow(QueryConnectionPool).Scan( + return p.Connection.Instance.QueryRow(QueryConnectionPool).Scan( &p.HostgroupID, &p.Hostname, &p.Port, @@ -45,5 +49,5 @@ func (p *ConnectionPool) Fetcher() error { } func (p *ConnectionPool) Reset() { - p.ProxySQL.Connection.Query(QueryConnectionPoolReset) + p.Connection.Query(QueryConnectionPoolReset) } diff --git a/proxysql/stats_test.go b/proxysql/stats_test.go index 6445db1..d2bc429 100644 --- a/proxysql/stats_test.go +++ b/proxysql/stats_test.go @@ -19,12 +19,12 @@ func TestStatsConnectionPoolFetcher(t *testing.T) { Status: proxysql.ONLINE, Weight: uint16(1), }) - p.Servers.First().Save() + p.Servers.First().Insert() p.ServersLoadToRunTime() p.ServersSaveToDisk() - p.Stats.ProxySQL = &p - p.Stats.ConnectionPool.ProxySQL = &p + p.Stats.Connection = p.Connection + p.Stats.ConnectionPool.Connection = p.Connection p.Stats.ConnectionPool.Fetcher() assert.Equal(t, p.Stats.ConnectionPool.HostgroupID, uint8(10))