-
Notifications
You must be signed in to change notification settings - Fork 2
/
replication_hostgroups.go
182 lines (140 loc) · 3.78 KB
/
replication_hostgroups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package proxysql
import (
"database/sql"
"fmt"
"strconv"
"github.com/go-sql-driver/mysql"
"github.com/juju/errors"
)
type (
ReplicationHostgroup struct {
WriterHostgroup uint64 `json:"writer_hostgroup" db:"writer_hostgroup"`
ReaderHostgroup uint64 `json:"reader_hostgroup" db:"reader_hostgroup"`
Comment string `json:"comment" db:"comment"`
}
)
const (
// add a new replication hostgroup
StmtAddOneRHG = `
INSERT INTO
mysql_replication_hostgroups(writer_hostgroup,reader_hostgroup,comment)
VALUES(%d,%d,%q)`
// Update a replication hostgroup.
StmtUpdateOneRHG = `
UPDATE
mysql_replication_hostgroups
SET
writer_hostgroup = %d,
reader_hostgroup = %d,
comment = %q
`
// Delete a replication hostgroup.
StmtDeleteOneRHG = `
DELETE FROM
mysql_replication_hostgroups
WHERE
writer_hostgroup = %d
AND
reader_hostgroup = %d
`
// query all RHG informations.
StmtQueryAllRHGs = `
SELECT
*
FROM
mysql_replication_hostgroups
LIMIT %d
OFFSET %d
`
)
// new replication hostgroup instance
func NewRHG(whg uint64, rhg uint64) (*ReplicationHostgroup, error) {
newrhg := new(ReplicationHostgroup)
newrhg.WriterHostgroup = whg
newrhg.ReaderHostgroup = rhg
newrhg.Comment = ""
// return new replication hostgroup instance.
return newrhg, nil
}
// set writer hostgroup
func (rhg *ReplicationHostgroup) SetWriterHostGroup(writer uint64) {
rhg.WriterHostgroup = writer
}
// set reader hostgroup
func (rhg *ReplicationHostgroup) SetReaderHostGroup(reader uint64) {
rhg.ReaderHostgroup = reader
}
// set comment
func (rhg *ReplicationHostgroup) SetComment(comment string) {
rhg.Comment = comment
}
// add one new replication hostgroup
func (rhg *ReplicationHostgroup) AddOneRHG(db *sql.DB) error {
Query := fmt.Sprintf(StmtAddOneRHG, rhg.WriterHostgroup, rhg.ReaderHostgroup, rhg.Comment)
_, err := db.Exec(Query)
if err != nil {
switch {
case err.(*mysql.MySQLError).Number == 1045:
return errors.NewAlreadyExists(err, strconv.Itoa(int(rhg.WriterHostgroup))+"-"+strconv.Itoa(int(rhg.ReaderHostgroup)))
default:
return errors.Trace(err) //add server failed
}
}
LoadServerToRuntime(db)
SaveServerToDisk(db)
return nil
}
// update one replication hostgroup
func (rhg *ReplicationHostgroup) UpdateOneRHG(db *sql.DB) error {
Query := fmt.Sprintf(StmtUpdateOneRHG, rhg.WriterHostgroup, rhg.ReaderHostgroup, rhg.Comment)
result, err := db.Exec(Query)
if err != nil {
return errors.Trace(err)
}
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
return errors.NotFoundf(strconv.Itoa(int(rhg.WriterHostgroup)) + "-" + strconv.Itoa(int(rhg.ReaderHostgroup)))
}
LoadServerToRuntime(db)
SaveServerToDisk(db)
return nil
}
// delete one replication hostgroup
func (rhg *ReplicationHostgroup) DeleteOneRHG(db *sql.DB) error {
Query := fmt.Sprintf(StmtDeleteOneRHG, rhg.WriterHostgroup, rhg.ReaderHostgroup)
result, err := db.Exec(Query)
if err != nil {
return errors.Trace(err)
}
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
return errors.NotFoundf(strconv.Itoa(int(rhg.WriterHostgroup)) + "-" + strconv.Itoa(int(rhg.ReaderHostgroup)))
}
LoadServerToRuntime(db)
SaveServerToDisk(db)
return nil
}
// query all replication hostgroups
/*list all mysql_servers*/
func QueryAllRHG(db *sql.DB, limit uint64, skip uint64) ([]ReplicationHostgroup, error) {
var allrhg []ReplicationHostgroup
Query := fmt.Sprintf(StmtQueryAllRHGs, limit, skip)
rows, err := db.Query(Query)
if err != nil {
return []ReplicationHostgroup{}, errors.Trace(err)
}
defer rows.Close()
for rows.Next() {
var tmprhg ReplicationHostgroup
err = rows.Scan(
&tmprhg.WriterHostgroup,
&tmprhg.ReaderHostgroup,
&tmprhg.Comment,
)
if err != nil {
continue
}
allrhg = append(allrhg, tmprhg)
}
return allrhg, nil
}