forked from vitessio/vitess
/
mysql_flavor_mariadb.go
280 lines (242 loc) · 10.1 KB
/
mysql_flavor_mariadb.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
// Copyright 2014, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mysqlctl
import (
"encoding/binary"
"fmt"
"strings"
"time"
"golang.org/x/net/context"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/vt/mysqlctl/replication"
)
// mariaDB10 is the implementation of MysqlFlavor for MariaDB 10.0.10
type mariaDB10 struct {
}
const mariadbFlavorID = "MariaDB"
// VersionMatch implements MysqlFlavor.VersionMatch().
func (*mariaDB10) VersionMatch(version string) bool {
return strings.HasPrefix(version, "10.0") && strings.Contains(strings.ToLower(version), "mariadb")
}
// MasterPosition implements MysqlFlavor.MasterPosition().
func (flavor *mariaDB10) MasterPosition(mysqld *Mysqld) (rp replication.Position, err error) {
qr, err := mysqld.FetchSuperQuery(context.TODO(), "SELECT @@GLOBAL.gtid_binlog_pos")
if err != nil {
return rp, err
}
if len(qr.Rows) != 1 || len(qr.Rows[0]) != 1 {
return rp, fmt.Errorf("unexpected result format for gtid_binlog_pos: %#v", qr)
}
return flavor.ParseReplicationPosition(qr.Rows[0][0].String())
}
// SlaveStatus implements MysqlFlavor.SlaveStatus().
func (flavor *mariaDB10) SlaveStatus(mysqld *Mysqld) (replication.Status, error) {
fields, err := mysqld.fetchSuperQueryMap(context.TODO(), "SHOW ALL SLAVES STATUS")
if err != nil {
return replication.Status{}, err
}
if len(fields) == 0 {
// The query returned no data, meaning the server
// is not configured as a slave.
return replication.Status{}, ErrNotSlave
}
status := parseSlaveStatus(fields)
status.Position, err = flavor.ParseReplicationPosition(fields["Gtid_Slave_Pos"])
if err != nil {
return replication.Status{}, fmt.Errorf("SlaveStatus can't parse MariaDB GTID (Gtid_Slave_Pos: %#v): %v", fields["Gtid_Slave_Pos"], err)
}
return status, nil
}
// WaitMasterPos implements MysqlFlavor.WaitMasterPos().
//
// Note: Unlike MASTER_POS_WAIT(), MASTER_GTID_WAIT() will continue waiting even
// if the slave thread stops. If that is a problem, we'll have to change this.
func (*mariaDB10) WaitMasterPos(ctx context.Context, mysqld *Mysqld, targetPos replication.Position) error {
var query string
if deadline, ok := ctx.Deadline(); ok {
timeout := deadline.Sub(time.Now())
if timeout <= 0 {
return fmt.Errorf("timed out waiting for position %v", targetPos)
}
query = fmt.Sprintf("SELECT MASTER_GTID_WAIT('%s', %.6f)", targetPos, timeout.Seconds())
} else {
// Omit the timeout to wait indefinitely. In MariaDB, a timeout of 0 means
// return immediately.
query = fmt.Sprintf("SELECT MASTER_GTID_WAIT('%s')", targetPos)
}
log.Infof("Waiting for minimum replication position with query: %v", query)
qr, err := mysqld.FetchSuperQuery(ctx, query)
if err != nil {
return fmt.Errorf("MASTER_GTID_WAIT() failed: %v", err)
}
if len(qr.Rows) != 1 || len(qr.Rows[0]) != 1 {
return fmt.Errorf("unexpected result format from MASTER_GTID_WAIT(): %#v", qr)
}
result := qr.Rows[0][0].String()
if result == "-1" {
return fmt.Errorf("timed out waiting for position %v", targetPos)
}
return nil
}
// ResetReplicationCommands implements MysqlFlavor.ResetReplicationCommands().
func (*mariaDB10) ResetReplicationCommands() []string {
return []string{
"STOP SLAVE",
"RESET SLAVE ALL", // "ALL" makes it forget the master host:port.
"RESET MASTER",
"SET GLOBAL gtid_slave_pos = ''",
}
}
// PromoteSlaveCommands implements MysqlFlavor.PromoteSlaveCommands().
func (*mariaDB10) PromoteSlaveCommands() []string {
return []string{
"RESET SLAVE ALL", // "ALL" makes it forget the master host:port.
}
}
// SetSlavePositionCommands implements MysqlFlavor.
func (*mariaDB10) SetSlavePositionCommands(pos replication.Position) ([]string, error) {
return []string{
// RESET MASTER will clear out gtid_binlog_pos,
// which then guarantees that gtid_current_pos = gtid_slave_pos,
// since gtid_current_pos = MAX(gtid_binlog_pos, gtid_slave_pos).
// This also emptys the binlogs, which allows us to set gtid_binlog_state.
"RESET MASTER",
// Set gtid_slave_pos to tell the slave where to start replicating.
fmt.Sprintf("SET GLOBAL gtid_slave_pos = '%s'", pos),
// Set gtid_binlog_state so that if this server later becomes a master,
// it will know that it has seen everything up to and including 'pos'.
// Otherwise, if another slave asks this server to replicate starting at
// exactly 'pos', this server will throw an error when in gtid_strict_mode,
// since it doesn't see 'pos' in its binlog - it only has everything AFTER.
fmt.Sprintf("SET GLOBAL gtid_binlog_state = '%s'", pos),
}, nil
}
// SetMasterCommands implements MysqlFlavor.SetMasterCommands().
func (*mariaDB10) SetMasterCommands(params *sqldb.ConnParams, masterHost string, masterPort int, masterConnectRetry int) ([]string, error) {
// Make CHANGE MASTER TO command.
args := changeMasterArgs(params, masterHost, masterPort, masterConnectRetry)
// MASTER_USE_GTID = current_pos means it will request binlogs starting at
// MAX(master position, slave position), which handles the case where a
// demoted master is being converted back into a slave. In that case, the
// slave position might be behind the master position, since it stopped
// updating when the server was promoted to master.
args = append(args, "MASTER_USE_GTID = current_pos")
changeMasterTo := "CHANGE MASTER TO\n " + strings.Join(args, ",\n ")
return []string{changeMasterTo}, nil
}
// ParseGTID implements MysqlFlavor.ParseGTID().
func (*mariaDB10) ParseGTID(s string) (replication.GTID, error) {
return replication.ParseGTID(mariadbFlavorID, s)
}
// ParseReplicationPosition implements MysqlFlavor.ParseReplicationposition().
func (*mariaDB10) ParseReplicationPosition(s string) (replication.Position, error) {
return replication.ParsePosition(mariadbFlavorID, s)
}
// SendBinlogDumpCommand implements MysqlFlavor.SendBinlogDumpCommand().
func (*mariaDB10) SendBinlogDumpCommand(conn *SlaveConnection, startPos replication.Position) error {
const ComBinlogDump = 0x12
// Tell the server that we understand GTIDs by setting our slave capability
// to MARIA_SLAVE_CAPABILITY_GTID = 4 (MariaDB >= 10.0.1).
if _, err := conn.ExecuteFetch("SET @mariadb_slave_capability=4", 0, false); err != nil {
return fmt.Errorf("failed to set @mariadb_slave_capability=4: %v", err)
}
// Tell the server that we understand the format of events that will be used
// if binlog_checksum is enabled on the server.
if _, err := conn.ExecuteFetch("SET @master_binlog_checksum=@@global.binlog_checksum", 0, false); err != nil {
return fmt.Errorf("failed to set @master_binlog_checksum=@@global.binlog_checksum: %v", err)
}
// Set the slave_connect_state variable before issuing COM_BINLOG_DUMP to
// provide the start position in GTID form.
query := fmt.Sprintf("SET @slave_connect_state='%s'", startPos)
if _, err := conn.ExecuteFetch(query, 0, false); err != nil {
return fmt.Errorf("failed to set @slave_connect_state='%s': %v", startPos, err)
}
// Real slaves set this upon connecting if their gtid_strict_mode option was
// enabled. We always use gtid_strict_mode because we need it to make our
// internal GTID comparisons safe.
if _, err := conn.ExecuteFetch("SET @slave_gtid_strict_mode=1", 0, false); err != nil {
return fmt.Errorf("failed to set @slave_gtid_strict_mode=1: %v", err)
}
// Since we use @slave_connect_state, the file and position here are ignored.
buf := makeBinlogDumpCommand(0, 0, conn.slaveID, "")
return conn.SendCommand(ComBinlogDump, buf)
}
// MakeBinlogEvent implements MysqlFlavor.MakeBinlogEvent().
func (*mariaDB10) MakeBinlogEvent(buf []byte) replication.BinlogEvent {
return NewMariadbBinlogEvent(buf)
}
// EnableBinlogPlayback implements MysqlFlavor.EnableBinlogPlayback().
func (*mariaDB10) EnableBinlogPlayback(mysqld *Mysqld) error {
return nil
}
// DisableBinlogPlayback implements MysqlFlavor.DisableBinlogPlayback().
func (*mariaDB10) DisableBinlogPlayback(mysqld *Mysqld) error {
return nil
}
// mariadbBinlogEvent wraps a raw packet buffer and provides methods to examine
// it by implementing replication.BinlogEvent. Some methods are pulled in from
// binlogEvent.
type mariadbBinlogEvent struct {
binlogEvent
}
// NewMariadbBinlogEvent creates a BinlogEvent instance from given byte array
func NewMariadbBinlogEvent(buf []byte) replication.BinlogEvent {
return mariadbBinlogEvent{binlogEvent: binlogEvent(buf)}
}
// HasGTID implements BinlogEvent.HasGTID().
func (ev mariadbBinlogEvent) HasGTID(f replication.BinlogFormat) bool {
// MariaDB provides GTIDs in a separate event type GTID_EVENT.
return ev.IsGTID()
}
// IsGTID implements BinlogEvent.IsGTID().
func (ev mariadbBinlogEvent) IsGTID() bool {
return ev.Type() == 162
}
// IsBeginGTID implements BinlogEvent.IsBeginGTID().
//
// Expected format:
// # bytes field
// 8 sequence number
// 4 domain ID
// 1 flags2
func (ev mariadbBinlogEvent) IsBeginGTID(f replication.BinlogFormat) bool {
const FLStandalone = 1
data := ev.Bytes()[f.HeaderLength:]
flags2 := data[8+4]
return flags2&FLStandalone == 0
}
// GTID implements BinlogEvent.GTID().
//
// Expected format:
// # bytes field
// 8 sequence number
// 4 domain ID
// 1 flags2
func (ev mariadbBinlogEvent) GTID(f replication.BinlogFormat) (replication.GTID, error) {
data := ev.Bytes()[f.HeaderLength:]
return replication.MariadbGTID{
Sequence: binary.LittleEndian.Uint64(data[:8]),
Domain: binary.LittleEndian.Uint32(data[8 : 8+4]),
Server: ev.ServerID(),
}, nil
}
// StripChecksum implements BinlogEvent.StripChecksum().
func (ev mariadbBinlogEvent) StripChecksum(f replication.BinlogFormat) (replication.BinlogEvent, []byte, error) {
switch f.ChecksumAlgorithm {
case BinlogChecksumAlgOff, BinlogChecksumAlgUndef:
// There is no checksum.
return ev, nil, nil
default:
// Checksum is the last 4 bytes of the event buffer.
data := ev.Bytes()
length := len(data)
checksum := data[length-4:]
data = data[:length-4]
return mariadbBinlogEvent{binlogEvent: binlogEvent(data)}, checksum, nil
}
}
func init() {
registerFlavorBuiltin(mariadbFlavorID, &mariaDB10{})
}