ledisdb/ledis/replication.go

168 lines
2.7 KiB
Go
Raw Normal View History

2014-06-04 10:42:02 +04:00
package ledis
import (
"bytes"
"errors"
2014-09-22 13:50:51 +04:00
"fmt"
"github.com/siddontang/go-log/log"
2014-09-22 13:50:51 +04:00
"github.com/siddontang/ledisdb/rpl"
2014-06-04 10:42:02 +04:00
"io"
"time"
2014-06-04 10:42:02 +04:00
)
2014-09-01 06:16:20 +04:00
const (
2014-09-22 13:50:51 +04:00
maxReplLogSize = 1 * 1024 * 1024
2014-09-01 06:16:20 +04:00
)
2014-07-11 09:28:34 +04:00
var (
2014-09-22 13:50:51 +04:00
ErrLogMissed = errors.New("log is pured in server")
2014-07-11 09:28:34 +04:00
)
2014-09-22 13:50:51 +04:00
func (l *Ledis) handleReplication() {
l.rwg.Add(1)
var rl *rpl.Log
for {
if err := l.r.NextCommitLog(rl); err != nil {
if err != rpl.ErrNoBehindLog {
log.Error("get next commit log err, %s", err.Error)
} else {
l.rwg.Done()
return
}
} else {
l.rbatch.Rollback()
decodeEventBatch(l.rbatch, rl.Data)
2014-08-30 13:39:44 +04:00
2014-09-22 13:50:51 +04:00
if err := l.rbatch.Commit(); err != nil {
log.Error("commit log error %s", err.Error())
} else if err = l.r.UpdateCommitID(rl.ID); err != nil {
log.Error("update commit id error %s", err.Error())
}
2014-08-30 13:39:44 +04:00
}
2014-09-22 13:50:51 +04:00
}
2014-08-30 13:39:44 +04:00
}
2014-09-22 13:50:51 +04:00
func (l *Ledis) onReplication() {
if l.r == nil {
return
2014-06-04 10:42:02 +04:00
}
2014-09-22 13:50:51 +04:00
for {
select {
case <-l.rc:
l.handleReplication()
case <-time.After(5 * time.Second):
l.handleReplication()
}
2014-06-04 10:42:02 +04:00
}
}
2014-09-22 13:50:51 +04:00
func (l *Ledis) WaitReplication() error {
l.rwg.Wait()
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-09-22 13:50:51 +04:00
func (l *Ledis) StoreLogsFromReader(rb io.Reader) (uint64, error) {
if l.r == nil {
return 0, fmt.Errorf("replication not enable")
2014-06-04 10:42:02 +04:00
}
2014-09-22 13:50:51 +04:00
var log *rpl.Log
var n uint64
2014-06-09 13:23:32 +04:00
for {
2014-09-22 13:50:51 +04:00
if err := log.Decode(rb); err != nil {
2014-06-09 13:23:32 +04:00
if err == io.EOF {
break
} else {
2014-09-22 13:50:51 +04:00
return 0, err
2014-06-09 13:23:32 +04:00
}
}
2014-09-22 13:50:51 +04:00
if err := l.r.StoreLog(log); err != nil {
return 0, err
2014-06-09 13:23:32 +04:00
}
2014-09-22 13:50:51 +04:00
n = log.ID
2014-06-09 13:23:32 +04:00
}
2014-09-22 13:50:51 +04:00
select {
case l.rc <- struct{}{}:
default:
break
2014-07-11 09:28:34 +04:00
}
2014-09-22 13:50:51 +04:00
return n, nil
2014-07-11 09:28:34 +04:00
}
2014-09-22 13:50:51 +04:00
func (l *Ledis) StoreLogsFromData(data []byte) (uint64, error) {
2014-06-09 13:23:32 +04:00
rb := bytes.NewReader(data)
2014-09-22 13:50:51 +04:00
return l.StoreLogsFromReader(rb)
2014-06-09 13:23:32 +04:00
}
2014-09-22 13:50:51 +04:00
func (l *Ledis) ReadLogsTo(startLogID uint64, w io.Writer) (n int, nextLogID uint64, err error) {
if l.r == nil {
// no replication log
nextLogID = 0
return
2014-06-04 10:42:02 +04:00
}
2014-09-22 13:50:51 +04:00
var firtID, lastID uint64
2014-06-04 10:42:02 +04:00
2014-09-22 13:50:51 +04:00
firtID, err = l.r.FirstLogID()
if err != nil {
return
}
2014-09-22 13:50:51 +04:00
if startLogID < firtID {
err = ErrLogMissed
return
}
2014-09-22 13:50:51 +04:00
lastID, err = l.r.LastLogID()
if err != nil {
2014-06-09 13:23:32 +04:00
return
2014-06-04 10:42:02 +04:00
}
2014-09-22 13:50:51 +04:00
var log *rpl.Log
for i := startLogID; i <= lastID; i++ {
if err = l.r.GetLog(i, log); err != nil {
return
}
2014-06-04 10:42:02 +04:00
2014-09-22 13:50:51 +04:00
if err = log.Encode(w); err != nil {
return
}
2014-06-09 13:23:32 +04:00
2014-09-22 13:50:51 +04:00
nextLogID = i + 1
2014-06-10 06:41:50 +04:00
2014-09-22 13:50:51 +04:00
n += log.Size()
2014-06-09 13:23:32 +04:00
2014-09-22 13:50:51 +04:00
if n > maxReplLogSize {
break
2014-06-10 06:41:50 +04:00
}
2014-06-09 13:23:32 +04:00
}
2014-09-22 13:50:51 +04:00
return
}
2014-06-10 06:41:50 +04:00
2014-09-22 13:50:51 +04:00
// try to read events, if no events read, try to wait the new event singal until timeout seconds
func (l *Ledis) ReadLogsToTimeout(startLogID uint64, w io.Writer, timeout int) (n int, nextLogID uint64, err error) {
n, nextLogID, err = l.ReadLogsTo(startLogID, w)
if err != nil {
2014-06-10 06:41:50 +04:00
return
2014-09-22 13:50:51 +04:00
} else if n == 0 || nextLogID == 0 {
2014-06-09 13:23:32 +04:00
return
}
2014-09-22 13:50:51 +04:00
//no events read
select {
//case <-l.binlog.Wait():
case <-time.After(time.Duration(timeout) * time.Second):
2014-06-04 10:42:02 +04:00
}
2014-09-22 13:50:51 +04:00
return l.ReadLogsTo(startLogID, w)
2014-06-04 10:42:02 +04:00
}