ledisdb/ledis/replication.go

251 lines
4.2 KiB
Go
Raw Normal View History

2014-06-04 10:42:02 +04:00
package ledis
import (
"bytes"
"errors"
2015-05-04 17:42:28 +03:00
"io"
"time"
2014-09-24 05:46:36 +04:00
"github.com/siddontang/go/log"
2014-09-27 06:08:45 +04:00
"github.com/siddontang/go/snappy"
2014-09-22 13:50:51 +04:00
"github.com/siddontang/ledisdb/rpl"
"github.com/siddontang/ledisdb/store"
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
)
func (l *Ledis) ReplicationUsed() bool {
return l.r != nil
}
2014-09-27 16:13:13 +04:00
func (l *Ledis) handleReplication() error {
2014-09-28 16:37:57 +04:00
l.wLock.Lock()
defer l.wLock.Unlock()
2015-06-18 16:47:35 +03:00
defer AsyncNotify(l.rDoneCh)
2014-09-22 18:03:44 +04:00
rl := &rpl.Log{}
2015-06-18 16:47:35 +03:00
2014-09-27 16:13:13 +04:00
var err error
2014-09-22 13:50:51 +04:00
for {
2014-09-27 16:13:13 +04:00
if err = l.r.NextNeedCommitLog(rl); err != nil {
2014-09-22 13:50:51 +04:00
if err != rpl.ErrNoBehindLog {
2015-01-12 05:32:03 +03:00
log.Errorf("get next commit log err, %s", err.Error)
2014-09-27 16:13:13 +04:00
return err
2014-09-22 13:50:51 +04:00
} else {
2014-09-27 16:13:13 +04:00
return nil
2014-09-22 13:50:51 +04:00
}
} else {
l.rbatch.Rollback()
2014-09-27 06:08:45 +04:00
if rl.Compression == 1 {
//todo optimize
if rl.Data, err = snappy.Decode(nil, rl.Data); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("decode log error %s", err.Error())
2014-09-27 16:13:13 +04:00
return err
2014-09-27 06:08:45 +04:00
}
}
if bd, err := store.NewBatchData(rl.Data); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("decode batch log error %s", err.Error())
return err
} else if err = bd.Replay(l.rbatch); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("replay batch log error %s", err.Error())
}
2014-08-30 13:39:44 +04:00
2014-09-27 16:13:13 +04:00
l.commitLock.Lock()
if err = l.rbatch.Commit(); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("commit log error %s", err.Error())
2014-09-22 13:50:51 +04:00
} else if err = l.r.UpdateCommitID(rl.ID); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("update commit id error %s", err.Error())
2014-09-27 16:13:13 +04:00
}
l.commitLock.Unlock()
if err != nil {
return err
2014-09-22 13:50:51 +04:00
}
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() {
2014-09-25 06:21:50 +04:00
defer l.wg.Done()
2014-09-29 13:01:58 +04:00
l.noticeReplication()
2014-06-04 10:42:02 +04:00
2014-09-22 13:50:51 +04:00
for {
select {
case <-l.rc:
l.handleReplication()
2014-09-25 06:21:50 +04:00
case <-l.quit:
return
2014-09-22 13:50:51 +04:00
}
2014-06-04 10:42:02 +04:00
}
}
2014-09-22 13:50:51 +04:00
func (l *Ledis) WaitReplication() error {
if !l.ReplicationUsed() {
return ErrRplNotSupport
}
2014-09-28 16:37:57 +04:00
2014-09-28 17:55:25 +04:00
for i := 0; i < 100; i++ {
2015-06-18 16:47:35 +03:00
l.noticeReplication()
select {
case <-l.rDoneCh:
case <-l.quit:
return nil
}
time.Sleep(100 * time.Millisecond)
2014-09-28 17:55:25 +04:00
b, err := l.r.CommitIDBehind()
if err != nil {
return err
2015-06-18 16:47:35 +03:00
} else if !b {
2014-09-28 17:55:25 +04:00
return nil
}
2014-09-22 18:03:44 +04:00
}
2014-09-29 13:01:58 +04:00
2014-09-28 17:55:25 +04:00
return errors.New("wait replication too many times")
2014-06-04 10:42:02 +04:00
}
2014-09-22 18:03:44 +04:00
func (l *Ledis) StoreLogsFromReader(rb io.Reader) error {
if !l.ReplicationUsed() {
return ErrRplNotSupport
2014-10-10 05:49:16 +04:00
} else if !l.cfg.Readonly {
return ErrRplInRDWR
2014-06-04 10:42:02 +04:00
}
2014-09-22 18:03:44 +04:00
log := &rpl.Log{}
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 18:03:44 +04:00
return 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 {
2014-09-22 18:03:44 +04:00
return err
2014-06-09 13:23:32 +04:00
}
}
2014-09-28 16:37:57 +04:00
l.noticeReplication()
2014-07-11 09:28:34 +04:00
2014-09-22 18:03:44 +04:00
return nil
2014-07-11 09:28:34 +04:00
}
2014-09-28 16:37:57 +04:00
func (l *Ledis) noticeReplication() {
AsyncNotify(l.rc)
}
2014-09-22 18:03:44 +04:00
func (l *Ledis) StoreLogsFromData(data []byte) 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.ReplicationUsed() {
2014-09-22 13:50:51 +04:00
// no replication log
nextLogID = 0
err = ErrRplNotSupport
2014-09-22 13:50:51 +04:00
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
}
nextLogID = startLogID
2014-09-22 18:03:44 +04:00
log := &rpl.Log{}
2014-09-22 13:50:51 +04:00
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, quitCh chan struct{}) (n int, nextLogID uint64, err error) {
2014-09-22 13:50:51 +04:00
n, nextLogID, err = l.ReadLogsTo(startLogID, w)
if err != nil {
2014-06-10 06:41:50 +04:00
return
} else if n != 0 {
2014-06-09 13:23:32 +04:00
return
}
2014-09-22 13:50:51 +04:00
//no events read
select {
case <-l.r.WaitLog():
2014-09-22 13:50:51 +04:00
case <-time.After(time.Duration(timeout) * time.Second):
case <-quitCh:
return
2014-06-04 10:42:02 +04:00
}
2014-09-22 13:50:51 +04:00
return l.ReadLogsTo(startLogID, w)
}
func (l *Ledis) propagate(rl *rpl.Log) {
for _, h := range l.rhs {
h(rl)
}
}
type NewLogEventHandler func(rl *rpl.Log)
func (l *Ledis) AddNewLogEventHandler(h NewLogEventHandler) error {
if !l.ReplicationUsed() {
return ErrRplNotSupport
}
l.rhs = append(l.rhs, h)
return nil
2014-06-04 10:42:02 +04:00
}
2014-09-23 13:53:52 +04:00
func (l *Ledis) ReplicationStat() (*rpl.Stat, error) {
if !l.ReplicationUsed() {
return nil, ErrRplNotSupport
}
return l.r.Stat()
}