forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mariadb_gtid_handler.go
142 lines (109 loc) · 2.98 KB
/
mariadb_gtid_handler.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
package failover
import (
"fmt"
"net"
"github.com/pingcap/errors"
. "github.com/sdgxdev/go-mysql/mysql"
)
// Limiatation
// + Multi source replication is not supported
// + Slave can not handle write transactions, so maybe readonly or strict_mode = 1 is better
type MariadbGTIDHandler struct {
Handler
}
func (h *MariadbGTIDHandler) Promote(s *Server) error {
if err := h.WaitRelayLogDone(s); err != nil {
return errors.Trace(err)
}
if err := s.StopSlave(); err != nil {
return errors.Trace(err)
}
return nil
}
func (h *MariadbGTIDHandler) FindBestSlaves(slaves []*Server) ([]*Server, error) {
bestSlaves := []*Server{}
ps := make([]uint64, len(slaves))
lastIndex := -1
var seq uint64
for i, slave := range slaves {
rr, err := slave.Execute("SELECT @@gtid_current_pos")
if err != nil {
return nil, errors.Trace(err)
}
str, _ := rr.GetString(0, 0)
if len(str) == 0 {
seq = 0
} else {
g, err := ParseMariadbGTID(str)
if err != nil {
return nil, errors.Trace(err)
}
seq = g.SequenceNumber
}
ps[i] = seq
if lastIndex == -1 {
lastIndex = i
bestSlaves = []*Server{slave}
} else {
if ps[lastIndex] < seq {
lastIndex = i
bestSlaves = []*Server{slave}
} else if ps[lastIndex] == seq {
// these two slaves have same data,
bestSlaves = append(bestSlaves, slave)
}
}
}
return bestSlaves, nil
}
const changeMasterToWithCurrentPos = `CHANGE MASTER TO
MASTER_HOST = "%s", MASTER_PORT = %s,
MASTER_USER = "%s", MASTER_PASSWORD = "%s",
MASTER_USE_GTID = current_pos`
func (h *MariadbGTIDHandler) ChangeMasterTo(s *Server, m *Server) error {
if err := h.WaitRelayLogDone(s); err != nil {
return errors.Trace(err)
}
if err := s.StopSlave(); err != nil {
return errors.Trace(err)
}
if err := s.ResetSlave(); err != nil {
return errors.Trace(err)
}
host, port, _ := net.SplitHostPort(m.Addr)
if _, err := s.Execute(fmt.Sprintf(changeMasterToWithCurrentPos,
host, port, m.ReplUser.Name, m.ReplUser.Password)); err != nil {
return errors.Trace(err)
}
if err := s.StartSlave(); err != nil {
return errors.Trace(err)
}
return nil
}
func (h *MariadbGTIDHandler) WaitRelayLogDone(s *Server) error {
if err := s.StopSlaveIOThread(); err != nil {
return errors.Trace(err)
}
r, err := s.SlaveStatus()
if err != nil {
return errors.Trace(err)
}
fname, _ := r.GetStringByName(0, "Master_Log_File")
pos, _ := r.GetIntByName(0, "Read_Master_Log_Pos")
return s.MasterPosWait(Position{Name: fname, Pos: uint32(pos)}, 0)
}
func (h *MariadbGTIDHandler) WaitCatchMaster(s *Server, m *Server) error {
r, err := m.Execute("SELECT @@gtid_binlog_pos")
if err != nil {
return errors.Trace(err)
}
pos, _ := r.GetString(0, 0)
return h.waitUntilAfterGTID(s, pos)
}
func (h *MariadbGTIDHandler) CheckGTIDMode(slaves []*Server) error {
return nil
}
func (h *MariadbGTIDHandler) waitUntilAfterGTID(s *Server, pos string) error {
_, err := s.Execute(fmt.Sprintf("SELECT MASTER_GTID_WAIT('%s')", pos))
return err
}