forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flavor_mysql.go
134 lines (112 loc) · 4.11 KB
/
flavor_mysql.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
/*
Copyright 2017 Google Inc.
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 mysql
import (
"fmt"
"time"
"golang.org/x/net/context"
)
// mysqlFlavor implements the Flavor interface for Mysql.
type mysqlFlavor struct{}
// masterGTIDSet is part of the Flavor interface.
func (mysqlFlavor) masterGTIDSet(c *Conn) (GTIDSet, error) {
qr, err := c.ExecuteFetch("SELECT @@GLOBAL.gtid_executed", 1, false)
if err != nil {
return nil, err
}
if len(qr.Rows) != 1 || len(qr.Rows[0]) != 1 {
return nil, fmt.Errorf("unexpected result format for gtid_executed: %#v", qr)
}
return parseMysql56GTIDSet(qr.Rows[0][0].ToString())
}
// sendBinlogDumpCommand is part of the Flavor interface.
func (mysqlFlavor) sendBinlogDumpCommand(c *Conn, slaveID uint32, startPos Position) error {
gtidSet, ok := startPos.GTIDSet.(Mysql56GTIDSet)
if !ok {
return fmt.Errorf("startPos.GTIDSet is wrong type - expected Mysql56GTIDSet, got: %#v", startPos.GTIDSet)
}
// Build the command.
sidBlock := gtidSet.SIDBlock()
return c.WriteComBinlogDumpGTID(slaveID, "", 4, 0, sidBlock)
}
// resetReplicationCommands is part of the Flavor interface.
func (mysqlFlavor) resetReplicationCommands() []string {
return []string{
"STOP SLAVE",
"RESET SLAVE ALL", // "ALL" makes it forget master host:port.
"RESET MASTER", // This will also clear gtid_executed and gtid_purged.
}
}
// setSlavePositionCommands is part of the Flavor interface.
func (mysqlFlavor) setSlavePositionCommands(pos Position) []string {
return []string{
"RESET MASTER", // We must clear gtid_executed before setting gtid_purged.
fmt.Sprintf("SET GLOBAL gtid_purged = '%s'", pos),
}
}
// setSlavePositionCommands is part of the Flavor interface.
func (mysqlFlavor) changeMasterArg() string {
return "MASTER_AUTO_POSITION = 1"
}
// status is part of the Flavor interface.
func (mysqlFlavor) status(c *Conn) (SlaveStatus, error) {
qr, err := c.ExecuteFetch("SHOW SLAVE STATUS", 100, true /* wantfields */)
if err != nil {
return SlaveStatus{}, err
}
if len(qr.Rows) == 0 {
// The query returned no data, meaning the server
// is not configured as a slave.
return SlaveStatus{}, ErrNotSlave
}
resultMap, err := resultToMap(qr)
if err != nil {
return SlaveStatus{}, err
}
status := parseSlaveStatus(resultMap)
status.Position.GTIDSet, err = parseMysql56GTIDSet(resultMap["Executed_Gtid_Set"])
if err != nil {
return SlaveStatus{}, fmt.Errorf("SlaveStatus can't parse MySQL 5.6 GTID (Executed_Gtid_Set: %#v): %v", resultMap["Executed_Gtid_Set"], err)
}
return status, nil
}
// waitUntilPositionCommand is part of the Flavor interface.
func (mysqlFlavor) waitUntilPositionCommand(ctx context.Context, pos Position) (string, error) {
// A timeout of 0 means wait indefinitely.
timeoutSeconds := 0
if deadline, ok := ctx.Deadline(); ok {
timeout := deadline.Sub(time.Now())
if timeout <= 0 {
return "", fmt.Errorf("timed out waiting for position %v", pos)
}
// Only whole numbers of seconds are supported.
timeoutSeconds = int(timeout.Seconds())
if timeoutSeconds == 0 {
// We don't want a timeout <1.0s to truncate down to become infinite.
timeoutSeconds = 1
}
}
return fmt.Sprintf("SELECT WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS('%s', %v)", pos, timeoutSeconds), nil
}
// makeBinlogEvent is part of the Flavor interface.
func (mysqlFlavor) makeBinlogEvent(buf []byte) BinlogEvent {
return NewMysql56BinlogEvent(buf)
}
// enableBinlogPlaybackCommand is part of the Flavor interface.
func (mysqlFlavor) enableBinlogPlaybackCommand() string {
return ""
}
// disableBinlogPlaybackCommand is part of the Flavor interface.
func (mysqlFlavor) disableBinlogPlaybackCommand() string {
return ""
}