2016-06-16 09:15:56 +00:00
|
|
|
package replication
|
|
|
|
|
|
|
|
import (
|
2019-01-01 08:57:46 +00:00
|
|
|
"context"
|
2021-07-15 19:49:50 +00:00
|
|
|
"time"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-02-12 11:13:54 +00:00
|
|
|
ErrNeedSyncAgain = errors.New("Last sync error or closed, try sync and get event again")
|
|
|
|
ErrSyncClosed = errors.New("Sync was closed")
|
2016-06-16 09:15:56 +00:00
|
|
|
)
|
|
|
|
|
2017-02-12 11:13:54 +00:00
|
|
|
// BinlogStreamer gets the streaming event.
|
2016-06-16 09:15:56 +00:00
|
|
|
type BinlogStreamer struct {
|
|
|
|
ch chan *BinlogEvent
|
|
|
|
ech chan error
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2017-02-12 11:13:54 +00:00
|
|
|
// GetEvent gets the binlog event one by one, it will block until Syncer receives any events from MySQL
|
|
|
|
// or meets a sync error. You can pass a context (like Cancel or Timeout) to break the block.
|
|
|
|
func (s *BinlogStreamer) GetEvent(ctx context.Context) (*BinlogEvent, error) {
|
2016-06-16 09:15:56 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return nil, ErrNeedSyncAgain
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case c := <-s.ch:
|
|
|
|
return c, nil
|
|
|
|
case s.err = <-s.ech:
|
|
|
|
return nil, s.err
|
2017-02-12 11:13:54 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:19:37 +00:00
|
|
|
// GetEventWithStartTime gets the binlog event with starttime, if current binlog event timestamp smaller than specify starttime
|
|
|
|
// return nil event
|
|
|
|
func (s *BinlogStreamer) GetEventWithStartTime(ctx context.Context, startTime time.Time) (*BinlogEvent, error) {
|
2019-01-01 08:57:46 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return nil, ErrNeedSyncAgain
|
|
|
|
}
|
|
|
|
startUnix := startTime.Unix()
|
|
|
|
select {
|
|
|
|
case c := <-s.ch:
|
|
|
|
if int64(c.Header.Timestamp) >= startUnix {
|
|
|
|
return c, nil
|
|
|
|
}
|
2021-06-24 18:19:37 +00:00
|
|
|
return nil, nil
|
2019-01-01 08:57:46 +00:00
|
|
|
case s.err = <-s.ech:
|
|
|
|
return nil, s.err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DumpEvents dumps all left events
|
|
|
|
func (s *BinlogStreamer) DumpEvents() []*BinlogEvent {
|
|
|
|
count := len(s.ch)
|
|
|
|
events := make([]*BinlogEvent, 0, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
events = append(events, <-s.ch)
|
|
|
|
}
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
2016-06-16 09:15:56 +00:00
|
|
|
func (s *BinlogStreamer) close() {
|
2021-06-24 18:19:37 +00:00
|
|
|
s.closeWithError(nil)
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BinlogStreamer) closeWithError(err error) {
|
|
|
|
if err == nil {
|
|
|
|
err = ErrSyncClosed
|
2021-06-24 18:19:37 +00:00
|
|
|
} else {
|
|
|
|
log.Errorf("close sync with err: %v", err)
|
2016-06-16 09:15:56 +00:00
|
|
|
}
|
2021-06-24 18:19:37 +00:00
|
|
|
|
2016-06-16 09:15:56 +00:00
|
|
|
select {
|
|
|
|
case s.ech <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBinlogStreamer() *BinlogStreamer {
|
|
|
|
s := new(BinlogStreamer)
|
|
|
|
|
2017-02-12 11:13:54 +00:00
|
|
|
s.ch = make(chan *BinlogEvent, 10240)
|
2016-06-16 09:15:56 +00:00
|
|
|
s.ech = make(chan error, 4)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|