Skip to content

Commit ee23e72

Browse files
Merge pull request #28 from debeando/proxysql-link
refact - proxysql double link
2 parents 703b79f + 258a829 commit ee23e72

File tree

6 files changed

+111
-42
lines changed

6 files changed

+111
-42
lines changed

proxysql/main.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ type ProxySQL struct {
2020
}
2121

2222
func (p *ProxySQL) AddServer(s Server) {
23-
s.ProxySQL = p
23+
s.Connection = p.Connection
2424
p.Servers.Add(s)
2525
}
26+
27+
func (p *ProxySQL) Link() {
28+
for i, _ := range p.Servers {
29+
p.Servers[i].Connection = p.Connection
30+
}
31+
32+
p.Stats.Connection = p.Connection
33+
p.Stats.ConnectionPool.Connection = p.Connection
34+
}

proxysql/main_test.go

+56-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/debeando/go-common/proxysql"
88

99
"github.com/stretchr/testify/assert"
10+
11+
"gopkg.in/yaml.v3"
1012
)
1113

1214
var p = proxysql.ProxySQL{}
@@ -22,7 +24,6 @@ func TestConnection(t *testing.T) {
2224
}
2325

2426
p.Connection = mysql.New("proxysql", p.MySQL.DSN())
25-
2627
assert.NoError(t, p.Connection.Connect())
2728
}
2829

@@ -34,7 +35,60 @@ func TestAddServer(t *testing.T) {
3435
})
3536

3637
assert.Equal(t, p.Servers.Count(), 1)
37-
assert.NotEmpty(t, p.Servers.First().ProxySQL)
38+
assert.NotEmpty(t, p.Servers.First().Connection)
3839

3940
p.Servers.Reset()
4041
}
42+
43+
func TestUnmarshalYaml(t *testing.T) {
44+
pT := proxysql.ProxySQL{}
45+
cT := `
46+
---
47+
mysql:
48+
host: 127.0.0.1
49+
port: 6032
50+
username: radmin
51+
password: radmin
52+
servers:
53+
- hostgroup_id: 20
54+
hostname: "127.0.0.1"
55+
port: 3306
56+
status: ONLINE
57+
weight: 1
58+
max_connections: 0
59+
max_replication_lag: 0
60+
`
61+
62+
err := yaml.Unmarshal([]byte(cT), &pT)
63+
64+
assert.NoError(t, err)
65+
assert.Equal(t, pT.Servers.Count(), 1)
66+
assert.Equal(t, pT.Servers.First().HostgroupID, uint8(20))
67+
assert.Equal(t, pT.Servers.First().Hostname, "127.0.0.1")
68+
assert.Equal(t, pT.Servers.First().Port, uint16(3306))
69+
assert.Equal(t, pT.Servers.First().Status, proxysql.ONLINE)
70+
}
71+
72+
func TestLink(t *testing.T) {
73+
z := proxysql.ProxySQL{
74+
MySQL: mysql.MySQL{
75+
Host: "127.0.0.1",
76+
Port: 6032,
77+
Username: "radmin",
78+
Password: "radmin",
79+
},
80+
Servers: []proxysql.Server{
81+
{HostgroupID: 10},
82+
{HostgroupID: 11},
83+
},
84+
}
85+
86+
z.Connection = mysql.New("proxysql", z.MySQL.DSN())
87+
z.Link()
88+
89+
assert.Equal(t, z.Servers.Count(), 2)
90+
assert.NotEmpty(t, z.Connection, nil)
91+
assert.NotEmpty(t, z.Servers.First(), nil)
92+
assert.NotEmpty(t, z.Stats.Connection, nil)
93+
assert.NotEmpty(t, z.Stats.ConnectionPool.Connection, nil)
94+
}

proxysql/server.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ package proxysql
22

33
import (
44
"fmt"
5+
6+
"github.com/debeando/go-common/mysql"
57
)
68

79
type Server struct {
8-
ProxySQL *ProxySQL `yaml:"-"`
9-
HostgroupID uint8 `yaml:"hostgroup_id"`
10-
Hostname string `yaml:"hostname"`
11-
MaxConnections uint16 `yaml:"max_connections"`
12-
MaxReplicationLag uint16 `yaml:"max_replication_lag"`
13-
Port uint16 `yaml:"port"`
14-
Status string `yaml:"status"`
15-
Weight uint16 `yaml:"weight"`
10+
Connection *mysql.Connection `yaml:"-"`
11+
HostgroupID uint8 `yaml:"hostgroup_id"`
12+
Hostname string `yaml:"hostname"`
13+
MaxConnections uint16 `yaml:"max_connections"`
14+
MaxReplicationLag uint16 `yaml:"max_replication_lag"`
15+
Port uint16 `yaml:"port"`
16+
Status string `yaml:"status"`
17+
Weight uint16 `yaml:"weight"`
1618
}
1719

