forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_flavor_google.go
105 lines (92 loc) · 2.51 KB
/
mysql_flavor_google.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
// 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 (
"fmt"
"strconv"
"github.com/youtube/vitess/go/vt/mysqlctl/proto"
)
// googleMysql51 is the implementation of MysqlFlavor for google mysql 51
type googleMysql51 struct {
}
// MasterStatus implements MysqlFlavor.MasterStatus
//
// The command looks like:
// mysql> show master status\G
// **************************** 1. row ***************************
// File: vt-000001c6-bin.000003
// Position: 106
// Binlog_Do_DB:
// Binlog_Ignore_DB:
// Group_ID:
func (*googleMysql51) MasterStatus(mysqld *Mysqld) (rp *proto.ReplicationPosition, err error) {
qr, err := mysqld.fetchSuperQuery("SHOW MASTER STATUS")
if err != nil {
return
}
if len(qr.Rows) != 1 {
return nil, ErrNotMaster
}
if len(qr.Rows[0]) < 5 {
return nil, fmt.Errorf("this db does not support group id")
}
rp = &proto.ReplicationPosition{}
rp.MasterLogFile = qr.Rows[0][0].String()
utemp, err := qr.Rows[0][1].ParseUint64()
if err != nil {
return nil, err
}
rp.MasterLogPosition = uint(utemp)
rp.MasterLogGroupId, err = qr.Rows[0][4].ParseInt64()
if err != nil {
return nil, err
}
// On the master, the SQL position and IO position are at
// necessarily the same point.
rp.MasterLogFileIo = rp.MasterLogFile
rp.MasterLogPositionIo = rp.MasterLogPosition
return
}
// PromoteSlaveCommands implements MysqlFlavor.PromoteSlaveCommands
func (*googleMysql51) PromoteSlaveCommands() []string {
return []string{
"RESET MASTER",
"RESET SLAVE",
"CHANGE MASTER TO MASTER_HOST = ''",
}
}
// ParseGTID implements MysqlFlavor.ParseGTID().
func (*googleMysql51) ParseGTID(s string) (GTID, error) {
id, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid Google MySQL group_id (%v): %v", s, err)
}
return googleGTID{groupID: id}, nil
}
type googleGTID struct {
groupID uint64
}
// String implements GTID.String().
func (gtid googleGTID) String() string {
return fmt.Sprintf("%d", gtid.groupID)
}
// TryCompare implements GTID.TryCompare().
func (gtid googleGTID) TryCompare(cmp GTID) (int, error) {
other, ok := cmp.(googleGTID)
if !ok {
return 0, fmt.Errorf("can't compare GTID, wrong type: %#v.TryCompare(%#v)",
gtid, cmp)
}
switch true {
case gtid.groupID < other.groupID:
return -1, nil
case gtid.groupID > other.groupID:
return 1, nil
default:
return 0, nil
}
}
func init() {
mysqlFlavors["GoogleMysql"] = &googleMysql51{}
}