ledisdb/ledis/replication.go

239 lines
4.0 KiB
Go
Raw Normal View History

2014-06-04 10:42:02 +04:00
package ledis
import (
"bytes"
"errors"
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"
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
)
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()
2014-09-22 13:50:51 +04:00
l.rwg.Add(1)
2014-09-22 18:03:44 +04:00
rl := &rpl.Log{}
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 {
log.Error("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 {
l.rwg.Done()
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 {
log.Error("decode log error %s", err.Error())
2014-09-27 16:13:13 +04:00
return err
2014-09-27 06:08:45 +04:00
}
}
2014-09-22 13:50:51 +04:00
decodeEventBatch(l.rbatch, rl.Data)
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 {
2014-09-22 13:50:51 +04:00
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-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
l.noticeReplication()
l.rwg.Wait()
2014-09-28 17:55:25 +04:00
for i := 0; i < 100; i++ {
b, err := l.r.CommitIDBehind()
if err != nil {
return err
} else if b {
l.noticeReplication()
l.rwg.Wait()
2014-09-29 13:01:58 +04:00
time.Sleep(100 * time.Millisecond)
2014-09-28 17:55:25 +04:00
} else {
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) (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
} 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):
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()
}