18-
func (s *Server) Save() error {
19-
_, err := s.ProxySQL.Connection.Instance.Query(s.QueryInsert())
20+
func (s *Server) Insert() error {
21+
_, err := s.Connection.Instance.Query(s.QueryInsert())
2022
if err != nil {
2123
return err
2224
}
@@ -25,7 +27,7 @@ func (s *Server) Save() error {
2527
}
2628

2729
func (s *Server) Update() error {
28-
_, err := s.ProxySQL.Connection.Instance.Query(s.QueryUpdate())
30+
_, err := s.Connection.Instance.Query(s.QueryUpdate())
2931
if err != nil {
3032
return err
3133
}
@@ -34,7 +36,7 @@ func (s *Server) Update() error {
3436
}
3537

3638
func (s *Server) Fetcher() error {
37-
return s.ProxySQL.Connection.Instance.QueryRow(s.QuerySelect()).Scan(
39+
return s.Connection.Instance.QueryRow(s.QuerySelect()).Scan(
3840
&s.HostgroupID,
3941
&s.Hostname,
4042
&s.Port,
@@ -45,7 +47,7 @@ func (s *Server) Fetcher() error {
4547
}
4648

4749
func (s *Server) Delete() error {
48-
_, err := s.ProxySQL.Connection.Instance.Query(s.QueryDelete())
50+
_, err := s.Connection.Instance.Query(s.QueryDelete())
4951
if err != nil {
5052
return err
5153
}

proxysql/server_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
func TestServerSave(t *testing.T) {
11+
func TestServerInsert(t *testing.T) {
1212
s := proxysql.Server{
1313
HostgroupID: uint8(30),
1414
Hostname: "127.0.0.1",
@@ -18,8 +18,8 @@ func TestServerSave(t *testing.T) {
1818
Status: proxysql.ONLINE,
1919
Weight: uint16(1),
2020
}
21-
s.ProxySQL = &p
22-
assert.NoError(t, s.Save())
21+
s.Connection = p.Connection
22+
assert.NoError(t, s.Insert())
2323
}
2424

2525
func TestServerUpdate(t *testing.T) {
@@ -32,7 +32,7 @@ func TestServerUpdate(t *testing.T) {
3232
Status: proxysql.OFFLINE_SOFT,
3333
Weight: uint16(1),
3434
}
35-
s.ProxySQL = &p
35+
s.Connection = p.Connection
3636
assert.NoError(t, s.Update())
3737
}
3838

@@ -58,7 +58,7 @@ func TestServerDelete(t *testing.T) {
5858
HostgroupID: uint8(30),
5959
Hostname: "127.0.0.1",
6060
}
61-
s.ProxySQL = &p
61+
s.Connection = p.Connection
6262
assert.NoError(t, s.Delete())
6363
}
6464

proxysql/stats.go

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
11
package proxysql
22

3+
import (
4+
"github.com/debeando/go-common/mysql"
5+
)
6+
37
type Stats struct {
4-
ProxySQL *ProxySQL
8+
Connection *mysql.Connection
59
ConnectionPool ConnectionPool
610
}
711

812
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;"
913
const QueryConnectionPoolReset = "SELECT * FROM stats_mysql_connection_pool_reset;"
1014

1115
type ConnectionPool struct {
12-
ProxySQL *ProxySQL `db:"-"`
13-
HostgroupID uint8 `db:"hostgroup"`
14-
Hostname string `db:"srv_host"`
15-
Port uint16 `db:"srv_port"`
16-
Status string `db:"status"`
17-
ConnUsed uint64 `db:"ConnUsed"`
18-
ConnFree uint64 `db:"ConnFree"`
19-
ConnOK uint64 `db:"ConnOK"`
20-
ConnERR uint64 `db:"ConnERR"`
21-
MaxConnUsed uint64 `db:"MaxConnUsed"`
22-
Queries uint64 `db:"Queries"`
23-
QueriesGTIDSync uint64 `db:"Queries_GTID_sync"`
24-
BytesDataSent uint64 `db:"Bytes_data_sent"`
25-
BytesDataRecv uint64 `db:"Bytes_data_recv"`
26-
Latency uint64 `db:"Latency_us"`
16+
Connection *mysql.Connection `db:"-"`
17+
HostgroupID uint8 `db:"hostgroup"`
18+
Hostname string `db:"srv_host"`
19+
Port uint16 `db:"srv_port"`
20+
Status string `db:"status"`
21+
ConnUsed uint64 `db:"ConnUsed"`
22+
ConnFree uint64 `db:"ConnFree"`
23+
ConnOK uint64 `db:"ConnOK"`
24+
ConnERR uint64 `db:"ConnERR"`
25+
MaxConnUsed uint64 `db:"MaxConnUsed"`
26+
Queries uint64 `db:"Queries"`
27+
QueriesGTIDSync uint64 `db:"Queries_GTID_sync"`
28+
BytesDataSent uint64 `db:"Bytes_data_sent"`
29+
BytesDataRecv uint64 `db:"Bytes_data_recv"`
30+
Latency uint64 `db:"Latency_us"`
2731
}
2832

2933
func (p *ConnectionPool) Fetcher() error {
30-
return p.ProxySQL.Connection.Instance.QueryRow(QueryConnectionPool).Scan(
34+
return p.Connection.Instance.QueryRow(QueryConnectionPool).Scan(
3135
&p.HostgroupID,
3236
&p.Hostname,
3337
&p.Port,
@@ -45,5 +49,5 @@ func (p *ConnectionPool) Fetcher() error {
4549
}
4650

4751
func (p *ConnectionPool) Reset() {
48-
p.ProxySQL.Connection.Query(QueryConnectionPoolReset)
52+
p.Connection.Query(QueryConnectionPoolReset)
4953
}

proxysql/stats_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func TestStatsConnectionPoolFetcher(t *testing.T) {
1919
Status: proxysql.ONLINE,
2020
Weight: uint16(1),
2121
})
22-
p.Servers.First().Save()
22+
p.Servers.First().Insert()
2323
p.ServersLoadToRunTime()
2424
p.ServersSaveToDisk()
2525

26-
p.Stats.ProxySQL = &p
27-
p.Stats.ConnectionPool.ProxySQL = &p
26+
p.Stats.Connection = p.Connection
27+
p.Stats.ConnectionPool.Connection = p.Connection
2828
p.Stats.ConnectionPool.Fetcher()
2929

3030
assert.Equal(t, p.Stats.ConnectionPool.HostgroupID, uint8(10))

0 commit comments

Comments
 (0)