/
sql.go
282 lines (237 loc) · 7.55 KB
/
sql.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright 2019 Pressinfra SRL
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package node
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
// add mysql driver
_ "github.com/go-sql-driver/mysql"
"github.com/presslabs/mysql-operator/pkg/util/constants"
)
const (
// connRetry represents the number of tries to connect to master server
connRetry = 10
)
// SQLInterface expose abstract operations that can be applied on a MySQL node
type SQLInterface interface {
Wait(ctx context.Context) error
DisableSuperReadOnly(ctx context.Context) (func(), error)
ChangeMasterTo(ctx context.Context, host string, user string, pass string) error
MarkConfigurationDone(ctx context.Context) error
IsConfigured(ctx context.Context) (bool, error)
SetPurgedGTID(ctx context.Context) error
MarkSetGTIDPurged(ctx context.Context) error
Host() string
}
type nodeSQLRunner struct {
dsn string
host string
enableBinLog bool
}
type sqlFactoryFunc func(dsn, host string) SQLInterface
func newNodeConn(dsn, host string) SQLInterface {
return &nodeSQLRunner{
dsn: dsn,
host: host,
enableBinLog: false,
}
}
// Wait method pings MySQL until it's ready (runs SELECT 1;). The timeout should be set in ctx context.Context
func (r *nodeSQLRunner) Wait(ctx context.Context) error {
log.V(1).Info("wait for mysql to be ready")
for {
select {
case <-ctx.Done():
// timeout expired
return fmt.Errorf("timeout: mysql is not ready")
case <-time.After(time.Second):
if err := r.runQuery(ctx, "SELECT 1"); err == nil {
return nil
}
}
}
}
func (r *nodeSQLRunner) DisableSuperReadOnly(ctx context.Context) (func(), error) {
enable := func() {
err := r.runQuery(ctx, "SET GLOBAL SUPER_READ_ONLY = 1;")
if err != nil {
log.Error(err, "failed to set node super read only", "node", r.Host())
}
}
return enable, r.runQuery(ctx, "SET GLOBAL READ_ONLY = 1; SET GLOBAL SUPER_READ_ONLY = 0;")
}
// ChangeMasterTo changes the master host and starts slave.
func (r *nodeSQLRunner) ChangeMasterTo(ctx context.Context, masterHost, user, pass string) error {
// slave node
query := `
STOP SLAVE;
CHANGE MASTER TO MASTER_AUTO_POSITION=1,
MASTER_HOST=?,
MASTER_USER=?,
MASTER_PASSWORD=?,
MASTER_CONNECT_RETRY=?;
`
if err := r.runQuery(ctx, query,
masterHost, user, pass, connRetry,
); err != nil {
return fmt.Errorf("failed to configure slave node, err: %s", err)
}
query = "START SLAVE;"
if err := r.runQuery(ctx, query); err != nil {
log.Info("failed to start slave in the simple mode, trying a second method")
// TODO: https://bugs.mysql.com/bug.php?id=83713
query2 := `
reset slave;
start slave IO_THREAD;
stop slave IO_THREAD;
reset slave;
start slave;
`
if err := r.runQuery(ctx, query2); err != nil {
return fmt.Errorf("failed to start slave node, err: %s", err)
}
}
return nil
}
// MarkConfigurationDone write in a MEMORY table value. The readiness probe checks for that value to exist to succeed.
func (r *nodeSQLRunner) MarkConfigurationDone(ctx context.Context) error {
return r.writeStatusValue(ctx, "configured", "1")
}
// IsConfigured returns true if MySQL was configured, a key was set in the status table
func (r *nodeSQLRunner) IsConfigured(ctx context.Context) (bool, error) {
val, err := r.readStatusValue(ctx, "configured")
return val == "1", err
}
func (r *nodeSQLRunner) MarkSetGTIDPurged(ctx context.Context) error {
return r.writeStatusValue(ctx, "set_gtid_purged", "skipped")
}
func (r *nodeSQLRunner) Host() string {
return r.host
}
// runQuery executes a query
func (r *nodeSQLRunner) runQuery(ctx context.Context, q string, args ...interface{}) error {
db, close, err := r.dbConn()
if err != nil {
return err
}
defer close()
log.V(1).Info("running query", "query", q)
if !r.enableBinLog {
q = "SET @@SESSION.SQL_LOG_BIN = 0;\n" + q
}
if _, err := db.ExecContext(ctx, q, args...); err != nil {
return err
}
return nil
}
// readFromMysql executes the given query and loads the values into give variables
func (r *nodeSQLRunner) readFromMysql(ctx context.Context, query string, values ...interface{}) error {
db, close, err := r.dbConn()
if err != nil {
return err
}
defer close()
log.V(1).Info("running query", "query", query)
row := db.QueryRowContext(ctx, query)
if row == nil {
return fmt.Errorf("no row found")
}
err = row.Scan(values...)
if err != nil {
return err
}
return nil
}
// dbConn this function returns a pointer to sql.DB. a function for closing the connection
// or an error if the MySQL can not be reached
func (r *nodeSQLRunner) dbConn() (*sql.DB, func(), error) {
db, err := sql.Open("mysql", r.dsn)
if err != nil {
return nil, func() {}, err
}
close := func() {
if cErr := db.Close(); cErr != nil {
log.Error(cErr, "failed closing the database connection")
}
}
return db, close, nil
}
func (r *nodeSQLRunner) SetPurgedGTID(ctx context.Context) error {
var (
err error
setGTID string
backupGTID string
)
// first check if the GTID was set before
if setGTID, err = r.readStatusValue(ctx, "set_gtid_purged"); err != nil {
return err
} else if len(setGTID) != 0 {
log.V(1).Info("GTID purged was already set", "host", r.Host(), "gtid_purged", setGTID)
return nil
}
// check if there is any GTID to set
if backupGTID, err = r.readStatusValue(ctx, "backup_gtid_purged"); err != nil {
return err
} else if len(backupGTID) == 0 {
log.V(1).Info("no GTID to set", "host", r.Host())
return nil
}
// GTID exists and should be set in a transaction
// nolint: gosec
query := fmt.Sprintf(`
SET @@SESSION.SQL_LOG_BIN = 0;
START TRANSACTION;
SELECT value INTO @gtid FROM %[1]s.%[2]s WHERE name='%[3]s';
RESET MASTER;
SET @@GLOBAL.GTID_PURGED = @gtid;
REPLACE INTO %[1]s.%[2]s VALUES ('%[4]s', @gtid);
COMMIT;
`, constants.OperatorDbName, constants.OperatorStatusTableName, "backup_gtid_purged", "set_gtid_purged")
if err := r.runQuery(ctx, query); err != nil {
return err
}
return nil
}
// readStatusValue read from status table the value under the given key
func (r *nodeSQLRunner) readStatusValue(ctx context.Context, key string) (string, error) {
// nolint: gosec
qq := fmt.Sprintf("SELECT value FROM %s.%s WHERE name='%s'",
constants.OperatorDbName, constants.OperatorStatusTableName, key)
var value string
if err := r.readFromMysql(ctx, qq, &value); err != nil {
if err != sql.ErrNoRows {
return "", err
}
}
return value, nil
}
// writeStatusValue updates the value at the provided key
func (r *nodeSQLRunner) writeStatusValue(ctx context.Context, key, value string) error {
// nolint: gosec
query := fmt.Sprintf("REPLACE INTO %s.%s VALUES ('%s', '%s');",
constants.OperatorDbName, constants.OperatorStatusTableName, key, value)
if err := r.runQuery(ctx, query); err != nil {
return err
}
return nil
}
// isMySQLError checks if a mysql error is of the given code.
// more information about mysql error codes can be found here:
// https://dev.mysql.com/doc/refman/8.0/en/server-error-reference.html
func isMySQLError(err error, no int) bool {
errStr := fmt.Sprintf("Error %d:", no)
return strings.Contains(err.Error(), errStr)
}