Skip to content

Commit

Permalink
refact - proxysql
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Strappazzon C committed Jan 30, 2024
1 parent 53b3912 commit b088156
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
4 changes: 0 additions & 4 deletions proxysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ type ProxySQL struct {
mysql.MySQL
Connection *mysql.Connection
Servers Servers
Stats Stats
}

func (p *ProxySQL) AddServer(s Server) {
Expand All @@ -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
}
2 changes: 0 additions & 2 deletions proxysql/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions proxysql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 15 additions & 10 deletions proxysql/stats.go
Original file line number Diff line number Diff line change
@@ -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"`
Expand All @@ -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,
Expand All @@ -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)
}
17 changes: 9 additions & 8 deletions proxysql/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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()
Expand Down

0 comments on commit b088156

Please sign in to comment.