2014-06-04 10:42:02 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2014-07-04 09:27:57 +04:00
|
|
|
"github.com/siddontang/go-log/log"
|
2014-08-30 13:39:44 +04:00
|
|
|
"github.com/siddontang/ledisdb/store/driver"
|
2014-06-04 10:42:02 +04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2014-07-11 09:28:34 +04:00
|
|
|
var (
|
|
|
|
ErrSkipEvent = errors.New("skip to next event")
|
|
|
|
)
|
|
|
|
|
2014-06-04 10:42:02 +04:00
|
|
|
var (
|
2014-06-06 04:30:10 +04:00
|
|
|
errInvalidBinLogEvent = errors.New("invalid binglog event")
|
2014-06-09 13:23:32 +04:00
|
|
|
errInvalidBinLogFile = errors.New("invalid binlog file")
|
2014-06-04 10:42:02 +04:00
|
|
|
)
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
type replBatch struct {
|
|
|
|
wb driver.IWriteBatch
|
|
|
|
events [][]byte
|
|
|
|
createTime uint32
|
|
|
|
l *Ledis
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *replBatch) Commit() error {
|
|
|
|
b.l.commitLock.Lock()
|
|
|
|
defer b.l.commitLock.Unlock()
|
|
|
|
|
|
|
|
err := b.wb.Commit()
|
|
|
|
if err != nil {
|
|
|
|
b.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.l.binlog != nil {
|
|
|
|
if err = b.l.binlog.Log(b.events...); err != nil {
|
|
|
|
b.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *replBatch) Rollback() error {
|
|
|
|
b.wb.Rollback()
|
|
|
|
b.events = [][]byte{}
|
|
|
|
b.createTime = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) replicateEvent(b *replBatch, event []byte) error {
|
2014-06-04 10:42:02 +04:00
|
|
|
if len(event) == 0 {
|
|
|
|
return errInvalidBinLogEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
logType := uint8(event[0])
|
|
|
|
switch logType {
|
|
|
|
case BinLogTypePut:
|
2014-08-30 13:39:44 +04:00
|
|
|
return l.replicatePutEvent(b, event)
|
2014-06-04 10:42:02 +04:00
|
|
|
case BinLogTypeDeletion:
|
2014-08-30 13:39:44 +04:00
|
|
|
return l.replicateDeleteEvent(b, event)
|
2014-06-04 10:42:02 +04:00
|
|
|
default:
|
|
|
|
return errInvalidBinLogEvent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
func (l *Ledis) replicatePutEvent(b *replBatch, event []byte) error {
|
2014-06-04 10:42:02 +04:00
|
|
|
key, value, err := decodeBinLogPut(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
b.wb.Put(key, value)
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
if b.l.binlog != nil {
|
|
|
|
b.events = append(b.events, event)
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
return nil
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
func (l *Ledis) replicateDeleteEvent(b *replBatch, event []byte) error {
|
2014-06-04 10:42:02 +04:00
|
|
|
key, err := decodeBinLogDelete(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
b.wb.Delete(key)
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
if b.l.binlog != nil {
|
|
|
|
b.events = append(b.events, event)
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
return nil
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 09:28:34 +04:00
|
|
|
func ReadEventFromReader(rb io.Reader, f func(createTime uint32, event []byte) error) error {
|
2014-06-09 13:23:32 +04:00
|
|
|
var createTime uint32
|
|
|
|
var dataLen uint32
|
|
|
|
var dataBuf bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err = binary.Read(rb, binary.BigEndian, &createTime); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = binary.Read(rb, binary.BigEndian, &dataLen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = io.CopyN(&dataBuf, rb, int64(dataLen)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-11 09:28:34 +04:00
|
|
|
err = f(createTime, dataBuf.Bytes())
|
|
|
|
if err != nil && err != ErrSkipEvent {
|
|
|
|
return err
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dataBuf.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-11 09:28:34 +04:00
|
|
|
func (l *Ledis) ReplicateFromReader(rb io.Reader) error {
|
2014-08-30 13:39:44 +04:00
|
|
|
b := new(replBatch)
|
|
|
|
|
|
|
|
b.wb = l.ldb.NewWriteBatch()
|
|
|
|
b.l = l
|
|
|
|
|
2014-07-11 09:28:34 +04:00
|
|
|
f := func(createTime uint32, event []byte) error {
|
2014-08-30 13:39:44 +04:00
|
|
|
if b.createTime == 0 {
|
|
|
|
b.createTime = createTime
|
|
|
|
} else if b.createTime != createTime {
|
|
|
|
if err := b.Commit(); err != nil {
|
|
|
|
log.Fatal("replication error %s, skip to next", err.Error())
|
|
|
|
return ErrSkipEvent
|
|
|
|
}
|
|
|
|
b.createTime = createTime
|
|
|
|
}
|
|
|
|
|
|
|
|
err := l.replicateEvent(b, event)
|
2014-07-11 09:28:34 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("replication error %s, skip to next", err.Error())
|
|
|
|
return ErrSkipEvent
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
err := ReadEventFromReader(rb, f)
|
|
|
|
if err != nil {
|
|
|
|
b.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b.Commit()
|
2014-07-11 09:28:34 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
func (l *Ledis) ReplicateFromData(data []byte) error {
|
|
|
|
rb := bytes.NewReader(data)
|
|
|
|
|
|
|
|
err := l.ReplicateFromReader(rb)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) ReplicateFromBinLog(filePath string) error {
|
2014-06-07 12:56:22 +04:00
|
|
|
f, err := os.Open(filePath)
|
2014-06-04 10:42:02 +04:00
|
|
|
if err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
return err
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
rb := bufio.NewReaderSize(f, 4096)
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
err = l.ReplicateFromReader(rb)
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
func (l *Ledis) ReadEventsTo(info *MasterInfo, w io.Writer) (n int, err error) {
|
|
|
|
n = 0
|
2014-06-09 13:23:32 +04:00
|
|
|
if l.binlog == nil {
|
|
|
|
//binlog not supported
|
|
|
|
info.LogFileIndex = 0
|
2014-06-10 06:41:50 +04:00
|
|
|
info.LogPos = 0
|
2014-06-09 13:23:32 +04:00
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
index := info.LogFileIndex
|
|
|
|
offset := info.LogPos
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
filePath := l.binlog.FormatLogFilePath(index)
|
|
|
|
|
|
|
|
var f *os.File
|
|
|
|
f, err = os.Open(filePath)
|
2014-06-10 06:41:50 +04:00
|
|
|
if os.IsNotExist(err) {
|
2014-06-09 13:23:32 +04:00
|
|
|
lastIndex := l.binlog.LogFileIndex()
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if index == lastIndex {
|
|
|
|
//no binlog at all
|
2014-06-10 06:41:50 +04:00
|
|
|
info.LogPos = 0
|
|
|
|
} else {
|
|
|
|
//slave binlog info had lost
|
|
|
|
info.LogFileIndex = -1
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
2014-06-10 06:41:50 +04:00
|
|
|
}
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
var fileSize int64
|
|
|
|
st, _ := f.Stat()
|
|
|
|
fileSize = st.Size()
|
|
|
|
|
|
|
|
if fileSize == info.LogPos {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if _, err = f.Seek(offset, os.SEEK_SET); err != nil {
|
|
|
|
//may be invliad seek offset
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastCreateTime uint32 = 0
|
2014-06-04 10:42:02 +04:00
|
|
|
var createTime uint32
|
|
|
|
var dataLen uint32
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-06-04 10:42:02 +04:00
|
|
|
for {
|
2014-06-09 13:23:32 +04:00
|
|
|
if err = binary.Read(f, binary.BigEndian, &createTime); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
//we will try to use next binlog
|
2014-06-10 06:41:50 +04:00
|
|
|
if index < l.binlog.LogFileIndex() {
|
|
|
|
info.LogFileIndex += 1
|
|
|
|
info.LogPos = 0
|
|
|
|
}
|
|
|
|
err = nil
|
2014-06-09 13:23:32 +04:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if lastCreateTime == 0 {
|
|
|
|
lastCreateTime = createTime
|
|
|
|
} else if lastCreateTime != createTime {
|
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if err = binary.Read(f, binary.BigEndian, &dataLen); err != nil {
|
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if err = binary.Write(w, binary.BigEndian, createTime); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if err = binary.Write(w, binary.BigEndian, dataLen); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
if _, err = io.CopyN(w, f, int64(dataLen)); err != nil {
|
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
n += (8 + int(dataLen))
|
2014-06-09 13:23:32 +04:00
|
|
|
info.LogPos = info.LogPos + 8 + int64(dataLen)
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|