Skip to content

Commit

Permalink
refact - proxysql double link
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Jan 22, 2024
1 parent 703b79f commit 6ef1b83
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 41 deletions.
27 changes: 26 additions & 1 deletion proxysql/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package proxysql

import (
// "fmt"
// "reflect"

"github.com/debeando/go-common/mysql"
// "gopkg.in/yaml.v3"
)

const (
Expand All @@ -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
// }
48 changes: 47 additions & 1 deletion proxysql/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/debeando/go-common/proxysql"

"github.com/stretchr/testify/assert"

"gopkg.in/yaml.v3"
)

var p = proxysql.ProxySQL{}
Expand Down Expand Up @@ -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())
}
28 changes: 15 additions & 13 deletions proxysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions proxysql/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) {
Expand All @@ -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())
}

Expand All @@ -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())
}

Expand Down
40 changes: 22 additions & 18 deletions proxysql/stats.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package proxysql

import (
"github.com/debeando/go-common/mysql"
)

type Stats struct {
ProxySQL *ProxySQL
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 {
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,
Expand All @@ -45,5 +49,5 @@ func (p *ConnectionPool) Fetcher() error {
}

func (p *ConnectionPool) Reset() {
p.ProxySQL.Connection.Query(QueryConnectionPoolReset)
p.Connection.Query(QueryConnectionPoolReset)
}
6 changes: 3 additions & 3 deletions proxysql/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 6ef1b83

Please sign in to comment.