2016-06-16 09:15:56 +00:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2019-01-01 08:57:46 +00:00
|
|
|
"bytes"
|
2016-06-16 09:15:56 +00:00
|
|
|
"fmt"
|
2021-07-15 19:49:50 +00:00
|
|
|
"sort"
|
2016-06-16 09:15:56 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-24 18:19:37 +00:00
|
|
|
"github.com/pingcap/errors"
|
2019-01-01 08:57:46 +00:00
|
|
|
"github.com/siddontang/go-log/log"
|
2016-06-16 09:15:56 +00:00
|
|
|
)
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// MariadbGTID represent mariadb gtid, [domain ID]-[server-id]-[sequence]
|
2016-06-16 09:15:56 +00:00
|
|
|
type MariadbGTID struct {
|
|
|
|
DomainID uint32
|
|
|
|
ServerID uint32
|
|
|
|
SequenceNumber uint64
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// ParseMariadbGTID parses mariadb gtid, [domain ID]-[server-id]-[sequence]
|
|
|
|
func ParseMariadbGTID(str string) (*MariadbGTID, error) {
|
2016-06-16 09:15:56 +00:00
|
|
|
if len(str) == 0 {
|
2019-01-01 08:57:46 +00:00
|
|
|
return &MariadbGTID{0, 0, 0}, nil
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
seps := strings.Split(str, "-")
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
gtid := new(MariadbGTID)
|
2016-06-16 09:15:56 +00:00
|
|
|
|
|
|
|
if len(seps) != 3 {
|
|
|
|
return gtid, errors.Errorf("invalid Mariadb GTID %v, must domain-server-sequence", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
domainID, err := strconv.ParseUint(seps[0], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return gtid, errors.Errorf("invalid MariaDB GTID Domain ID (%v): %v", seps[0], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverID, err := strconv.ParseUint(seps[1], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return gtid, errors.Errorf("invalid MariaDB GTID Server ID (%v): %v", seps[1], err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sequenceID, err := strconv.ParseUint(seps[2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return gtid, errors.Errorf("invalid MariaDB GTID Sequence number (%v): %v", seps[2], err)
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
return &MariadbGTID{
|
2016-06-16 09:15:56 +00:00
|
|
|
DomainID: uint32(domainID),
|
|
|
|
ServerID: uint32(serverID),
|
|
|
|
SequenceNumber: sequenceID}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
func (gtid *MariadbGTID) String() string {
|
2016-06-16 09:15:56 +00:00
|
|
|
if gtid.DomainID == 0 && gtid.ServerID == 0 && gtid.SequenceNumber == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%d-%d-%d", gtid.DomainID, gtid.ServerID, gtid.SequenceNumber)
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// Contain return whether one mariadb gtid covers another mariadb gtid
|
|
|
|
func (gtid *MariadbGTID) Contain(other *MariadbGTID) bool {
|
|
|
|
return gtid.DomainID == other.DomainID && gtid.SequenceNumber >= other.SequenceNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone clones a mariadb gtid
|
|
|
|
func (gtid *MariadbGTID) Clone() *MariadbGTID {
|
|
|
|
o := new(MariadbGTID)
|
|
|
|
*o = *gtid
|
|
|
|
return o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gtid *MariadbGTID) forward(newer *MariadbGTID) error {
|
|
|
|
if newer.DomainID != gtid.DomainID {
|
|
|
|
return errors.Errorf("%s is not same with doamin of %s", newer, gtid)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Here's a simplified example of binlog events.
|
|
|
|
Although I think one domain should have only one update at same time, we can't limit the user's usage.
|
|
|
|
we just output a warn log and let it go on
|
|
|
|
| mysqld-bin.000001 | 1453 | Gtid | 112 | 1495 | BEGIN GTID 0-112-6 |
|
|
|
|
| mysqld-bin.000001 | 1624 | Xid | 112 | 1655 | COMMIT xid=74 |
|
|
|
|
| mysqld-bin.000001 | 1655 | Gtid | 112 | 1697 | BEGIN GTID 0-112-7 |
|
|
|
|
| mysqld-bin.000001 | 1826 | Xid | 112 | 1857 | COMMIT xid=75 |
|
|
|
|
| mysqld-bin.000001 | 1857 | Gtid | 111 | 1899 | BEGIN GTID 0-111-5 |
|
|
|
|
| mysqld-bin.000001 | 1981 | Xid | 111 | 2012 | COMMIT xid=77 |
|
|
|
|
| mysqld-bin.000001 | 2012 | Gtid | 112 | 2054 | BEGIN GTID 0-112-8 |
|
|
|
|
| mysqld-bin.000001 | 2184 | Xid | 112 | 2215 | COMMIT xid=116 |
|
|
|
|
| mysqld-bin.000001 | 2215 | Gtid | 111 | 2257 | BEGIN GTID 0-111-6 |
|
|
|
|
*/
|
|
|
|
if newer.SequenceNumber <= gtid.SequenceNumber {
|
|
|
|
log.Warnf("out of order binlog appears with gtid %s vs current position gtid %s", newer, gtid)
|
|
|
|
}
|
|
|
|
|
|
|
|
gtid.ServerID = newer.ServerID
|
|
|
|
gtid.SequenceNumber = newer.SequenceNumber
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MariadbGTIDSet is a set of mariadb gtid
|
|
|
|
type MariadbGTIDSet struct {
|
|
|
|
Sets map[uint32]*MariadbGTID
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseMariadbGTIDSet parses str into mariadb gtid sets
|
|
|
|
func ParseMariadbGTIDSet(str string) (GTIDSet, error) {
|
|
|
|
s := new(MariadbGTIDSet)
|
|
|
|
s.Sets = make(map[uint32]*MariadbGTID)
|
|
|
|
if str == "" {
|
|
|
|
return s, nil
|
|
|
|
}
|
2021-07-15 19:49:50 +00:00
|
|
|
err := s.Update(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-01 08:57:46 +00:00
|
|
|
}
|
|
|
|
return s, nil
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// AddSet adds mariadb gtid into mariadb gtid set
|
|
|
|
func (s *MariadbGTIDSet) AddSet(gtid *MariadbGTID) error {
|
|
|
|
if gtid == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
o, ok := s.Sets[gtid.DomainID]
|
|
|
|
if ok {
|
|
|
|
err := o.forward(gtid)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Trace(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.Sets[gtid.DomainID] = gtid
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates mariadb gtid set
|
|
|
|
func (s *MariadbGTIDSet) Update(GTIDStr string) error {
|
2021-07-15 19:49:50 +00:00
|
|
|
sp := strings.Split(GTIDStr, ",")
|
|
|
|
//todo, handle redundant same uuid
|
|
|
|
for i := 0; i < len(sp); i++ {
|
|
|
|
gtid, err := ParseMariadbGTID(sp[i])
|
|
|
|
if err != nil {
|
|
|
|
return errors.Trace(err)
|
|
|
|
}
|
|
|
|
err = s.AddSet(gtid)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Trace(err)
|
|
|
|
}
|
2019-01-01 08:57:46 +00:00
|
|
|
}
|
2021-07-15 19:49:50 +00:00
|
|
|
return nil
|
2019-01-01 08:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MariadbGTIDSet) String() string {
|
2021-07-15 19:49:50 +00:00
|
|
|
sets := make([]string, 0, len(s.Sets))
|
|
|
|
for _, set := range s.Sets {
|
|
|
|
sets = append(sets, set.String())
|
|
|
|
}
|
|
|
|
sort.Strings(sets)
|
|
|
|
|
|
|
|
return strings.Join(sets, ",")
|
2019-01-01 08:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode encodes mariadb gtid set
|
|
|
|
func (s *MariadbGTIDSet) Encode() []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
sep := ""
|
|
|
|
for _, gtid := range s.Sets {
|
|
|
|
buf.WriteString(sep)
|
|
|
|
buf.WriteString(gtid.String())
|
|
|
|
sep = ","
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone clones a mariadb gtid set
|
|
|
|
func (s *MariadbGTIDSet) Clone() GTIDSet {
|
|
|
|
clone := &MariadbGTIDSet{
|
|
|
|
Sets: make(map[uint32]*MariadbGTID),
|
|
|
|
}
|
|
|
|
for domainID, gtid := range s.Sets {
|
|
|
|
clone.Sets[domainID] = gtid.Clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if two mariadb gtid set is same, otherwise return false
|
|
|
|
func (s *MariadbGTIDSet) Equal(o GTIDSet) bool {
|
|
|
|
other, ok := o.(*MariadbGTIDSet)
|
2016-06-16 09:15:56 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
if len(other.Sets) != len(s.Sets) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for domainID, gtid := range other.Sets {
|
|
|
|
o, ok := s.Sets[domainID]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if *gtid != *o {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
// Contain return whether one mariadb gtid set covers another mariadb gtid set
|
|
|
|
func (s *MariadbGTIDSet) Contain(o GTIDSet) bool {
|
|
|
|
other, ok := o.(*MariadbGTIDSet)
|
2016-06-16 09:15:56 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-01 08:57:46 +00:00
|
|
|
for doaminID, gtid := range other.Sets {
|
|
|
|
o, ok := s.Sets[doaminID]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !o.Contain(gtid) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|