Skip to content

add previousgtidsevent #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions replication/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package replication

import (
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"strconv"
Expand Down Expand Up @@ -217,6 +218,55 @@ func (e *RotateEvent) Dump(w io.Writer) {
fmt.Fprintln(w)
}

type PreviousGTIDsEvent struct {
GTIDSets string
}

func (e *PreviousGTIDsEvent) Decode(data []byte) error {
var previousGTIDSets []string
pos := 0
uuidCount := binary.LittleEndian.Uint16(data[pos:pos+8])
pos += 8

for i := uint16(0);i < uuidCount; i++ {
uuid := e.decodeUuid(data[pos:pos+16])
pos += 16
sliceCount := binary.LittleEndian.Uint16(data[pos:pos+8])
pos += 8
var intervals []string
for i := uint16(0);i < sliceCount; i++ {
start := e.decodeInterval(data[pos:pos+8])
pos += 8
stop := e.decodeInterval(data[pos:pos+8])
pos += 8
interval := ""
if stop == start+1 {
interval = fmt.Sprintf("%d",start)
}else {
interval = fmt.Sprintf("%d-%d",start,stop-1)
}
intervals = append(intervals,interval)
}
previousGTIDSets = append(previousGTIDSets,fmt.Sprintf("%s:%s",uuid,strings.Join(intervals,":")))
}
e.GTIDSets = fmt.Sprintf("%s",strings.Join(previousGTIDSets,","))
return nil
}

func (e *PreviousGTIDsEvent) Dump(w io.Writer) {
fmt.Fprintf(w, "Previous GTID Event: %s\n", e.GTIDSets)
fmt.Fprintln(w)
}

func (e *PreviousGTIDsEvent) decodeUuid(data []byte) string {
return fmt.Sprintf("%s-%s-%s-%s-%s",hex.EncodeToString(data[0:4]),hex.EncodeToString(data[4:6]),
hex.EncodeToString(data[6:8]),hex.EncodeToString(data[8:10]),hex.EncodeToString(data[10:]))
}

func (e *PreviousGTIDsEvent) decodeInterval(data []byte) uint64 {
return binary.LittleEndian.Uint64(data)
}

type XIDEvent struct {
XID uint64

Expand Down
2 changes: 2 additions & 0 deletions replication/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ func (p *BinlogParser) parseEvent(h *EventHeader, data []byte, rawData []byte) (
ee := &MariadbGTIDEvent{}
ee.GTID.ServerID = h.ServerID
e = ee
case PREVIOUS_GTIDS_EVENT:
e = &PreviousGTIDsEvent{}
default:
e = &GenericEvent{}
}
Expand Down